main.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace Statistics\Modules;
  3. function main($module, $interface, $date, $start_time, $offset)
  4. {
  5. $module = 'WorkerMan';
  6. $interface = 'Statistics';
  7. $today = date('Y-m-d');
  8. $time_now = time();
  9. multiRequestStAndModules($module, $interface, $date);
  10. $all_st_str = '';
  11. if(is_array(\Statistics\Lib\Cache::$statisticDataCache['statistic']))
  12. {
  13. foreach(\Statistics\Lib\Cache::$statisticDataCache['statistic'] as $ip=>$st_str)
  14. {
  15. $all_st_str .= $st_str;
  16. }
  17. }
  18. $code_map = array();
  19. $data = formatSt($all_st_str, $date, $code_map);
  20. $interface_name = '整体';
  21. $success_series_data = $fail_series_data = $success_time_series_data = $fail_time_series_data = array();
  22. $total_count = $fail_count = 0;
  23. foreach($data as $time_point=>$item)
  24. {
  25. if($item['total_count'])
  26. {
  27. $success_series_data[] = "[".($time_point*1000).",{$item['total_count']}]";
  28. $total_count += $item['total_count'];
  29. }
  30. $fail_series_data[] = "[".($time_point*1000).",{$item['fail_count']}]";
  31. $fail_count += $item['fail_count'];
  32. if($item['total_avg_time'])
  33. {
  34. $success_time_series_data[] = "[".($time_point*1000).",{$item['total_avg_time']}]";
  35. }
  36. $fail_time_series_data[] = "[".($time_point*1000).",{$item['fail_avg_time']}]";
  37. }
  38. $success_series_data = implode(',', $success_series_data);
  39. $fail_series_data = implode(',', $fail_series_data);
  40. $success_time_series_data = implode(',', $success_time_series_data);
  41. $fail_time_series_data = implode(',', $fail_time_series_data);
  42. // 总体成功率
  43. $global_rate = $total_count ? round((($total_count - $fail_count)/$total_count)*100, 4) : 100;
  44. // 返回码分布
  45. $code_pie_data = '';
  46. $code_pie_array = array();
  47. unset($code_map[0]);
  48. if(empty($code_map))
  49. {
  50. $code_map[0] = $total_count > 0 ? $total_count : 1;
  51. }
  52. if(is_array($code_map))
  53. {
  54. $total_item_count = array_sum($code_map);
  55. foreach($code_map as $code=>$count)
  56. {
  57. $code_pie_array[] = "[\"$code:{$count}个\", ".round($count*100/$total_item_count, 4)."]";
  58. }
  59. $code_pie_data = implode(',', $code_pie_array);
  60. }
  61. unset($_GET['start_time'], $_GET['end_time'], $_GET['date'], $_GET['fn']);
  62. $query = http_build_query($_GET);
  63. // 删除末尾0的记录
  64. if($today == $date)
  65. {
  66. while(!empty($data) && ($item = end($data)) && $item['total_count'] == 0 && ($key = key($data)) && $time_now < $key)
  67. {
  68. unset($data[$key]);
  69. }
  70. }
  71. $table_data = '';
  72. if($data)
  73. {
  74. $first_line = true;
  75. foreach($data as $item)
  76. {
  77. if($first_line)
  78. {
  79. $first_line = false;
  80. if($item['total_count'] == 0)
  81. {
  82. continue;
  83. }
  84. }
  85. $html_class = 'class="danger"';
  86. if($item['total_count'] == 0)
  87. {
  88. $html_class = '';
  89. }
  90. elseif($item['precent']>=99.99)
  91. {
  92. $html_class = 'class="success"';
  93. }
  94. elseif($item['precent']>=99)
  95. {
  96. $html_class = '';
  97. }
  98. elseif($item['precent']>=98)
  99. {
  100. $html_class = 'class="warning"';
  101. }
  102. $table_data .= "\n<tr $html_class>
  103. <td>{$item['time']}</td>
  104. <td>{$item['total_count']}</td>
  105. <td> {$item['total_avg_time']}</td>
  106. <td>{$item['suc_count']}</td>
  107. <td>{$item['suc_avg_time']}</td>
  108. <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>
  109. <td>{$item['fail_avg_time']}</td>
  110. <td>{$item['precent']}%</td>
  111. </tr>
  112. ";
  113. }
  114. }
  115. // date btn
  116. $date_btn_str = '';
  117. for($i=13;$i>=1;$i--)
  118. {
  119. $the_time = strtotime("-$i day");
  120. $the_date = date('Y-m-d',$the_time);
  121. $html_the_date = $date == $the_date ? "<b>$the_date</b>" : $the_date;
  122. $date_btn_str .= '<a href="/?date='."$the_date&$query".'" class="btn '.$html_class.'" type="button">'.$html_the_date.'</a>';
  123. if($i == 7)
  124. {
  125. $date_btn_str .= '</br>';
  126. }
  127. }
  128. $the_date = date('Y-m-d');
  129. $html_the_date = $date == $the_date ? "<b>$the_date</b>" : $the_date;
  130. $date_btn_str .= '<a href="/?date='."$the_date&$query".'" class="btn" type="button">'.$html_the_date.'</a>';
  131. include ST_ROOT . '/Views/header.tpl.php';
  132. include ST_ROOT . '/Views/main.tpl.php';
  133. include ST_ROOT . '/Views/footer.tpl.php';
  134. }
  135. function multiRequestStAndModules($module, $interface, $date)
  136. {
  137. \Statistics\Lib\Cache::$statisticDataCache['statistic'] = '';
  138. $buffer = json_encode(array('cmd'=>'get_statistic','module'=>$module, 'interface'=>$interface, 'date'=>$date))."\n";
  139. $ip_list = (!empty($_GET['ip']) && is_array($_GET['ip'])) ? $_GET['ip'] : \Statistics\Lib\Cache::$ServerIpList;
  140. $reqest_buffer_array = array();
  141. $port = 55858;
  142. foreach($ip_list as $ip)
  143. {
  144. $reqest_buffer_array["$ip:$port"] = $buffer;
  145. }
  146. $read_buffer_array = multiRequest($reqest_buffer_array);
  147. foreach($read_buffer_array as $address => $buf)
  148. {
  149. list($ip, $port) = explode(':',$address);
  150. $body_data = json_decode(trim($buf), true);
  151. $statistic_data = isset($body_data['statistic']) ? $body_data['statistic'] : '';
  152. $modules_data = isset($body_data['modules']) ? $body_data['modules'] : array();
  153. // 整理modules
  154. foreach($modules_data as $mod => $interfaces)
  155. {
  156. if(!isset(\Statistics\Lib\Cache::$modulesDataCache[$mod]))
  157. {
  158. \Statistics\Lib\Cache::$modulesDataCache[$mod] = array();
  159. }
  160. foreach($interfaces as $if)
  161. {
  162. \Statistics\Lib\Cache::$modulesDataCache[$mod][$if] = $if;
  163. }
  164. }
  165. \Statistics\Lib\Cache::$statisticDataCache['statistic'][$ip] = $statistic_data;
  166. }
  167. }
  168. function formatSt($str, $date, &$code_map)
  169. {
  170. // time:[suc_count:xx,suc_cost_time:xx,fail_count:xx,fail_cost_time:xx]
  171. $st_data = $code_map = array();
  172. $st_explode = explode("\n", $str);
  173. // 汇总计算
  174. foreach($st_explode as $line)
  175. {
  176. // line = IP time suc_count suc_cost_time fail_count fail_cost_time code_json
  177. $line_data = explode("\t", $line);
  178. if(!isset($line_data[5]))
  179. {
  180. continue;
  181. }
  182. $time_line = $line_data[1];
  183. $time_line = ceil($time_line/300)*300;
  184. $suc_count = $line_data[2];
  185. $suc_cost_time = $line_data[3];
  186. $fail_count = $line_data[4];
  187. $fail_cost_time = $line_data[5];
  188. $tmp_code_map = json_decode($line_data[6], true);
  189. if(!isset($st_data[$time_line]))
  190. {
  191. $st_data[$time_line] = array('suc_count'=>0, 'suc_cost_time'=>0, 'fail_count'=>0, 'fail_cost_time'=>0);
  192. }
  193. $st_data[$time_line]['suc_count'] += $suc_count;
  194. $st_data[$time_line]['suc_cost_time'] += $suc_cost_time;
  195. $st_data[$time_line]['fail_count'] += $fail_count;
  196. $st_data[$time_line]['fail_cost_time'] += $fail_cost_time;
  197. if(is_array($tmp_code_map))
  198. {
  199. foreach($tmp_code_map as $code=>$count)
  200. {
  201. if(!isset($code_map[$code]))
  202. {
  203. $code_map[$code] = 0;
  204. }
  205. $code_map[$code] += $count;
  206. }
  207. }
  208. }
  209. // 按照时间排序
  210. ksort($st_data);
  211. // time => [total_count:xx,suc_count:xx,suc_avg_time:xx,fail_count:xx,fail_avg_time:xx,percent:xx]
  212. $data = array();
  213. // 计算成功率 耗时
  214. foreach($st_data as $time_line=>$item)
  215. {
  216. $data[$time_line] = array(
  217. 'time' => date('Y-m-d H:i:s', $time_line),
  218. 'total_count' => $item['suc_count']+$item['fail_count'],
  219. 'total_avg_time'=> $item['suc_count']+$item['fail_count'] == 0 ? 0 : round(($item['suc_cost_time']+$item['fail_cost_time'])/($item['suc_count']+$item['fail_count']), 4),
  220. 'suc_count' => $item['suc_count'],
  221. 'suc_avg_time' => $item['suc_count'] == 0 ? $item['suc_count'] : round($item['suc_cost_time']/$item['suc_count'], 4),
  222. 'fail_count' => $item['fail_count'],
  223. 'fail_avg_time' => $item['fail_count'] == 0 ? 0 : round($item['fail_cost_time']/$item['fail_count'], 4),
  224. 'precent' => $item['suc_count']+$item['fail_count'] == 0 ? 0 : round(($item['suc_count']*100/($item['suc_count']+$item['fail_count'])), 4),
  225. );
  226. }
  227. $time_point = strtotime($date);
  228. for($i=0;$i<288;$i++)
  229. {
  230. $data[$time_point] = isset($data[$time_point]) ? $data[$time_point] :
  231. array(
  232. 'time' => date('Y-m-d H:i:s', $time_point),
  233. 'total_count' => 0,
  234. 'total_avg_time'=> 0,
  235. 'suc_count' => 0,
  236. 'suc_avg_time' => 0,
  237. 'fail_count' => 0,
  238. 'fail_avg_time' => 0,
  239. 'precent' => 100,
  240. );
  241. $time_point +=300;
  242. }
  243. ksort($data);
  244. return $data;
  245. }