Skip to content

Commit

Permalink
Remove JSON manipulation from SwiftMetricsDash Fixes #185 (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-spruce authored and sjanuary committed Jun 28, 2018
1 parent 34013d2 commit a8eccd5
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 139 deletions.
3 changes: 2 additions & 1 deletion AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
Authors ordered by first contribution:

- Matt Colegate (https://github.com/mattcolegate)
- Ian Partridge (https://github.com/ianpartridge)
- Ian Partridge (https://github.com/ianpartridge)
- Toby Corbin (https://github.com/tobespc)
- Julie Stalley (https://github.com/stalleyj)
- Navneet Gupta (https://github.com/na-gupta)
- Ricardo Olivieri (https://github.com/rolivieri)
- qibobo (https://github.com/qibobo)
- Sian January (https://github.com/sjanuary)
- Kye Maloy (https://github.com/kyemaloy97)
- Jonathan Spruce (https://github.com/jonathan-spruce)
87 changes: 45 additions & 42 deletions Sources/SwiftMetrics/SwiftMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,53 +120,56 @@ public class SwiftMonitor {

}

private func formatCPU(messages: String) {
if(running) {
for message in messages.components(separatedBy: "\n") {
if message.contains("@#") {
swiftMetrics.loaderApi.logMessage(debug, "formatCPU(): Raising CPU event")
//cpu: startCPU@#1412609879696@#0.00499877@#0.137468
let values = message.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).components(separatedBy: "@#")
if let timeOfSample = Int(values[1]), let percentUsedByApplication = Float(values[2]),
let percentUsedBySystem = Float(values[3]) {
let cpu = CPUData(timeOfSample: timeOfSample, percentUsedByApplication: percentUsedByApplication,
percentUsedBySystem: percentUsedBySystem)
raiseEvent(data: cpu)
} else {
swiftMetrics.loaderApi.logMessage(warning, "formatCPU(): Could not obtain/parse CPU usage data.")
}
private func formatCPU(messages: String) {
if(running) {
for message in messages.split(separator: "\n") {
if message.contains("@#") {
swiftMetrics.loaderApi.logMessage(debug, "formatCPU(): Raising CPU event")
//cpu: startCPU@#1412609879696@#0.00499877@#0.137468
var values:[Substring] = []
for value in message.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).split(separator: "#") {
values.append(value.dropLast())
}
if let timeOfSample = Int(values[1]), let percentUsedByApplication = Float(values[2]),
let percentUsedBySystem = Float(values[3]) {
let cpu = CPUData(timeOfSample: timeOfSample, percentUsedByApplication: percentUsedByApplication,
percentUsedBySystem: percentUsedBySystem)
raiseEvent(data: cpu)
} else {
swiftMetrics.loaderApi.logMessage(warning, "formatCPU(): Could not obtain/parse CPU usage data.")
}
}
}
}
}
}
}

private func formatMemory(messages: String) {
if(running) {
for message in messages.components(separatedBy: "\n") {
if message.contains(",") {
swiftMetrics.loaderApi.logMessage(debug, "formatMemory(): Raising Memory event")
///MemorySource,1415976582652,totalphysicalmemory=16725618688,physicalmemory=52428800,privatememory=374747136,virtualmemory=374747136,freephysicalmemory=1591525376
let values = message.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).components(separatedBy: ",")
if let physicalTotal = Int(values[2].components(separatedBy: "=")[1]),
let physicalFree = Int(values[6].components(separatedBy: "=")[1]),
let timeOfSample = Int(values[1]),
let applicationAddressSpaceSize = Int(values[5].components(separatedBy: "=")[1]),
let applicationPrivateSize = Int(values[4].components(separatedBy: "=")[1]),
let applicationRAMUsed = Int(values[3].components(separatedBy: "=")[1]) {
let physicalUsed = (physicalTotal >= 0 && physicalFree >= 0) ? (physicalTotal - physicalFree) : -1
let memory = MemData(timeOfSample: timeOfSample,
totalRAMOnSystem: physicalTotal,
totalRAMUsed: physicalUsed,
totalRAMFree: physicalFree,
applicationAddressSpaceSize: applicationAddressSpaceSize,
applicationPrivateSize: applicationPrivateSize,
applicationRAMUsed: applicationRAMUsed)
raiseEvent(data: memory)
}
private func formatMemory(messages: String) {
if(running) {
for message in messages.split(separator: "\n") {
if message.contains(",") {
swiftMetrics.loaderApi.logMessage(debug, "formatMemory(): Raising Memory event")
///MemorySource,1415976582652,totalphysicalmemory=16725618688,physicalmemory=52428800,privatememory=374747136,virtualmemory=374747136,freephysicalmemory=1591525376
let values = message.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).split(separator: ",")
if let physicalTotal = Int(values[2].split(separator: "=")[1]),
let physicalFree = Int(values[6].split(separator: "=")[1]),
let timeOfSample = Int(values[1]),
let applicationAddressSpaceSize = Int(values[5].split(separator: "=")[1]),
let applicationPrivateSize = Int(values[4].split(separator: "=")[1]),
let applicationRAMUsed = Int(values[3].split(separator: "=")[1]) {
let physicalUsed = (physicalTotal >= 0 && physicalFree >= 0) ? (physicalTotal - physicalFree) : -1
let memory = MemData(timeOfSample: timeOfSample,
totalRAMOnSystem: physicalTotal,
totalRAMUsed: physicalUsed,
totalRAMFree: physicalFree,
applicationAddressSpaceSize: applicationAddressSpaceSize,
applicationPrivateSize: applicationPrivateSize,
applicationRAMUsed: applicationRAMUsed)
raiseEvent(data: memory)
}
}
}
}
}
}
}

private func formatOSEnv(message: String) {
if(running) {
Expand Down
Loading

0 comments on commit a8eccd5

Please sign in to comment.