Skip to content

Commit 7197133

Browse files
committed
Refactoring code design
* Create each OperatingSystem class * Each function change to trait and each OperatingSystem mixin it
1 parent b337b9d commit 7197133

File tree

17 files changed

+236
-260
lines changed

17 files changed

+236
-260
lines changed

src/main/scala/gitbucket/monitoring/controllers/IndexController.scala

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package gitbucket.monitoring.controllers
22

33
import gitbucket.core.util.AdminAuthenticator
4-
import gitbucket.monitoring.models.{SystemInformation, EnvironmentVariable ,MachineResources, Process}
4+
import gitbucket.monitoring.models._
55
import gitbucket.monitoring.information._
66

77
class IndexController extends MonitoringControllerBase with JavaController with LogController {
@@ -10,41 +10,35 @@ class IndexController extends MonitoringControllerBase with JavaController with
1010
redirect(s"/admin/monitoring/systeminformation");
1111
})
1212

13-
val sysInfo = new SystemInformation
14-
1513
get("/admin/monitoring/systeminformation")(adminOnly {
1614
html.system(
17-
sysInfo.instance.timeZone.toString,
18-
sysInfo.instance.nowTime.toString,
19-
sysInfo.instance.zoneOffset.toString,
20-
sysInfo.instance.dayOfWeek.toString,
21-
sysInfo.instance.onDocker,
22-
sysInfo.instance.getUpTime
15+
os.timeZone.toString,
16+
os.nowTime.toString,
17+
os.zoneOffset.toString,
18+
os.dayOfWeek.toString,
19+
os.onDocker,
20+
os.getUpTime
2321
);
2422
})
2523

2624
get("/admin/monitoring/environmentvaliable")(adminOnly {
2725
html.environmentValiable(EnvironmentVariable.valiables);
2826
})
2927

30-
val machineResources = new MachineResources
31-
3228
get("/admin/monitoring/machineresources")(adminOnly {
3329
html.resources(
34-
machineResources.instance.cpuCore,
35-
machineResources.instance.getCpu,
36-
machineResources.instance.getMemory,
37-
machineResources.instance.getSwap,
38-
machineResources.instance.getDiskSpace
30+
os.cpuCore,
31+
os.getCpu,
32+
os.getMemory,
33+
os.getSwap,
34+
os.getDiskSpace
3935
);
4036
})
4137

42-
val process = new Process
43-
4438
get("/admin/monitoring/process")(adminOnly {
4539
html.process(
46-
process.instance.getTasks,
47-
process.instance.getLoadAverage
40+
os.getTasks,
41+
os.getLoadAverage
4842
);
4943
})
5044

src/main/scala/gitbucket/monitoring/controllers/LogController.scala

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,23 @@ trait LogController extends MonitoringControllerBase {
1212
);
1313
})
1414

15-
val gitbucketLog = new GitBucketLog
16-
1715
get("/admin/monitoring/logs/gitbucketlog")(adminOnly {
18-
val defaultSettings = gitbucketLog.getDefaultSettings
16+
val defaultSettings = GitBucketLog.getDefaultSettings
1917
val lineNum = request.getParameter("line-num")
2018

2119
if (lineNum != null){
2220
try {
2321
val n = lineNum.toInt
2422
if (n > defaultSettings.displayLimitLines) {
25-
html.gitbucketlog(defaultSettings, gitbucketLog.getLog(defaultSettings.displayLimitLines));
23+
html.gitbucketlog(defaultSettings, os.getLog(defaultSettings.displayLimitLines));
2624
} else {
27-
html.gitbucketlog(defaultSettings, gitbucketLog.getLog(n));
25+
html.gitbucketlog(defaultSettings, os.getLog(n));
2826
}
2927
} catch {
30-
case e: Exception => html.gitbucketlog(defaultSettings, gitbucketLog.getLog());
28+
case e: Exception => html.gitbucketlog(defaultSettings, os.getLog());
3129
}
3230
} else {
33-
html.gitbucketlog(defaultSettings, gitbucketLog.getLog());
31+
html.gitbucketlog(defaultSettings, os.getLog());
3432
}
3533
})
3634
}

src/main/scala/gitbucket/monitoring/controllers/MonitoringControllerBase.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ import io.github.gitbucket.scalatra.forms._
44
import gitbucket.core.controller.ControllerBase
55
import gitbucket.core.util.AdminAuthenticator
66

7-
trait MonitoringControllerBase extends ControllerBase with AdminAuthenticator
7+
trait MonitoringControllerBase extends ControllerBase with AdminAuthenticator{
8+
val os = gitbucket.monitoring.models.operatingsystem.OperatingSystem.getInstance
9+
}

src/main/scala/gitbucket/monitoring/models/EnvironmentVariable.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package gitbucket.monitoring.models
22

33
import scala.collection.JavaConversions._
4-
import scala.sys.process._
5-
import gitbucket.monitoring.utils._
4+
import gitbucket.monitoring.utils.Message
65

