diff --git a/Sources/SwiftMetricsDash/SwiftMetricsDash.swift b/Sources/SwiftMetricsDash/SwiftMetricsDash.swift index f02d10c..619e1d5 100644 --- a/Sources/SwiftMetricsDash/SwiftMetricsDash.swift +++ b/Sources/SwiftMetricsDash/SwiftMetricsDash.swift @@ -69,7 +69,7 @@ public class SwiftMetricsDash { func startServer(router: Router) throws { router.all("/swiftmetrics-dash", middleware: StaticFileServer(path: self.SM.localSourceDirectory + "/public")) - + if self.createServer { let configMgr = ConfigurationManager().load(.environmentVariables) Kitura.addHTTPServer(onPort: configMgr.port, with: router) @@ -82,12 +82,23 @@ class SwiftMetricsService: WebSocketService { private var connections = [String: WebSocketConnection]() var httpAggregateData: HTTPAggregateData = HTTPAggregateData() - var httpURLData:[String:(totalTime:Double, numHits:Double)] = [:] + var httpURLData:[String:(totalTime:Double, numHits:Double, longestTime:Double)] = [:] let httpURLsQueue = DispatchQueue(label: "httpURLsQueue") let httpQueue = DispatchQueue(label: "httpStoreQueue") let jobsQueue = DispatchQueue(label: "jobsQueue") var monitor:SwiftMonitor + // CPU summary data + var totalProcessCPULoad: Double = 0.0; + var totalSystemCPULoad: Double = 0.0; + var cpuLoadSamples: Double = 0 + + // Memory summary data + var totalProcessMemory: Int = 0; + var totalSystemMemory: Int = 0; + var memorySamples: Int = 0; + + public init(monitor: SwiftMonitor) { self.monitor = monitor @@ -100,7 +111,13 @@ class SwiftMetricsService: WebSocketService { func sendCPU(cpu: CPUData) { - let cpuLine = JSON(["topic":"cpu", "payload":["time":"\(cpu.timeOfSample)","process":"\(cpu.percentUsedByApplication)","system":"\(cpu.percentUsedBySystem)"]]) + totalProcessCPULoad += Double(cpu.percentUsedByApplication); + totalSystemCPULoad += Double(cpu.percentUsedBySystem); + cpuLoadSamples += 1; + let processMean = (totalProcessCPULoad / cpuLoadSamples); + let systemMean = (totalSystemCPULoad / cpuLoadSamples); + + let cpuLine = JSON(["topic":"cpu", "payload":["time":"\(cpu.timeOfSample)","process":"\(cpu.percentUsedByApplication)","system":"\(cpu.percentUsedBySystem)","processMean":"\(processMean)","systemMean":"\(systemMean)"]]) for (_,connection) in connections { if let messageToSend = cpuLine.rawString() { @@ -112,11 +129,18 @@ class SwiftMetricsService: WebSocketService { func sendMEM(mem: MemData) { + totalProcessMemory += mem.applicationRAMUsed; + totalSystemMemory += mem.totalRAMUsed; + memorySamples += 1; + let processMean = (totalProcessMemory / memorySamples); + let systemMean = (totalSystemMemory / memorySamples); let memLine = JSON(["topic":"memory","payload":[ "time":"\(mem.timeOfSample)", "physical":"\(mem.applicationRAMUsed)", - "physical_used":"\(mem.totalRAMUsed)" + "physical_used":"\(mem.totalRAMUsed)", + "processMean":"\(processMean)", + "systemMean":"\(systemMean)" ]]) for (_,connection) in connections { @@ -219,10 +243,14 @@ class SwiftMetricsService: WebSocketService { if(urlTuple != nil) { let averageResponseTime = urlTuple!.0 let hits = urlTuple!.1 + var longest = urlTuple!.2 + if (localmyhttp.duration > longest) { + longest = localmyhttp.duration + } // Recalculate the average - self.httpURLData.updateValue(((averageResponseTime * hits + localmyhttp.duration)/(hits + 1), hits + 1), forKey: localmyhttp.url) + self.httpURLData.updateValue(((averageResponseTime * hits + localmyhttp.duration)/(hits + 1), hits + 1, longest), forKey: localmyhttp.url) } else { - self.httpURLData.updateValue((localmyhttp.duration, 1), forKey: localmyhttp.url) + self.httpURLData.updateValue((localmyhttp.duration, 1, localmyhttp.duration), forKey: localmyhttp.url) } } } @@ -251,7 +279,7 @@ class SwiftMetricsService: WebSocketService { var responseData:[JSON] = [] let localCopy = self.httpURLData for (key, value) in localCopy { - let json = JSON(["url":key, "averageResponseTime": value.0]) + let json = JSON(["url":key, "averageResponseTime": value.0, "hits": value.1, "longestResponseTime": value.2]) responseData.append(json) } var messageToSend:String="" diff --git a/public/graphmetrics b/public/graphmetrics index fa44723..5cbd2f6 160000 --- a/public/graphmetrics +++ b/public/graphmetrics @@ -1 +1 @@ -Subproject commit fa44723a1e0179ee5364ef4f9ab6ed571ce62aeb +Subproject commit 5cbd2f6ad0d46f548e223d36eef7ef1e3d6b1a4d diff --git a/public/index.html b/public/index.html index 7c4ef7a..9447579 100755 --- a/public/index.html +++ b/public/index.html @@ -34,21 +34,47 @@