diff options
author | Jo-Philipp Wich <jo@mein.io> | 2020-01-30 12:08:35 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2020-01-31 08:51:55 +0100 |
commit | 40c56ddd7797f9e916abe5443784b21ed9ba51cf (patch) | |
tree | 2845066826f787cfc2d100a3e073737cfa03cf89 /applications/luci-app-vnstat2/htdocs/luci-static/resources/view | |
parent | 7cfce565530cbf6103275002ad20af57a509ad7b (diff) |
luci-app-vnstat2: fully convert to client side rendering
This converts the graph rendering to client side JavaScript and replaces
the route registration with declarative JSON.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'applications/luci-app-vnstat2/htdocs/luci-static/resources/view')
-rw-r--r-- | applications/luci-app-vnstat2/htdocs/luci-static/resources/view/vnstat2/graphs.js | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/applications/luci-app-vnstat2/htdocs/luci-static/resources/view/vnstat2/graphs.js b/applications/luci-app-vnstat2/htdocs/luci-static/resources/view/vnstat2/graphs.js new file mode 100644 index 0000000000..770884655a --- /dev/null +++ b/applications/luci-app-vnstat2/htdocs/luci-static/resources/view/vnstat2/graphs.js @@ -0,0 +1,73 @@ +// This is free software, licensed under the Apache License, Version 2.0 + +'use strict'; +'require fs'; +'require ui'; + +return L.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('p', {}, E('img', { 'data-iface': iface, 'style': 'display:none' }))); + 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.display = ''; + 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 +}); |