secubox-openwrt/luci-app-bandwidth-manager/htdocs/luci-static/resources/view/bandwidth-manager/usage.js
CyberMind-FR fa9bb2aee7 feat: complete Bandwidth Manager implementation with QoS and quotas
Implements comprehensive bandwidth management system with QoS traffic shaping,
client quotas, and SQM/CAKE integration for OpenWrt.

Features:
- QoS traffic shaping with rule-based control (application/port/IP/MAC)
- Per-rule download/upload limits with 8-level priority system
- Time-based scheduling support for rules
- Monthly data quotas per client (MAC address)
- iptables-based usage tracking with real-time statistics
- Configurable quota actions: throttle, block, or notify
- Automatic monthly reset with configurable reset day
- SQM/CAKE integration with NAT-aware configuration
- Link overhead compensation (Ethernet, PPPoE, VLAN)
- Alternative FQ_CoDel and HTB qdisc support

Components:
- RPCD backend (luci.bandwidth-manager): 10 ubus methods
  * status, list_rules, add_rule, delete_rule
  * list_quotas, get_quota, set_quota, reset_quota
  * get_usage_realtime, get_usage_history
- 5 JavaScript views: overview, rules, quotas, usage, settings
- ACL with read/write permissions for all methods
- UCI config with global, SQM, tracking, alerts, rules, and quotas sections
- Comprehensive README with API docs and examples

Technical implementation:
- Traffic tracking via iptables BW_TRACKING chain
- Usage database in /tmp/bandwidth_usage.db (pipe-delimited format)
- Real-time client usage with 5-second auto-refresh
- Historical data with configurable timeframes (1h to 30d)
- Per-client quota progress visualization with color-coded bars
- TC (traffic control) integration for QoS enforcement

Architecture follows SecuBox standards:
- RPCD naming convention (luci. prefix)
- Menu paths match view file structure
- All JavaScript in strict mode
- Form-based configuration management
- Comprehensive error handling

Dependencies: tc, kmod-sched-core, kmod-sched-cake, kmod-ifb, sqm-scripts,
iptables, iptables-mod-conntrack-extra, ip-full

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-24 10:48:51 +01:00

97 lines
3.1 KiB
JavaScript

'use strict';
'require view';
'require poll';
'require bandwidth-manager/api as API';
return L.view.extend({
load: function() {
return API.getUsageRealtime();
},
render: function(clients) {
var v = E('div', { 'class': 'cbi-map' }, [
E('h2', {}, _('Real-time Usage')),
E('div', { 'class': 'cbi-map-descr' }, _('Live bandwidth usage per client (updates every 5 seconds)'))
]);
var container = E('div', { 'id': 'usage-container', 'class': 'cbi-section' });
v.appendChild(container);
// Initial render
this.renderUsageTable(container, clients);
// Auto-refresh every 5 seconds
poll.add(L.bind(function() {
return API.getUsageRealtime().then(L.bind(function(data) {
this.renderUsageTable(container, data);
}, this));
}, this), 5);
return v;
},
renderUsageTable: function(container, clients) {
L.dom.content(container, [
E('h3', {}, _('Active Clients')),
E('table', { 'class': 'table' }, [
E('tr', { 'class': 'tr table-titles' }, [
E('th', { 'class': 'th' }, _('Client')),
E('th', { 'class': 'th' }, _('IP')),
E('th', { 'class': 'th' }, _('MAC')),
E('th', { 'class': 'th' }, _('Download')),
E('th', { 'class': 'th' }, _('Upload')),
E('th', { 'class': 'th' }, _('Total')),
E('th', { 'class': 'th' }, _('Quota'))
])
].concat(clients.length > 0 ? clients.map(L.bind(function(client) {
var total = (client.rx_bytes || 0) + (client.tx_bytes || 0);
var quotaCell = _('None');
if (client.has_quota) {
var percent = client.limit_mb > 0 ? Math.floor((client.used_mb * 100) / client.limit_mb) : 0;
var color = percent > 90 ? 'red' : (percent > 75 ? 'orange' : 'green');
quotaCell = [
E('div', {}, client.used_mb + ' / ' + client.limit_mb + ' MB'),
E('div', { 'style': 'background: #eee; width: 80px; height: 8px; border-radius: 4px;' }, [
E('div', {
'style': 'background: ' + color + '; width: ' + Math.min(percent, 100) + '%; height: 100%; border-radius: 4px;'
})
])
];
}
return E('tr', { 'class': 'tr' }, [
E('td', { 'class': 'td' }, client.hostname),
E('td', { 'class': 'td' }, client.ip),
E('td', { 'class': 'td' }, E('code', {}, client.mac)),
E('td', { 'class': 'td' }, [
E('span', { 'style': 'color: #28a745' }, '⬇ ' + this.formatBytes(client.rx_bytes))
]),
E('td', { 'class': 'td' }, [
E('span', { 'style': 'color: #dc3545' }, '⬆ ' + this.formatBytes(client.tx_bytes))
]),
E('td', { 'class': 'td' }, E('strong', {}, this.formatBytes(total))),
E('td', { 'class': 'td' }, quotaCell)
]);
}, this)) : [
E('tr', { 'class': 'tr' }, [
E('td', { 'class': 'td center', 'colspan': 7 },
E('em', {}, _('No active clients')))
])
]))
]);
},
formatBytes: function(bytes) {
if (bytes === 0) return '0 B';
var k = 1024;
var sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
var i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
},
handleSaveApply: null,
handleSave: null,
handleReset: null
});