Guest User

Untitled

a guest
Jan 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. <?php
  2. define('GRAPH_DIR','');
  3. $dbFile = '';
  4. $db = new PDO('sqlite:' . $dbFile);
  5. $get_distinct_channels = $db->prepare(
  6. 'select distinct(channel) as channel from channel_stats;'
  7. );
  8. $storeChannelStatsData = $db->prepare(
  9. 'insert into channel_stats (time,channel,users,topic)
  10. values(:time, :channel, :users, :topic)'
  11. );
  12. $get_min_users = $db->prepare(
  13. 'select min(users) as users from channel_stats where channel=:channel'
  14. );
  15. $get_max_users = $db->prepare(
  16. 'select max(users) as users from channel_stats where channel=:channel'
  17. );
  18. $get_start_date = $db->prepare(
  19. "select datetime(`time`,'unixepoch') as date from channel_stats where channel=:channel order by date asc limit 1;"
  20. );
  21. $get_end_date = $db->prepare(
  22. "select datetime(`time`,'unixepoch') as date from channel_stats where channel=:channel order by date desc limit 1;"
  23. );
  24. $get_data_list = $db->prepare(
  25. "select datetime(`time`,'unixepoch') as date, users as users from channel_stats where channel=:channel order by time;"
  26. );
  27.  
  28. function get_list($channel){
  29. global $db,$get_data_list;
  30. $core_data = array();
  31. $data = $get_data_list->execute(array(':channel' => $channel));
  32. foreach($get_data_list->fetchAll() as $row){
  33. if(!isset($core_data[$row['date']])){
  34. $core_data[$row['date']] = intval($row['users']); }
  35. }
  36. return $core_data;
  37. }
  38. function get_date($channel,$order){
  39. global $db,$get_start_date,$get_end_date;
  40. if($order == 'start'){
  41. $get_start_date->execute(array(':channel' => $channel));
  42. $result = $get_start_date->fetch();
  43. } else {
  44. $get_end_date->execute(array(':channel' => $channel));
  45. $result = $get_end_date->fetch();
  46. }
  47. return $result['date'];
  48. }
  49. function get_max_users($channel){
  50. global $db,$get_max_users,$get_min_users;
  51. $get_max_users->execute(array(':channel' => $channel));
  52. $result = $get_max_users->fetch();
  53. $ammount = intval($result['users']);
  54. return round($ammount + ($ammount * .30));
  55. }
  56. function get_min_users($channel){
  57. global $db,$get_max_users,$get_min_users;
  58. $get_min_users->execute(array(':channel' => $channel));
  59. $result = $get_min_users->fetch();
  60. $ammount = intval($result['users']);
  61. return round($ammount - ($ammount * .30));
  62. }
  63. function create_tmp_data_file($channel,$path='/tmp'){
  64. global $db,$get_data_list;
  65. $safe_channel = str_replace('#','',$channel);
  66. $file_contents = ''; foreach(get_list($channel) as $time => $count){
  67. $file_contents .= "$time $count\n";
  68. }
  69. $filename = $path.'/ircstats-'.$safe_channel.'.'.substr(md5(time()),0,8);
  70.  
  71. if(touch($filename)) {
  72. file_put_contents($filename,$file_contents);
  73. return $filename;
  74. } else {
  75. return false;
  76. }
  77. }
  78. function make_gnuplotter($channel,$startdate=''){
  79. $safe_channel = str_replace('#','',$channel);
  80. $chart_path = GRAPH_DIR . '/' . $safe_channel . '.png';
  81. $tmp_data = create_tmp_data_file($channel);
  82. $start_date = get_date($channel,'start');
  83. $end_date = get_date($channel,'end');
  84. $min_users = get_min_users($channel);
  85. $max_users = get_max_users($channel);
  86.  
  87. $string =
  88. "reset\n"
  89. ."set terminal png size 612,400\n"
  90. ."set output \"$chart_path\"\n"
  91. ."set xdata time\n"
  92. ."set timefmt \"%Y-%m-%d %H:%M:%S\"\n"
  93. ."set format x \"%m/%d\"\n"
  94. ."set xlabel \"time\"\n"
  95. ."set ylabel \"total users (30m)\"\n"
  96. ."set xrange [\"$start_date\":\"$end_date\"]\n"
  97. ."set yrange [$min_users:$max_users]\n"
  98. ."set title \"Users $channel\"\n"
  99. ."set key Left outside\n"
  100. ."set grid\n"
  101. ."set style data line\n"
  102. ."plot \"$tmp_data\" using 1:3 title \"Users\"\n";
  103. $filename = '/tmp'.'/ircstats-'.$safe_channel.'.'.substr(md5(time()),0,8).'.rg';
  104. if(touch($filename)) {
  105. file_put_contents($filename,$string);
  106. passthru("gnuplot $filename");
  107. unlink($filename);
  108. unlink($tmp_data);
  109. } else {
  110. return false;
  111. }
  112. }
  113. function channels(){
  114. global $db,$get_distinct_channels;
  115. $get_distinct_channels->execute();
  116. $data = $get_distinct_channels->fetchAll();
  117. foreach($data as $row){
  118. $channel = $row['channel'];
  119. if(stristr($channel,'#')){
  120. if(!stristr($channel,"#()")){
  121. echo "Generating graph for $channel\n";
  122. make_gnuplotter($channel);
  123. }
  124. } else {
  125. echo "Not generating graph for $channel\n";
  126. }
  127. }
  128. }
  129. channels();
  130. ?>
Add Comment
Please, Sign In to add comment