Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fixs and enhancments #5

Merged
merged 3 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions app/includes/vnstat.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,14 @@ public function getInterfaceData($timeperiod, $type, $interface) {

// Blank placeholder
$trafficData = [];
$i = 0;
$i = -1;

// Get the array index for the chosen interface
$arrayIndex = array_search($interface, array_column($this->vnstatData['interfaces'], 'id'));
if ($this->vnstatJsonVersion == 1) {
$arrayIndex = array_search($interface, array_column($this->vnstatData['interfaces'], 'id'));
} else {
$arrayIndex = array_search($interface, array_column($this->vnstatData['interfaces'], 'name'));
}

if ($timeperiod == 'top10') {
if ($type == 'table') {
Expand All @@ -119,29 +123,26 @@ public function getInterfaceData($timeperiod, $type, $interface) {
}
}

if ($timeperiod == 'five') {
if (($this->vnstatJsonVersion > 1) && ($timeperiod == 'five')) {
if ($type == 'table') {
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['fiveminute'] as $traffic) {
if (is_array($traffic)) {
$i++;

$fiveHour = $traffic['time']['hour'];
$fiveMinute = $traffic['time']['minute'];

$trafficData[$i]['label'] = date("d/m/Y H:i", mktime($traffic['time']['hour'], $traffic['time']['minute'], 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']));
$trafficData[$i]['time'] = mktime($traffic['time']['hour'], $traffic['time']['minute'], 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']);
$trafficData[$i]['rx'] = formatSize($traffic['rx'], $this->vnstatJsonVersion);
$trafficData[$i]['tx'] = formatSize($traffic['tx'], $this->vnstatJsonVersion);
$trafficData[$i]['total'] = formatSize(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
}
}

} else if ($type == 'graph') {
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['fiveminute'] as $traffic) {
if (is_array($traffic)) {
$i++;

$trafficData[$i]['label'] = sprintf("Date(%d, %d, %d, %d, %d, %d)", $traffic['date']['year'], $traffic['date']['month']-1, $traffic['date']['day'], $traffic['time']['hour'], $traffic['time']['minute'], 0);
$trafficData[$i]['label'] = sprintf("Date(%d, %d, %d, %d, %d)", $traffic['date']['year'], $traffic['date']['month']-1, $traffic['date']['day'], $traffic['time']['hour'], $traffic['time']['minute']);
$trafficData[$i]['time'] = mktime($traffic['time']['hour'], $traffic['time']['minute'], 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']);
$trafficData[$i]['rx'] = kibibytesToBytes($traffic['rx'], $this->vnstatJsonVersion);
$trafficData[$i]['tx'] = kibibytesToBytes($traffic['tx'], $this->vnstatJsonVersion);
$trafficData[$i]['total'] = kibibytesToBytes(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
Expand Down Expand Up @@ -170,9 +171,6 @@ public function getInterfaceData($timeperiod, $type, $interface) {
}
}

if ($this->vnstatJsonVersion == 1) {
usort($trafficData, sortingFunction);
}

} else if ($type == 'graph') {
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['hour'.$typeAppend] as $traffic) {
Expand All @@ -185,7 +183,8 @@ public function getInterfaceData($timeperiod, $type, $interface) {
$hour = $traffic['time']['hour'];
}

$trafficData[$i]['label'] = sprintf("Date(%d, %d, %d, %d, %d, %d)", $traffic['date']['year'], $traffic['date']['month']-1, $traffic['date']['day'], $hour, 0, 0);
$trafficData[$i]['label'] = sprintf("Date(%d, %d, %d, %d)", $traffic['date']['year'], $traffic['date']['month']-1, $traffic['date']['day'], $hour);
$trafficData[$i]['time'] = mktime($hour, 0, 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']);
$trafficData[$i]['rx'] = kibibytesToBytes($traffic['rx'], $this->vnstatJsonVersion);
$trafficData[$i]['tx'] = kibibytesToBytes($traffic['tx'], $this->vnstatJsonVersion);
$trafficData[$i]['total'] = kibibytesToBytes(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
Expand All @@ -201,6 +200,7 @@ public function getInterfaceData($timeperiod, $type, $interface) {
$i++;

$trafficData[$i]['label'] = date('d/m/Y', mktime(0, 0, 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']));
$trafficData[$i]['time'] = mktime(0, 0, 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']);
$trafficData[$i]['rx'] = formatSize($traffic['rx'], $this->vnstatJsonVersion);
$trafficData[$i]['tx'] = formatSize($traffic['tx'], $this->vnstatJsonVersion);
$trafficData[$i]['total'] = formatSize(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
Expand All @@ -211,7 +211,8 @@ public function getInterfaceData($timeperiod, $type, $interface) {
if (is_array($traffic)) {
$i++;

$trafficData[$i]['label'] = sprintf("Date(%d, %d, %d, %d, %d, %d)", $traffic['date']['year'], $traffic['date']['month']-1, $traffic['date']['day'], 0, 0, 0);
$trafficData[$i]['label'] = sprintf("Date(%d, %d, %d)", $traffic['date']['year'], $traffic['date']['month']-1, $traffic['date']['day']);
$trafficData[$i]['time'] = mktime(0, 0, 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']);
$trafficData[$i]['rx'] = kibibytesToBytes($traffic['rx'], $this->vnstatJsonVersion);
$trafficData[$i]['tx'] = kibibytesToBytes($traffic['tx'], $this->vnstatJsonVersion);
$trafficData[$i]['total'] = kibibytesToBytes(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
Expand All @@ -227,6 +228,7 @@ public function getInterfaceData($timeperiod, $type, $interface) {
$i++;

$trafficData[$i]['label'] = date('F Y', mktime(0, 0, 0, $traffic['date']['month'], 10, $traffic['date']['year']));
$trafficData[$i]['time'] = mktime(0, 0, 0, $traffic['date']['month'], 10, $traffic['date']['year']);
$trafficData[$i]['rx'] = formatSize($traffic['rx'], $this->vnstatJsonVersion);
$trafficData[$i]['tx'] = formatSize($traffic['tx'], $this->vnstatJsonVersion);
$trafficData[$i]['total'] = formatSize(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
Expand All @@ -237,7 +239,8 @@ public function getInterfaceData($timeperiod, $type, $interface) {
if (is_array($traffic)) {
$i++;

$trafficData[$i]['label'] = sprintf("Date(%d, %d, %d, %d, %d, %d)", $traffic['date']['year'], $traffic['date']['month'] - 1, 10, 0, 0, 0);
$trafficData[$i]['label'] = sprintf("Date(%d, %d, %d)", $traffic['date']['year'], $traffic['date']['month'] - 1, 10);
$trafficData[$i]['time'] = mktime(0, 0, 0, $traffic['date']['month'], 10, $traffic['date']['year']);
$trafficData[$i]['rx'] = kibibytesToBytes($traffic['rx'], $this->vnstatJsonVersion);
$trafficData[$i]['tx'] = kibibytesToBytes($traffic['tx'], $this->vnstatJsonVersion);
$trafficData[$i]['total'] = kibibytesToBytes(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
Expand All @@ -246,17 +249,22 @@ public function getInterfaceData($timeperiod, $type, $interface) {
}
}

if ($timeperiod != 'top10') {
usort($trafficData, 'sortingFunction');
}

if ($type == 'graph') {
// Get the largest value and then prefix (B, KB, MB, GB, etc)
$trafficLargestValue = getLargestValue($trafficData);
$trafficLargestPrefix = getLargestPrefix($trafficLargestValue);

foreach($trafficData as $key => $value) {
$trafficData[$key]['rx'] = formatBytesTo($value['rx'], $trafficLargestPrefix);
$trafficData[$key]['tx'] = formatBytesTo($value['tx'], $trafficLargestPrefix);
$trafficData[$key]['total'] = formatBytesTo($value['total'], $trafficLargestPrefix);
$trafficData[$key]['delimiter'] = $trafficLargestPrefix;
foreach($trafficData as &$value) {
$value['rx'] = formatBytesTo($value['rx'], $trafficLargestPrefix);
$value['tx'] = formatBytesTo($value['tx'], $trafficLargestPrefix);
$value['total'] = formatBytesTo($value['total'], $trafficLargestPrefix);
}
unset($value);
$trafficData[0]['delimiter'] = $trafficLargestPrefix;
}

return $trafficData;
Expand Down
16 changes: 9 additions & 7 deletions app/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

// Require includes
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/includes/vnstat.php';
require __DIR__ . '/includes/utilities.php';
require __DIR__ . '/includes/vnstat.php';
require __DIR__ . '/includes/config.php';

if (isset($vnstat_config)) {
Expand Down Expand Up @@ -83,21 +83,23 @@
$smarty->assign('top10TableData', $top10Data);

// Populate graph data
$fiveGraphData = $vnstat->getInterfaceData('five', 'graph', $thisInterface);
$smarty->assign('fiveGraphData', $fiveGraphData);
$smarty->assign('fiveLargestPrefix', $fiveGraphData[1]['delimiter']);
if ($vnstat->getVnstatJsonVersion() > 1) {
$fiveGraphData = $vnstat->getInterfaceData('five', 'graph', $thisInterface);
$smarty->assign('fiveGraphData', $fiveGraphData);
$smarty->assign('fiveLargestPrefix', $fiveGraphData[0]['delimiter']);
}

$hourlyGraphData = $vnstat->getInterfaceData('hourly', 'graph', $thisInterface);
$smarty->assign('hourlyGraphData', $hourlyGraphData);
$smarty->assign('hourlyLargestPrefix', $hourlyGraphData[1]['delimiter']);
$smarty->assign('hourlyLargestPrefix', $hourlyGraphData[0]['delimiter']);

$dailyGraphData = $vnstat->getInterfaceData('daily', 'graph', $thisInterface);
$smarty->assign('dailyGraphData', $dailyGraphData);
$smarty->assign('dailyLargestPrefix', $dailyGraphData[1]['delimiter']);
$smarty->assign('dailyLargestPrefix', $dailyGraphData[0]['delimiter']);

$monthlyGraphData = $vnstat->getInterfaceData('monthly', 'graph', $thisInterface);
$smarty->assign('monthlyGraphData', $monthlyGraphData);
$smarty->assign('monthlyLargestPrefix', $monthlyGraphData[1]['delimiter']);
$smarty->assign('monthlyLargestPrefix', $monthlyGraphData[0]['delimiter']);

// Display the page
$smarty->display('templates/site_index.tpl');
Expand Down
68 changes: 42 additions & 26 deletions app/templates/module_graph_js.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

function drawFiveChart()
{
{if $jsonVersion gt 1}
var data = new google.visualization.DataTable();

data.addColumn('date', 'Time');
data.addColumn('datetime', 'Time');
data.addColumn('number', 'Traffic In');
data.addColumn('number', 'Traffic Out');
data.addColumn('number', 'Total Traffic');
Expand All @@ -22,41 +23,44 @@
{/foreach}
]);

let endD = (new {$fiveGraphData[0]['label']}).getTime();

let options = {
title: 'Five minute Network Traffic',
orientation: 'horizontal',
legend: { position: 'right' },
explorer: {
axis: 'horizontal',
maxZoomIn: 4.0,
maxZoomOut: 3.0
zoomDelta: 1.1,
maxZoomIn: 0.1,
maxZoomOut: 10.0
},
vAxis: {
title: 'Data',
format: '##.## {$fiveLargestPrefix}'
},
hAxis: {
title: 'Hour',
format: 'HH:mm',
direction: -1,
ticks: [
{foreach from=$fiveGraphData key=key item=value}
new {$value.label},
{/foreach}
]
format: 'd/H:mm',
minorGridlines: { count: 0 },
title: 'Day/Hour:Minute',
viewWindow: {
min: 'Date('+(endD-7200000).toString()+')',
max: 'Date('+(endD+150000).toString()+')'
}
}
};

var formatDate = new google.visualization.DateFormat({ pattern: 'dd/MM/yyyy HH:mm' });
formatDate.format(data, 0);

var formatNumber = new google.visualization.NumberFormat({ pattern: '##.## {$fiveLargestPrefix}' });
formatNumber.format(data, 1);
formatNumber.format(data, 2);
formatNumber.format(data, 3);

let chart = new google.visualization.BarChart(document.getElementById('fiveNetworkTrafficGraph'));
chart.draw(data, google.charts.Bar.convertOptions(options));
{/if}
}

function drawHourlyChart()
Expand All @@ -74,23 +78,29 @@
{/foreach}
]);

let endD = (new {$hourlyGraphData[0]['label']}).getTime();

let options = {
title: 'Hourly Network Traffic',
orientation: 'horizontal',
legend: { position: 'right' },
explorer: {
axis: 'horizontal',
maxZoomIn: 4.0,
maxZoomOut: 3.0
zoomDelta: 1.1,
maxZoomIn: 0.1,
maxZoomOut: 10.0
},
vAxis: {
title: 'Data',
format: '##.## {$hourlyLargestPrefix}'
},
hAxis: {
title: 'Hour',
format: 'HH:mm',
title: 'Day/Hour',
format: 'd/H',
direction: -1,
viewWindow: {
min: 'Date('+(endD-86400000).toString()+')',
max: 'Date('+(endD+1800000).toString()+')'
},
ticks: [
{foreach from=$hourlyGraphData key=key item=value}
new {$value.label},
Expand Down Expand Up @@ -125,27 +135,33 @@
[new {$value.label}, {$value.rx}, {$value.tx}, {$value.total}],
{/foreach}
]);


let endD = (new {$dailyGraphData[0]['label']}).getTime();

let options = {
title: 'Daily Network Traffic',
orientation: 'horizontal',
legend: { position: 'right' },
explorer: {
axis: 'horizontal',
maxZoomIn: 4.0,
maxZoomOut: 3.0
zoomDelta: 1.1,
maxZoomIn: 0.1,
maxZoomOut: 10.0
},
vAxis: {
title: 'Data',
format: '##.## {$dailyLargestPrefix}'
},
hAxis: {
title: 'Day',
format: 'dd/MM/YYYY',
viewWindow: {
min: 'Date('+(endD-2592000000).toString()+')',
max: 'Date('+(endD+43200000).toString()+')'
},
direction: -1
}
};

var formatDate = new google.visualization.DateFormat({ pattern: 'dd/MM/yyyy' });
formatDate.format(data, 0);

Expand Down Expand Up @@ -179,11 +195,11 @@
legend: { position: 'right' },
explorer: {
axis: 'horizontal',
maxZoomIn: 4.0,
maxZoomOut: 3.0
zoomDelta: 1.1,
maxZoomIn: 0.1,
maxZoomOut: 10.0
},
vAxis: {
title: 'Data',
format: '##.## {$monthlyLargestPrefix}'
},
hAxis: {
Expand Down
4 changes: 4 additions & 0 deletions app/templates/module_table.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</ul>

<div class="tab-content" id="tableTabContent">
{if $jsonVersion gt 1}
<div class="tab-pane fade show active" id="five-table" role="tabpanel" aria-labelledby="five-table-tab">
<table class="table table-bordered">
<thead>
Expand All @@ -47,7 +48,10 @@
</table>
</div>

<div class="tab-pane fade" id="hourly-table" role="tabpanel" aria-labelledby="hourly-table-tab">
{else}
<div class="tab-pane fade show active" id="hourly-table" role="tabpanel" aria-labelledby="hourly-table-tab">
{/if}
<table class="table table-bordered">
<thead>
<tr>
Expand Down