main.php 9.0 KB

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