forked from RafalWilinski/express-status-monitor
-
Notifications
You must be signed in to change notification settings - Fork 16
/
koa-monitor-frontend.js
224 lines (194 loc) · 6.84 KB
/
koa-monitor-frontend.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* global Chart, location, io */
Chart.defaults.global.defaultFontSize = 8
Chart.defaults.global.animation.duration = 500
Chart.defaults.global.legend.display = false
Chart.defaults.global.elements.line.backgroundColor = 'rgba(0,0,0,0)'
Chart.defaults.global.elements.line.borderColor = 'rgba(0,0,0,0.9)'
Chart.defaults.global.elements.line.borderWidth = 2
const socket = io(`${location.protocol}//${location.hostname}:${location.port}`)
let defaultSpan = 0
const spans = []
const defaultDataset = {
label: '',
data: [],
lineTension: 0.2,
pointRadius: 0
}
const defaultOptions = {
scales: {
yAxes: [
{
ticks: {
beginAtZero: true
}
}
],
xAxes: [
{
type: 'time',
time: {
unitStepSize: 30
},
gridLines: {
display: false
}
}
]
},
tooltips: {
enabled: false
},
responsive: true,
maintainAspectRatio: false,
animation: false
}
const createChart = (ctx, dataset) =>
new Chart(ctx, {
type: 'line',
data: {
labels: [],
datasets: dataset
},
options: defaultOptions
})
const addTimestamp = ({ timestamp }) => timestamp
const cpuDataset = [Object.create(defaultDataset)]
const memDataset = [Object.create(defaultDataset)]
const loadDataset = [Object.create(defaultDataset)]
const responseTimeDataset = [Object.create(defaultDataset)]
const rpsDataset = [Object.create(defaultDataset)]
const cpuStat = document.getElementById('cpuStat')
const memStat = document.getElementById('memStat')
const loadStat = document.getElementById('loadStat')
const responseTimeStat = document.getElementById('responseTimeStat')
const rpsStat = document.getElementById('rpsStat')
const cpuChartCtx = document.getElementById('cpuChart')
const memChartCtx = document.getElementById('memChart')
const loadChartCtx = document.getElementById('loadChart')
const responseTimeChartCtx = document.getElementById('responseTimeChart')
const rpsChartCtx = document.getElementById('rpsChart')
const cpuChart = createChart(cpuChartCtx, cpuDataset)
const memChart = createChart(memChartCtx, memDataset)
const loadChart = createChart(loadChartCtx, loadDataset)
const responseTimeChart = createChart(responseTimeChartCtx, responseTimeDataset)
const rpsChart = createChart(rpsChartCtx, rpsDataset)
const charts = [cpuChart, memChart, loadChart, responseTimeChart, rpsChart]
const onSpanChange = ({ target }) => {
target.classList.add('active')
defaultSpan = parseInt(target.id)
const otherSpans = document.getElementsByTagName('span')
for (let i = 0; i < otherSpans.length; i++) {
if (otherSpans[i] !== target) otherSpans[i].classList.remove('active')
}
socket.emit('change')
}
socket.on('start', (data) => {
// Remove last element of Array because it contains malformed responses data.
// To keep consistency we also remove os data.
data[defaultSpan].responses.pop()
data[defaultSpan].os.pop()
// Bug fix for requiring browser refresh when koa-server restarted
const osData = data[defaultSpan].os[data[defaultSpan].os.length - 1]
if (!osData || !('cpu' in osData)) {
socket.emit('change')
return
}
cpuStat.textContent = `${data[defaultSpan].os[
data[defaultSpan].os.length - 1
].cpu.toFixed(1)}%`
cpuChart.data.datasets[0].data = data[defaultSpan].os.map(({ cpu }) => cpu)
cpuChart.data.labels = data[defaultSpan].os.map(addTimestamp)
memStat.textContent = `${data[defaultSpan].os[
data[defaultSpan].os.length - 1
].memory.toFixed(1)}MB`
memChart.data.datasets[0].data = data[defaultSpan].os.map(
({ memory }) => memory
)
memChart.data.labels = data[defaultSpan].os.map(addTimestamp)
loadStat.textContent = data[defaultSpan].os[
data[defaultSpan].os.length - 1
].load[defaultSpan].toFixed(2)
loadChart.data.datasets[0].data = data[defaultSpan].os.map(
({ load }) => load[0]
)
loadChart.data.labels = data[defaultSpan].os.map(addTimestamp)
responseTimeStat.textContent = `${data[defaultSpan].responses[
data[defaultSpan].responses.length - 1
].mean.toFixed(2)}ms`
responseTimeChart.data.datasets[0].data = data[defaultSpan].responses.map(
({ mean }) => mean
)
responseTimeChart.data.labels = data[defaultSpan].responses.map(addTimestamp)
if (data[defaultSpan].responses.length >= 2) {
const deltaTime =
data[defaultSpan].responses[data[defaultSpan].responses.length - 1]
.timestamp -
data[defaultSpan].responses[data[defaultSpan].responses.length - 2]
.timestamp
rpsStat.textContent = (
(data[defaultSpan].responses[data[defaultSpan].responses.length - 1]
.count /
deltaTime) *
1000
).toFixed(2)
rpsChart.data.datasets[0].data = data[defaultSpan].responses.map(
({ count }) => (count / deltaTime) * 1000
)
rpsChart.data.labels = data[defaultSpan].responses.map(addTimestamp)
}
charts.forEach((chart) => {
chart.update()
})
const spanControls = document.getElementById('span-controls')
if (data.length !== spans.length) {
data.forEach(({ retention, interval }, index) => {
spans.push({
retention: retention,
interval: interval
})
const spanNode = document.createElement('span')
const textNode = document.createTextNode(
`${(retention * interval) / 60}M`
)
spanNode.appendChild(textNode)
spanNode.setAttribute('id', index)
spanNode.onclick = onSpanChange
spanControls.appendChild(spanNode)
})
document.getElementsByTagName('span')[0].classList.add('active')
}
})
socket.on('stats', ({ retention, interval, os, responses }) => {
if (
retention === spans[defaultSpan].retention &&
interval === spans[defaultSpan].interval
) {
cpuStat.textContent = `${os.cpu.toFixed(1)}%`
cpuChart.data.datasets[0].data.push(os.cpu)
cpuChart.data.labels.push(os.timestamp)
memStat.textContent = `${os.memory.toFixed(1)}MB`
memChart.data.datasets[0].data.push(os.memory)
memChart.data.labels.push(os.timestamp)
loadStat.textContent = os.load[0].toFixed(2)
loadChart.data.datasets[0].data.push(os.load[0])
loadChart.data.labels.push(os.timestamp)
responseTimeStat.textContent = `${responses.mean.toFixed(2)}ms`
responseTimeChart.data.datasets[0].data.push(responses.mean)
responseTimeChart.data.labels.push(responses.timestamp)
const deltaTime =
responses.timestamp -
rpsChart.data.labels[rpsChart.data.labels.length - 1]
if (deltaTime > 0) {
rpsStat.textContent = ((responses.count / deltaTime) * 1000).toFixed(2)
rpsChart.data.datasets[0].data.push((responses.count / deltaTime) * 1000)
rpsChart.data.labels.push(responses.timestamp)
}
charts.forEach((chart) => {
if (spans[defaultSpan].retention < chart.data.labels.length) {
chart.data.datasets[0].data.shift()
chart.data.labels.shift()
}
chart.update()
})
}
})