Rebased from upstream / out of band repository.
[librecmc/librecmc.git] / package / luci / modules / luci-mod-status / htdocs / luci-static / resources / view / status / index.js
1 function progressbar(q, v, m)
2 {
3         var pg = document.querySelector(q),
4             vn = parseInt(v) || 0,
5             mn = parseInt(m) || 100,
6             pc = Math.floor((100 / mn) * vn);
7
8         if (pg) {
9                 pg.firstElementChild.style.width = pc + '%';
10                 pg.setAttribute('title', '%s / %s (%d%%)'.format(v, m, pc));
11         }
12 }
13
14 function renderBox(title, active, childs) {
15         childs = childs || [];
16         childs.unshift(L.itemlist(E('span'), [].slice.call(arguments, 3)));
17
18         return E('div', { class: 'ifacebox' }, [
19                 E('div', { class: 'ifacebox-head center ' + (active ? 'active' : '') },
20                         E('strong', title)),
21                 E('div', { class: 'ifacebox-body left' }, childs)
22         ]);
23 }
24
25 function renderBadge(icon, title) {
26         return E('span', { class: 'ifacebadge' }, [
27                 E('img', { src: icon, title: title || '' }),
28                 L.itemlist(E('span'), [].slice.call(arguments, 2))
29         ]);
30 }
31
32 L.poll(5, L.location(), { status: 1 },
33         function(x, info)
34         {
35                 var us = document.getElementById('upstream_status_table');
36
37                 while (us.lastElementChild)
38                         us.removeChild(us.lastElementChild);
39
40                 var wan_list = info.wan || [];
41
42                 for (var i = 0; i < wan_list.length; i++) {
43                         var ifc = wan_list[i];
44
45                         us.appendChild(renderBox(
46                                 _('IPv4 Upstream'),
47                                 (ifc.ifname && ifc.proto != 'none'),
48                                 [ E('div', {}, renderBadge(
49                                 L.resource('icons/%s.png').format((ifc && ifc.type) ? ifc.type : 'ethernet_disabled'), null,
50                                 _('Device'), ifc ? (ifc.name || ifc.ifname || '-') : '-',
51                                 _('MAC-Address'), (ifc && ifc.ether) ? ifc.mac : null)) ],
52                                 _('Protocol'), ifc.i18n || E('em', _('Not connected')),
53                                 _('Address'), (ifc.ipaddr) ? ifc.ipaddr : '0.0.0.0',
54                                 _('Netmask'), (ifc.netmask && ifc.netmask != ifc.ipaddr) ? ifc.netmask : '255.255.255.255',
55                                 _('Gateway'), (ifc.gwaddr) ? ifc.gwaddr : '0.0.0.0',
56                                 _('DNS') + ' 1', (ifc.dns) ? ifc.dns[0] : null,
57                                 _('DNS') + ' 2', (ifc.dns) ? ifc.dns[1] : null,
58                                 _('DNS') + ' 3', (ifc.dns) ? ifc.dns[2] : null,
59                                 _('DNS') + ' 4', (ifc.dns) ? ifc.dns[3] : null,
60                                 _('DNS') + ' 5', (ifc.dns) ? ifc.dns[4] : null,
61                                 _('Expires'), (ifc.expires > -1) ? '%t'.format(ifc.expires) : null,
62                                 _('Connected'), (ifc.uptime > 0) ? '%t'.format(ifc.uptime) : null));
63                 }
64
65                 var wan6_list = info.wan6 || [];
66
67                 for (var i = 0; i < wan6_list.length; i++) {
68                         var ifc6 = wan6_list[i];
69
70                         us.appendChild(renderBox(
71                                 _('IPv6 Upstream'),
72                                 (ifc6.ifname && ifc6.proto != 'none'),
73                                 [ E('div', {}, renderBadge(
74                                         L.resource('icons/%s.png').format(ifc6.type || 'ethernet_disabled'), null,
75                                         _('Device'), ifc6 ? (ifc6.name || ifc6.ifname || '-') : '-',
76                                         _('MAC-Address'), (ifc6 && ifc6.ether) ? ifc6.mac : null)) ],
77                                 _('Protocol'), ifc6.i18n ? (ifc6.i18n + (ifc6.proto === 'dhcp' && ifc6.ip6prefix ? '-PD' : '')) : E('em', _('Not connected')),
78                                 _('Prefix Delegated'), ifc6.ip6prefix,
79                                 _('Address'), (ifc6.ip6prefix) ? (ifc6.ip6addr || null) : (ifc6.ip6addr || '::'),
80                                 _('Gateway'), (ifc6.gw6addr) ? ifc6.gw6addr : '::',
81                                 _('DNS') + ' 1', (ifc6.dns) ? ifc6.dns[0] : null,
82                                 _('DNS') + ' 2', (ifc6.dns) ? ifc6.dns[1] : null,
83                                 _('DNS') + ' 3', (ifc6.dns) ? ifc6.dns[2] : null,
84                                 _('DNS') + ' 4', (ifc6.dns) ? ifc6.dns[3] : null,
85                                 _('DNS') + ' 5', (ifc6.dns) ? ifc6.dns[4] : null,
86                                 _('Connected'), (ifc6.uptime > 0) ? '%t'.format(ifc6.uptime) : null));
87                 }
88
89                 var ds = document.getElementById('dsl_status_table');
90                 if (ds) {
91                         while (ds.lastElementChild)
92                                 ds.removeChild(ds.lastElementChild);
93
94                         ds.appendChild(renderBox(
95                                 _('DSL Status'),
96                                 (info.dsl.line_state === 'UP'), [ ],
97                                 _('Line State'), '%s [0x%x]'.format(info.dsl.line_state, info.dsl.line_state_detail),
98                                 _('Line Mode'), info.dsl.line_mode_s || '-',
99                                 _('Line Uptime'), info.dsl.line_uptime_s || '-',
100                                 _('Annex'), info.dsl.annex_s || '-',
101                                 _('Profile'), info.dsl.profile_s || '-',
102                                 _('Data Rate'), '%s/s / %s/s'.format(info.dsl.data_rate_down_s, info.dsl.data_rate_up_s),
103                                 _('Max. Attainable Data Rate (ATTNDR)'), '%s/s / %s/s'.format(info.dsl.max_data_rate_down_s, info.dsl.max_data_rate_up_s),
104                                 _('Latency'), '%s / %s'.format(info.dsl.latency_num_down, info.dsl.latency_num_up),
105                                 _('Line Attenuation (LATN)'), '%.1f dB / %.1f dB'.format(info.dsl.line_attenuation_down, info.dsl.line_attenuation_up),
106                                 _('Signal Attenuation (SATN)'), '%.1f dB / %.1f dB'.format(info.dsl.signal_attenuation_down, info.dsl.signal_attenuation_up),
107                                 _('Noise Margin (SNR)'), '%.1f dB / %.1f dB'.format(info.dsl.noise_margin_down, info.dsl.noise_margin_up),
108                                 _('Aggregate Transmit Power(ACTATP)'), '%.1f dB / %.1f dB'.format(info.dsl.actatp_down, info.dsl.actatp_up),
109                                 _('Forward Error Correction Seconds (FECS)'), '%d / %d'.format(info.dsl.errors_fec_near, info.dsl.errors_fec_far),
110                                 _('Errored seconds (ES)'), '%d / %d'.format(info.dsl.errors_es_near, info.dsl.errors_es_far),
111                                 _('Severely Errored Seconds (SES)'), '%d / %d'.format(info.dsl.errors_ses_near, info.dsl.errors_ses_far),
112                                 _('Loss of Signal Seconds (LOSS)'), '%d / %d'.format(info.dsl.errors_loss_near, info.dsl.errors_loss_far),
113                                 _('Unavailable Seconds (UAS)'), '%d / %d'.format(info.dsl.errors_uas_near, info.dsl.errors_uas_far),
114                                 _('Header Error Code Errors (HEC)'), '%d / %d'.format(info.dsl.errors_hec_near, info.dsl.errors_hec_far),
115                                 _('Non Pre-emtive CRC errors (CRC_P)'), '%d / %d'.format(info.dsl.errors_crc_p_near, info.dsl.errors_crc_p_far),
116                                 _('Pre-emtive CRC errors (CRCP_P)'), '%d / %d'.format(info.dsl.errors_crcp_p_near, info.dsl.errors_crcp_p_far),
117                                 _('ATU-C System Vendor ID'), info.dsl.atuc_vendor_id,
118                                 _('Power Management Mode'), info.dsl.power_mode_s));
119                 }
120
121                 var ws = document.getElementById('wifi_status_table');
122                 if (ws)
123                 {
124                         while (ws.lastElementChild)
125                                 ws.removeChild(ws.lastElementChild);
126
127                         for (var didx = 0; didx < info.wifinets.length; didx++)
128                         {
129                                 var dev = info.wifinets[didx];
130                                 var net0 = (dev.networks && dev.networks[0]) ? dev.networks[0] : {};
131                                 var vifs = [];
132
133                                 for (var nidx = 0; nidx < dev.networks.length; nidx++)
134                                 {
135                                         var net = dev.networks[nidx];
136                                         var is_assoc = (net.bssid != '00:00:00:00:00:00' && net.channel && !net.disabled);
137
138                                         var icon;
139                                         if (net.disabled)
140                                                 icon = L.resource('icons/signal-none.png');
141                                         else if (net.quality <= 0)
142                                                 icon = L.resource('icons/signal-0.png');
143                                         else if (net.quality < 25)
144                                                 icon = L.resource('icons/signal-0-25.png');
145                                         else if (net.quality < 50)
146                                                 icon = L.resource('icons/signal-25-50.png');
147                                         else if (net.quality < 75)
148                                                 icon = L.resource('icons/signal-50-75.png');
149                                         else
150                                                 icon = L.resource('icons/signal-75-100.png');
151
152                                         vifs.push(renderBadge(
153                                                 icon,
154                                                 '%s: %d dBm / %s: %d%%'.format(_('Signal'), net.signal, _('Quality'), net.quality),
155                                                 _('SSID'), E('a', { href: net.link }, [ net.ssid || '?' ]),
156                                                 _('Mode'), net.mode,
157                                                 _('BSSID'), is_assoc ? (net.bssid || '-') : null,
158                                                 _('Encryption'), is_assoc ? net.encryption : null,
159                                                 _('Associations'), is_assoc ? (net.num_assoc || '-') : null,
160                                                 null, is_assoc ? null : E('em', net.disabled ? _('Wireless is disabled') : _('Wireless is not associated'))));
161                                 }
162
163                                 ws.appendChild(renderBox(
164                                         dev.device, dev.up || net0.up,
165                                         [ E('div', vifs) ],
166                                         _('Type'), dev.name.replace(/^Generic | Wireless Controller .+$/g, ''),
167                                         _('Channel'), net0.channel ? '%d (%.3f %s)'.format(net0.channel, net0.frequency, _('GHz')) : '-',
168                                         _('Bitrate'), net0.bitrate ? '%d %s'.format(net0.bitrate, _('Mbit/s')) : '-'));
169                         }
170
171                         if (!ws.lastElementChild)
172                                 ws.appendChild(E('em', _('No information available')));
173                 }
174
175                 var e;
176
177                 if (e = document.getElementById('localtime'))
178                         e.innerHTML = info.localtime;
179
180                 if (e = document.getElementById('uptime'))
181                         e.innerHTML = String.format('%t', info.uptime);
182
183                 if (e = document.getElementById('loadavg'))
184                         e.innerHTML = String.format(
185                                 '%.02f, %.02f, %.02f',
186                                 info.loadavg[0] / 65535.0,
187                                 info.loadavg[1] / 65535.0,
188                                 info.loadavg[2] / 65535.0
189                         );
190
191                 progressbar('#memtotal',
192                         ((info.memory.free + info.memory.buffered) / 1024) + ' ' + _('kB'),
193                         (info.memory.total / 1024) + ' ' + _('kB'));
194
195                 progressbar('#memfree',
196                         (info.memory.free / 1024) + ' ' + _('kB'),
197                         (info.memory.total / 1024) + ' ' + _('kB'));
198
199                 progressbar('#membuff',
200                         (info.memory.buffered / 1024) + ' ' + _('kB'),
201                         (info.memory.total / 1024) + ' ' + _('kB'));
202
203                 progressbar('#swaptotal',
204                         (info.swap.free / 1024) + ' ' + _('kB'),
205                         (info.swap.total / 1024) + ' ' + _('kB'));
206
207                 progressbar('#swapfree',
208                         (info.swap.free / 1024) + ' ' + _('kB'),
209                         (info.swap.total / 1024) + ' ' + _('kB'));
210
211                 progressbar('#conns',
212                         info.conncount, info.connmax);
213
214         }
215 );