76
object EnvironmentVariable {
87
def valiables: Either[String, Map[String, String]] = {

src/main/scala/gitbucket/monitoring/models/GitBucketLog.scala

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
package gitbucket.monitoring.models
22

33
import java.nio.file.{Files, Paths}
4-
54
import scala.sys.process._
6-
import gitbucket.monitoring.models.{OperatingSystem, LogBack}
75
import gitbucket.monitoring.utils._
86

97
object GitBucketLog {
108
val disableMessage = "Log setting is disable."
11-
}
129

13-
trait GitBucketLogBase {
1410
def getDefaultSettings: DefaultSettings = {
1511
DefaultSettings(
1612
LogBack.getLogBackInfo,
1713
1000,
1814
30000
1915
)
2016
}
21-
def getLog(lines: Int = getDefaultSettings.defaultDisplayLines): Either[String, Log] = {
22-
if (getDefaultSettings.logBackInfo.enableLogging) {
23-
getDefaultSettings.logBackInfo.logFilePath match {
17+
}
18+
19+
trait GitBucketLog {
20+
def getLog(lines: Int = GitBucketLog.getDefaultSettings.defaultDisplayLines): Either[String, Log] = {
21+
if (GitBucketLog.getDefaultSettings.logBackInfo.enableLogging) {
22+
GitBucketLog.getDefaultSettings.logBackInfo.logFilePath match {
2423
case Left(message) => Left(Message.notFound)
2524
case Right(p) => {
2625
try {
@@ -39,51 +38,6 @@ trait GitBucketLogBase {
3938
}
4039
}
4140

42-
class GitBucketLog extends GitBucketLogBase {
43-
val instance = OperatingSystem.osType match {
44-
case OperatingSystem.Linux => new GitBucketLogBase with Linux
45-
case OperatingSystem.Mac => new GitBucketLogBase with Mac
46-
case OperatingSystem.Windows => new GitBucketLogBase with Windows
47-
case _ => new GitBucketLogBase with Other
48-
}
49-
50-
trait Linux extends GitBucketLogBase {
51-
52-
}
53-
54-
trait Mac extends GitBucketLogBase {
55-
56-
}
57-
58-
trait Windows extends GitBucketLogBase {
59-
override def getLog(lines: Int = getDefaultSettings.defaultDisplayLines): Either[String, Log] = {
60-
if (getDefaultSettings.logBackInfo.enableLogging) {
61-
getDefaultSettings.logBackInfo.logFilePath match {
62-
case Left(message) => Left(Message.notFound)
63-
case Right(p) => {
64-
try {
65-
Right(Log(
66-
Process(s"powershell -Command Get-Content -Path $p -Tail $lines") !!,
67-
lines
68-
))
69-
} catch {
70-
case e: Exception => Left(Message.error)
71-
}
72-
}
73-
}
74-
} else {
75-
Left(GitBucketLog.disableMessage)
76-
}
77-
}
78-
}
79-
80-
trait Other extends GitBucketLogBase {
81-
override def getLog(lines: Int = getDefaultSettings.defaultDisplayLines): Either[String, Log] = {
82-
Left(Message.notSupported)
83-
}
84-
}
85-
}
86-
8741
case class DefaultSettings (
8842
logBackInfo: LogBack.LogBackInfo,
8943
defaultDisplayLines: Int,

src/main/scala/gitbucket/monitoring/models/Java.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import scala.collection.JavaConversions._
44
import gitbucket.monitoring.utils._
55

66
object Java {
7-
8-
def getSystemProperties: Map[String, String] = System.getProperties().toMap
9-
107
val memTotal = (UnitConverter.byteToMB(Runtime.getRuntime().totalMemory()))
118
val memMax = (UnitConverter.byteToMB(Runtime.getRuntime().maxMemory()))
129

10+
def getSystemProperties: Map[String, String] = System.getProperties().toMap
11+
1312
def getMemoryInfo: Memory = {
1413
val memFree = UnitConverter.byteToMB(Runtime.getRuntime().freeMemory())
1514
Memory(

src/main/scala/gitbucket/monitoring/models/LogBack.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import gitbucket.core.util.StringUtil
66
import gitbucket.monitoring.utils._
77

88
object LogBack {
9+
val notFoundMessage = "Can not find logback configuration file."
10+
val dosentConfigureMessage = "Dosen't configure Logback."
911
val enableLogging = Java.getSystemProperties.contains("logback.configurationFile")
10-
val confPath = Java.getSystemProperties.getOrElse("logback.configurationFile", "Can not find logback settings.xml")
11-
val notFoundMessage = "Can not find logback settings.xml"
12-
val dosentConfigureMessage = "Dosen't configure Logback"
12+
val confPath = Java.getSystemProperties.getOrElse("logback.configurationFile", notFoundMessage)
13+
1314
def getLogBackSettings: Either[String, String] = {
1415
if (enableLogging) {
1516
try {
@@ -24,6 +25,7 @@ object LogBack {
2425
Left(dosentConfigureMessage)
2526
}
2627
}
28+
2729
def getLogBackInfo: LogBackInfo = {
2830
val logFilePath: Either[String, String] = {
2931
if (enableLogging) {

src/main/scala/gitbucket/monitoring/models/MachineResources.scala

Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import java.nio.file._
44
import scala.sys.process._
55
import gitbucket.monitoring.utils._
66

7-
trait MachineResourcesBase {
7+
trait MachineResources {
88
private val fileStore = Files.getFileStore(Paths.get("."))
9+
910
def cpuCore = Runtime.getRuntime().availableProcessors()
11+
1012
def getCpu: Either[String, Cpu] = {
1113
try {
1214
val resouces = StringUtil.dropAndToArray(Process("top -b -n 1") #| Process("grep Cpu(s)") !! ,":" , ",")
@@ -29,6 +31,7 @@ trait MachineResourcesBase {
2931
case e: Exception => Left(Message.error)
3032
}
3133
}
34+
3235
def getMemory: Either[String, Memory] = {
3336
try {
3437
//Estimated available memory
@@ -57,6 +60,7 @@ trait MachineResourcesBase {
5760
case e: Exception => Left(Message.error)
5861
}
5962
}
63+
6064
def getSwap: Either[String, Swap] = {
6165
try {
6266
val swap = StringUtil.dropAndToArray(Process("free -mt") #| Process("grep Swap") !! ,":" , "\\s+")
@@ -69,6 +73,7 @@ trait MachineResourcesBase {
6973
case e: Exception => Left(Message.error)
7074
}
7175
}
76+
7277
def getDiskSpace: DiskSpace = {
7378
val totalSpace = UnitConverter.byteToGB(fileStore.getTotalSpace())
7479
val freeSpace = UnitConverter.byteToGB(fileStore.getUnallocatedSpace())
@@ -81,77 +86,6 @@ trait MachineResourcesBase {
8186
}
8287
}
8388

84-
class MachineResources extends MachineResourcesBase {
85-
val instance = OperatingSystem.osType match {
86-
case OperatingSystem.Linux => new MachineResourcesBase with Linux
87-
case OperatingSystem.Mac => new MachineResourcesBase with Mac
88-
case OperatingSystem.Windows => new MachineResourcesBase with Windows
89-
case _ => new MachineResourcesBase with Other
90-
}
91-
92-
trait Linux extends MachineResourcesBase {
93-
94-
}
95-
96-
trait Mac extends MachineResourcesBase {
97-
override def getCpu: Either[String, Cpu] = {
98-
Left(Message.notSupported)
99-
}
100-
}
101-
102-
trait Windows extends MachineResourcesBase {
103-
override def getCpu: Either[String, Cpu] = {
104-
try {
105-
Right(Cpu(
106-
"-",
107-
"-",
108-
"-",
109-
"-",
110-
"-",
111-
"-",
112-
"-",
113-
"-",
114-
(Process("powershell -Command Get-WmiObject Win32_PerfFormattedData_PerfOS_Processor | Where-Object {$_.Name -eq '_Total'} | %{ $_.PercentProcessorTime }") !!).toString
115-
))
116-
} catch {
117-
case e: Exception => Left(Message.error)
118-
}
119-
}
120-
override def getMemory: Either[String, Memory] = {
121-
try {
122-
val totalMem = (Process("powershell -Command Get-WmiObject -Class Win32_PhysicalMemory | %{ $_.Capacity} | Measure-Object -Sum | %{ ($_.sum /1024/1024) }") !!).toDouble
123-
val availableMem = (Process("powershell -Command Get-WmiObject -Class Win32_PerfFormattedData_PerfOS_Memory | %{ $_.AvailableMBytes}") !!).toDouble
124-
Right(Memory(
125-
totalMem.toString,
126-
(totalMem - availableMem).toString,
127-
Message.notSupported,
128-
Message.notSupported,
129-
//(Process("powershell -Command Get-WmiObject -Class Win32_PerfFormattedData_PerfOS_Memory | %{ $_.CacheBytes /1024/1024 }") !!),
130-
Message.notSupported,
131-
availableMem.toString
132-
))
133-
} catch {
134-
case e: Exception => Left(Message.error)
135-
}
136-
}
137-
override def getSwap: Either[String, Swap] = {
138-
Left(Message.notSupported)
139-
}
140-
}
141-
142-
trait Other extends MachineResourcesBase {
143-
override def getCpu: Either[String, Cpu] = {
144-
Left(Message.notSupported)
145-
}
146-
override def getMemory: Either[String, Memory] = {
147-
Left(Message.notSupported)
148-
}
149-
override def getSwap: Either[String, Swap] = {
150-
Left(Message.notSupported)
151-
}
152-
}
153-
}
154-
15589
case class Cpu (
15690
us: String,
15791
sy: String,

0 commit comments

Comments
 (0)