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

'use strict';
'require poll';
'require view';
'require fs';
'require ui';
'require uci';
'require rpc';

var RefreshIfaces = "";
var RefreshTabs = ['s', 't', '5', 'h', 'd', 'm', 'y'];

var callServiceList = rpc.declare({
	object: 'service',
	method: 'list',
	params: [ 'name' ],
	expect: { '': {} }
});

var isReadonlyView = !L.hasViewPermission() || null;

function RefreshGraphs() {
	RefreshTabs.forEach(function (id) {
		RefreshIfaces.forEach(function (iface) {
			fs.exec_direct('/usr/bin/vnstati', [ '-' + id, '-i', iface, '-o', '-' ], 'blob').then(function(res) {
				document.getElementById('graph_' + id + '_' + iface).src = URL.createObjectURL(res);
			});
		});
	});
}

function IfacesResetData(ev) {
	ui.showModal(_('Delete data for ALL interfaces'), [
		E('p', _('The data will be removed from the database permanently. This cannot be undone.')),
		E('div', { 'class': 'right' }, [
			E('div', {
				'class': 'btn',
				'click': L.hideModal
			}, _('Cancel')),
			' ',
			E('div', {
				'class': 'btn cbi-button-negative',
				'click': function(ev) {
					var if_count = 0;

					RefreshIfaces.forEach(function (iface) {
						fs.exec_direct('/usr/bin/vnstat', [ '--remove', '-i', iface, '--force' ], 'blob').then(function() {
							fs.exec_direct('/usr/bin/vnstat', [ '--add', '-i', iface ], 'blob').then(function() {
								if_count++;
								if (if_count == RefreshIfaces.length) {
									RefreshGraphs();
								}
							});
						});
					});
					ui.hideModal();
				}
			}, _('Delete'))
		])
	]);
}	

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) {
			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.alt = _('Could not load graph, no data available: ') + iface;
				img.align = 'middle';
				img.style.visibility = 'visible';
				img.id = 'graph_' + style + '_' + iface;
				tab.firstElementChild.style.display = 'none';
			});
			tab.appendChild(E('span', {}, E('img', { 'data-iface': iface, 'style': 'visibility:hidden; margin:5px 10px' })));
		});

		return tab;
	},

	load: function() {
		return Promise.all([
			L.resolveDefault(callServiceList('vnstat'), {}),
			uci.load('vnstat'),
		]);
	},

	render: function(data) {
		var ifaces = uci.get_first('vnstat', 'vnstat', 'interface') || [];
		var isRunning = false;

		try {
			isRunning = data[0]['vnstat']['instances']['instance1']['running'];
		} catch (e) { }

		var view = E([], [
			E('h2', [_('vnStat Graphs')]),

			(isRunning == false) ? E('p', { 'class': 'alert-message warning' }, _('Warning: The service is not running, graphs will not be updated!')):E('p'),

			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);

			view.appendChild(E('div', { 'class': 'right' }, [
				E('br'),
				E('button', {
					'class': 'cbi-button cbi-button-neutral',
					'click': IfacesResetData,
					'disabled': isReadonlyView
				}, [ _('Clear data for all interfaces') ])
			]));
		}

		// Preserve the interfaces for the poll/refresh function
		RefreshIfaces = ifaces;

		poll.add(RefreshGraphs, 60);

		return view;
	},

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