summaryrefslogtreecommitdiffstats
path: root/web/gui/src/dashboard.js/units-conversion.js
blob: 26b8403446b27a2f342b9f6cef1c823f306c3c95 (plain)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
NETDATA.unitsConversion = {
    keys: {},       // keys for data-common-units
    latest: {},     // latest selected units for data-common-units

    globalReset: function () {
        this.keys = {};
        this.latest = {};
    },

    scalableUnits: {
        'packets/s': {
            'pps': 1,
            'Kpps': 1000,
            'Mpps': 1000000
        },
        'pps': {
            'pps': 1,
            'Kpps': 1000,
            'Mpps': 1000000
        },
        'kilobits/s': {
            'bits/s': 1 / 1000,
            'kilobits/s': 1,
            'megabits/s': 1000,
            'gigabits/s': 1000000,
            'terabits/s': 1000000000
        },
        'kilobytes/s': {
            'bytes/s': 1 / 1024,
            'kilobytes/s': 1,
            'megabytes/s': 1024,
            'gigabytes/s': 1024 * 1024,
            'terabytes/s': 1024 * 1024 * 1024
        },
        'KB/s': {
            'B/s': 1 / 1024,
            'KB/s': 1,
            'MB/s': 1024,
            'GB/s': 1024 * 1024,
            'TB/s': 1024 * 1024 * 1024
        },
        'KiB/s': {
            'B/s': 1 / 1024,
            'KiB/s': 1,
            'MiB/s': 1024,
            'GiB/s': 1024 * 1024,
            'TiB/s': 1024 * 1024 * 1024
        },
        'B': {
            'B': 1,
            'KiB': 1024,
            'MiB': 1024 * 1024,
            'GiB': 1024 * 1024 * 1024,
            'TiB': 1024 * 1024 * 1024 * 1024,
            'PiB': 1024 * 1024 * 1024 * 1024 * 1024
        },
        'KB': {
            'B': 1 / 1024,
            'KB': 1,
            'MB': 1024,
            'GB': 1024 * 1024,
            'TB': 1024 * 1024 * 1024
        },
        'KiB': {
            'B': 1 / 1024,
            'KiB': 1,
            'MiB': 1024,
            'GiB': 1024 * 1024,
            'TiB': 1024 * 1024 * 1024
        },
        'MB': {
            'B': 1 / (1024 * 1024),
            'KB': 1 / 1024,
            'MB': 1,
            'GB': 1024,
            'TB': 1024 * 1024,
            'PB': 1024 * 1024 * 1024
        },
        'MiB': {
            'B': 1 / (1024 * 1024),
            'KiB': 1 / 1024,
            'MiB': 1,
            'GiB': 1024,
            'TiB': 1024 * 1024,
            'PiB': 1024 * 1024 * 1024
        },
        'GB': {
            'B': 1 / (1024 * 1024 * 1024),
            'KB': 1 / (1024 * 1024),
            'MB': 1 / 1024,
            'GB': 1,
            'TB': 1024,
            'PB': 1024 * 1024,
            'EB': 1024 * 1024 * 1024
        },
        'GiB': {
            'B': 1 / (1024 * 1024 * 1024),
            'KiB': 1 / (1024 * 1024),
            'MiB': 1 / 1024,
            'GiB': 1,
            'TiB': 1024,
            'PiB': 1024 * 1024,
            'EiB': 1024 * 1024 * 1024
        }
        /*
        'milliseconds': {
            'seconds': 1000
        },
        'seconds': {
            'milliseconds': 0.001,
            'seconds': 1,
            'minutes': 60,
            'hours': 3600,
            'days': 86400
        }
        */
    },

    convertibleUnits: {
        'Celsius': {
            'Fahrenheit': {
                check: function (max) {
                    void(max);
                    return NETDATA.options.current.temperature === 'fahrenheit';
                },
                convert: function (value) {
                    return value * 9 / 5 + 32;
                }
            }
        },
        'celsius': {
            'fahrenheit': {
                check: function (max) {
                    void(max);
                    return NETDATA.options.current.temperature === 'fahrenheit';
                },
                convert: function (value) {
                    return value * 9 / 5 + 32;
                }
            }
        },
        'seconds': {
            'time': {
                check: function (max) {
                    void(max);
                    return NETDATA.options.current.seconds_as_time;
                },
                convert: function (seconds) {
                    return NETDATA.unitsConversion.seconds2time(seconds);
                }
            }
        },
        'milliseconds': {
            'milliseconds': {
                check: function (max) {
                    return NETDATA.options.current.seconds_as_time && max < 1000;
                },
                convert: function (milliseconds) {
                    let tms = Math.round(milliseconds * 10);
                    milliseconds = Math.floor(tms / 10);

                    tms -= milliseconds * 10;

                    return (milliseconds).toString() + '.' + tms.toString();
                }
            },
            'seconds': {
                check: function (max) {
                    return NETDATA.options.current.seconds_as_time && max >= 1000 && max < 60000;
                },
                convert: function (milliseconds) {
                    milliseconds = Math.round(milliseconds);

                    let seconds = Math.floor(milliseconds / 1000);
                    milliseconds -= seconds * 1000;

                    milliseconds = Math.round(milliseconds / 10);

                    return seconds.toString() + '.'
                        + NETDATA.zeropad(milliseconds);
                }
            },
            'M:SS.ms': {
                check: function (max) {
                    return NETDATA.options.current.seconds_as_time && max >= 60000;
                },
                convert: function (milliseconds) {
                    milliseconds = Math.round(milliseconds);

                    let minutes = Math.floor(milliseconds / 60000);
                    milliseconds -= minutes * 60000;

                    let seconds = Math.floor(milliseconds / 1000);
                    milliseconds -= seconds * 1000;

                    milliseconds = Math.round(milliseconds / 10);

                    return minutes.toString() + ':'
                        + NETDATA.zeropad(seconds) + '.'
                        + NETDATA.zeropad(milliseconds);
                }
            }
        }
    },

    seconds2time: function (seconds) {
        seconds = Math.abs(seconds);

        let days = Math.floor(seconds / 86400);
        seconds -= days * 86400;

        let hours = Math.floor(seconds / 3600);
        seconds -= hours * 3600;

        let minutes = Math.floor(seconds / 60);
        seconds -= minutes * 60;

        seconds = Math.round(seconds);

        let ms_txt = '';
        /*
        let ms = seconds - Math.floor(seconds);
        seconds -= ms;
        ms = Math.round(ms * 1000);

        if (ms > 1) {
            if (ms < 10)
                ms_txt = '.00' + ms.toString();
            else if (ms < 100)
                ms_txt = '.0' + ms.toString();
            else
                ms_txt = '.' + ms.toString();
        }
        */

        return ((days > 0) ? days.toString() + 'd:' : '').toString()
            + NETDATA.zeropad(hours) + ':'
            + NETDATA.zeropad(minutes) + ':'
            + NETDATA.zeropad(seconds)
            + ms_txt;
    },

    // get a function that converts the units
    // + every time units are switched call the callback
    get: function (uuid, min, max, units, desired_units, common_units_name, switch_units_callback) {
        // validate the parameters
        if (typeof units === 'undefined') {
            units = 'undefined';
        }

        // check if we support units conversion
        if (typeof this.scalableUnits[units] === 'undefined' && typeof this.convertibleUnits[units] === 'undefined') {
            // we can't convert these units
            //console.log('DEBUG: ' + uuid.toString() + ' can\'t convert units: ' + units.toString());
            return function (value) {
                return value;
            };
        }

        // check if the caller wants the original units
        if (typeof desired_units === 'undefined' || desired_units === null || desired_units === 'original' || desired_units === units) {
            //console.log('DEBUG: ' + uuid.toString() + ' original units wanted');
            switch_units_callback(units);
            return function (value) {
                return value;
            };
        }

        // now we know we can convert the units
        // and the caller wants some kind of conversion

        let tunits = null;
        let tdivider = 0;

        if (typeof this.scalableUnits[units] !== 'undefined') {
            // units that can be scaled
            // we decide a divider

            // console.log('NETDATA.unitsConversion.get(' + units.toString() + ', ' + desired_units.toString() + ', function()) decide divider with min = ' + min.toString() + ', max = ' + max.toString());

            if (desired_units === 'auto') {
                // the caller wants to auto-scale the units

                // find the absolute maximum value that is rendered on the chart
                // based on this we decide the scale
                min = Math.abs(min);
                max = Math.abs(max);
                if (min > max) {
                    max = min;
                }

                // find the smallest scale that provides integers
                // for (x in this.scalableUnits[units]) {
                //     if (this.scalableUnits[units].hasOwnProperty(x)) {
                //         let m = this.scalableUnits[units][x];
                //         if (m <= max && m > tdivider) {
                //             tunits = x;
                //             tdivider = m;
                //         }
                //     }
                // }
                const sunit = this.scalableUnits[units];
                for (var x of Object.keys(sunit)) {
                    let m = sunit[x];
                    if (m <= max && m > tdivider) {
                        tunits = x;
                        tdivider = m;
                    }
                }

                if (tunits === null || tdivider <= 0) {
                    // we couldn't find one
                    //console.log('DEBUG: ' + uuid.toString() + ' cannot find an auto-scaling candidate for units: ' + units.toString() + ' (max: ' + max.toString() + ')');
                    switch_units_callback(units);
                    return function (value) {
                        return value;
                    };
                }

                if (typeof common_units_name === 'string' && typeof uuid === 'string') {
                    // the caller wants several charts to have the same units
                    // data-common-units

                    let common_units_key = common_units_name + '-' + units;

                    // add our divider into the list of keys
                    let t = this.keys[common_units_key];
                    if (typeof t === 'undefined') {
                        this.keys[common_units_key] = {};
                        t = this.keys[common_units_key];
                    }
                    t[uuid] = {
                        units: tunits,
                        divider: tdivider
                    };

                    // find the max divider of all charts
                    let common_units = t[uuid];
                    for (var x in t) {
                        if (t.hasOwnProperty(x) && t[x].divider > common_units.divider) {
                            common_units = t[x];
                        }
                    }

                    // save our common_max to the latest keys
                    let latest = this.latest[common_units_key];
                    if (typeof latest === 'undefined') {
                        this.latest[common_units_key] = {};
                        latest = this.latest[common_units_key];
                    }
                    latest.units = common_units.units;
                    latest.divider = common_units.divider;

                    tunits = latest.units;
                    tdivider = latest.divider;

                    //console.log('DEBUG: ' + uuid.toString() + ' converted units: ' + units.toString() + ' to units: ' + tunits.toString() + ' with divider ' + tdivider.toString() + ', common-units=' + common_units_name.toString() + ((t[uuid].divider !== tdivider)?' USED COMMON, mine was ' + t[uuid].units:' set common').toString());

                    // apply it to this chart
                    switch_units_callback(tunits);
                    return function (value) {
                        if (tdivider !== latest.divider) {
                            // another chart switched our common units
                            // we should switch them too
                            //console.log('DEBUG: ' + uuid + ' switching units due to a common-units change, from ' + tunits.toString() + ' to ' + latest.units.toString());
                            tunits = latest.units;
                            tdivider = latest.divider;
                            switch_units_callback(tunits);
                        }

                        return value / tdivider;
                    };
                } else {
                    // the caller did not give data-common-units
                    // this chart auto-scales independently of all others
                    //console.log('DEBUG: ' + uuid.toString() + ' converted units: ' + units.toString() + ' to units: ' + tunits.toString() + ' with divider ' + tdivider.toString() + ', autonomously');

                    switch_units_callback(tunits);
                    return function (value) {
                        return value / tdivider;
                    };
                }
            } else {
                // the caller wants specific units

                if (typeof this.scalableUnits[units][desired_units] !== 'undefined') {
                    // all good, set the new units
                    tdivider = this.scalableUnits[units][desired_units];
                    // console.log('DEBUG: ' + uuid.toString() + ' converted units: ' + units.toString() + ' to units: ' + desired_units.toString() + ' with divider ' + tdivider.toString() + ', by reference');
                    switch_units_callback(desired_units);
                    return function (value) {
                        return value / tdivider;
                    };
                } else {
                    // oops! switch back to original units
                    console.log('Units conversion from ' + units.toString() + ' to ' + desired_units.toString() + ' is not supported.');
                    switch_units_callback(units);
                    return function (value) {
                        return value;
                    };
                }
            }
        } else if (typeof this.convertibleUnits[units] !== 'undefined') {
            // units that can be converted
            if (desired_units === 'auto') {
                for (var x in this.convertibleUnits[units]) {
                    if (this.convertibleUnits[units].hasOwnProperty(x)) {
                        if (this.convertibleUnits[units][x].check(max)) {
                            //console.log('DEBUG: ' + uuid.toString() + ' converting ' + units.toString() + ' to: ' + x.toString());
                            switch_units_callback(x);
                            return this.convertibleUnits[units][x].convert;
                        }
                    }
                }

                // none checked ok
                //console.log('DEBUG: ' + uuid.toString() + ' no conversion available for ' + units.toString() + ' to: ' + desired_units.toString());
                switch_units_callback(units);
                return function (value) {
                    return value;
                };
            } else if (typeof this.convertibleUnits[units][desired_units] !== 'undefined') {
                switch_units_callback(desired_units);
                return this.convertibleUnits[units][desired_units].convert;
            } else {
                console.log('Units conversion from ' + units.toString() + ' to ' + desired_units.toString() + ' is not supported.');
                switch_units_callback(units);
                return function (value) {
                    return value;
                };
            }
        } else {
            // hm... did we forget to implement the new type?
            console.log(`Unmatched unit conversion method for units ${units.toString()}`);
            switch_units_callback(units);
            return function (value) {
                return value;
            };
        }
    }
};