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
* (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 value: 1
* @param dec_point Decimal separator character. Default value: .
* @param thousands_sep Thousands separator character. Default value: ,
* @param float $number Number to be rendered.
* @param integer $decimals Numbers after comma (default 1).
* @param string $dec_point Decimal separator character (default .).
* @param string $thousands_sep Thousands separator character (default ,).
* @param integer $divider Number to divide the rendered number.
* @param string $sufix Units of the multiple.
*
* @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 = [
'',
'K',
@ -268,15 +274,15 @@ function format_for_graph($number, $decimals=1, $dec_point='.', $thousands_sep='
'Y',
];
$pos = 0;
while ($number >= 1000) {
// as long as the number can be divided by 1000
while ($number >= $divider) {
// As long as the number can be divided by divider.
$pos++;
// Position in array starting with 0
$number = ($number / 1000);
// Position in array starting with 0.
$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%';
// Print the header.
$table->head = [];
$table->head[] = __('IP');
$table->head['main'] = __('IP');
if (!$is_network) {
$table->head[] = __('Flows');
$table->head['flows'] = __('Flows');
}
$table->head[] = __('Packets');
$table->head[] = __('Bytes');
$table->head['pkts'] = __('Packets');
$table->head['bytes'] = __('Bytes');
// Print the data.
$table->data = [];
foreach ($data as $item) {
$table->data[] = [
$item['host'],
$row = [];
$row['main'] = $item['host'];
$row['pkts'] = format_for_graph($item['sum_pkts'], 2);
$row['bytes'] = format_for_graph(
$item['sum_bytes'],
$item['sum_pkts'],
];
2,
'.',
',',
1024,
'B'
);
$table->data[] = $row;
}
html_print_table($table);