* @license https://files.froxlor.org/misc/COPYING.txt GPLv2 */ 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 { /** * @param array $userinfo * @param ?string $range * @return array * @throws \Exception */ public static function getCustomerStats(array $userinfo, string $range = null): array { $trafficCollectionObj = (new Collection(TrafficAPI::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 = []; $days = []; 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'] += ($item['http'] + $item['ftp_up'] + $item['ftp_down'] + $item['mail']); $years[$item['year']]['http'] += $item['http']; $years[$item['year']]['ftp'] += ($item['ftp_up'] + $item['ftp_down']); $years[$item['year']]['mail'] += $item['mail']; // per month $months[$item['month'] . '/' . $item['year']]['total'] += ($item['http'] + $item['ftp_up'] + $item['ftp_down'] + $item['mail']); $months[$item['month'] . '/' . $item['year']]['http'] += $item['http']; $months[$item['month'] . '/' . $item['year']]['ftp'] += ($item['ftp_up'] + $item['ftp_down']); $months[$item['month'] . '/' . $item['year']]['mail'] += $item['mail']; // 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']]['http'] += $item['http']; $days[$item['day'] . '.' . $item['month'] . '.' . $item['year']]['ftp'] += ($item['ftp_up'] + $item['ftp_down']); $days[$item['day'] . '.' . $item['month'] . '.' . $item['year']]['mail'] += $item['mail']; } // calculate overview for given range from users $metrics = []; foreach ($users as $user) { $metrics['total'] += $user['total']; $metrics['http'] += $user['http']; $metrics['ftp'] += $user['ftp']; $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, 'days' => $days, 'range' => $range, 'years_avail' => $years_avail ]; } /** * @param ?string $range * @param array $params * @return array * @throws \Exception */ private static function getParamsByRange(string $range = null, array $params = []): array { $dateParams = []; if (preg_match("/year:([0-9]{4})/", $range, $matches)) { $dateParams = ['year' => $matches[1]]; } 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); } }