Skip to content

Commit

Permalink
Refactor vnstat
Browse files Browse the repository at this point in the history
- Motivation
The function getVnstatData is very complex. I created a function
generateGraph that perform the same of 4 foreachs on that function
getVnstatData.
  • Loading branch information
Allan Klaus committed Oct 5, 2018
1 parent b427952 commit 3006184
Showing 1 changed file with 61 additions and 73 deletions.
134 changes: 61 additions & 73 deletions vnstat.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,79 +110,37 @@ function getVnstatData($path, $type, $interface)
$monthlyGraph = [];
$monthly = [];
$top10 = [];

$i = 0;
foreach ($vnstatDecoded['interfaces'][0]['traffic']['tops'] as $top) {
if (is_array($top)) {
++$i;

$top10[$i]['label'] = date('d/m/Y', strtotime($top['date']['month'] . "/" . $top['date']['day'] . "/" . $top['date']['year']));
$top10[$i]['rx'] = kbytesToString($top['rx']);
$top10[$i]['tx'] = kbytesToString($top['tx']);
$top10[$i]['totalraw'] = ($top['rx'] + $top['tx']);
$top10[$i]['total'] = kbytesToString($top['rx'] + $top['tx']);
}
}

$i = 0;
foreach ($vnstatDecoded['interfaces'][0]['traffic']['days'] as $day) {
if (is_array($day)) {
++$i;

$daily[$i]['label'] = date('d/m/Y', mktime(0, 0, 0, $day['date']['month'], $day['date']['day'], $day['date']['year']));
$daily[$i]['rx'] = kbytesToString($day['rx']);
$daily[$i]['tx'] = kbytesToString($day['tx']);
$daily[$i]['totalraw'] = ($day['rx'] + $day['tx']);
$daily[$i]['total'] = kbytesToString($day['rx'] + $day['tx']);
$daily[$i]['time'] = mktime(0, 0, 0, $day['date']['month'], $day['date']['day'], $day['date']['year']);

$dailyGraph[$i]['label'] = date('jS', mktime(0, 0, 0, $day['date']['month'], $day['date']['day'], $day['date']['year']));
$dailyGraph[$i]['rx'] = $day['rx'];
$dailyGraph[$i]['tx'] = $day['tx'];
$dailyGraph[$i]['total'] = ($day['rx'] + $day['tx']);
$dailyGraph[$i]['time'] = mktime(0, 0, 0, $day['date']['month'], $day['date']['day'], $day['date']['year']);
}
}

$i = 0;
foreach ($vnstatDecoded['interfaces'][0]['traffic']['hours'] as $hour) {
if (is_array($hour)) {
++$i;

$hourly[$i]['label'] = date("ga", mktime($hour['id'], 0, 0, $hour['date']['month'], $hour['date']['day'], $hour['date']['year']));
$hourly[$i]['rx'] = kbytesToString($hour['rx']);
$hourly[$i]['tx'] = kbytesToString($hour['tx']);
$hourly[$i]['totalraw'] = ($hour['rx'] + $hour['tx']);
$hourly[$i]['total'] = kbytesToString($hour['rx'] + $hour['tx']);
$hourly[$i]['time'] = mktime($hour['id'], 0, 0, $hour['date']['month'], $hour['date']['day'], $hour['date']['year']);

$hourlyGraph[$i]['label'] = date("ga", mktime($hour['id'], 0, 0, $hour['date']['month'], $hour['date']['day'], $hour['date']['year']));
$hourlyGraph[$i]['rx'] = $hour['rx'];
$hourlyGraph[$i]['tx'] = $hour['tx'];
$hourlyGraph[$i]['total'] = ($hour['rx'] + $hour['tx']);
$hourlyGraph[$i]['time'] = mktime($hour['id'], 0, 0, $hour['date']['month'], $hour['date']['day'], $hour['date']['year']);
}
}

$i = 0;
foreach ($vnstatDecoded['interfaces'][0]['traffic']['months'] as $month) {
if (is_array($month)) {
++$i;

$monthly[$i]['label'] = date('F', mktime(0, 0, 0, $month['date']['month'], 10));
$monthly[$i]['rx'] = kbytesToString($month['rx']);
$monthly[$i]['tx'] = kbytesToString($month['tx']);
$monthly[$i]['totalraw'] = ($month['rx'] + $month['tx']);
$monthly[$i]['total'] = kbytesToString($month['rx'] + $month['tx']);
$monthly[$i]['time'] = mktime(0, 0, 0, $hour['date']['month'], 1, $hour['date']['year']);

$monthlyGraph[$i]['label'] = date('F', mktime(0, 0, 0, $month['date']['month'], 10));
$monthlyGraph[$i]['rx'] = $month['rx'];
$monthlyGraph[$i]['tx'] = $month['tx'];
$monthlyGraph[$i]['total'] = ($month['rx'] + $month['tx']);
$monthlyGraph[$i]['time'] = mktime(0, 0, 0, $hour['date']['month'], 1, $hour['date']['year']);
}
}
$top10Graph = [];

generateGraph(
$vnstatDecoded['interfaces'][0]['traffic']['tops'],
['type' => 'd/m/Y'],
$top,
$top10Graph
);
unset($top10Graph);

generateGraph(
$vnstatDecoded['interfaces'][0]['traffic']['days'],
['type' => 'd/m/Y', 'typeGraph' => 'jS'],
$daily,
$dailyGraph
);

generateGraph(
$vnstatDecoded['interfaces'][0]['traffic']['hours'],
['type' => 'ga', 'typeGraph' => 'ga'],
$hourly,
$hourlyGraph,
true
);

generateGraph(
$vnstatDecoded['interfaces'][0]['traffic']['months'],
['type' => 'F', 'typeGraph' => 'F'],
$monthly,
$monthlyGraph
);

$sorting_function = function ($item1, $item2) {
if ($item1['time'] == $item2['time']) {
Expand Down Expand Up @@ -227,3 +185,33 @@ function getVnstatData($path, $type, $interface)

return false;
}

function generateGraph($traffic, $dateFormat, &$type, &$typeGraph, $hour = false)
{
$i = 0;
foreach ($traffic as $kind) {
if (is_array($kind)) {
++$i;
$startTime = 0;
if($hour) {
$startTime = $kind['id'];
}

$type[$i]['label'] = date($dateFormat['type'], mktime($startTime, 0, 0, $kind['date']['month'], $kind['date']['day'], $kind['date']['year']));
$type[$i]['rx'] = kbytesToString($kind['rx']);
$type[$i]['tx'] = kbytesToString($kind['tx']);
$type[$i]['totalraw'] = ($kind['rx'] + $kind['tx']);
$type[$i]['total'] = kbytesToString($kind['rx'] + $kind['tx']);

if (is_array($typeGraph)) {
$type[$i]['time'] = mktime($startTime, 0, 0, $kind['date']['month'], $kind['date']['day'], $kind['date']['year']);

$typeGraph[$i]['label'] = date($dateFormat['typeGraph'], mktime($startTime, 0, 0, $kind['date']['month'], $kind['date']['day'], $kind['date']['year']));
$typeGraph[$i]['rx'] = $kind['rx'];
$typeGraph[$i]['tx'] = $kind['tx'];
$typeGraph[$i]['total'] = ($kind['rx'] + $kind['tx']);
$typeGraph[$i]['time'] = mktime($startTime, 0, 0, $kind['date']['month'], $kind['date']['day'], $kind['date']['year']);
}
}
}
}

0 comments on commit 3006184

Please sign in to comment.