main.php 9.6 KB

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