main.php 8.8 KB

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