values.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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-hour':
  163. format = (use_higher_precision)?'%.2f %s':'%.1f %s';
  164. value = value / 1000000;
  165. ending = 'Wh';
  166. break;
  167. case 'load':
  168. format = (use_higher_precision)?'%.2f %s':'%.1f %s';
  169. break;
  170. default:
  171. format = '%s';
  172. break;
  173. }
  174. return format.format(value, ending);
  175. }
  176. returnIfDifferent(dwell, label, value, type, format, key) {
  177. let output = [];
  178. // make sure the keys exist
  179. if (!(type in this._history)) this._history[type] = {};
  180. // no sense in continuing when the raw value has not changed
  181. if (type != 'network-rx' && type != 'network-tx' &&
  182. key in this._history[type] && this._history[type][key][1] == value)
  183. return output;
  184. // is the value different from last time?
  185. let legible = this._legible(value, format);
  186. // don't return early when dealing with network traffic
  187. if (type != 'network-rx' && type != 'network-tx') {
  188. // only update when we are coming through for the first time, or if a value has changed
  189. if (key in this._history[type] && this._history[type][key][0] == legible)
  190. return output;
  191. // add label as it was sent from sensors class
  192. output.push([label, legible, type, key]);
  193. }
  194. // save previous values to update screen on changes only
  195. let previousValue = this._history[type][key];
  196. this._history[type][key] = [legible, value];
  197. // process average, min and max values
  198. if (type == 'temperature' || type == 'voltage' || type == 'fan') {
  199. let vals = Object.values(this._history[type]).map(x => parseFloat(x[1]));
  200. // show value in group even if there is one value present
  201. let sum = vals.reduce((a, b) => a + b);
  202. let avg = this._legible(sum / vals.length, format);
  203. output.push([type, avg, type + '-group', '']);
  204. // If only one value is present, don't display avg, min and max
  205. if (vals.length > 1) {
  206. output.push(['Average', avg, type, '__' + type + '_avg__']);
  207. // calculate Minimum value
  208. let min = Math.min(...vals);
  209. min = this._legible(min, format);
  210. output.push(['Minimum', min, type, '__' + type + '_min__']);
  211. // calculate Maximum value
  212. let max = Math.max(...vals);
  213. max = this._legible(max, format);
  214. output.push(['Maximum', max, type, '__' + type + '_max__']);
  215. }
  216. } else if (type == 'network-rx' || type == 'network-tx') {
  217. let direction = type.split('-')[1];
  218. // appends total upload and download for all interfaces for #216
  219. let vals = Object.values(this._history[type]).map(x => parseFloat(x[1]));
  220. let sum = vals.reduce((partialSum, a) => partialSum + a, 0);
  221. output.push(['Boot ' + direction, this._legible(sum, format), type, '__' + type + '_boot__']);
  222. // keeps track of session start point
  223. if (!(key in this._networkSpeedOffset) || this._networkSpeedOffset[key] <= 0)
  224. this._networkSpeedOffset[key] = sum;
  225. // outputs session upload and download for all interfaces for #234
  226. output.push(['Session ' + direction, this._legible(sum - this._networkSpeedOffset[key], format), type, '__' + type + '_ses__']);
  227. // calculate speed for this interface
  228. let speed = (value - previousValue[1]) / dwell;
  229. output.push([label, this._legible(speed, 'speed'), type, key]);
  230. // store speed for Device report
  231. if (!(direction in this._networkSpeeds)) this._networkSpeeds[direction] = {};
  232. if (!(label in this._networkSpeeds[direction])) this._networkSpeeds[direction][label] = 0;
  233. // store value for next go around
  234. if (value > 0 || (value == 0 && !this._settings.get_boolean('hide-zeros')))
  235. this._networkSpeeds[direction][label] = speed;
  236. // calculate total upload and download device speed
  237. for (let direction in this._networkSpeeds) {
  238. let sum = 0;
  239. for (let iface in this._networkSpeeds[direction])
  240. sum += parseFloat(this._networkSpeeds[direction][iface]);
  241. sum = this._legible(sum, 'speed');
  242. output.push(['Device ' + direction, sum, 'network-' + direction, '__network-' + direction + '_max__']);
  243. // append download speed to group itself
  244. if (direction == 'rx') output.push([type, sum, type + '-group', '']);
  245. }
  246. }
  247. /*
  248. global.log('before', JSON.stringify(output));
  249. for (let i = output.length - 1; i >= 0; i--) {
  250. let sensor = output[i];
  251. // sensor[0]=label, sensor[1]=value, sensor[2]=type, sensor[3]=key)
  252. //["CPU Core 5","46°C","temperature","_temperature_hwmon8temp7_"]
  253. // make sure the keys exist
  254. if (!(sensor[2] in this._history2)) this._history2[sensor[2]] = {};
  255. if (sensor[3] in this._history2[sensor[2]]) {
  256. if (this._history2[sensor[2]][sensor[3]] == sensor[1]) {
  257. output.splice(i, 1);
  258. }
  259. }
  260. this._history2[sensor[2]][sensor[3]] = sensor[1];
  261. }
  262. global.log(' after', JSON.stringify(output));
  263. global.log('***************************');
  264. */
  265. return output;
  266. }
  267. resetHistory() {
  268. // don't call this._history = {}, as we want to keep network-rx and network-tx
  269. // otherwise network history statistics will start over
  270. for (let sensor in this._sensorIcons) {
  271. this._history[sensor] = {};
  272. this._history[sensor + '-group'] = {};
  273. //this._history2[sensor] = {};
  274. //this._history2[sensor + '-group'] = {};
  275. }
  276. }
  277. });