graph.php 1.7 KB

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