api: Added Billing Graphs & Data (#8245)

* Move Billing/Bandwidth Graphs to common format, extract data generation to central function

* Add API functions to access billing graphs and graph data

* Scrutinizer fixes

* Fix transfer graphs with from/to

* Scrutinizer Fix

* Fix docs, transfer page and missing from API param

* Document and fix reducefactor, Add Graph Stats to Graph Data

* Standardise times in graphdata

* Fixed renamed function for History GraphData
This commit is contained in:
Richard Lawley
2018-02-20 14:57:56 +00:00
committed by Neil Lathwood
parent 1c5b9d3524
commit 10829893ec
13 changed files with 920 additions and 530 deletions

View File

@ -1320,25 +1320,157 @@ function list_bills()
api_success($bills, 'bills');
}
function get_bill_graph()
{
global $config;
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$bill_id = mres($router['bill_id']);
$graph_type = $router['graph_type'];
if (!is_admin() && !is_read()) {
check_bill_permission($bill_id);
}
if ($graph_type == 'monthly') {
$graph_type = 'historicmonthly';
}
$vars = array();
$vars['type'] = "bill_$graph_type";
$vars['id'] = $bill_id;
$vars['width'] = $_GET['width'] ?: 1075;
$vars['height'] = $_GET['height'] ?: 300;
$app->response->headers->set('Content-Type', 'image/png');
include 'includes/graphs/graph.inc.php';
}
function get_bill_graphdata()
{
global $config;
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$bill_id = mres($router['bill_id']);
$graph_type = $router['graph_type'];
if (!is_admin() && !is_read()) {
check_bill_permission($bill_id);
}
if ($graph_type == 'bits') {
$from = (isset($_GET['from']) ? $_GET['from'] : time() - 60 * 60 * 24);
$to = (isset($_GET['to']) ? $_GET['to'] : time());
$reducefactor = $_GET['reducefactor'];
$graph_data = getBillingBitsGraphData($bill_id, $from, $to, $reducefactor);
} else if ($graph_type == 'monthly') {
$graph_data = getHistoricTransferGraphData($bill_id);
}
if (!isset($graph_data)) {
api_error(400, "Unsupported graph type $graph_type");
} else {
api_success($graph_data, 'graph_data');
}
}
function get_bill_history()
{
global $config;
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$bill_id = mres($router['bill_id']);
if (!is_admin() && !is_read()) {
check_bill_permission($bill_id);
}
$result = [];
foreach (dbFetchRows('SELECT * FROM `bill_history` WHERE `bill_id` = ? ORDER BY `bill_datefrom` DESC LIMIT 24', array($bill_id)) as $history) {
$result[] = $history;
}
api_success($result, 'bill_history');
}
function get_bill_history_graph()
{
global $config;
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$bill_id = mres($router['bill_id']);
$bill_hist_id = mres($router['bill_hist_id']);
$graph_type = $router['graph_type'];
if (!is_admin() && !is_read()) {
check_bill_permission($bill_id);
}
$vars = array();
switch ($graph_type) {
case 'bits':
$graph_type = 'historicbits';
$vars['reducefactor'] = $_GET['reducefactor'];
break;
case 'day':
case 'hour':
$vars['imgtype'] = $graph_type;
$graph_type = 'historictransfer';
break;
default:
api_error(400, "Unknown Graph Type $graph_type");
break;
}
global $dur; // Needed for callback within graph code
$vars['type'] = "bill_$graph_type";
$vars['id'] = $bill_id;
$vars['bill_hist_id'] = $bill_hist_id;
$vars['width'] = $_GET['width'] ?: 1075;
$vars['height'] = $_GET['height'] ?: 300;
$app->response->headers->set('Content-Type', 'image/png');
include 'includes/graphs/graph.inc.php';
}
function get_bill_history_graphdata()
{
global $config;
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$bill_id = mres($router['bill_id']);
$bill_hist_id = mres($router['bill_hist_id']);
$graph_type = $router['graph_type'];
if (!is_admin() && !is_read()) {
check_bill_permission($bill_id);
}
switch ($graph_type) {
case 'bits':
$reducefactor = $_GET['reducefactor'];
$graph_data = getBillingHistoryBitsGraphData($bill_id, $bill_hist_id, $reducefactor);
break;
case 'day':
case 'hour':
$graph_data = getBillingBandwidthGraphData($bill_id, $bill_hist_id, null, null, $graph_type);
break;
}
if (!isset($graph_data)) {
api_error(400, "Unsupported graph type $graph_type");
} else {
api_success($graph_data, 'graph_data');
}
}
function update_device()
{
check_is_admin();