-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathStats.js
More file actions
146 lines (122 loc) · 4.5 KB
/
Stats.js
File metadata and controls
146 lines (122 loc) · 4.5 KB
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
/* global */
import {Character} from "../Character.js";
import {Output} from "../output/Output.js";
import {Panel} from "./Panel.js";
import {Utils} from "../Utils.js";
export class StatsPanel extends Panel {
constructor () {
super("stats");
this.addTitle("Stats");
this.addHelpButton([
"Numeric fields representing a timestamp are visible as string.",
"Numeric fields representing a duration are visible as string.",
"Trivial information on worker threads may have been removed."
]);
this.addTable(["/stats"]);
this.addMsg();
}
onShow () {
if (this.table.tBodies[0].children.length === 0) {
// cannot do this in the constructor
// since the framework removes all rows
const tr = Utils.createTr();
this.table.tBodies[0].appendChild(tr);
const td = Utils.createTd();
tr.appendChild(td);
this.statsTd = td;
}
this.onShowNow();
this.updateStatsInterval = window.setInterval(() => {
this.onShowNow();
}, 5000);
}
onShowNow () {
const statsPromise = this.api.getStats();
statsPromise.then((pStatsData) => {
this._handleStats(pStatsData);
return true;
}, (pStatsMsg) => {
this._handleStats(JSON.stringify(pStatsMsg));
return false;
});
}
onHide () {
if (this.updateStatsInterval) {
// stop the timer when nobody is looking
window.clearInterval(this.updateStatsInterval);
this.updateStatsInterval = null;
}
}
// provide a shortened date format for cases
// where we see the timezone multiple times on one screen
static _explainDateTime (pDateTimeInMs) {
if (pDateTimeInMs === null) {
return pDateTimeInMs;
}
return pDateTimeInMs + " (=" + Output.dateTimeStr(pDateTimeInMs) + ")";
}
_handleStats (pStatsData) {
if (this.showErrorRowInstead(pStatsData)) {
this.statsTd.innerHTML = "<span class='text-error'>this error is typically caused by using the <tt>collect_stats: True</tt> setting in the master configuration file, which is broken in at least the recent versions of salt-api</span>";
window.clearInterval(this.updateStatsInterval);
this.updateStatsInterval = null;
return;
}
this.setMsg(null);
for (const topKey in pStatsData) {
// this section should not contain threads
if (topKey === "CherryPy Applications") {
continue;
}
// loop over all threads
const workerThreads = pStatsData[topKey]["Worker Threads"];
if (!workerThreads) {
continue;
}
let first = true;
/* eslint-disable no-labels */
nextThread: for (const threadName of Object.keys(workerThreads).sort((aa, bb) => aa.localeCompare(bb, "en", {"numeric": true}))) {
if (first) {
// always show the first item
// so that the structure is known even when all threads show zeroes
first = false;
continue;
}
const thread = workerThreads[threadName];
// find threads with all-zero statistics
for (const counterName in thread) {
if (counterName === "Enabled") {
// not a counter
continue;
}
if (thread[counterName] !== 0) {
continue nextThread;
}
}
// thread has all-zero statistics, remove that part
workerThreads[threadName] = Character.HORIZONTAL_ELLIPSIS;
}
/* eslint-enable no-labels */
}
const appData = pStatsData["CherryPy Applications"];
if (appData) {
// annotate 3 fields that have a huge integer value
// this turns the fields into strings (was number)
// we'll ignore that now
appData["Current Time"] = StatsPanel._explainDateTime(appData["Current Time"]);
appData["Start Time"] = StatsPanel._explainDateTime(appData["Start Time"]);
appData["Uptime"] = StatsPanel._explainDateTime(appData["Uptime"]);
const requests = appData["Requests"];
for (const key in requests) {
requests[key]["Start Time"] = StatsPanel._explainDateTime(requests[key]["Start Time"]);
requests[key]["End Time"] = StatsPanel._explainDateTime(requests[key]["End Time"]);
}
const slowQueries = appData["Slow Queries"];
for (const key in slowQueries) {
slowQueries[key]["Start Time"] = StatsPanel._explainDateTime(slowQueries[key]["Start Time"]);
slowQueries[key]["End Time"] = StatsPanel._explainDateTime(slowQueries[key]["End Time"]);
}
}
this.statsTd.innerText = Output.formatObject(pStatsData);
}
}