make overview of customers faster by reducing mysql and php load when calculating traffic details; fixes #1161

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2023-09-24 17:54:05 +02:00
parent d1cb32b47f
commit 41ac713325
2 changed files with 41 additions and 29 deletions

View File

@@ -25,10 +25,10 @@
namespace Froxlor\Traffic; namespace Froxlor\Traffic;
use Froxlor\Database\Database;
use Froxlor\Api\Commands\Customers; use Froxlor\Api\Commands\Customers;
use Froxlor\UI\Collection;
use Froxlor\Api\Commands\Traffic as TrafficAPI; use Froxlor\Api\Commands\Traffic as TrafficAPI;
use Froxlor\Database\Database;
use Froxlor\UI\Collection;
class Traffic class Traffic
{ {
@@ -38,10 +38,10 @@ class Traffic
* @return array * @return array
* @throws \Exception * @throws \Exception
*/ */
public static function getCustomerStats(array $userinfo, string $range = null): array public static function getCustomerStats(array $userinfo, string $range = null, bool $overview = false): array
{ {
$trafficCollectionObj = (new Collection(TrafficAPI::class, $userinfo, $trafficCollectionObj = (new Collection(TrafficAPI::class, $userinfo,
self::getParamsByRange($range, ['customer_traffic' => true,]))); self::getParamsByRange($range, ['customer_traffic' => true])));
if ($userinfo['adminsession'] == 1) { if ($userinfo['adminsession'] == 1) {
$trafficCollectionObj->has('customer', Customers::class, 'customerid', 'customerid'); $trafficCollectionObj->has('customer', Customers::class, 'customerid', 'customerid');
} }
@@ -53,27 +53,36 @@ class Traffic
$months = []; $months = [];
$days = []; $days = [];
foreach ($trafficCollection['data']['list'] as $item) { foreach ($trafficCollection['data']['list'] as $item) {
$http = $item['http'];
$ftp = ($item['ftp_up'] + $item['ftp_down']);
$mail = $item['mail'];
$total = $http + $ftp + $mail;
// per user total // per user total
if ($userinfo['adminsession'] == 1) {
$users[$item['customerid']]['loginname'] = $item['customer']['loginname']; $users[$item['customerid']]['loginname'] = $item['customer']['loginname'];
$users[$item['customerid']]['total'] += ($item['http'] + $item['ftp_up'] + $item['ftp_down'] + $item['mail']); }
$users[$item['customerid']]['http'] += $item['http']; $users[$item['customerid']]['total'] += $total;
$users[$item['customerid']]['ftp'] += ($item['ftp_up'] + $item['ftp_down']); $users[$item['customerid']]['http'] += $http;
$users[$item['customerid']]['mail'] += $item['mail']; $users[$item['customerid']]['ftp'] += $ftp;
$users[$item['customerid']]['mail'] += $mail;
if (!$overview) {
// per year // per year
$years[$item['year']]['total'] += ($item['http'] + $item['ftp_up'] + $item['ftp_down'] + $item['mail']); $years[$item['year']]['total'] += $total;
$years[$item['year']]['http'] += $item['http']; $years[$item['year']]['http'] += $http;
$years[$item['year']]['ftp'] += ($item['ftp_up'] + $item['ftp_down']); $years[$item['year']]['ftp'] += $ftp;
$years[$item['year']]['mail'] += $item['mail']; $years[$item['year']]['mail'] += $mail;
// per month // per month
$months[$item['month'] . '/' . $item['year']]['total'] += ($item['http'] + $item['ftp_up'] + $item['ftp_down'] + $item['mail']); $months[$item['month'] . '/' . $item['year']]['total'] += $total;
$months[$item['month'] . '/' . $item['year']]['http'] += $item['http']; $months[$item['month'] . '/' . $item['year']]['http'] += $http;
$months[$item['month'] . '/' . $item['year']]['ftp'] += ($item['ftp_up'] + $item['ftp_down']); $months[$item['month'] . '/' . $item['year']]['ftp'] += $ftp;
$months[$item['month'] . '/' . $item['year']]['mail'] += $item['mail']; $months[$item['month'] . '/' . $item['year']]['mail'] += $mail;
// per day // per day
$days[$item['day'] . '.' . $item['month'] . '.' . $item['year']]['total'] += ($item['http'] + $item['ftp_up'] + $item['ftp_down'] + $item['mail']); $days[$item['day'] . '.' . $item['month'] . '.' . $item['year']]['total'] += $total;
$days[$item['day'] . '.' . $item['month'] . '.' . $item['year']]['http'] += $item['http']; $days[$item['day'] . '.' . $item['month'] . '.' . $item['year']]['http'] += $http;
$days[$item['day'] . '.' . $item['month'] . '.' . $item['year']]['ftp'] += ($item['ftp_up'] + $item['ftp_down']); $days[$item['day'] . '.' . $item['month'] . '.' . $item['year']]['ftp'] += $ftp;
$days[$item['day'] . '.' . $item['month'] . '.' . $item['year']]['mail'] += $item['mail']; $days[$item['day'] . '.' . $item['month'] . '.' . $item['year']]['mail'] += $mail;
}
} }
// calculate overview for given range from users // calculate overview for given range from users
@@ -85,10 +94,13 @@ class Traffic
$metrics['mail'] += $user['mail']; $metrics['mail'] += $user['mail'];
} }
$years_avail = [];
if (!$overview) {
// get all possible years for filter // get all possible years for filter
$sel_stmt = Database::prepare("SELECT DISTINCT year FROM `" . TABLE_PANEL_TRAFFIC . "` WHERE 1 ORDER BY `year` DESC"); $sel_stmt = Database::prepare("SELECT DISTINCT year FROM `" . TABLE_PANEL_TRAFFIC . "` WHERE 1 ORDER BY `year` DESC");
Database::pexecute($sel_stmt); Database::pexecute($sel_stmt);
$years_avail = $sel_stmt->fetchAll(\PDO::FETCH_ASSOC); $years_avail = $sel_stmt->fetchAll(\PDO::FETCH_ASSOC);
}
return [ return [
'metrics' => $metrics, 'metrics' => $metrics,

View File

@@ -95,7 +95,7 @@ class ProgressBar
$skip_customer_traffic = false; $skip_customer_traffic = false;
try { try {
$attributes['fields']['deactivated'] = 0; $attributes['fields']['deactivated'] = 0;
$result = Traffic::getCustomerStats($attributes['fields'], 'currentmonth'); $result = Traffic::getCustomerStats($attributes['fields'], 'currentmonth', true);
} catch (Exception $e) { } catch (Exception $e) {
if ($e->getCode() === 405) { if ($e->getCode() === 405) {
$skip_customer_traffic = true; $skip_customer_traffic = true;