diff --git a/customer_traffic.php b/customer_traffic.php index aba995c3..81d45d23 100644 --- a/customer_traffic.php +++ b/customer_traffic.php @@ -26,7 +26,7 @@ const AREA = 'customer'; require __DIR__ . '/lib/init.php'; -use Froxlor\Database\Database; +use Froxlor\Traffic\Traffic; use Froxlor\Settings; use Froxlor\UI\Panel\UI; use Froxlor\UI\Request; @@ -37,13 +37,19 @@ if (Settings::IsInList('panel.customer_hide_options', 'traffic')) { Response::redirectTo('customer_index.php'); } +$range = Request::get('range', 'days:30'); + if ($page === null || $page == 'overview') { } elseif ($page == 'current') { } -UI::view('user/traffic.html.twig', [ - 'metrics' => \Froxlor\Traffic\Traffic::getCustomerMetrics($userinfo), - 'chart' => \Froxlor\Traffic\Traffic::getCustomerChart($userinfo, 30), -]); +try { + $context = Traffic::getCustomerStats($userinfo, $range); +} catch (Exception $e) { + Response::dynamicError($e->getMessage()); +} + +// pass metrics to the view +UI::view('user/traffic.html.twig', $context); diff --git a/lib/Froxlor/Traffic/Traffic.php b/lib/Froxlor/Traffic/Traffic.php index 63f1b284..4aa5b96b 100644 --- a/lib/Froxlor/Traffic/Traffic.php +++ b/lib/Froxlor/Traffic/Traffic.php @@ -32,18 +32,33 @@ class Traffic { public static function getCustomerStats($userinfo, $range = null): array { - $trafficCollection = (new Collection(\Froxlor\Api\Commands\Traffic::class, $userinfo, self::getParamsByRange($range, ['customer_traffic' => true,]))) - ->has('customer', Customers::class, 'customerid', 'customerid') - ->get(); + $trafficCollectionObj = (new Collection(\Froxlor\Api\Commands\Traffic::class, $userinfo, self::getParamsByRange($range, ['customer_traffic' => true,]))); + if ($userinfo['adminsession'] == 1) { + $trafficCollectionObj->has('customer', Customers::class, 'customerid', 'customerid'); + } + $trafficCollection = $trafficCollectionObj->get(); // build stats for each user $users = []; + $years = []; + $months = []; foreach ($trafficCollection['data']['list'] as $item) { + // per user total $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']]['ftp'] += ($item['ftp_up'] + $item['ftp_down']); $users[$item['customerid']]['mail'] += $item['mail']; + // per year + $years[$item['year']]['total']['total'] += ($item['http'] + $item['ftp_up'] + $item['ftp_down'] + $item['mail']); + $years[$item['year']]['total']['http'] += $item['http']; + $years[$item['year']]['total']['ftp'] += ($item['ftp_up'] + $item['ftp_down']); + $years[$item['year']]['total']['mail'] += $item['mail']; + // per month + $months[$item['year']][$item['month']]['total'] += ($item['http'] + $item['ftp_up'] + $item['ftp_down'] + $item['mail']); + $months[$item['year']][$item['month']]['http'] += $item['http']; + $months[$item['year']][$item['month']]['ftp'] += ($item['ftp_up'] + $item['ftp_down']); + $months[$item['year']][$item['month']]['mail'] += $item['mail']; } // calculate overview for given range from users @@ -58,10 +73,12 @@ class Traffic return [ 'metrics' => $metrics, 'users' => $users, + 'years' => $years, + 'months' => $months ]; } - private static function getParamsByRange(string $range, array $params = []) + private static function getParamsByRange($range = null, array $params = []) { $dateParams = []; diff --git a/lng/de.lng.php b/lng/de.lng.php index 08fc9903..c9e0f533 100644 --- a/lng/de.lng.php +++ b/lng/de.lng.php @@ -356,6 +356,7 @@ return [ 'store_defaultindex' => 'Standard-Index-Datei im Kundenordner erstellen', 'phpfpm_settings' => 'PHP-FPM', 'traffic' => 'Traffic', + 'traffic_sub' => 'Details der Traffic-Nutzung', 'customertraffic' => 'Kunden', 'assignedmax' => 'Zugewiesen / Max.', 'usedmax' => 'Benutzt / Max.', diff --git a/lng/en.lng.php b/lng/en.lng.php index 605d1d34..6aa676fc 100644 --- a/lng/en.lng.php +++ b/lng/en.lng.php @@ -358,6 +358,7 @@ return [ 'store_defaultindex' => 'Store default index-file to customers docroot', 'phpfpm_settings' => 'PHP-FPM', 'traffic' => 'Traffic', + 'traffic_sub' => 'Details on traffic usage', 'domaintraffic' => 'Domains', 'customertraffic' => 'Customers', 'assignedmax' => 'Assigned / Max', diff --git a/templates/Froxlor/user/traffic.html.twig b/templates/Froxlor/user/traffic.html.twig index 3e56963c..cd261984 100644 --- a/templates/Froxlor/user/traffic.html.twig +++ b/templates/Froxlor/user/traffic.html.twig @@ -4,9 +4,10 @@
- Traffic + + {{ lng('admin.traffic') }}
- View your traffic + {{ lng('admin.traffic_sub') }}
{% endblock %} @@ -29,6 +30,15 @@ +
+
+ +
+
+ +
+
+
@@ -49,40 +59,213 @@
- - - {% if users is not empty %} -
- - - - - - - - - - - - {% for user in users %} - - - - - - - - {% endfor %} - -
{{ lng('login.username') }}TotalHTTPFTPMail -
{{ user.loginname }}{{ user.total|formatBytes }}{{ user.http|formatBytes }}{{ user.ftp|formatBytes }}{{ user.mail|formatBytes }}
-
- {% else %} -
-
-

No data for given range found.

+ {% if userinfo.adminsession == 1 %} + + + {% if users is not empty %} +
+ + + + + + + + + + + + {% for uid,user in users %} + + + + + + + + {% endfor %} + +
{{ lng('login.username') }}TotalHTTPFTPMail +
+ {{ user.loginname }} + {{ user.total|formatBytes }}{{ user.http|formatBytes }}{{ user.ftp|formatBytes }}{{ user.mail|formatBytes }}
-
+ {% else %} +
+
+

No data for given range found.

+
+
+ {% endif %} {% endif %} + + {% endblock %}