graph.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. date_default_timezone_set('America/Toronto');
  3. include "../../data/config.php";
  4. function get_requests($slug){
  5. $time = time();
  6. $requests = [];
  7. for($i=0; $i<73; $i++){
  8. $data = apcu_fetch(intdiv($time - (3600 * $i), 3600) . ".$slug");
  9. $requests[] = [$i, ($data === false ? 0 : $data)];
  10. }
  11. // rtrim() out all entries that are 0
  12. $out = [];
  13. $found = false;
  14. for($i=count($requests) - 1; $i>=0; $i--){
  15. if($requests[$i][1] !== 0){
  16. $found = $i;
  17. break;
  18. }
  19. }
  20. if($found === false){
  21. return [];
  22. }
  23. $requests = array_slice($requests, 0, $found + 1);
  24. return $requests;
  25. }
  26. $bot_requests = get_requests("bot_requests");
  27. $real_requests = get_requests("real_requests");
  28. $real_reqs_24h = 0;
  29. for($i=0; $i<count($real_requests); $i++){
  30. if($real_requests[$i][1] !== false){
  31. $real_reqs_24h += $real_requests[$i][1];
  32. }
  33. }
  34. $real_reqs_72h = 0;
  35. for($i=0; $i<count($real_requests); $i++){
  36. if($real_requests[$i][1] !== false){
  37. $real_reqs_72h += $real_requests[$i][1];
  38. }
  39. }
  40. header("Content-Type: image/png");
  41. include "../../lib/graph_gen.php";
  42. $graph = new graph();
  43. $c = count($real_requests);
  44. if($c > 25){
  45. $c = 25;
  46. }elseif($c <= 2){
  47. $c = 2;
  48. }
  49. echo $graph->render([
  50. "title" => "Number of requests within the last 72 hours (non-cummulative)",
  51. "subtitle" => date("jS M y @ g:ia", time()) . " || 24h=" . number_format($real_reqs_24h) . ", 72h=" . number_format($real_reqs_72h) . " || " . config::SERVER_NAME,
  52. "x_label" => "<= Hours passed =>",
  53. "y_label" => "<= Request count/hour =>",
  54. "grad_x" => $c,
  55. "grad_y" => 20,
  56. "data" => [
  57. "Bot requests" => $bot_requests,
  58. "Real requests" => $real_requests,
  59. ]
  60. ]);