Use multiples in network explorer table

Former-commit-id: 2aec7e59e4ea46c693f5fe89c847f524cd882f63
This commit is contained in:
Fermin 2019-03-05 08:02:41 +01:00
parent d62b7f0d79
commit 942b160a3d
2 changed files with 37 additions and 23 deletions

View File

@ -243,19 +243,25 @@ function format_numeric($number, $decimals=1)
/** /**
* Render numeric data for a graph. It adds magnitude suffix to the number * Render numeric data for a graph. It adds magnitude suffix to the number
* (M for millions, K for thousands...) base-10 * (M for millions, K for thousands...). Base can be modified with divider.
* *
* TODO: base-2 multiplication * @param float $number Number to be rendered.
* * @param integer $decimals Numbers after comma (default 1).
* @param float $number Number to be rendered * @param string $dec_point Decimal separator character (default .).
* @param integer $decimals Numbers after comma. Default value: 1 * @param string $thousands_sep Thousands separator character (default ,).
* @param dec_point Decimal separator character. Default value: . * @param integer $divider Number to divide the rendered number.
* @param thousands_sep Thousands separator character. Default value: , * @param string $sufix Units of the multiple.
* *
* @return string A string with the number and the multiplier * @return string A string with the number and the multiplier
*/ */
function format_for_graph($number, $decimals=1, $dec_point='.', $thousands_sep=',') function format_for_graph(
{ $number,
$decimals=1,
$dec_point='.',
$thousands_sep=',',
$divider=1000,
$sufix=''
) {
$shorts = [ $shorts = [
'', '',
'K', 'K',
@ -268,15 +274,15 @@ function format_for_graph($number, $decimals=1, $dec_point='.', $thousands_sep='
'Y', 'Y',
]; ];
$pos = 0; $pos = 0;
while ($number >= 1000) { while ($number >= $divider) {
// as long as the number can be divided by 1000 // As long as the number can be divided by divider.
$pos++; $pos++;
// Position in array starting with 0 // Position in array starting with 0.
$number = ($number / 1000); $number = ($number / $divider);
} }
return remove_right_zeros(format_numeric($number, $decimals)).$shorts[$pos]; // This will actually do the rounding and the decimals.
// This will actually do the rounding and the decimals return remove_right_zeros(format_numeric($number, $decimals)).$shorts[$pos].$sufix;
} }

View File

@ -144,22 +144,30 @@ $table = new stdClass();
$table->styleTable = 'width: 100%'; $table->styleTable = 'width: 100%';
// Print the header. // Print the header.
$table->head = []; $table->head = [];
$table->head[] = __('IP'); $table->head['main'] = __('IP');
if (!$is_network) { if (!$is_network) {
$table->head[] = __('Flows'); $table->head['flows'] = __('Flows');
} }
$table->head[] = __('Packets'); $table->head['pkts'] = __('Packets');
$table->head[] = __('Bytes'); $table->head['bytes'] = __('Bytes');
// Print the data. // Print the data.
$table->data = []; $table->data = [];
foreach ($data as $item) { foreach ($data as $item) {
$table->data[] = [ $row = [];
$item['host'], $row['main'] = $item['host'];
$row['pkts'] = format_for_graph($item['sum_pkts'], 2);
$row['bytes'] = format_for_graph(
$item['sum_bytes'], $item['sum_bytes'],
$item['sum_pkts'], 2,
]; '.',
',',
1024,
'B'
);
$table->data[] = $row;
} }
html_print_table($table); html_print_table($table);