-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
values.js
354 lines (292 loc) · 13.9 KB
/
values.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
Copyright (c) 2018, Chris Monahan <[email protected]>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the GNOME nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import GObject from 'gi://GObject';
const cbFun = (d, c) => {
let bb = d[1] % c[0],
aa = (d[1] - bb) / c[0];
aa = aa > 0 ? aa + c[1] : '';
return [d[0] + aa, bb];
};
export const Values = GObject.registerClass({
GTypeName: 'Values',
}, class Values extends GObject.Object {
_init(settings, sensorIcons) {
this._settings = settings;
this._sensorIcons = sensorIcons;
this._networkSpeedOffset = {};
this._networkSpeeds = {};
this._history = {};
//this._history2 = {};
this.resetHistory();
}
_legible(value, sensorClass) {
let unit = 1000;
if (value === null) return 'N/A';
let use_higher_precision = this._settings.get_boolean('use-higher-precision');
let memory_measurement = this._settings.get_int('memory-measurement')
let storage_measurement = this._settings.get_int('storage-measurement')
let use_bps = (this._settings.get_int('network-speed-format') == 1);
let format = '';
let ending = '';
let exp = 0;
var decimal = [ 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ];
var binary = [ 'B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB' ];
var hertz = [ 'Hz', 'KHz', 'MHz', 'GHz', 'THz', 'PHz', 'EHz', 'ZHz' ];
switch (sensorClass) {
case 'percent':
format = (use_higher_precision)?'%.1f%s':'%d%s';
value = value * 100;
if (value > 100) value = 100;
ending = '%';
break;
case 'temp':
value = value / 1000;
ending = '°C';
// are we converting to fahrenheit?
if (this._settings.get_int('unit') == 1) {
value = ((9 / 5) * value + 32);
ending = '°F';
}
format = (use_higher_precision)?'%.1f%s':'%d%s';
break;
case 'fan':
format = '%d %s';
ending = 'RPM';
break;
case 'in': // voltage
value = value / 1000;
format = ((value >= 0) ? '+' : '-') + ((use_higher_precision)?'%.2f %s':'%.1f %s');
ending = 'V';
break;
case 'hertz':
if (value > 0) {
exp = Math.floor(Math.log(value) / Math.log(unit));
if (value >= Math.pow(unit, exp) * (unit - 0.05)) exp++;
value = parseFloat((value / Math.pow(unit, exp)));
}
format = (use_higher_precision)?'%.2f %s':'%.1f %s';
ending = hertz[exp];
break;
case 'memory':
unit = (memory_measurement)?1000:1024;
if (value > 0) {
value *= unit;
exp = Math.floor(Math.log(value) / Math.log(unit));
if (value >= Math.pow(unit, exp) * (unit - 0.05)) exp++;
value = parseFloat((value / Math.pow(unit, exp)));
}
format = (use_higher_precision)?'%.2f %s':'%.1f %s';
if (memory_measurement)
ending = decimal[exp];
else
ending = binary[exp];
break;
case 'storage':
unit = (storage_measurement)?1000:1024;
if (value > 0) {
exp = Math.floor(Math.log(value) / Math.log(unit));
if (value >= Math.pow(unit, exp) * (unit - 0.05)) exp++;
value = parseFloat((value / Math.pow(unit, exp)));
}
format = (use_higher_precision)?'%.2f %s':'%.1f %s';
if (storage_measurement)
ending = decimal[exp];
else
ending = binary[exp];
break;
case 'speed':
if (value > 0) {
if (use_bps) value *= 8;
exp = Math.floor(Math.log(value) / Math.log(unit));
if (value >= Math.pow(unit, exp) * (unit - 0.05)) exp++;
value = parseFloat((value / Math.pow(unit, exp)));
}
format = (use_higher_precision)?'%.1f %s':'%.0f %s';
if (use_bps) {
ending = decimal[exp].replace('B', 'bps');
} else {
ending = decimal[exp] + '/s';
}
break;
case 'runtime':
case 'uptime':
let scale = [24, 60, 60];
let units = ['d ', 'h ', 'm '];
// show seconds on higher precision or if value under a minute
if (sensorClass != 'runtime' && (use_higher_precision || value < 60)) {
scale.push(1);
units.push('s ');
}
let rslt = scale.map((d, i, a) => a.slice(i).reduce((d, c) => d * c))
.map((d, i) => ([d, units[i]]))
.reduce(cbFun, ['', value]);
value = rslt[0].trim();
format = '%s';
break;
case 'milliamp':
format = (use_higher_precision)?'%.1f %s':'%d %s';
value = value / 1000;
ending = 'mA';
break;
case 'milliamp-hour':
format = (use_higher_precision)?'%.1f %s':'%d %s';
value = value / 1000;
ending = 'mAh';
break;
case 'watt':
format = (use_higher_precision)?'%.2f %s':'%.1f %s';
value = value / 1000000;
ending = 'W';
break;
case 'watt-gpu':
format = (use_higher_precision)?'%.2f %s':'%.1f %s';
ending = 'W';
break;
case 'watt-hour':
format = (use_higher_precision)?'%.2f %s':'%.1f %s';
value = value / 1000000;
ending = 'Wh';
break;
case 'load':
format = (use_higher_precision)?'%.2f %s':'%.1f %s';
break;
case 'pcie':
let split = value.split('x');
value = 'PCIe ' + parseInt(split[0]) + (split.length > 1 ? ' x' + parseInt(split[1]) : '');
format = '%s';
break;
default:
format = '%s';
break;
}
return format.format(value, ending);
}
returnIfDifferent(dwell, label, value, type, format, key) {
let output = [];
// make sure the keys exist
if (!(type in this._history)) this._history[type] = {};
// no sense in continuing when the raw value has not changed
if (type != 'network-rx' && type != 'network-tx' &&
key in this._history[type] && this._history[type][key][1] == value)
return output;
// is the value different from last time?
let legible = this._legible(value, format);
// don't return early when dealing with network traffic
if (type != 'network-rx' && type != 'network-tx') {
// only update when we are coming through for the first time, or if a value has changed
if (key in this._history[type] && this._history[type][key][0] == legible)
return output;
// add label as it was sent from sensors class
output.push([label, legible, type, key]);
}
// save previous values to update screen on changes only
let previousValue = this._history[type][key];
this._history[type][key] = [legible, value];
// process average, min and max values
if (type == 'temperature' || type == 'voltage' || type == 'fan') {
let vals = Object.values(this._history[type]).map(x => parseFloat(x[1]));
// show value in group even if there is one value present
let sum = vals.reduce((a, b) => a + b);
let avg = this._legible(sum / vals.length, format);
output.push([type, avg, type + '-group', '']);
// If only one value is present, don't display avg, min and max
if (vals.length > 1) {
output.push(['Average', avg, type, '__' + type + '_avg__']);
// calculate Minimum value
let min = Math.min(...vals);
min = this._legible(min, format);
output.push(['Minimum', min, type, '__' + type + '_min__']);
// calculate Maximum value
let max = Math.max(...vals);
max = this._legible(max, format);
output.push(['Maximum', max, type, '__' + type + '_max__']);
}
} else if (type == 'network-rx' || type == 'network-tx') {
let direction = type.split('-')[1];
// appends total upload and download for all interfaces for #216
let vals = Object.values(this._history[type]).map(x => parseFloat(x[1]));
let sum = vals.reduce((partialSum, a) => partialSum + a, 0);
output.push(['Boot ' + direction, this._legible(sum, format), type, '__' + type + '_boot__']);
// keeps track of session start point
if (!(key in this._networkSpeedOffset) || this._networkSpeedOffset[key] <= 0)
this._networkSpeedOffset[key] = sum;
// outputs session upload and download for all interfaces for #234
output.push(['Session ' + direction, this._legible(sum - this._networkSpeedOffset[key], format), type, '__' + type + '_ses__']);
// calculate speed for this interface
let speed = (value - previousValue[1]) / dwell;
output.push([label, this._legible(speed, 'speed'), type, key]);
// store speed for Device report
if (!(direction in this._networkSpeeds)) this._networkSpeeds[direction] = {};
if (!(label in this._networkSpeeds[direction])) this._networkSpeeds[direction][label] = 0;
// store value for next go around
if (value > 0 || (value == 0 && !this._settings.get_boolean('hide-zeros')))
this._networkSpeeds[direction][label] = speed;
// calculate total upload and download device speed
for (let direction in this._networkSpeeds) {
let sum = 0;
for (let iface in this._networkSpeeds[direction])
sum += parseFloat(this._networkSpeeds[direction][iface]);
sum = this._legible(sum, 'speed');
output.push(['Device ' + direction, sum, 'network-' + direction, '__network-' + direction + '_max__']);
// append download speed to group itself
if (direction == 'rx') output.push([type, sum, type + '-group', '']);
}
}
/*
global.log('before', JSON.stringify(output));
for (let i = output.length - 1; i >= 0; i--) {
let sensor = output[i];
// sensor[0]=label, sensor[1]=value, sensor[2]=type, sensor[3]=key)
//["CPU Core 5","46°C","temperature","_temperature_hwmon8temp7_"]
// make sure the keys exist
if (!(sensor[2] in this._history2)) this._history2[sensor[2]] = {};
if (sensor[3] in this._history2[sensor[2]]) {
if (this._history2[sensor[2]][sensor[3]] == sensor[1]) {
output.splice(i, 1);
}
}
this._history2[sensor[2]][sensor[3]] = sensor[1];
}
global.log(' after', JSON.stringify(output));
global.log('***************************');
*/
return output;
}
resetHistory(numGpus) {
// don't call this._history = {}, as we want to keep network-rx and network-tx
// otherwise network history statistics will start over
for (let sensor in this._sensorIcons) {
//each gpu has it's own sensor name and thus must be handled separately
if(sensor === 'gpu') continue;
this._history[sensor] = {};
this._history[sensor + '-group'] = {};
//this._history2[sensor] = {};
//this._history2[sensor + '-group'] = {};
}
for(let i = 1; i <= numGpus; i++){
this._history['gpu#' + i] = {};
this._history['gpu#' + i + '-group'] = {};
}
}
});