add new billing graphs, may still be some bugs :)

git-svn-id: http://www.observium.org/svn/observer/trunk@2744 61d68cd4-352d-0410-923a-c4978735b2b8
This commit is contained in:
Adam Amstrong
2011-11-30 12:45:46 +00:00
parent 94d9e0efd4
commit 00c76d5071
4 changed files with 426 additions and 1 deletions

234
html/bandwidth-graph.php Normal file
View File

@@ -0,0 +1,234 @@
<?php
if (strpos($_SERVER['REQUEST_URI'], "debug")) {
$debug = "1";
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('log_errors', 1);
ini_set('error_reporting', E_ALL);
} else {
$debug = FALSE;
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
ini_set('log_errors', 0);
ini_set('error_reporting', 0);
}
include("../includes/defaults.inc.php");
include("../config.php");
include("../includes/functions.php");
include("includes/functions.inc.php");
include("includes/authenticate.inc.php");
if (!$_SESSION['authenticated']) { echo("unauthenticated"); exit; }
require_once("includes/jpgraph/src/jpgraph.php");
require_once("includes/jpgraph/src/jpgraph_line.php");
require_once("includes/jpgraph/src/jpgraph_bar.php");
require_once("includes/jpgraph/src/jpgraph_utils.inc.php");
require_once("includes/jpgraph/src/jpgraph_date.php");
if (is_numeric($_GET['bill_id'])) {
if (bill_permitted($_GET['bill_id'])) {
$bill_id = $_GET['bill_id'];
} else {
echo("Unauthorised Access Prohibited.");
exit;
}
} else {
echo("Unauthorised Access Prohibited.");
exit;
}
$start = $_GET['from'];
$end = $_GET['to'];
$xsize = (is_numeric($_GET['x']) ? $_GET['x'] : "800" );
$ysize = (is_numeric($_GET['y']) ? $_GET['y'] : "250" );
$count = (is_numeric($_GET['count']) ? $_GET['count'] : "0" );
$iter = 1;
$type = (isset($_GET['type']) ? $_GET['type'] : "date" );
$dur = $end - $start;
$datefrom = date('Ymthis', $start);
$dateto = date('Ymthis', $end);
$imgtype = (isset($_GET['imgtype']) ? $_GET['imgtype'] : "historical" );
$imgbill = (isset($_GET['imgbill']) ? $_GET['imgbill'] : false);
$yaxistitle = "Gigabytes";
$in_data = array();
$out_data = array();
$tot_data = array();
$allow_data = array();
$ave_data = array();
$overuse_data = array();
$ticklabels = array();
function yaxisTitle($value) {
if ($value == "TB") { $res = "Terabytes"; }
elseif ($value == "GB") { $res = "Gigabytes"; }
elseif ($value == "MB") { $res = "Megabytes"; }
elseif ($value == "KB") { $res = "Kilobytes"; }
else { $res = "Bytes"; }
return $res;
}
if ($imgtype == "historical") {
$i = "0";
foreach (dbFetchRows("SELECT * FROM `bill_history` WHERE `bill_id` = ? ORDER BY `bill_datefrom` LIMIT 12", array($bill_id)) as $data) {
$datefrom = strftime("%e %b %Y", strtotime($data['bill_datefrom']));
$dateto = strftime("%e %b %Y", strtotime($data['bill_dateto']));
$datelabel = $datefrom."\n".$dateto;
$traf['in'] = round(substr(formatStorage($data['traf_in'] * 1024 * 1024), 0, -2));
$traf['out'] = round(substr(formatStorage($data['traf_out'] * 1024 * 1024), 0, -2));
$traf['total'] = round(substr(formatStorage($data['traf_total'] * 1024 * 1024), 0, -2));
if ($data['bill_type'] == "Quota") {
$traf['allowed']= round(substr(formatStorage($data['bill_allowed'] * 1024 * 1024), 0, -2));
$traf['overuse']= round(substr(formatStorage($data['bill_overuse'] * 1024 * 1024), 0, -2));
} else {
$traf['allowed']= "0";
$traf['overuse']= "0";
}
array_push($ticklabels, $datelabel);
array_push($in_data, $traf['in']);
array_push($out_data, $traf['out']);
array_push($tot_data, $traf['total']);
array_push($allow_data, $traf['allowed']);
array_push($overuse_data, $traf['overuse']);
$i++;
//print_r($data);
}
if ($i < 12) {
$y = 12 - $i;
for ($x=0;$x<$y;$x++) {
$allowed = (($x == "0") ? $traf['allowed'] : "0" );
array_push($in_data, "0");
array_push($out_data, "0");
array_push($tot_data, "0");
array_push($allow_data, $allowed);
array_push($overuse_data, "0");
array_push($ticklabels, "");
}
}
$graph_name = "Historical bandwidth over the last 12 billing periods";
} else {
$data = array();
$average = 0;
if ($imgtype == "day") {
foreach(dbFetch("SELECT DISTINCT UNIX_TIMESTAMP(timestamp) as timestamp, SUM(delta) as traf_total, SUM(in_delta) as traf_in, SUM(out_delta) as traf_out FROM bill_data WHERE `bill_id` = ? AND `timestamp` >= FROM_UNIXTIME(?) AND `timestamp` <= FROM_UNIXTIME(?) GROUP BY DATE(timestamp) ORDER BY timestamp ASC", array($bill_id, $start, $end)) as $data) {
$traf['in'] = round(substr(formatStorage($data['traf_in']), 0, -2));
$traf['out'] = round(substr(formatStorage($data['traf_out']), 0, -2));
$traf['total'] = round(substr(formatStorage($data['traf_total']), 0, -2));
$yaxistitle = yaxisTitle(substr(formatStorage($data['traf_total']), -2));
$datelabel = strftime("%e\n%b", $data['timestamp']);
array_push($ticklabels, $datelabel);
array_push($in_data, $traf['in']);
array_push($out_data, $traf['out']);
array_push($tot_data, $traf['total']);
$average += $data['traf_total'];
}
$ave_count = count($tot_data);
if ($imgbill != false) {
$days = strftime("%e", date($end - $start)) - $ave_count;
for ($x=0;$x<$days;$x++) {
array_push($ticklabels, "");
array_push($in_data, "0");
array_push($out_data, "0");
array_push($tot_data, "0");
}
}
} elseif ($imgtype == "hour") {
foreach(dbFetch("SELECT DISTINCT UNIX_TIMESTAMP(timestamp) as timestamp, SUM(delta) as traf_total, SUM(in_delta) as traf_in, SUM(out_delta) as traf_out FROM bill_data WHERE `bill_id` = ? AND `timestamp` >= FROM_UNIXTIME(?) AND `timestamp` <= FROM_UNIXTIME(?) GROUP BY HOUR(timestamp) ORDER BY timestamp ASC", array($bill_id, $start, $end)) as $data) {
$traf['in'] = round(substr(formatStorage($data['traf_in']), 0, -2));
$traf['out'] = round(substr(formatStorage($data['traf_out']), 0, -2));
$traf['total'] = round(substr(formatStorage($data['traf_total']), 0, -2));
$datelabel = strftime("%H:%M", $data['timestamp']);
$yaxistitle = yaxisTitle(substr(formatStorage($data['traf_total']), -2));
array_push($ticklabels, $datelabel);
array_push($in_data, $traf['in']);
array_push($out_data, $traf['out']);
array_push($tot_data, $traf['total']);
$average += $data['traf_total'];
}
$ave_count = count($tot_data);
}
$average = round(substr(formatStorage(($average / $ave_count)), 0, -2));
for ($x=0;$x<=count($tot_data);$x++) {
array_push($ave_data, $average);
}
$graph_name = date('M j g:ia', $start)." - ".date('M j g:ia', $end);
}
// Create the graph. These two calls are always required
$graph = new Graph($xsize, $ysize, $graph_name);
$graph->img->SetImgFormat("png");
$graph->SetScale("textlin");
$graph->title->Set("$graph_name");
$graph->title->SetFont(FF_FONT2, FS_BOLD, 10);
$graph->SetMarginColor("white");
$graph->SetFrame(false);
$graph->SetMargin("75", "30", "30", "65");
$graph->legend->SetFont(FF_FONT1, FS_NORMAL);
$graph->legend->SetLayout(LEGEND_HOR);
$graph->legend->Pos("0.52", "0.91", "center");
$graph->xaxis->SetFont(FF_FONT1, FS_BOLD);
$graph->xaxis->SetPos('min');
$graph->xaxis->SetTitleMargin(30);
$graph->xaxis->SetTickLabels($ticklabels);
$graph->xgrid->Show(true,true);
$graph->xgrid->SetColor('#e0e0e0','#efefef');
$graph->yaxis->SetFont(FF_FONT1);
$graph->yaxis->SetTitleMargin(50);
$graph->yaxis->title->SetFont(FF_FONT1, FS_NORMAL, 10);
$graph->yaxis->title->Set($yaxistitle);
$graph->ygrid->SetFill(true,'#EFEFEF@0.5','#FFFFFF@0.5');
// Create the bar plots
$barplot_tot = new BarPlot($tot_data);
$barplot_tot->SetLegend("Traffic total");
$barplot_tot->SetColor("#d5d5d5");
$barplot_tot->SetFillColor("#d5d5d5@0.5");
$barplot_in = new BarPlot($in_data);
$barplot_in->SetLegend("Traffic In");
$barplot_in->SetColor('darkgreen');
$barplot_in->SetFillColor('lightgreen@0.4');
$barplot_in->SetWeight(1);
$barplot_out = new BarPlot($out_data);
$barplot_out->SetLegend("Traffic Out");
$barplot_out->SetColor('darkblue');
$barplot_out->SetFillColor('lightblue@0.4');
$barplot_out->SetWeight(1);
if ($imgtype == "historical") {
$barplot_over = new BarPlot($overuse_data);
$barplot_over->SetLegend("Traffic Overusage");
$barplot_over->SetColor('darkred');
$barplot_over->SetFillColor('lightred@0.4');
$barplot_over->SetWeight(1);
$lineplot_allow = new LinePlot($allow_data);
$lineplot_allow->SetLegend("Traffic Allowed");
$lineplot_allow->SetColor('black');
$lineplot_allow->SetWeight(1);
$gbplot = new GroupBarPlot(array($barplot_in, $barplot_out, $barplot_tot, $barplot_over));
} else {
$lineplot_allow = new LinePlot($ave_data);
$lineplot_allow->SetLegend("Average per day");
$lineplot_allow->SetColor('black');
//$lineplot_allow->SetFillColor('lightred@0.4');
$lineplot_allow->SetWeight(1);
$gbplot = new GroupBarPlot(array($barplot_in, $barplot_out, $barplot_tot));
}
$graph->Add($gbplot);
$graph->Add($lineplot_allow);
// Display the graph
$graph->Stroke();
?>

