From 00c76d50716ef43797c304ae5c596885100cfcda Mon Sep 17 00:00:00 2001 From: Adam Amstrong Date: Wed, 30 Nov 2011 12:45:46 +0000 Subject: [PATCH] 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 --- html/bandwidth-graph.php | 234 ++++++++++++++++++++++++++++++ html/pages/bill.inc.php | 10 ++ html/pages/bill/bandwidth.inc.php | 174 ++++++++++++++++++++++ html/pages/bill/history.inc.php | 9 +- 4 files changed, 426 insertions(+), 1 deletion(-) create mode 100644 html/bandwidth-graph.php create mode 100644 html/pages/bill/bandwidth.inc.php diff --git a/html/bandwidth-graph.php b/html/bandwidth-graph.php new file mode 100644 index 0000000000..855513546a --- /dev/null +++ b/html/bandwidth-graph.php @@ -0,0 +1,234 @@ += 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(); + +?> diff --git a/html/pages/bill.inc.php b/html/pages/bill.inc.php index ce123f5d56..63e7c9e91c 100644 --- a/html/pages/bill.inc.php +++ b/html/pages/bill.inc.php @@ -96,6 +96,12 @@ if (bill_permitted($bill_id)) echo(" | "); + if ($_GET['optb'] == "bandwidth") { echo(""); } + echo("Bandwidth Graphs"); + if ($_GET['optb'] == "bandwidth") { echo(""); } + + echo(" | "); + if ($_GET['optb'] == "history") { echo(""); } echo("Historical Usage"); if ($_GET['optb'] == "history") { echo(""); } @@ -130,6 +136,10 @@ if (bill_permitted($bill_id)) { include("pages/bill/history.inc.php"); } + elseif ($_GET['optb'] == "bandwidth") + { + include("pages/bill/bandwidth.inc.php"); + } elseif ($_GET['optb'] == "details" || $_GET['optb'] == "basic") { $bill_data = dbFetchRow("SELECT * FROM bills WHERE bill_id = ?", array($bill_id)); diff --git a/html/pages/bill/bandwidth.inc.php b/html/pages/bill/bandwidth.inc.php new file mode 100644 index 0000000000..7da9179466 --- /dev/null +++ b/html/pages/bill/bandwidth.inc.php @@ -0,0 +1,174 @@ +Billed Ports"); + + $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) . "
"); + } + + $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("

Bill Summary

"); + echo("

Quota Bill

"); + echo(""); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + if ($ousage['over'] > 0 && $bill_data['bill_type'] == "quota") { + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + echo(" "); + } + echo("
Billing Period from ".$fromtext." to ".$totext."
BandwidthUsedAllowedAverageEstimated
Transferred:".$total['data']."".$total['allow']."".$total['ave']."".$total['est']."".showPercent($total['per'])."
Inbound:".$in['data']."".$in['allow']."".$in['ave']."".$in['est']."".showPercent($in['per'])."
Outbound:".$out['data']."".$out['allow']."".$out['ave']."".$out['est']."".showPercent($out['per'])."
Already overusage:".$ousage['data']."".$ousage['allow']."".$ousage['ave']."".$ousage['est']."".showPercent($ousage['per'])."
"); + + $bi = "Billing View$bi"); + #echo("

Previous Bill View

$li"); + echo("

24 Hour View

$di"); + echo("

Monthly View

$mi"); + +?> diff --git a/html/pages/bill/history.inc.php b/html/pages/bill/history.inc.php index 2b226dd044..53048311e8 100644 --- a/html/pages/bill/history.inc.php +++ b/html/pages/bill/history.inc.php @@ -3,6 +3,13 @@ $pagetitle[] = "Historical Usage"; $i=0; + $img = ""; + + echo($img); + echo(" @@ -62,7 +69,7 @@ - + "); $i++;
$total_data $rate_95th $overuse".print_percentage_bar (250, 20, $perc, NULL, "ffffff", $background['left'], $percent . "%", "ffffff", $background['right'])."".print_percentage_bar(250, 20, $perc, NULL, "ffffff", $background['left'], $percent."%", "ffffff", $background['right'])."