2012-02-08 23:14:28 +01:00
< ? php
class ModelStatChart extends Model {
2013-03-14 23:09:24 +01:00
public function lineChartHamSpam ( $timespan , $title , $size_x , $size_y , $output ){
2012-02-08 23:14:28 +01:00
$ydata = array ();
$ydata2 = array ();
$dates = array ();
2013-11-18 19:24:33 +01:00
$session = Registry :: get ( 'session' );
2012-02-08 23:14:28 +01:00
$chart = new LineChart ( $size_x , $size_y );
$chart -> getPlot () -> getPalette () -> setLineColor ( array (
//new Color(26, 192, 144),
new Color ( 208 , 48 , 128 ),
));
$line1 = new XYDataSet ();
$limit = $this -> getDataPoints ( $timespan );
$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 ;
2012-02-08 23:14:28 +01:00
$date_format = " H:i " ;
} else {
2013-03-14 23:09:24 +01:00
$delta = 86400 ;
2012-02-08 23:14:28 +01:00
$date_format = " m.d. " ;
}
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 );
2019-11-14 12:18:01 +01: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 $limit " , $auditdomains );
2013-03-14 23:09:24 +01:00
} else {
2019-11-14 12:18:01 +01: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 $limit " );
2013-03-14 23:09:24 +01:00
}
2012-02-08 23:14:28 +01:00
foreach ( $query -> rows as $q ) {
array_push ( $ydata , $q [ 'num' ]);
array_push ( $dates , date ( $date_format , $q [ 'ts' ]));
}
if ( $query -> num_rows >= 15 ) {
$i = 0 ;
while ( list ( $k , $v ) = each ( $dates )) {
$i ++ ;
if ( $i % 3 ) { $dates [ $k ] = " " ; }
}
reset ( $dates );
}
$ydata = array_reverse ( $ydata );
$dates = array_reverse ( $dates );
for ( $i = 0 ; $i < count ( $ydata ); $i ++ ){
$ts = $dates [ $i ];
$line1 -> addPoint ( new Point ( " $ts " , $ydata [ $i ]));
}
$dataSet = new XYSeriesDataSet ();
$dataSet -> addSerie ( " RCVD " , $line1 );
$chart -> setDataSet ( $dataSet );
$chart -> setTitle ( $title );
$chart -> getPlot () -> setGraphCaptionRatio ( 0.80 );
$this -> sendOutput ( $chart , $output );
}
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 ();
}
}
}
?>