View File

@@ -96,6 +96,12 @@ if (bill_permitted($bill_id))
echo(" | "); echo(" | ");
if ($_GET['optb'] == "bandwidth") { echo("<span class='pagemenu-selected'>"); }
echo("<a href='bill/".$bill_id."/bandwidth/'>Bandwidth Graphs</a>");
if ($_GET['optb'] == "bandwidth") { echo("</span>"); }
echo(" | ");
if ($_GET['optb'] == "history") { echo("<span class='pagemenu-selected'>"); } if ($_GET['optb'] == "history") { echo("<span class='pagemenu-selected'>"); }
echo("<a href='bill/".$bill_id."/history/'>Historical Usage</a>"); echo("<a href='bill/".$bill_id."/history/'>Historical Usage</a>");
if ($_GET['optb'] == "history") { echo("</span>"); } if ($_GET['optb'] == "history") { echo("</span>"); }
@@ -130,6 +136,10 @@ if (bill_permitted($bill_id))
{ {
include("pages/bill/history.inc.php"); include("pages/bill/history.inc.php");
} }
elseif ($_GET['optb'] == "bandwidth")
{
include("pages/bill/bandwidth.inc.php");
}
elseif ($_GET['optb'] == "details" || $_GET['optb'] == "basic") { elseif ($_GET['optb'] == "details" || $_GET['optb'] == "basic") {
$bill_data = dbFetchRow("SELECT * FROM bills WHERE bill_id = ?", array($bill_id)); $bill_data = dbFetchRow("SELECT * FROM bills WHERE bill_id = ?", array($bill_id));

View File

@@ -0,0 +1,174 @@
<?php
$pagetitle[] = "Bandwidth Graphs";
$bill_data = dbFetchRow("SELECT * FROM bills WHERE bill_id = ?", array($bill_id));
$today = str_replace("-", "", dbFetchCell("SELECT CURDATE()"));
$tomorrow = str_replace("-", "", dbFetchCell("SELECT DATE_ADD(CURDATE(), INTERVAL 1 DAY)"));
$last_month = str_replace("-", "", dbFetchCell("SELECT DATE_SUB(CURDATE(), INTERVAL 1 MONTH)"));
$rightnow = $today . date(His);
$before = $yesterday . date(His);
$lastmonth = $last_month . date(His);
$dayofmonth = $bill_data['bill_day'];
$day_data = getDates($dayofmonth);
$datefrom = $day_data['0'];
$dateto = $day_data['1'];
$lastfrom = $day_data['2'];
$lastto = $day_data['3'];
$total_data = $bill_data['total_data'];
$in_data = $bill_data['total_data_in'];
$out_data = $bill_data['total_data_out'];
$fromtext = dbFetchCell("SELECT DATE_FORMAT($datefrom, '%M %D %Y')");
$totext = dbFetchCell("SELECT DATE_FORMAT($dateto, '%M %D %Y')");
$unixfrom = dbFetchCell("SELECT UNIX_TIMESTAMP('$datefrom')");
$unixto = dbFetchCell("SELECT UNIX_TIMESTAMP('$dateto')");
$unix_prev_from = dbFetchCell("SELECT UNIX_TIMESTAMP('$lastfrom')");
$unix_prev_to = dbFetchCell("SELECT UNIX_TIMESTAMP('$lastto')");
$lastmonth = dbFetchCell("SELECT UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 MONTH))");
$yesterday = dbFetchCell("SELECT UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))");
$rightnow = date(U);
echo("<h3>Billed Ports</h3>");
$ports = dbFetchRows("SELECT * FROM `bill_ports` AS B, `ports` AS P, `devices` AS D
WHERE B.bill_id = ? AND P.interface_id = B.port_id
AND D.device_id = P.device_id", array($bill_id));
foreach ($ports as $port)
{
echo(generate_port_link($port) . " on " . generate_device_link($port) . "<br />");
}
$cur_days = date('d', (strtotime("now") - strtotime($datefrom)));
$total_days = date('d', (strtotime($dateto) - strtotime($datefrom)));
$total['data'] = formatStorage($bill_data['total_data'] * 1024 * 1024);
if ($bill_data['bill_type'] == "quota") {
$total['allow'] = formatStorage($bill_data['bill_gb'] * 1024 * 1024 * 1024);
} else {
$total['allow'] = "-";
}
$total['ave'] = formatStorage(($bill_data['total_data'] / $cur_days) * 1024 * 1024);
$total['est'] = formatStorage(($bill_data['total_data'] / $cur_days * $total_days) * 1024 * 1024);
$total['per'] = round((($bill_data['total_data'] / 1024) / $bill_data['bill_gb'] * 100), 2);
$total['bg'] = get_percentage_colours($total['per']);
$in['data'] = formatStorage($bill_data['total_data_in'] * 1024 * 1024);
$in['allow'] = $total['data'];
$in['ave'] = formatStorage(($bill_data['total_data_in'] / $cur_days) * 1024 * 1024);
$in['est'] = formatStorage(($bill_data['total_data_in'] / $cur_days * $total_days) * 1024 * 1024);
$in['per'] = round(($bill_data['total_data_in'] / $bill_data['total_data'] * 100), 2);
$in['bg'] = get_percentage_colours($in['per']);
$out['data'] = formatStorage($bill_data['total_data_out'] * 1024 * 1024);
$out['allow'] = $total['data'];
$out['ave'] = formatStorage(($bill_data['total_data_out'] / $cur_days) * 1024 * 1024);
$out['est'] = formatStorage(($bill_data['total_data_out'] / $cur_days * $total_days) * 1024 * 1024);
$out['per'] = round(($bill_data['total_data_out'] / $bill_data['total_data'] * 100), 2);
$out['bg'] = get_percentage_colours($out['per']);
$ousage['over'] = $bill_data['total_data'] - ($bill_data['bill_gb'] * 1024);
$ousage['over'] = (($ousage['over'] < 0) ? "0" : $ousage['over']);
$ousage['data'] = formatStorage($ousage['over'] * 1024 * 1024);
$ousage['allow'] = $total['allow'];
$ousage['ave'] = formatStorage(($ousage['over'] / $cur_days ) * 1024 * 1024);
$ousage['est'] = formatStorage(($ousage['over'] / $cur_days * $total_days) * 1024 * 1024);
$ousage['per'] = round(((($bill_data['total_data'] / 1024) / $bill_data['bill_gb'] * 100) - 100), 2);
$ousage['per'] = (($ousage['per'] < 0) ? "0" : $ousage['per']);
$ousage['bg'] = get_percentage_colours($ousage['per']);
function showPercent($per) {
$background = get_percentage_colours($per);
$right_background = $background['right'];
$left_background = $background['left'];
$res = print_percentage_bar(350, 20, $perc, NULL, "ffffff", $left_background, $per."%", "ffffff", $right_background);
return $res;
}
echo("<h3>Bill Summary</h3>");
echo("<h4>Quota Bill</h4>");
echo("<table cellpadding=\"5\" cellspacing=\"0\" border=\"0\" class=\"devicetable\">");
echo(" <tr><td colspan=\"7\">Billing Period from ".$fromtext." to ".$totext."</td></tr>");
echo(" <tr style=\"font-weight: bold;\">");
echo(" <td width=\"125\">Bandwidth</td>");
echo(" <td width=\"10\"></td>");
echo(" <td width=\"100\">Used</td>");
echo(" <td width=\"100\">Allowed</td>");
echo(" <td width=\"100\">Average</td>");
echo(" <td width=\"100\">Estimated</td>");
echo(" <td width=\"360\"></td>");
echo(" </tr>");
echo(" <tr style=\"background: ".$list_colour_b.";\">");
echo(" <td>Transferred</td>");
echo(" <td>:</td>");
echo(" <td>".$total['data']."</td>");
echo(" <td>".$total['allow']."</td>");
echo(" <td>".$total['ave']."</td>");
echo(" <td>".$total['est']."</td>");
echo(" <td width=\"360\">".showPercent($total['per'])."</td>");
echo(" </tr>");
echo(" <tr style=\"background: ".$list_colour_a.";\">");
echo(" <td>Inbound</td>");
echo(" <td>:</td>");
echo(" <td>".$in['data']."</td>");
echo(" <td>".$in['allow']."</td>");
echo(" <td>".$in['ave']."</td>");
echo(" <td>".$in['est']."</td>");
echo(" <td>".showPercent($in['per'])."</td>");
echo(" </tr>");
echo(" <tr style=\"background: ".$list_colour_b.";\">");
echo(" <td>Outbound</td>");
echo(" <td>:</td>");
echo(" <td>".$out['data']."</td>");
echo(" <td>".$out['allow']."</td>");
echo(" <td>".$out['ave']."</td>");
echo(" <td>".$out['est']."</td>");
echo(" <td>".showPercent($out['per'])."</td>");
echo(" </tr>");
if ($ousage['over'] > 0 && $bill_data['bill_type'] == "quota") {
echo(" <tr style=\"background: ".$list_colour_a.";\">");
echo(" <td>Already overusage</td>");
echo(" <td>:</td>");
echo(" <td><span style=\"color: #".$total['bg']['left']."; font-weight: bold;\">".$ousage['data']."</span></td>");
echo(" <td>".$ousage['allow']."</td>");
echo(" <td>".$ousage['ave']."</td>");
echo(" <td>".$ousage['est']."</td>");
echo(" <td>".showPercent($ousage['per'])."</td>");
}
echo("</table>");
$bi = "<img src='bandwidth-graph.php?bill_id=" . $bill_id . "&amp;bill_code=" . $_GET['bill_code'];
$bi .= "&amp;from=" . $unixfrom . "&amp;to=" . $unixto;
$bi .= "&amp;imgtype=day&imgbill=1";
$bi .= "&amp;x=1190&amp;y=250";
$bi .= "$type'>";
$li = "<img src='bandwidth-graph.php?bill_id=" . $bill_id . "&amp;bill_code=" . $_GET['bill_code'];
$li .= "&amp;from=" . $unix_prev_from . "&amp;to=" . $unix_prev_to;
$li .= "&amp;imgtype=day";
$li .= "&amp;x=1190&amp;y=250";
$li .= "$type'>";
$di = "<img src='bandwidth-graph.php?bill_id=" . $bill_id . "&amp;bill_code=" . $_GET['bill_code'];
$di .= "&amp;from=" . $config['time']['day'] . "&amp;to=" . $config['time']['now'];
$di .= "&amp;imgtype=hour";
$di .= "&amp;x=1190&amp;y=250";
$di .= "$type'>";
$mi = "<img src='bandwidth-graph.php?bill_id=" . $bill_id . "&amp;bill_code=" . $_GET['bill_code'];
$mi .= "&amp;from=" . $lastmonth . "&amp;to=" . $rightnow;
$mi .= "&amp;&imgtype=day";
$mi .= "&amp;x=1190&amp;y=250";
$mi .= "$type'>";
echo("<h3>Billing View</h3>$bi");
#echo("<h3>Previous Bill View</h3>$li");
echo("<h3>24 Hour View</h3>$di");
echo("<h3>Monthly View</h3>$mi");
?>

View File

@@ -3,6 +3,13 @@
$pagetitle[] = "Historical Usage"; $pagetitle[] = "Historical Usage";
$i=0; $i=0;
$img = "<img src=\"bandwidth-graph.php?bill_id=".$bill_id;
$img .= "&amp;imgtype=historical";
$img .= "&amp;x=1190&amp;y=250";
$img .= "\" style=\"margin: 15px 5px 25px 5px;\" />";
echo($img);
echo("<table border=0 cellspacing=0 cellpadding=5 class=devicetable width=100%> echo("<table border=0 cellspacing=0 cellpadding=5 class=devicetable width=100%>
<tr style=\"font-weight: bold; \"> <tr style=\"font-weight: bold; \">
<td width=\"7\"></td> <td width=\"7\"></td>
@@ -62,7 +69,7 @@
<td>$total_data</td> <td>$total_data</td>
<td>$rate_95th</td> <td>$rate_95th</td>
<td style=\"text-align: center;\">$overuse</td> <td style=\"text-align: center;\">$overuse</td>
<td>".print_percentage_bar (250, 20, $perc, NULL, "ffffff", $background['left'], $percent . "%", "ffffff", $background['right'])."</td> <td>".print_percentage_bar(250, 20, $perc, NULL, "ffffff", $background['left'], $percent."%", "ffffff", $background['right'])."</td>
</tr>"); </tr>");
$i++; $i++;