statistic.php 5.2 KB

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