summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-vnstat2/htdocs/luci-static/resources/view/vnstat2/graphs.js
blob: 7ac3b3f7e779e239e3187ab961ffdab7976a97f3 (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
// This is free software, licensed under the Apache License, Version 2.0

'use strict';
'require view';
'require fs';
'require ui';

return view.extend({
	renderTab: function(ifaces, style, title) {
		var tab = E('div', {
			'class': 'cbi-section',
			'data-tab': style,
			'data-tab-title': title
		}, [
			E('p', {}, E('em', { 'class': 'spinning' }, [ _('Loading graphs…') ]))
		]);

		ifaces.forEach(function(iface) {
			tab.appendChild(E('span', {}, E('img', { 'data-iface': iface, 'style': 'visibility:hidden; margin:5px 10px' })));
			fs.exec_direct('/usr/bin/vnstati', [ '-'+style, '-i', iface, '-o', '-' ], 'blob').then(function(res) {
				var img = tab.querySelector('img[data-iface="%s"]'.format(iface));
				img.src = URL.createObjectURL(res);
				img.style.visibility = 'visible';
				tab.firstElementChild.style.display = 'none';
			});
		});

		return tab;
	},

	load: function() {
		return fs.exec_direct('/usr/bin/vnstat', [ '--json', 'f', '1' ], 'text').then(function(res) {
			var json = null;

			try {
				json = JSON.parse(res)
			}
			catch(e) {
				throw new Error(res.replace(/^Error: /, ''));
			}

			return (L.isObject(json) ? L.toArray(json.interfaces) : []).map(function(iface) {
				return iface.name;
			}).sort();
		}).catch(function(err) {
			ui.addNotification(null, E('p', { 'style': 'white-space:pre' }, [err]));
			return [];
		});
	},

	render: function(ifaces) {
		var view = E([], [
			E('h2', [_('vnStat Graphs')]),
			E('div', ifaces.length ? [
				this.renderTab(ifaces, 's', _('Summary')),
				this.renderTab(ifaces, 't', _('Top')),
				this.renderTab(ifaces, '5', _('5 Minute')),
				this.renderTab(ifaces, 'h', _('Hourly')),
				this.renderTab(ifaces, 'd', _('Daily')),
				this.renderTab(ifaces, 'm', _('Monthly')),
				this.renderTab(ifaces, 'y', _('Yearly'))
			] : [ E('em', [_('No monitored interfaces have been found. Go to the configuration to enable monitoring for one or more interfaces.')]) ])
		]);

		if (ifaces.length)
			ui.tabs.initTabGroup(view.lastElementChild.childNodes);

		return view;
	},

	handleSave: null,
	handleSaveApply: null,
	handleReset: null
});