diff --git a/admin_traffic.php b/admin_traffic.php index c2ab51b8..2060fe18 100644 --- a/admin_traffic.php +++ b/admin_traffic.php @@ -31,7 +31,7 @@ use Froxlor\UI\Panel\UI; use Froxlor\UI\Request; use Froxlor\UI\Response; -$range = Request::get('range', 'days:30'); +$range = Request::get('range', 'currentmonth'); if ($page == 'overview' || $page == 'customers') { try { diff --git a/customer_traffic.php b/customer_traffic.php index 81d45d23..b5484b6c 100644 --- a/customer_traffic.php +++ b/customer_traffic.php @@ -37,12 +37,10 @@ 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') { +$range = Request::get('range', 'currentyear'); +if ($page == 'current') { + $range = 'currentmonth'; } try { diff --git a/install/updates/froxlor/update_2.x.inc.php b/install/updates/froxlor/update_2.x.inc.php index fef40021..1f799417 100644 --- a/install/updates/froxlor/update_2.x.inc.php +++ b/install/updates/froxlor/update_2.x.inc.php @@ -37,11 +37,11 @@ if (!defined('_CRON_UPDATE')) { } // last 0.10.x release -if (Froxlor::isFroxlorVersion('0.10.99')) { +if (Froxlor::isFroxlorVersion('0.10.38')) { $update_to = '2.0.0-beta1'; - Update::showUpdateStep("Updating from 0.10.99 to ".$update_to, false); + Update::showUpdateStep("Updating from 0.10.38 to ".$update_to, false); Update::showUpdateStep("Removing unused table"); Database::query("DROP TABLE IF EXISTS `panel_sessions`;"); diff --git a/lib/Froxlor/Traffic/Traffic.php b/lib/Froxlor/Traffic/Traffic.php index 4aa5b96b..48d4f525 100644 --- a/lib/Froxlor/Traffic/Traffic.php +++ b/lib/Froxlor/Traffic/Traffic.php @@ -25,14 +25,16 @@ namespace Froxlor\Traffic; +use Froxlor\Database\Database; use Froxlor\Api\Commands\Customers; use Froxlor\UI\Collection; +use Froxlor\Api\Commands\Traffic as TrafficAPI; class Traffic { public static function getCustomerStats($userinfo, $range = null): array { - $trafficCollectionObj = (new Collection(\Froxlor\Api\Commands\Traffic::class, $userinfo, self::getParamsByRange($range, ['customer_traffic' => true,]))); + $trafficCollectionObj = (new Collection(TrafficAPI::class, $userinfo, self::getParamsByRange($range, ['customer_traffic' => true,]))); if ($userinfo['adminsession'] == 1) { $trafficCollectionObj->has('customer', Customers::class, 'customerid', 'customerid'); } @@ -70,11 +72,18 @@ class Traffic $metrics['mail'] += $user['mail']; } + // get all possible years for filter + $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 [ 'metrics' => $metrics, 'users' => $users, 'years' => $years, - 'months' => $months + 'months' => $months, + 'range' => $range, + 'years_avail' => $years_avail ]; } @@ -85,33 +94,27 @@ class Traffic if (preg_match("/year:([0-9]{4})/", $range, $matches)) { $dateParams = ['year' => $matches[1]]; } - - // TODO: get params by range: hours:x, days:x, months:x + elseif (preg_match("/months:([1-9]([0-9]+)?)/", $range, $matches)) { + $dt = (new \DateTime())->sub(new \DateInterval('P'.$matches[1].'M'))->format('U'); + $dateParams = ['date_from' => $dt]; + } + elseif (preg_match("/days:([1-9]([0-9]+)?)/", $range, $matches)) { + $dt = (new \DateTime())->sub(new \DateInterval('P'.$matches[1].'D'))->format('U'); + $dateParams = ['date_from' => $dt]; + } + elseif (preg_match("/hours:([1-9]([0-9]+)?)/", $range, $matches)) { + $dt = (new \DateTime())->sub(new \DateInterval('PT'.$matches[1].'H'))->format('U'); + $dateParams = ['date_from' => $dt]; + } + elseif (preg_match("/currentmonth/", $range, $matches)) { + $dt = (new \DateTime("first day of this month"))->setTime(0,0,0,1)->format('U'); + $dateParams = ['date_from' => $dt]; + } + elseif (preg_match("/currentyear/", $range, $matches)) { + $dt = \DateTime::createFromFormat("d.m.Y", '01.01.'.date('Y'))->setTime(0,0,0,1)->format('U'); + $dateParams = ['date_from' => $dt]; + } return array_merge($dateParams, $params); } - - public static function getCustomerChart($userinfo, $range = 30): array - { - // FIXME: this is currently an example for the chart - - $data = []; - - for ($i = 0; $i < $range; $i++) { - $data['labels'][] = date("d.m", strtotime('-' . $i . ' days')); - - // put data for given date - $data['http'][] = 0; - $data['ftp'][] = 0; - $data['mail'][] = 0; - } - - return [ - 'labels' => array_reverse($data['labels']), - 'http' => array_reverse($data['http']), - 'ftp' => array_reverse($data['ftp']), - 'mail' => array_reverse($data['mail']), - 'range' => $range, - ]; - } } diff --git a/templates/Froxlor/src/js/components/traffic.js b/templates/Froxlor/src/js/components/traffic.js new file mode 100644 index 00000000..abd013f9 --- /dev/null +++ b/templates/Froxlor/src/js/components/traffic.js @@ -0,0 +1,9 @@ +$(function () { + + // Display helptext to content box according to dns-record type selected + $("select[name='range']").on('change', function () { + var selVal = $(this).val(); + var baseRef = $(this).data('baseref'); + window.location.href = baseRef + '?range=' + selVal; + }); +}); diff --git a/templates/Froxlor/src/js/main.js b/templates/Froxlor/src/js/main.js index a2c7979a..a7144a14 100644 --- a/templates/Froxlor/src/js/main.js +++ b/templates/Froxlor/src/js/main.js @@ -24,3 +24,4 @@ require('./components/configfiles') require('./components/apikeys') require('./components/install') require('./components/dnseditor') +require('./components/traffic') diff --git a/templates/Froxlor/user/traffic.html.twig b/templates/Froxlor/user/traffic.html.twig index cd261984..ddb82f74 100644 --- a/templates/Froxlor/user/traffic.html.twig +++ b/templates/Froxlor/user/traffic.html.twig @@ -17,16 +17,20 @@
- + + + + + + + + + {% for yd in years_avail %} + {% if yd.year != "now"|date('Y') %} + + {% endif %} + {% endfor %}