modules/stats.php
changeset 17 b07b23187b17
parent 15 5e2d1514ccd0
child 20 e2f6e3af9959
equal deleted inserted replaced
16:01c6a003d762 17:b07b23187b17
   231   stats_cron();
   231   stats_cron();
   232 }
   232 }
   233 
   233 
   234 function stats_cron()
   234 function stats_cron()
   235 {
   235 {
   236   global $stats_day;
       
   237   $stats_day = gmdate('Ymd');
       
   238   
       
   239   static $commit_time = 0;
   236   static $commit_time = 0;
   240   $now = time();
   237   $now = time();
   241   // commit to disk every 1 minute
   238   // commit to disk every 1 minute
   242   if ( $commit_time + 60 < $now )
   239   if ( $commit_time + 60 < $now )
   243   {
   240   {
   247 }
   244 }
   248 
   245 
   249 function stats_commit()
   246 function stats_commit()
   250 {
   247 {
   251   global $stats_data, $stats_day;
   248   global $stats_data, $stats_day;
       
   249   
   252   ob_start();
   250   ob_start();
   253   var_export($stats_data);
   251   var_export($stats_data);
   254   $stats_data_exported = ob_get_contents();
   252   $stats_data_exported = ob_get_contents();
   255   ob_end_clean();
   253   ob_end_clean();
   256   
   254   
   257   $fp = @fopen("./stats/stats-data-$stats_day.php", 'w');
   255   $fp = @fopen("./stats/stats-data-$stats_day.php", 'w');
   258   if ( !$fp )
   256   if ( !$fp )
   259     return false;
   257     return false;
   260   fwrite($fp, "<?php\n\$stats_data = $stats_data_exported;\n");
   258   fwrite($fp, "<?php\n\$stats_data = $stats_data_exported;\n");
   261   fclose($fp);
   259   fclose($fp);
   262 }
   260   
   263 
   261   if ( $stats_day != gmdate('Ymd') )
       
   262   {
       
   263     // it's a new day! flush all our logs
       
   264     foreach ( $stats_data['messages'] as &$data )
       
   265     {
       
   266       $data = array();
       
   267     }
       
   268   }
       
   269   
       
   270   $stats_day = gmdate('Ymd');
       
   271 }
       
   272