more work on new traffic view

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2022-09-08 10:27:03 +02:00
parent 6f2652f9dd
commit bc7e4be47a
7 changed files with 61 additions and 46 deletions

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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`;");

View File

@@ -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,
];
}
}

View File

@@ -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;
});
});

View File

@@ -24,3 +24,4 @@ require('./components/configfiles')
require('./components/apikeys')
require('./components/install')
require('./components/dnseditor')
require('./components/traffic')

View File

@@ -17,16 +17,20 @@
<!-- Range -->
<!-- TODO: set url on change. e.g.: ?param=days:7 -->
<div class="d-flex justify-content-end">
<select class="form-select mb-4 w-auto mt-n4" aria-label="select the traffic range" name="range">
<option value="hours:24">last 24 hours</option>
<option value="days:7">last 7 days</option>
<option value="days:30">last 30 days</option>
<option value="months:3">last 3 months</option>
<option value="months:6">last 6 months</option>
<option value="months:12">last 12 months</option>
<option value="year:2022">2022</option>
<option value="year:2021">2021</option>
<option value="year:2020">2020</option>
<select class="form-select mb-4 w-auto mt-n4" aria-label="select the traffic range" name="range" data-baseref="{{ linker({'section':'traffic'}) }}">
<option value="hours:24" {% if range == 'hours:24' %}selected{% endif %}>last 24 hours</option>
<option value="days:7" {% if range == 'days:7' %}selected{% endif %}>last 7 days</option>
<option value="days:30" {% if range == 'days:30' %}selected{% endif %}>last 30 days</option>
<option value="currentmonth" {% if range == 'currentmonth' %}selected{% endif %}>current month</option>
<option value="months:3" {% if range == 'months:3' %}selected{% endif %}>last 3 months</option>
<option value="months:6" {% if range == 'months:6' %}selected{% endif %}>last 6 months</option>
<option value="months:12" {% if range == 'months:12' %}selected{% endif %}>last 12 months</option>
<option value="currentyear" {% if range == 'currentyear' %}selected{% endif %}>current year</option>
{% for yd in years_avail %}
{% if yd.year != "now"|date('Y') %}
<option value="year:{{ yd.year }}" {% if range == 'year:' ~ yd.year %}selected{% endif %}>{{ yd.year }}</option>
{% endif %}
{% endfor %}
</select>
</div>