statistic.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Statistics\Modules;
  3. function statistic($module, $interface, $date, $start_time, $offset)
  4. {
  5. $today = date('Y-m-d');
  6. $time_now = time();
  7. multiRequestStAndModules($module, $interface, $date);
  8. $all_st_str = '';
  9. if(is_array(\Statistics\Lib\Cache::$statisticDataCache['statistic']))
  10. {
  11. foreach(\Statistics\Lib\Cache::$statisticDataCache['statistic'] as $ip=>$st_str)
  12. {
  13. $all_st_str .= $st_str;
  14. }
  15. }
  16. $code_map = array();
  17. $data = formatSt($all_st_str, $date, $code_map);
  18. $interface_name = "$module::$interface";
  19. $success_series_data = $fail_series_data = $success_time_series_data = $fail_time_series_data = array();
  20. $total_count = $fail_count = 0;
  21. foreach($data as $time_point=>$item)
  22. {
  23. if($item['total_count'])
  24. {
  25. $success_series_data[] = "[".($time_point*1000).",{$item['total_count']}]";
  26. $total_count += $item['total_count'];
  27. }
  28. $fail_series_data[] = "[".($time_point*1000).",{$item['fail_count']}]";
  29. $fail_count += $item['fail_count'];
  30. if($item['total_avg_time'])
  31. {
  32. $success_time_series_data[] = "[".($time_point*1000).",{$item['total_avg_time']}]";
  33. }
  34. $fail_time_series_data[] = "[".($time_point*1000).",{$item['fail_avg_time']}]";
  35. }
  36. $success_series_data = implode(',', $success_series_data);
  37. $fail_series_data = implode(',', $fail_series_data);
  38. $success_time_series_data = implode(',', $success_time_series_data);
  39. $fail_time_series_data = implode(',', $fail_time_series_data);
  40. unset($_GET['start_time'], $_GET['end_time'], $_GET['date'], $_GET['fn']);
  41. $query = http_build_query($_GET);
  42. // 删除末尾0的记录
  43. if($today == $date)
  44. {
  45. while(!empty($data) && ($item = end($data)) && $item['total_count'] == 0 && ($key = key($data)) && $time_now < $key)
  46. {
  47. unset($data[$key]);
  48. }
  49. }
  50. $table_data = '';
  51. if($data)
  52. {
  53. $first_line = true;
  54. foreach($data as $item)
  55. {
  56. if($first_line)
  57. {
  58. $first_line = false;
  59. if($item['total_count'] == 0)
  60. {
  61. continue;
  62. }
  63. }
  64. $html_class = 'class="danger"';
  65. if($item['total_count'] == 0)
  66. {
  67. $html_class = '';
  68. }
  69. elseif($item['precent']>=99.99)
  70. {
  71. $html_class = 'class="success"';
  72. }
  73. elseif($item['precent']>=99)
  74. {
  75. $html_class = '';
  76. }
  77. elseif($item['precent']>=98)
  78. {
  79. $html_class = 'class="warning"';
  80. }
  81. $table_data .= "\n<tr $html_class>
  82. <td>{$item['time']}</td>
  83. <td>{$item['total_count']}</td>
  84. <td> {$item['total_avg_time']}</td>
  85. <td>{$item['suc_count']}</td>
  86. <td>{$item['suc_avg_time']}</td>
  87. <td>".($item['fail_count']>0?("<a href='/?fn=logger&$query&start_time=".strtotime($item['time'])."&end_time=".(strtotime($item['time'])+300)."'>{$item['fail_count']}</a>"):$item['fail_count'])."</td>
  88. <td>{$item['fail_avg_time']}</td>
  89. <td>{$item['precent']}%</td>
  90. </tr>
  91. ";
  92. }
  93. }
  94. // date btn
  95. $date_btn_str = '';
  96. for($i=13;$i>=1;$i--)
  97. {
  98. $the_time = strtotime("-$i day");
  99. $the_date = date('Y-m-d',$the_time);
  100. $html_the_date = $date == $the_date ? "<b>$the_date</b>" : $the_date;
  101. $date_btn_str .= '<a href="/?fn=statistic&date='."$the_date&$query".'" class="btn '.$html_class.'" type="button">'.$html_the_date.'</a>';
  102. if($i == 7)
  103. {
  104. $date_btn_str .= '</br>';
  105. }
  106. }
  107. $the_date = date('Y-m-d');
  108. $html_the_date = $date == $the_date ? "<b>$the_date</b>" : $the_date;
  109. $date_btn_str .= '<a href="/?date='."$the_date&$query".'" class="btn" type="button">'.$html_the_date.'</a>';
  110. $module_str ='';
  111. foreach(\Statistics\Lib\Cache::$modulesDataCache as $mod => $interfaces)
  112. {
  113. if($mod == 'WorkerMan')
  114. {
  115. continue;
  116. }
  117. $module_str .= '<li><a href="/?fn=statistic&module='.$mod.'">'.$mod.'</a></li>';
  118. if($module == $mod)
  119. {
  120. foreach ($interfaces as $if)
  121. {
  122. $module_str .= '<li>&nbsp;&nbsp;<a href="/?fn=statistic&module='.$mod.'&interface='.$if.'">'.$if.'</a></li>';
  123. }
  124. }
  125. }
  126. include ST_ROOT . '/Views/header.tpl.php';
  127. include ST_ROOT . '/Views/statistic.tpl.php';
  128. include ST_ROOT . '/Views/footer.tpl.php';
  129. }