root > WikiSense-trunk > tools > LogStats.php

LogStats.php

application/x-php, 4252 bytes (load raw)
<?php
define("WS_ADMIN",true);
require_once( "../common/WSInit.php" );

if (isset($options['help'])) {
  print "USAGE: LogStats.php [--period=time] [--rrd] [<field>...] \n";
  print "   OR: LogStats.php --tilt [--period=time] [--rrd] [<script>...] \n";
  die();
}

if (isset($options['period'])) $period= $options['period'];
else $period= '1d';

if (preg_match('/(\d+)s$/',$period,$m)) $period= $m[1];
else if (preg_match('/(\d+)m$/',$period,$m)) $period= $m[1] * 60;
else if (preg_match('/(\d+)h$/',$period,$m)) $period= $m[1] * 60 * 60;
else if (preg_match('/(\d+)d$/',$period,$m)) $period= $m[1] * 60 * 60 * 24;
else if (preg_match('/(\d+)w$/',$period,$m)) $period= $m[1] * 60 * 40 * 24 * 7;
else {
    $period= (int)$period;
    if (!$period) die("not a time period: $period\n");
}

$since= time() - $period;
$since= wfTimestamp(TS_MW, $since);

$rows= false;
if (@$options['rows']) $rows= preg_split('/\s*[,;:|\s]\s*/',$options['rows']);

$db=& wsfGetLogDB();

if (!$db) {
    die("no log database configured, or connection failed!");
}

$sql= "SELECT script,
              count(*) as runs,
              sum(time) as time,
              count( if ( status = 'done', NULL, 1 ) ) as errors
       FROM $wsgLogTable
       WHERE timestamp >= $since
       GROUP BY script"
;

$res= $db->query($sql, 'LogStats');

$stats= array();

$total= array(
    'runs' => 0,
    'errors' => 0,
    'time' => 0,
);

while ($row= $db->fetchRow($res)) {
    if (@$options['tilt']) {
        $stats['runs'][$row['script']]= $row['runs'];
        $stats['time'][$row['script']]= $row['time'];
        $stats['errors'][$row['script']]= $row['errors'];
        $stats['errors/run'][$row['script']]= (float)$row['errors'] / $row['runs'];
        $stats['time/run'][$row['script']]= (float)$row['time'] / $row['runs'];
    }
    else {
        $e= array();
       
        $e['runs']= $row['runs'];
        $e['time']= $row['time'];
        $e['errors']= $row['errors'];
       
        $e['time/run']= (float)$row['time'] / $row['runs'];
        $e['errors/run']= (float)$row['errors'] / $row['runs'];
       
        $stats[$row['script']]= $e;
    }
   
    $total['runs']+= $row['runs'];
    $total['time']+= $row['time'];
    $total['errors']+= $row['errors'];
}

$db->freeResult($res);

$total['time/run']= (float)$total['time'] / $total['runs'];
$total['errors/run']= (float)$total['errors'] / $total['runs'];

if (@$options['tilt']) {
        $stats['runs']['total']= $total['runs'];
        $stats['time']['total']= $total['time'];
        $stats['errors']['total']= $total['errors'];
        $stats['time/run']['total']= $total['time'] / $total['runs'];
        $stats['errors/run']['total']= $total['errors'] / $total['runs'];
}
else {
    $stats['total']= $total;
}

#print_r($stats);

if (!$rows) $rows= array_keys($stats);

if (@$options['tilt']) {
    if ($args) $scripts= $args;
    else $scripts= array_keys($stats['runs']);
   
    if (!@$options['rrd']) {
        print "field";
        foreach ($scripts as $scr) print "\t" . $scr;
        print "\n";
    }
   
    foreach ($rows as $field) {
        $e= isset($stats[$field]) ? $stats[$field] : NULL;
       
        if (@$options['rrd']) {
            print "N";
            foreach ( $scripts as $k ) print ":" . ( isset($e[$k]) ? $e[$k] : 0 );
            print "\n";
        }
        else {
            print $field;
            foreach ( $scripts as $k ) print "\t" . ( isset($e[$k]) ? $e[$k] : 0 );
            print "\n";
        }
    }
}
else {
    if ($args) $fields= $args;
    else $fields= array(
        'runs',
        'errors',
        'errors/run',
        'time',
        'time/run',
    );
   
    if (!@$options['rrd']) {
        print "scipt";
        foreach ($fields as $f) print "\t" . $f;
        print "\n";
    }
   
    foreach ($rows as $script) {
        $e= isset($stats[$script]) ? $stats[$script] : NULL;
   
        if (@$options['rrd']) {
            print "N";
            foreach ( $fields as $k ) print ":" . ( isset($e[$k]) ? $e[$k] : 0 );
            print "\n";
        }
        else {
            print $script;
            foreach ( $fields as $k ) print "\t" . ( isset($e[$k]) ? $e[$k] : 0 );
            print "\n";
        }
    }
}

?>