1<?php
2
3use Amenadiel\JpGraph\Graph\Graph;
4use Amenadiel\JpGraph\Plot\LinePlot;
5
6$bill_hist_id = $vars['bill_hist_id'];
7$reducefactor = $vars['reducefactor'];
8
9if (is_numeric($bill_hist_id)) {
10    if ($reducefactor < 2) {
11        $extents = dbFetchRow('SELECT UNIX_TIMESTAMP(bill_datefrom) as `from`, UNIX_TIMESTAMP(bill_dateto) AS `to`FROM bill_history WHERE bill_id = ? AND bill_hist_id = ?', [$bill_id, $bill_hist_id]);
12        $dur = $extents['to'] - $extents['from'];
13        $reducefactor = round(($dur / 300 / (($vars['height'] - 100) * 3)), 0);
14
15        if ($reducefactor < 2) {
16            $reducefactor = 2;
17        }
18    }
19    $graph_data = getBillingHistoryBitsGraphData($bill_id, $bill_hist_id, $reducefactor);
20} else {
21    if ($reducefactor < 2) {
22        $dur = $vars['to'] - $vars['from'];
23        $reducefactor = round(($dur / 300 / (($vars['height'] - 100) * 3)), 0);
24
25        if ($reducefactor < 2) {
26            $reducefactor = 2;
27        }
28    }
29    $graph_data = getBillingBitsGraphData($bill_id, $vars['from'], $vars['to'], $reducefactor);
30}
31
32// header('Content-Type: application/json');
33// print_r(json_encode($graph_data));
34// exit();
35
36$n = count($graph_data['ticks']);
37$xmin = $graph_data['ticks'][0];
38$xmax = $graph_data['ticks'][($n - 1)];
39
40function TimeCallback($aVal)
41{
42    global $dur;
43
44    if ($dur < 172800) {
45        return date('H:i', $aVal);
46    } elseif ($dur < 604800) {
47        return date('D', $aVal);
48    } else {
49        return date('j M', $aVal);
50    }
51}//end TimeCallback()
52
53function InvertCallback($x)
54{
55    return $x * -1;
56}//end InvertCallback
57
58function YCallback($y)
59{
60    return \LibreNMS\Util\Number::formatSi($y, 2, 0, '');
61}
62
63$graph = new Graph($vars['width'], $vars['height'], $graph_data['graph_name']);
64$graph->img->SetImgFormat('png');
65
66// work around bug in jpgraph error handling
67$graph->title->Set(' ');
68$graph->subtitle->Set(' ');
69$graph->subsubtitle->Set(' ');
70$graph->footer->left->Set(' ');
71$graph->footer->center->Set(' ');
72$graph->footer->right->Set(' ');
73
74$graph->SetScale('datlin', 0, 0, $graph_data['from'], $graph_data['to']);
75$graph->SetMarginColor('white');
76$graph->SetFrame(false);
77$graph->SetMargin('75', '30', '30', '65');
78
79$graph->legend->SetFont(FF_FONT1, FS_NORMAL);
80$graph->legend->SetLayout(LEGEND_HOR);
81$graph->legend->Pos('0.52', '0.91', 'center');
82
83$graph->xaxis->SetFont(FF_FONT1, FS_BOLD);
84$graph->xaxis->SetPos('min');
85$graph->xaxis->SetTitleMargin(30);
86$graph->xaxis->title->Set(' ');
87$graph->xaxis->SetTextLabelInterval(2);
88$graph->xaxis->SetLabelFormatCallback('TimeCallBack');
89
90$graph->yaxis->SetFont(FF_FONT1);
91$graph->yaxis->SetTitleMargin(50);
92$graph->yaxis->SetLabelFormatCallback('YCallback');
93$graph->yaxis->HideZeroLabel(1);
94$graph->yaxis->title->SetFont(FF_FONT1, FS_NORMAL, 10);
95$graph->yaxis->title->Set('Bits per second');
96
97$graph->xgrid->Show(true, true);
98$graph->xgrid->SetColor('#e0e0e0', '#efefef');
99$graph->ygrid->SetFill(true, '#EFEFEF@0.5', '#FFFFFF@0.5');
100
101// Graph Series
102$lineplot = new LinePlot($graph_data['tot_data'], $graph_data['ticks']);
103$lineplot->SetLegend('Traffic total');
104$lineplot->SetColor('#d5d5d5');
105$lineplot->SetFillColor('#d5d5d5@0.5');
106
107$lineplot_in = new LinePlot($graph_data['in_data'], $graph_data['ticks']);
108$lineplot_in->SetLegend('Traffic In');
109$lineplot_in->SetColor('darkgreen');
110$lineplot_in->SetFillColor('lightgreen@0.4');
111$lineplot_in->SetWeight(1);
112
113$lineplot_out = new LinePlot(array_map('InvertCallback', $graph_data['out_data']), $graph_data['ticks']);
114$lineplot_out->SetLegend('Traffic Out');
115$lineplot_out->SetColor('darkblue');
116$lineplot_out->SetFillColor('lightblue@0.4');
117$lineplot_out->SetWeight(1);
118
119if (strtolower($graph_data['bill_type']) == 'cdr') {
120    $lineplot_95th = new LinePlot([$graph_data['rate_95th'], $graph_data['rate_95th']], [$xmin, $xmax]);
121    $lineplot_95th->SetColor('red');
122} elseif (strtolower($graph_data['bill_type']) == 'quota') {
123    $lineplot_ave = new LinePlot([$graph_data['rate_average'], $graph_data['rate_average']], [$xmin, $xmax]);
124    $lineplot_ave->SetColor('red');
125}
126
127$graph->legend->SetLayout(LEGEND_HOR);
128$graph->legend->Pos(0.52, 0.90, 'center');
129
130$graph->Add($lineplot);
131$graph->Add($lineplot_in);
132$graph->Add($lineplot_out);
133
134if (strtolower($graph_data['bill_type']) == 'cdr') {
135    $graph->Add($lineplot_95th);
136} elseif (strtolower($graph_data['bill_type']) == 'quota') {
137    $graph->Add($lineplot_ave);
138}
139
140$graph->stroke();
141