-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-edgerouter-throughput.js
204 lines (188 loc) · 6.99 KB
/
MMM-edgerouter-throughput.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
/**
* EdgeRouter Throughput
* A MagicMirror² Module to show the Interface throughput of an Ubiquity EdgeRouter
*
* Version 1.0.0
* By Michael Scharl <michael.scharl@me.com>
*
* License MIT
*
* This is an autogenerated file. DO NOT EDIT!
*/
(function () {
'use strict';
const BYTE_UNITS = [
'B',
'kB',
'MB',
'GB',
'TB',
'PB',
'EB',
'ZB',
'YB'
];
const BIT_UNITS = [
'b',
'kbit',
'Mbit',
'Gbit',
'Tbit',
'Pbit',
'Ebit',
'Zbit',
'Ybit'
];
/*
Formats the given number using `Number#toLocaleString`.
- If locale is a string, the value is expected to be a locale-key (for example: `de`).
- If locale is true, the system default locale is used for translation.
- If no value for locale is specified, the number is returned unmodified.
*/
const toLocaleString = (number, locale) => {
let result = number;
if (typeof locale === 'string') {
result = number.toLocaleString(locale);
} else if (locale === true) {
result = number.toLocaleString();
}
return result;
};
var prettyBytes = (number, options) => {
if (!Number.isFinite(number)) {
throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`);
}
options = Object.assign({bits: false}, options);
const UNITS = options.bits ? BIT_UNITS : BYTE_UNITS;
if (options.signed && number === 0) {
return ' 0 ' + UNITS[0];
}
const isNegative = number < 0;
const prefix = isNegative ? '-' : (options.signed ? '+' : '');
if (isNegative) {
number = -number;
}
if (number < 1) {
const numberString = toLocaleString(number, options.locale);
return prefix + numberString + ' ' + UNITS[0];
}
const exponent = Math.min(Math.floor(Math.log10(number) / 3), UNITS.length - 1);
// eslint-disable-next-line unicorn/prefer-exponentiation-operator
number = Number((number / Math.pow(1000, exponent)).toPrecision(3));
const numberString = toLocaleString(number, options.locale);
const unit = UNITS[exponent];
return prefix + numberString + ' ' + unit;
};
var ModuleNotification;
(function (ModuleNotification) {
ModuleNotification["CONFIG"] = "CONFIG";
ModuleNotification["THROUGHPUT"] = "THROUGHPUT";
})(ModuleNotification || (ModuleNotification = {}));
var DOM_INSTANCES = {};
Module.register('MMM-edgerouter-throughput', {
/**
* Define the default instance config
*/
defaults: {
title: undefined,
showInterfaceName: true,
},
_throughputData: {},
_lastThroughputData: {},
start: function () {
this.sendSocketNotification(ModuleNotification.CONFIG, this.config);
},
/**
* Core-Function to return the modules DOM-Tree.
*/
getDom: function () {
var _a = this._getDomInstance(), down = _a.down, interfaceName = _a.interfaceName, root = _a.root, title = _a.title, up = _a.up;
var config = this.config;
title.innerText = config.title;
interfaceName.innerText = config.interface;
if (this._lastThroughputData[config.gateway] &&
this._lastThroughputData[config.gateway][config.interface] &&
this._throughputData[config.gateway] &&
this._throughputData[config.gateway][config.interface]) {
var lastTxBytes = this._lastThroughputData[config.gateway][config.interface].tx_bytes;
var currentTxBytes = this._throughputData[config.gateway][config.interface].tx_bytes;
var lastRxBytes = this._lastThroughputData[config.gateway][config.interface].rx_bytes;
var currentRxBytes = this._throughputData[config.gateway][config.interface].rx_bytes;
var txRate = currentTxBytes - lastTxBytes;
var rxRate = currentRxBytes - lastRxBytes;
up.innerText = prettyBytes(txRate) + '/s';
down.innerText = prettyBytes(rxRate) + '/s';
}
return root;
},
socketNotificationReceived: function (notifiction, payload) {
switch (notifiction) {
case ModuleNotification.THROUGHPUT:
this._setThroughputData(payload);
break;
}
},
_getDomInstance: function () {
var identifier = this.identifier;
// Create DOM Elements only if not created before.
if (!DOM_INSTANCES[identifier]) {
var root = document.createElement('div');
var titleWrapper = document.createElement('div');
var title = document.createElement('span');
var interfaceName = document.createElement('span');
var throughput = document.createElement('div');
var upIcon = document.createElement('span');
var up = document.createElement('span');
var downIcon = document.createElement('span');
var down = document.createElement('span');
// Helper functions.
var space = function () { return document.createTextNode(' '); };
var br = function () { return document.createElement('br'); };
titleWrapper.classList.add('xsmall');
// Append title only when interface name is hidden and title is available.
if (this.config.title && !this.config.showInterfaceName) {
root.append(titleWrapper);
titleWrapper.append(title);
titleWrapper.classList.add('bold');
}
// Append interface Name when no title is given and interface should be shown.
else if (!this.config.title && this.config.showInterfaceName) {
root.append(titleWrapper);
titleWrapper.append(interfaceName);
titleWrapper.classList.add('bold');
}
// Append title and interface name when both should be shown
else if (this.config.title && this.config.showInterfaceName) {
root.append(titleWrapper);
titleWrapper.classList.add('normal');
interfaceName.classList.add('bold');
titleWrapper.append(title, space(), interfaceName);
}
root.append(throughput);
throughput.append(downIcon, space(), down, br(), upIcon, space(), up);
throughput.classList.add('bright');
throughput.classList.add('small');
throughput.classList.add('light');
upIcon.classList.add('normal');
upIcon.classList.add('bold');
upIcon.innerText = '↑';
downIcon.classList.add('normal');
downIcon.classList.add('bold');
downIcon.innerText = '↓';
DOM_INSTANCES[identifier] = {
interfaceName: interfaceName,
root: root,
title: title,
up: up,
down: down,
};
}
return DOM_INSTANCES[identifier];
},
_setThroughputData: function (data) {
this._lastThroughputData = this._throughputData;
this._throughputData = data;
this.updateDom();
},
});
}());