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
}
public function pieChartHamSpam ( $emails = '' , $timespan , $title , $output ) {
$ham = $spam = 0 ;
$range = $this -> getRangeInSeconds ( $timespan );
$chart = new PieChart ( SIZE_X , SIZE_Y );
$query = $this -> db -> query ( " SELECT COUNT(*) AS SPAM FROM " . TABLE_META . " WHERE $emails AND arrived > $range " );
if ( $query -> num_rows > 0 ) { $spam = $query -> row [ 'SPAM' ]; }
$query = $this -> db -> query ( " SELECT COUNT(*) AS HAM FROM " . TABLE_META . " WHERE $emails AND arrived > $range " );
if ( $query -> num_rows > 0 ) { $ham = $query -> row [ 'HAM' ]; }
if ( $ham > $spam ) {
$chart -> getPlot () -> getPalette () -> setPieColor ( array ( new Color ( 26 , 192 , 144 ), new Color ( 208 , 48 , 128 ) ));
} else {
$chart -> getPlot () -> getPalette () -> setPieColor ( array ( new Color ( 208 , 48 , 128 ), new Color ( 26 , 192 , 144 ) ));
}
$dataSet = new XYDataSet ();
$dataSet -> addPoint ( new Point ( " HAM ( $ham ) " , $ham ));
$dataSet -> addPoint ( new Point ( " SPAM ( $spam ) " , $spam ));
$chart -> setDataSet ( $dataSet );
$chart -> setTitle ( $title );
2018-08-15 09:03:34 +02:00
@ $this -> sendOutput ( $chart , $output );
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 ;
}
private function getDataPoints ( $timespan ) {
if ( $timespan == " daily " ) { return 24 ; }
if ( $timespan == " weekly " ) { return 7 ; }
return 30 ;
}
private function sendOutput ( $chart , $output = '' ) {
if ( $output == " " ) {
header ( " Content-type: image/png " );
header ( " Expires: now " );
}
if ( $output ) {
$chart -> render ( $output );
}
else {
$chart -> render ();
}
}
}
?>