Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new summary tab to dashboard #177

Merged
merged 6 commits into from
Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions Sources/SwiftMetricsDash/SwiftMetricsDash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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() {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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=""
Expand Down
114 changes: 89 additions & 25 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,47 @@

<div class="headerDiv"><span class="rightHeader"></span><span class="leftHeader"></span></div>

<div class="container-fluid">
<div class="row">
<div class="col-md-6 hideable" id="httpDiv1"></div>
<div class="col-md-3 hideable" id="httpDiv2"></div>
<div class="col-md-3 hideable" id="httpDiv3"></div>
<div id="tabs" class="container">

<ul class="nav nav-tabs">
<li class="active">
<a href="#dashboard" id="dashboard-tab" data-toggle="tab">Dashboard</a>
</li>
<li>
<a href="#summary" id="summary-tab" data-toggle="tab">Summary</a>
</li>
</ul>

<div class="tab-content ">
<div class="tab-pane active" id="dashboard">
<div class="container-fluid">
<div class="row">
<div class="col-md-6 hideable" id="cpuDiv1"></div>
<div class="col-md-6 hideable" id="httpDiv1"></div>
</div>
<div class="row">
<div class="col-md-6 hideable" id="memDiv1"></div>
<div class="col-md-6 hideable" id="httpDiv2"></div>
</div>
</div>
</div>

<div class="row">
<div class="col-md-4 hideable" id="cpuDiv1"></div>
<div class="col-md-4 hideable" id="memDiv1"></div>
<div class="col-md-4 hideable" id="envDiv"></div>
<div class="tab-pane" id="summary">
<div class="container-fluid">
<div class="row">
<div class="graph-container col-md-7">
<div class="col-md-12 height-fill hideable" id="httpSummaryDiv"></div>
</div>
<div class="graph-container col-md-5">
<div class="col-md-12 hideable" id="envDiv"></div>
<div class="col-md-12 hideable" id="summaryDiv"></div>
</div>
</div>
</div>
</div>
</div>
</div>


<script type="text/javascript" src="graphmetrics/js/i18n.js"></script>
<script>
// Global variables
Expand Down Expand Up @@ -90,11 +116,12 @@

</script>
<script type="text/javascript" src="graphmetrics/js/header.js"></script>
<script type="text/javascript" src="graphmetrics/js/envTable.js"></script>
<script type="text/javascript" src="graphmetrics/js/textTable.js"></script>
<script type="text/javascript" src="graphmetrics/js/cpuChart.js"></script>
<script type="text/javascript" src="graphmetrics/js/httpThroughPutChart.js"></script>
<script type="text/javascript" src="graphmetrics/js/httpRequestsChart.js"></script>
<script type="text/javascript" src="graphmetrics/js/httpTop5.js"></script>
<script type="text/javascript" src="graphmetrics/js/httpSummary.js"></script>
<script type="text/javascript" src="graphmetrics/js/top5.js"></script>
<script type="text/javascript" src="graphmetrics/js/memChart.js"></script>
<script>
let hostname = location.host;
Expand All @@ -107,8 +134,12 @@
webSocketProtocol = "wss://"
}
var client = new WebSocket(webSocketProtocol + hostname + dashboardRoot,"swiftmetrics-dash")
let summary = {cpu:{}, gc:{}, memory:{}};

setHttpTop5Options({host: hostname, filteredPath: location.origin + dashboardRoot});
let envTable = new TextTable('#envDiv', '#summary', localizedStrings.envTitle);
let summaryTable = new TextTable('#summaryDiv', '#summary', localizedStrings.summaryTitle);
let httpSummary = new HttpSummary('#httpSummaryDiv', '#summary', localizedStrings.httpSummaryTitle);
httpSummary.setHttpSummaryOptions({host: hostname, filteredPath: location.origin + dashboardRoot});

client.onmessage = function(message) {
received = JSON.parse(message.data);
Expand All @@ -118,39 +149,72 @@
switch (topic) {
case 'cpu':
updateCPUData(payload);
summary.cpu.systemMean = received.payload.systemMean;
summary.cpu.processMean = received.payload.processMean;
break;
case 'memory':
updateMemData(payload);
summary.memory.systemMean = received.payload.systemMean;
summary.memory.processMean = received.payload.processMean;
break;
case 'env':
populateEnvTable(payload);
envTable.populateTableJSON(payload);
break;
case 'http':
updateHttpData(payload);
break;
case 'httpURLs':
updateURLData(payload);
httpSummary.updateURLData(payload);
break;
case 'title' :
updateHeader(payload);
break;

}
let summaryData = [];
if( summary.cpu.processMean ) {
let value = new Number(summary.cpu.processMean);
let valueStr = value.toLocaleString([],{style: 'percent', minimumSignificantDigits: 4, maximumSignificantDigits: 4});
summaryData.push({Parameter: 'Average Process CPU', Value: valueStr});
}
if( summary.cpu.systemMean ) {
let value = new Number(summary.cpu.systemMean);
let valueStr = value.toLocaleString([],{style: 'percent', minimumSignificantDigits: 4, maximumSignificantDigits: 4});
summaryData.push({Parameter: 'Average System CPU', Value: valueStr});
}
if( summary.memory.processMean ) {
summaryData.push({Parameter: 'Average Process Memory', Value: `${summary.memory.processMean} bytes`});
}
if( summary.memory.systemMean ) {
summaryData.push({Parameter: 'Average System Memory', Value: `${summary.memory.systemMean} bytes`});
}
summaryTable.populateTable(summaryData);
}

let selected_tab = "dashboard-tab"
window.addEventListener('resize', resize);
// Also re-size when we change tabs in case we re-sized
// while the new tab wasn't visible.
$('.nav-tabs a').on('shown.bs.tab', function(event) {
selected_tab = event.target.id;
resize();
});

function resize() {
canvasWidth = $("#cpuDiv1").width() - 8,
httpCanvasWidth = $("#httpDiv1").width() - 8,
graphWidth = canvasWidth - margin.left - margin.right,
httpGraphWidth = httpCanvasWidth - margin.left - margin.right;
resizeCPUChart();
resizeHttpChart();
resizeHttpThroughputChart();
resizeHttpTop5Chart();
resizeMemChart();
resizeEnvTable();
if (selected_tab == "dashboard-tab") {
canvasWidth = $("#cpuDiv1").width() - 8,
httpCanvasWidth = $("#httpDiv1").width() - 8,
graphWidth = canvasWidth - margin.left - margin.right,
httpGraphWidth = httpCanvasWidth - margin.left - margin.right;
resizeCPUChart();
resizeHttpChart();
resizeHttpThroughputChart();
resizeMemChart();
} else if(selected_tab == "summary-tab") {
envTable.resizeTable();
summaryTable.resizeTable();
httpSummary.resizeTable();
}
}

</script>
Expand Down