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:
@@ -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
|
||||||
$users[$item['customerid']]['loginname'] = $item['customer']['loginname'];
|
if ($userinfo['adminsession'] == 1) {
|
||||||
$users[$item['customerid']]['total'] += ($item['http'] + $item['ftp_up'] + $item['ftp_down'] + $item['mail']);
|
$users[$item['customerid']]['loginname'] = $item['customer']['loginname'];
|
||||||
$users[$item['customerid']]['http'] += $item['http'];
|
}
|
||||||
$users[$item['customerid']]['ftp'] += ($item['ftp_up'] + $item['ftp_down']);
|
$users[$item['customerid']]['total'] += $total;
|
||||||
$users[$item['customerid']]['mail'] += $item['mail'];
|
$users[$item['customerid']]['http'] += $http;
|
||||||
// per year
|
$users[$item['customerid']]['ftp'] += $ftp;
|
||||||
$years[$item['year']]['total'] += ($item['http'] + $item['ftp_up'] + $item['ftp_down'] + $item['mail']);
|
$users[$item['customerid']]['mail'] += $mail;
|
||||||
$years[$item['year']]['http'] += $item['http'];
|
if (!$overview) {
|
||||||
$years[$item['year']]['ftp'] += ($item['ftp_up'] + $item['ftp_down']);
|
// per year
|
||||||
$years[$item['year']]['mail'] += $item['mail'];
|
$years[$item['year']]['total'] += $total;
|
||||||
// per month
|
$years[$item['year']]['http'] += $http;
|
||||||
$months[$item['month'] . '/' . $item['year']]['total'] += ($item['http'] + $item['ftp_up'] + $item['ftp_down'] + $item['mail']);
|
$years[$item['year']]['ftp'] += $ftp;
|
||||||
$months[$item['month'] . '/' . $item['year']]['http'] += $item['http'];
|
$years[$item['year']]['mail'] += $mail;
|
||||||
$months[$item['month'] . '/' . $item['year']]['ftp'] += ($item['ftp_up'] + $item['ftp_down']);
|
// per month
|
||||||
$months[$item['month'] . '/' . $item['year']]['mail'] += $item['mail'];
|
$months[$item['month'] . '/' . $item['year']]['total'] += $total;
|
||||||
// per day
|
$months[$item['month'] . '/' . $item['year']]['http'] += $http;
|
||||||
$days[$item['day'] . '.' . $item['month'] . '.' . $item['year']]['total'] += ($item['http'] + $item['ftp_up'] + $item['ftp_down'] + $item['mail']);
|
$months[$item['month'] . '/' . $item['year']]['ftp'] += $ftp;
|
||||||
$days[$item['day'] . '.' . $item['month'] . '.' . $item['year']]['http'] += $item['http'];
|
$months[$item['month'] . '/' . $item['year']]['mail'] += $mail;
|
||||||
$days[$item['day'] . '.' . $item['month'] . '.' . $item['year']]['ftp'] += ($item['ftp_up'] + $item['ftp_down']);
|
// per day
|
||||||
$days[$item['day'] . '.' . $item['month'] . '.' . $item['year']]['mail'] += $item['mail'];
|
$days[$item['day'] . '.' . $item['month'] . '.' . $item['year']]['total'] += $total;
|
||||||
|
$days[$item['day'] . '.' . $item['month'] . '.' . $item['year']]['http'] += $http;
|
||||||
|
$days[$item['day'] . '.' . $item['month'] . '.' . $item['year']]['ftp'] += $ftp;
|
||||||
|
$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'];
|
||||||
}
|
}
|
||||||
|
|
||||||
// get all possible years for filter
|
$years_avail = [];
|
||||||
$sel_stmt = Database::prepare("SELECT DISTINCT year FROM `" . TABLE_PANEL_TRAFFIC . "` WHERE 1 ORDER BY `year` DESC");
|
if (!$overview) {
|
||||||
Database::pexecute($sel_stmt);
|
// get all possible years for filter
|
||||||
$years_avail = $sel_stmt->fetchAll(\PDO::FETCH_ASSOC);
|
$sel_stmt = Database::prepare("SELECT DISTINCT year FROM `" . TABLE_PANEL_TRAFFIC . "` WHERE 1 ORDER BY `year` DESC");
|
||||||
|
Database::pexecute($sel_stmt);
|
||||||
|
$years_avail = $sel_stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'metrics' => $metrics,
|
'metrics' => $metrics,
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user