htdocs/graph.php
changeset 43 ce2e9caf2dfa
parent 34 a8daa2016cf1
child 44 73f74d395f95
equal deleted inserted replaced
42:c24a2311f232 43:ce2e9caf2dfa
     5 
     5 
     6 $channel_list = stats_channel_list();
     6 $channel_list = stats_channel_list();
     7 $first_channel = $channel_list[0];
     7 $first_channel = $channel_list[0];
     8 $channel = ( isset($_REQUEST['channel']) && in_array($_REQUEST['channel'], $channel_list) ) ? $_REQUEST['channel'] : $first_channel;
     8 $channel = ( isset($_REQUEST['channel']) && in_array($_REQUEST['channel'], $channel_list) ) ? $_REQUEST['channel'] : $first_channel;
     9 
     9 
    10 $g = new GraphMaker(); // _Compat();
    10 function makeGraph($type = 'bar')
    11 
    11 {
    12 $g->SetGraphPadding(20, 30, 20, 15);
    12   $class = ( $type == 'line' ) ? 'LineGraph' : 'BarGraph';
    13 $g->SetGraphAreaHeight(200);
    13   $g = new $class(); // _Compat();
    14 $g->SetBarDimensions(26, 0);
    14   
    15 $g->SetBarPadding(7);
    15   $g->SetGraphAreaHeight(200);
    16 $g->SetGraphBackgroundTransparent(240, 250, 255, 0);
    16   $g->SetGraphPadding(30, 30, 20, 15);
    17 $g->SetGraphTransparency(25);
    17   if ( get_class($g) == 'BarGraph' )
       
    18   {
       
    19     $g->SetBarPadding(7);
       
    20     $g->SetBarFont(1);
       
    21     $g->SetBarDimensions(25, 7);
       
    22   }
       
    23   else if ( get_class($g) == 'LineGraph' )
       
    24   {
       
    25     $g->SetGraphAreaWidth(800);
       
    26   }
       
    27   $g->SetAxisDeepness(7);
       
    28   $g->SetGraphTitleFont(2);
       
    29   $g->SetGraphBackgroundTransparent(false, 240, 250, 255);
       
    30   $g->SetGraphTransparency(25);
       
    31   $g->SetAxisScaleColor(90, 90, 90);
       
    32   $g->SetAxisScaleFont(1);
       
    33   $g->SetScaleRoundY(0);
       
    34   $g->SetScaleRoundX(0);
       
    35   $g->SetAxisStepSize(7);
       
    36   return $g;
       
    37 }
    18 
    38 
    19 // generate the data
    39 // generate the data
    20 // we're doing this by absolute hours, not by strictly "24 hours ago", e.g. on-the-hour stats
    40 // we're doing this by absolute hours, not by strictly "24 hours ago", e.g. on-the-hour stats
    21 $mode = ( isset($_GET['mode']) ) ? $_GET['mode'] : 'lastday';
    41 $mode = ( isset($_GET['mode']) ) ? $_GET['mode'] : 'lastday';
    22 switch ( $mode )
    42 switch ( $mode )
    23 {
    43 {
    24   case 'lastday':
    44   case 'lastday':
    25   default:
    45   default:
       
    46     $g = makeGraph();
    26     $graph_title = $channel . ' message count - last 24 hours';
    47     $graph_title = $channel . ' message count - last 24 hours';
    27     $this_hour = gmmktime(gmdate('H'), 0, 0);
    48     $this_hour = gmmktime(gmdate('H'), 0, 0);
    28     $graphdata = array();
    49     $graphdata = array();
    29     
    50     
    30     for ( $i = 23; $i >= 0; $i-- )
    51     for ( $i = 23; $i >= 0; $i-- )
    34       $basetime += 3600;
    55       $basetime += 3600;
    35       $graphdata[$ts] = stats_message_count($channel, 60, $basetime);
    56       $graphdata[$ts] = stats_message_count($channel, 60, $basetime);
    36     }
    57     }
    37     break;
    58     break;
    38   case 'lastweek':
    59   case 'lastweek':
       
    60     $g = makeGraph();
    39     $graph_title = $channel . ' activity - last 14 days';
    61     $graph_title = $channel . ' activity - last 14 days';
    40     $this_day = gmmktime(0, 0, 0);
    62     $this_day = gmmktime(0, 0, 0);
    41     $graphdata = array();
    63     $graphdata = array();
    42     
    64     
    43     for ( $i = 13; $i >= 0; $i-- )
    65     for ( $i = 13; $i >= 0; $i-- )
    48       $graphdata[$ts] = stats_message_count($channel, 1440, $basetime);
    70       $graphdata[$ts] = stats_message_count($channel, 1440, $basetime);
    49     }
    71     }
    50     $g->SetBarPadding(12);
    72     $g->SetBarPadding(12);
    51     break;
    73     break;
    52   case 'lastmonth':
    74   case 'lastmonth':
       
    75     $g = makeGraph();
    53     $graph_title = $channel . ' activity - last 30 days';
    76     $graph_title = $channel . ' activity - last 30 days';
    54     $this_day = gmmktime(0, 0, 0);
    77     $this_day = gmmktime(0, 0, 0);
    55     $graphdata = array();
    78     $graphdata = array();
    56     
    79     
    57     for ( $i = 29; $i >= 0; $i-- )
    80     for ( $i = 29; $i >= 0; $i-- )
    60       $ts = date('Y-m-d', $basetime);
    83       $ts = date('Y-m-d', $basetime);
    61       $basetime += 86400;
    84       $basetime += 86400;
    62       $graphdata[$ts] = stats_message_count($channel, 1440, $basetime);
    85       $graphdata[$ts] = stats_message_count($channel, 1440, $basetime);
    63     }
    86     }
    64     $g->SetBarPadding(15);
    87     $g->SetBarPadding(15);
       
    88     break;
       
    89   case 'lasthour':
       
    90     $g = makeGraph('line');
       
    91     $g->SetAxisStepX(5);
       
    92     $g->SetScaleFunctionX('lasthour_scaler');
       
    93     function lasthour_scaler($v, $g)
       
    94     {
       
    95       $k = array_keys($g->data);
       
    96       return ( isset($k[$v]) ) ? $k[$v] : 'now';
       
    97     }
       
    98     $graph_title = $channel . ' activity - last hour';
       
    99     $data = stats_raw_data($channel, 59);
       
   100     $agg = array();
       
   101     foreach ( $data as $message )
       
   102     {
       
   103       $tid = intval(ltrim(date('i', $message['time']), '0'));
       
   104       if ( !isset($agg[$tid]) )
       
   105         $agg[$tid] = 0;
       
   106       $agg[$tid]++;
       
   107     }
       
   108     $graphdata = array();
       
   109     $minutenow = intval(ltrim(date('i', NOW), '0'));
       
   110     $hournow = intval(ltrim(date('H', NOW), '0'));
       
   111     $hourthen = intval(ltrim(date('H', NOW-3600), '0'));
       
   112     for ( $i = $minutenow + 1; $i < 60; $i++ )
       
   113     {
       
   114       $istr = ( $i < 10 ) ? "0$i" : "$i";
       
   115       $graphdata["$hourthen:$istr"] = ( isset($agg[$i]) ) ? $agg[$i] : 0;
       
   116     }
       
   117     for ( $i = 0; $i <= $minutenow; $i++ )
       
   118     {
       
   119       $istr = ( $i < 10 ) ? "0$i" : "$i";
       
   120       $graphdata["$hournow:$istr"] = ( isset($agg[$i]) ) ? $agg[$i] : 0;
       
   121     }
       
   122     
    65     break;
   123     break;
    66 }
   124 }
    67 
   125 
    68 $max = max($graphdata);
   126 $max = max($graphdata);
    69 
   127 
    92 if ( $max > 15000 )
   150 if ( $max > 15000 )
    93   $interval = 1500;
   151   $interval = 1500;
    94 if ( $max > 30000 )
   152 if ( $max > 30000 )
    95   $interval = round($max / 10);
   153   $interval = round($max / 10);
    96 
   154 
    97 $g->SetBarData($graphdata);
   155 $g->data = $graphdata;
    98 $g->SetAxisStep($interval);
   156 
    99 $g->SetGraphTitle($graph_title);
   157 $g->SetGraphTitle($graph_title);
       
   158 $g->SetAxisStepY($interval);
   100 
   159 
   101 $g->DrawGraph();
   160 $g->DrawGraph();
   102 
   161