2012-02-08 23:14:28 +01:00
< ? php
class ModelStatChart extends Model {
2022-09-04 13:12:51 +02:00
public function lineChartArchivedMessages ( $timespan ){
$data = [];
2012-02-08 23:14:28 +01:00
2013-11-18 19:24:33 +01:00
$session = Registry :: get ( 'session' );
2012-02-08 23:14:28 +01:00
$range = $this -> getRangeInSeconds ( $timespan );
if ( $timespan == " daily " ){ $grouping = " GROUP BY FROM_UNIXTIME(ts, '%Y.%m.%d. %H') " ; }
else { $grouping = " GROUP BY FROM_UNIXTIME(ts, '%Y.%m.%d.') " ; }
if ( $timespan == " daily " ){
2013-03-14 23:09:24 +01:00
$delta = 3600 ;
2022-09-04 13:12:51 +02:00
$data_points = 24 ;
2012-02-08 23:14:28 +01:00
} else {
2013-03-14 23:09:24 +01:00
$delta = 86400 ;
2022-09-04 13:12:51 +02:00
$data_points = 30 ;
}
$now = time ();
$now -= $now % $delta + ( $data_points - 1 ) * $delta ;
for ( $i = 0 ; $i < $data_points ; $i ++ ) {
$data [ $now ] = 0 ;
$now += $delta ;
2012-02-08 23:14:28 +01:00
}
2013-03-14 23:09:24 +01:00
if ( Registry :: get ( 'admin_user' ) == 0 ) {
$q = '' ;
2013-11-18 19:24:33 +01:00
$auditdomains = $session -> get ( 'auditdomains' );
foreach ( $auditdomains as $a ) {
2013-03-14 23:09:24 +01:00
if ( $q ) { $q .= " ,? " ; } else { $q = " ? " ; }
}
2013-11-18 19:24:33 +01:00
reset ( $auditdomains );
2022-09-04 13:12:51 +02:00
$query = $this -> db -> query ( " select (arrived-(arrived% $delta )) as ts, count(*) as num from " . VIEW_MESSAGES . " where arrived > $range AND todomain IN ( $q ) $domains $grouping ORDER BY ts DESC limit $data_points " , $auditdomains );
2013-03-14 23:09:24 +01:00
} else {
2022-09-04 13:12:51 +02:00
$query = $this -> db -> query ( " select (arrived-(arrived% $delta )) as ts, count(*) as num from " . TABLE_META . " where arrived > $range $grouping ORDER BY ts DESC limit $data_points " );
2013-03-14 23:09:24 +01:00
}
2012-02-08 23:14:28 +01:00
foreach ( $query -> rows as $q ) {
2022-09-04 13:12:51 +02:00
if ( isset ( $data [ $q [ 'ts' ]])) {
$data [ $q [ 'ts' ]] = $q [ 'num' ];
2020-03-14 09:04:57 +01:00
}
}
2022-09-04 13:12:51 +02:00
return $data ;
2012-02-08 23:14:28 +01:00
}
private function getRangeInSeconds ( $timespan ) {
$range = 0 ;
if ( $timespan == " daily " ) { return time () - 86400 ; }
if ( $timespan == " weekly " ) { return time () - 604800 ; }
return time () - 2592000 ;
}
}