Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define('GRAPH_DIR','');
- $dbFile = '';
- $db = new PDO('sqlite:' . $dbFile);
- $get_distinct_channels = $db->prepare(
- 'select distinct(channel) as channel from channel_stats;'
- );
- $storeChannelStatsData = $db->prepare(
- 'insert into channel_stats (time,channel,users,topic)
- values(:time, :channel, :users, :topic)'
- );
- $get_min_users = $db->prepare(
- 'select min(users) as users from channel_stats where channel=:channel'
- );
- $get_max_users = $db->prepare(
- 'select max(users) as users from channel_stats where channel=:channel'
- );
- $get_start_date = $db->prepare(
- "select datetime(`time`,'unixepoch') as date from channel_stats where channel=:channel order by date asc limit 1;"
- );
- $get_end_date = $db->prepare(
- "select datetime(`time`,'unixepoch') as date from channel_stats where channel=:channel order by date desc limit 1;"
- );
- $get_data_list = $db->prepare(
- "select datetime(`time`,'unixepoch') as date, users as users from channel_stats where channel=:channel order by time;"
- );
- function get_list($channel){
- global $db,$get_data_list;
- $core_data = array();
- $data = $get_data_list->execute(array(':channel' => $channel));
- foreach($get_data_list->fetchAll() as $row){
- if(!isset($core_data[$row['date']])){
- $core_data[$row['date']] = intval($row['users']); }
- }
- return $core_data;
- }
- function get_date($channel,$order){
- global $db,$get_start_date,$get_end_date;
- if($order == 'start'){
- $get_start_date->execute(array(':channel' => $channel));
- $result = $get_start_date->fetch();
- } else {
- $get_end_date->execute(array(':channel' => $channel));
- $result = $get_end_date->fetch();
- }
- return $result['date'];
- }
- function get_max_users($channel){
- global $db,$get_max_users,$get_min_users;
- $get_max_users->execute(array(':channel' => $channel));
- $result = $get_max_users->fetch();
- $ammount = intval($result['users']);
- return round($ammount + ($ammount * .30));
- }
- function get_min_users($channel){
- global $db,$get_max_users,$get_min_users;
- $get_min_users->execute(array(':channel' => $channel));
- $result = $get_min_users->fetch();
- $ammount = intval($result['users']);
- return round($ammount - ($ammount * .30));
- }
- function create_tmp_data_file($channel,$path='/tmp'){
- global $db,$get_data_list;
- $safe_channel = str_replace('#','',$channel);
- $file_contents = ''; foreach(get_list($channel) as $time => $count){
- $file_contents .= "$time $count\n";
- }
- $filename = $path.'/ircstats-'.$safe_channel.'.'.substr(md5(time()),0,8);
- if(touch($filename)) {
- file_put_contents($filename,$file_contents);
- return $filename;
- } else {
- return false;
- }
- }
- function make_gnuplotter($channel,$startdate=''){
- $safe_channel = str_replace('#','',$channel);
- $chart_path = GRAPH_DIR . '/' . $safe_channel . '.png';
- $tmp_data = create_tmp_data_file($channel);
- $start_date = get_date($channel,'start');
- $end_date = get_date($channel,'end');
- $min_users = get_min_users($channel);
- $max_users = get_max_users($channel);
- $string =
- "reset\n"
- ."set terminal png size 612,400\n"
- ."set output \"$chart_path\"\n"
- ."set xdata time\n"
- ."set timefmt \"%Y-%m-%d %H:%M:%S\"\n"
- ."set format x \"%m/%d\"\n"
- ."set xlabel \"time\"\n"
- ."set ylabel \"total users (30m)\"\n"
- ."set xrange [\"$start_date\":\"$end_date\"]\n"
- ."set yrange [$min_users:$max_users]\n"
- ."set title \"Users $channel\"\n"
- ."set key Left outside\n"
- ."set grid\n"
- ."set style data line\n"
- ."plot \"$tmp_data\" using 1:3 title \"Users\"\n";
- $filename = '/tmp'.'/ircstats-'.$safe_channel.'.'.substr(md5(time()),0,8).'.rg';
- if(touch($filename)) {
- file_put_contents($filename,$string);
- passthru("gnuplot $filename");
- unlink($filename);
- unlink($tmp_data);
- } else {
- return false;
- }
- }
- function channels(){
- global $db,$get_distinct_channels;
- $get_distinct_channels->execute();
- $data = $get_distinct_channels->fetchAll();
- foreach($data as $row){
- $channel = $row['channel'];
- if(stristr($channel,'#')){
- if(!stristr($channel,"#()")){
- echo "Generating graph for $channel\n";
- make_gnuplotter($channel);
- }
- } else {
- echo "Not generating graph for $channel\n";
- }
- }
- }
- channels();
- ?>
Add Comment
Please, Sign In to add comment