update admin traffic overview

This commit is contained in:
envoyr
2022-08-21 17:47:17 +02:00
parent 293c0ceb9d
commit b5f5c4f4b5
6 changed files with 171 additions and 296 deletions

View File

@@ -26,118 +26,20 @@
const AREA = 'admin';
require __DIR__ . '/lib/init.php';
use Froxlor\Database\Database;
use Froxlor\PhpHelper;
use Froxlor\Settings;
use Froxlor\Traffic\Traffic;
use Froxlor\UI\Panel\UI;
use Froxlor\UI\Request;
use Froxlor\UI\Response;
$id = (int)Request::get('id');
$months = [
'0' => 'empty',
'1' => 'jan',
'2' => 'feb',
'3' => 'mar',
'4' => 'apr',
'5' => 'may',
'6' => 'jun',
'7' => 'jul',
'8' => 'aug',
'9' => 'sep',
'10' => 'oct',
'11' => 'nov',
'12' => 'dec'
];
$range = Request::get('range', 'days:30');
if ($page == 'overview' || $page == 'customers') {
$minyear_stmt = Database::query("SELECT `year` FROM `" . TABLE_PANEL_TRAFFIC . "` ORDER BY `year` ASC LIMIT 1");
$minyear = $minyear_stmt->fetch(PDO::FETCH_ASSOC);
if (!isset($minyear['year']) || $minyear['year'] == 0) {
$maxyears = 0;
} else {
$maxyears = date("Y") - $minyear['year'];
try {
$context = Traffic::getCustomerStats($userinfo, $range);
} catch (Exception $e) {
Response::dynamicError($e->getMessage());
}
$params = [];
if ($userinfo['customers_see_all'] == '0') {
$params = [
'id' => $userinfo['adminid']
];
}
$customer_name_list_stmt = Database::prepare("
SELECT `customerid`,`company`,`name`,`firstname`
FROM `" . TABLE_PANEL_CUSTOMERS . "`
WHERE `deactivated`='0'" . ($userinfo['customers_see_all'] ? '' : ' AND `adminid` = :id') . "
ORDER BY name");
$traffic_list_stmt = Database::prepare("
SELECT month, SUM(http+ftp_up+ftp_down+mail)*1024 AS traffic
FROM `" . TABLE_PANEL_TRAFFIC . "`
WHERE year = :year AND `customerid` = :id
GROUP BY month ORDER BY month");
$stats = [];
for ($years = 0; $years <= $maxyears; $years++) {
$totals = [
'jan' => 0,
'feb' => 0,
'mar' => 0,
'apr' => 0,
'may' => 0,
'jun' => 0,
'jul' => 0,
'aug' => 0,
'sep' => 0,
'oct' => 0,
'nov' => 0,
'dec' => 0
];
Database::pexecute($customer_name_list_stmt, $params);
$data = [];
while ($customer_name = $customer_name_list_stmt->fetch(PDO::FETCH_ASSOC)) {
$virtual_host = [
'name' => ($customer_name['company'] == '' ? $customer_name['name'] . ", " . $customer_name['firstname'] : $customer_name['company']),
'customerid' => $customer_name['customerid'],
'jan' => '-',
'feb' => '-',
'mar' => '-',
'apr' => '-',
'may' => '-',
'jun' => '-',
'jul' => '-',
'aug' => '-',
'sep' => '-',
'oct' => '-',
'nov' => '-',
'dec' => '-'
];
Database::pexecute($traffic_list_stmt, [
'year' => (date("Y") - $years),
'id' => $customer_name['customerid']
]);
while ($traffic_month = $traffic_list_stmt->fetch(PDO::FETCH_ASSOC)) {
$virtual_host[$months[(int)$traffic_month['month']]] = PhpHelper::sizeReadable($traffic_month['traffic'], 'GiB', 'bi', '%01.' . (int)Settings::Get('panel.decimal_places') . 'f %s');
$totals[$months[(int)$traffic_month['month']]] += $traffic_month['traffic'];
}
$data = $virtual_host;
}
$stats[] = [
'year' => date("Y") - $years,
'type' => lng('traffic.customer'),
'data' => $data,
];
}
UI::view('user/traffic.html.twig', [
'stats' => $stats
]);
// pass metrics to the view
UI::view('user/traffic.html.twig', $context);
}