Reporting / Traffic: change api output to combined in/out per row and change user interface to handle new format. closes https://github.com/opnsense/core/issues/4724

This commit is contained in:
Ad Schellevis 2021-02-23 16:21:25 +01:00
parent bcb7c26897
commit 277ffb6ac5
2 changed files with 34 additions and 31 deletions

View File

@ -236,9 +236,8 @@ POSSIBILITY OF SUCH DAMAGE.
let update_stamp_iso = (new Date()).toISOString();
Object.keys(data).forEach(function(intf) {
let intf_label = $("#interfaces > option[value="+intf+"]").data('content');
['in', 'out'].forEach(function(dir) {
for (var i=0; i < data[intf][dir].length ; i++) {
let item = data[intf][dir][i];
for (var i=0; i < data[intf]['records'].length ; i++) {
let item = data[intf]['records'][i];
let tr = target.find("tr[data-address='"+item.address+"']");
if (tr.length === 0) {
tr = $("<tr/>");
@ -257,18 +256,19 @@ POSSIBILITY OF SUCH DAMAGE.
tr.append($("<td class='last_seen'/>"));
target.append(tr);
}
tr.data('bps_'+dir, item.rate_bits);
tr.data('total_'+ dir, tr.data('total_'+ dir) + item.cumulative_bytes);
tr.data('last_seen', update_stamp);
tr.find('td.last_seen').text(update_stamp_iso);
if (parseInt(tr.data('bps_max_'+dir)) < item.rate_bits) {
tr.data('bps_max_'+dir, item.rate_bits);
tr.find('td.bps_max_'+dir).text(item.rate);
}
tr.find('td.bps_'+dir).text(item.rate);
tr.find('td.total_'+dir).text(byteFormat(tr.data('total_'+ dir)));
['in', 'out'].forEach(function(dir) {
tr.data('bps_'+dir, item['rate_bits'+dir]);
tr.data('total_'+ dir, tr.data('total_'+ dir) + item.cumulative_bytes);
tr.data('last_seen', update_stamp);
tr.find('td.last_seen').text(update_stamp_iso);
if (parseInt(tr.data('bps_max_'+dir)) < item['rate_bits_'+dir]) {
tr.data('bps_max_'+dir, item['rate_bits_'+dir]);
tr.find('td.bps_max_'+dir).text(item['rate_'+dir]);
}
tr.find('td.bps_'+dir).text(item['rate_'+dir]);
tr.find('td.total_'+dir).text(byteFormat(tr.data('total_'+ dir)));
});
}
});
});
let ttl = 120; // keep visible for ttl seconds
target.find('tr').each(function(){
@ -374,14 +374,14 @@ POSSIBILITY OF SUCH DAMAGE.
Object.keys(data).forEach(function(intf) {
this_chart.config.data.datasets.forEach(function(dataset) {
if (dataset.intf == intf) {
let calc_data = data[intf][dataset.src_field];
let calc_data = data[intf]['records'];
dataset.hidden = !$("#interfaces").val().includes(intf);
for (var i=0; i < data[intf][dataset.src_field].length ; ++i) {
for (var i=0; i < data[intf]['records'].length ; ++i) {
dataset.data.push({
x: Date.now(),
y: data[intf][dataset.src_field][i]['rate_bits'],
y: data[intf]['records'][i]['rate_bits_' + dataset.src_field],
r: 4,
address: data[intf][dataset.src_field][i]['address']
address: data[intf]['records'][i]['address']
});
}
return;

View File

@ -94,7 +94,7 @@ if __name__ == '__main__':
for intf in iftop_data:
agg_results = dict()
result[intf] = {'in' : [], 'out': []}
result[intf] = {'records' : []}
if iftop_data[intf] is None:
result[intf]['status'] = 'timeout'
continue
@ -133,29 +133,32 @@ if __name__ == '__main__':
'address': item['address'],
'rate_bits_in': 0,
'rate_bits_out': 0,
'rate_bits': 0,
'cumulative_bytes_in': 0,
'cumulative_bytes_out': 0,
'cumulative_bytes': 0,
'tags': item['tags'],
'details': []
}
agg_results[item['address']]['rate_bits_out'] += item['rate_bits']
agg_results[item['address']]['rate_bits_in'] += other_end['rate_bits']
agg_results[item['address']]['rate_bits'] += (item['rate_bits'] + other_end['rate_bits'])
agg_results[item['address']]['cumulative_bytes_out'] += item['cumulative_bytes']
agg_results[item['address']]['cumulative_bytes_in'] += other_end['cumulative_bytes']
agg_results[item['address']]['cumulative_bytes'] += (
item['cumulative_bytes'] + other_end['cumulative_bytes']
)
agg_results[item['address']]['details'].append(other_end)
# XXX: sort output, limit output results to max 200 (safety precaution)
for direction in ['in', 'out']:
# XXX split in/out, we should probably change the output type later\
top_hosts = sorted(agg_results.values(), key=lambda x: x['rate_bits_%s' % direction], reverse=True)[:200]
for host in top_hosts:
result[intf][direction].append({
'address': host['address'],
'rate': to_bformat(host['rate_bits_%s' % direction]),
'rate_bits': host['rate_bits_%s' % direction],
'cumulative_bytes': host['cumulative_bytes_%s' % direction],
'cumulative': to_bformat(host['cumulative_bytes_%s' % direction]),
'tags': host['tags'],
})
top_hosts = sorted(agg_results.values(), key=lambda x: x['rate_bits'], reverse=True)[:200]
for host in top_hosts:
host['rate_in'] = to_bformat(host['rate_bits_in'])
host['rate_out'] = to_bformat(host['rate_bits_out'])
host['rate'] = to_bformat(host['rate_bits'])
host['cumulative_in'] = to_bformat(host['cumulative_bytes_in'])
host['cumulative_out'] = to_bformat(host['cumulative_bytes_out'])
host['cumulative'] = to_bformat(host['cumulative_bytes'])
result[intf]['records'].append(host)
print(ujson.dumps(result))