values.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. Copyright (c) 2018, Chris Monahan <chris@corecoding.com>
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright
  6. notice, this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright
  8. notice, this list of conditions and the following disclaimer in the
  9. documentation and/or other materials provided with the distribution.
  10. * Neither the name of the GNOME nor the names of its contributors may be
  11. used to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  14. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  17. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. import GObject from 'gi://GObject';
  25. const cbFun = (d, c) => {
  26. let bb = d[1] % c[0],
  27. aa = (d[1] - bb) / c[0];
  28. aa = aa > 0 ? aa + c[1] : '';
  29. return [d[0] + aa, bb];
  30. };
  31. export const Values = GObject.registerClass({
  32. GTypeName: 'Values',
  33. }, class Values extends GObject.Object {
  34. _init(settings, sensorIcons) {
  35. this._settings = settings;
  36. this._sensorIcons = sensorIcons;
  37. this._networkSpeedOffset = {};
  38. this._networkSpeeds = {};
  39. this._history = {};
  40. //this._history2 = {};
  41. this.resetHistory();
  42. }
  43. _legible(value, sensorClass) {
  44. let unit = 1000;
  45. if (value === null) return 'N/A';
  46. let use_higher_precision = this._settings.get_boolean('use-higher-precision');
  47. let memory_measurement = this._settings.get_int('memory-measurement')
  48. let storage_measurement = this._settings.get_int('storage-measurement')
  49. let use_bps = (this._settings.get_int('network-speed-format') == 1);
  50. let format = '';
  51. let ending = '';
  52. let exp = 0;
  53. var decimal = [ 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ];
  54. var binary = [ 'B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB' ];
  55. var hertz = [ 'Hz', 'KHz', 'MHz', 'GHz', 'THz', 'PHz', 'EHz', 'ZHz' ];
  56. switch (sensorClass) {
  57. case 'percent':
  58. format = (use_higher_precision)?'%.1f%s':'%d%s';
  59. value = value * 100;
  60. if (value > 100) value = 100;
  61. ending = '%';
  62. break;
  63. case 'temp':
  64. value = value / 1000;
  65. ending = '°C';
  66. // are we converting to fahrenheit?
  67. if (this._settings.get_int('unit') == 1) {
  68. value = ((9 / 5) * value + 32);
  69. ending = '°F';
  70. }
  71. format = (use_higher_precision)?'%.1f%s':'%d%s';
  72. break;
  73. case 'fan':
  74. format = '%d %s';
  75. ending = 'RPM';
  76. break;
  77. case 'in': // voltage
  78. value = value / 1000;
  79. format = ((value >= 0) ? '+' : '-') + ((use_higher_precision)?'%.2f %s':'%.1f %s');
  80. ending = 'V';
  81. break;
  82. case 'hertz':
  83. if (value > 0) {
  84. exp = Math.floor(Math.log(value) / Math.log(unit));
  85. if (value >= Math.pow(unit, exp) * (unit - 0.05)) exp++;
  86. value = parseFloat((value / Math.pow(unit, exp)));
  87. }
  88. format = (use_higher_precision)?'%.2f %s':'%.1f %s';
  89. ending = hertz[exp];
  90. break;
  91. case 'memory':
  92. unit = (memory_measurement)?1000:1024;
  93. if (value > 0) {
  94. value *= unit;
  95. exp = Math.floor(Math.log(value) / Math.log(unit));
  96. if (value >= Math.pow(unit, exp) * (unit - 0.05)) exp++;
  97. value = parseFloat((value / Math.pow(unit, exp)));
  98. }
  99. format = (use_higher_precision)?'%.2f %s':'%.1f %s';
  100. if (memory_measurement)
  101. ending = decimal[exp];
  102. else
  103. ending = binary[exp];
  104. break;
  105. case 'storage':
  106. unit = (storage_measurement)?1000:1024;
  107. if (value > 0) {
  108. exp = Math.floor(Math.log(value) / Math.log(unit));
  109. if (value >= Math.pow(unit, exp) * (unit - 0.05)) exp++;
  110. value = parseFloat((value / Math.pow(unit, exp)));
  111. }
  112. format = (use_higher_precision)?'%.2f %s':'%.1f %s';
  113. if (storage_measurement)
  114. ending = decimal[exp];
  115. else
  116. ending = binary[exp];
  117. break;
  118. case 'speed':
  119. if (value > 0) {
  120. if (use_bps) value *= 8;
  121. exp = Math.floor(Math.log(value) / Math.log(unit));
  122. if (value >= Math.pow(unit, exp) * (unit - 0.05)) exp++;
  123. value = parseFloat((value / Math.pow(unit, exp)));
  124. }
  125. format = (use_higher_precision)?'%.1f %s':'%.0f %s';
  126. if (use_bps) {
  127. ending = decimal[exp].replace('B', 'bps');
  128. } else {
  129. ending = decimal[exp] + '/s';
  130. }
  131. break;
  132. case 'runtime':
  133. case 'uptime':
  134. let scale = [24, 60, 60];
  135. let units = ['d ', 'h ', 'm '];
  136. // show seconds on higher precision or if value under a minute
  137. if (sensorClass != 'runtime' && (use_higher_precision || value < 60)) {
  138. scale.push(1);
  139. units.push('s ');
  140. }
  141. let rslt = scale.map((d, i, a) => a.slice(i).reduce((d, c) => d * c))
  142. .map((d, i) => ([d, units[i]]))
  143. .reduce(cbFun, ['', value]);
  144. value = rslt[0].trim();
  145. format = '%s';
  146. break;
  147. case 'milliamp':
  148. format = (use_higher_precision)?'%.1f %s':'%d %s';
  149. value = value / 1000;
  150. ending = 'mA';
  151. break;
  152. case 'milliamp-hour':
  153. format = (use_higher_precision)?'%.1f %s':'%d %s';
  154. value = value / 1000;
  155. ending = 'mAh';
  156. break;
  157. case 'watt':
  158. format = (use_higher_precision)?'%.2f %s':'%.1f %s';
  159. value = value / 1000000;
  160. ending = 'W';
  161. break;
  162. case 'watt-gpu':
  163. format = (use_higher_precision)?'%.2f %s':'%.1f %s';
  164. ending = 'W';
  165. break;
  166. case 'watt-hour':
  167. format = (use_higher_precision)?'%.2f %s':'%.1f %s';
  168. value = value / 1000000;
  169. ending = 'Wh';
  170. break;
  171. case 'load':
  172. format = (use_higher_precision)?'%.2f %s':'%.1f %s';
  173. break;
  174. case 'pcie':
  175. let split = value.split('x');
  176. value = 'PCIe ' + parseInt(split[0]) + (split.length > 1 ? ' x' + parseInt(split[1]) : '');
  177. format = '%s';
  178. break;
  179. default:
  180. format = '%s';
  181. break;
  182. }
  183. return format.format(value, ending);
  184. }
  185. returnIfDifferent(dwell, label, value, type, format, key) {
  186. let output = [];
  187. // make sure the keys exist
  188. if (!(type in this._history)) this._history[type] = {};
  189. // no sense in continuing when the raw value has not changed
  190. if (type != 'network-rx' && type != 'network-tx' &&
  191. key in this._history[type] && this._history[type][key][1] == value)
  192. return output;
  193. // is the value different from last time?
  194. let legible = this._legible(value, format);
  195. // don't return early when dealing with network traffic
  196. if (type != 'network-rx' && type != 'network-tx') {
  197. // only update when we are coming through for the first time, or if a value has changed
  198. if (key in this._history[type] && this._history[type][key][0] == legible)
  199. return output;
  200. // add label as it was sent from sensors class
  201. output.push([label, legible, type, key]);
  202. }
  203. // save previous values to update screen on changes only
  204. let previousValue = this._history[type][key];
  205. this._history[type][key] = [legible, value];
  206. // process average, min and max values
  207. if (type == 'temperature' || type == 'voltage' || type == 'fan') {
  208. let vals = Object.values(this._history[type]).map(x => parseFloat(x[1]));
  209. // show value in group even if there is one value present
  210. let sum = vals.reduce((a, b) => a + b);
  211. let avg = this._legible(sum / vals.length, format);
  212. output.push([type, avg, type + '-group', '']);
  213. // If only one value is present, don't display avg, min and max
  214. if (vals.length > 1) {
  215. output.push(['Average', avg, type, '__' + type + '_avg__']);
  216. // calculate Minimum value
  217. let min = Math.min(...vals);
  218. min = this._legible(min, format);
  219. output.push(['Minimum', min, type, '__' + type + '_min__']);
  220. // calculate Maximum value
  221. let max = Math.max(...vals);
  222. max = this._legible(max, format);
  223. output.push(['Maximum', max, type, '__' + type + '_max__']);
  224. }
  225. } else if (type == 'network-rx' || type == 'network-tx') {
  226. let direction = type.split('-')[1];
  227. // appends total upload and download for all interfaces for #216
  228. let vals = Object.values(this._history[type]).map(x => parseFloat(x[1]));
  229. let sum = vals.reduce((partialSum, a) => partialSum + a, 0);
  230. output.push(['Boot ' + direction, this._legible(sum, format), type, '__' + type + '_boot__']);
  231. // keeps track of session start point
  232. if (!(key in this._networkSpeedOffset) || this._networkSpeedOffset[key] <= 0)
  233. this._networkSpeedOffset[key] = sum;
  234. // outputs session upload and download for all interfaces for #234
  235. output.push(['Session ' + direction, this._legible(sum - this._networkSpeedOffset[key], format), type, '__' + type + '_ses__']);
  236. // calculate speed for this interface
  237. let speed = (value - previousValue[1]) / dwell;
  238. output.push([label, this._legible(speed, 'speed'), type, key]);
  239. // store speed for Device report
  240. if (!(direction in this._networkSpeeds)) this._networkSpeeds[direction] = {};
  241. if (!(label in this._networkSpeeds[direction])) this._networkSpeeds[direction][label] = 0;
  242. // store value for next go around
  243. if (value > 0 || (value == 0 && !this._settings.get_boolean('hide-zeros')))
  244. this._networkSpeeds[direction][label] = speed;
  245. // calculate total upload and download device speed
  246. for (let direction in this._networkSpeeds) {
  247. let sum = 0;
  248. for (let iface in this._networkSpeeds[direction])
  249. sum += parseFloat(this._networkSpeeds[direction][iface]);
  250. sum = this._legible(sum, 'speed');
  251. output.push(['Device ' + direction, sum, 'network-' + direction, '__network-' + direction + '_max__']);
  252. // append download speed to group itself
  253. if (direction == 'rx') output.push([type, sum, type + '-group', '']);
  254. }
  255. }
  256. /*
  257. global.log('before', JSON.stringify(output));
  258. for (let i = output.length - 1; i >= 0; i--) {
  259. let sensor = output[i];
  260. // sensor[0]=label, sensor[1]=value, sensor[2]=type, sensor[3]=key)
  261. //["CPU Core 5","46°C","temperature","_temperature_hwmon8temp7_"]
  262. // make sure the keys exist
  263. if (!(sensor[2] in this._history2)) this._history2[sensor[2]] = {};
  264. if (sensor[3] in this._history2[sensor[2]]) {
  265. if (this._history2[sensor[2]][sensor[3]] == sensor[1]) {
  266. output.splice(i, 1);
  267. }
  268. }
  269. this._history2[sensor[2]][sensor[3]] = sensor[1];
  270. }
  271. global.log(' after', JSON.stringify(output));
  272. global.log('***************************');
  273. */
  274. return output;
  275. }
  276. resetHistory(numGpus) {
  277. // don't call this._history = {}, as we want to keep network-rx and network-tx
  278. // otherwise network history statistics will start over
  279. for (let sensor in this._sensorIcons) {
  280. //each gpu has it's own sensor name and thus must be handled separately
  281. if(sensor === 'gpu') continue;
  282. this._history[sensor] = {};
  283. this._history[sensor + '-group'] = {};
  284. //this._history2[sensor] = {};
  285. //this._history2[sensor + '-group'] = {};
  286. }
  287. for(let i = 1; i <= numGpus; i++){
  288. this._history['gpu#' + i] = {};
  289. this._history['gpu#' + i + '-group'] = {};
  290. }
  291. }
  292. });