main.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace Statistics\Modules;
  3. function main($module, $interface, $date, $start_time, $offset)
  4. {
  5. $err_msg = $notice_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'])-300)."&end_time=".(strtotime($item['time']))."'>{$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>';
  135. foreach (\Statistics\Lib\Cache::$lastFailedIpArray as $ip)
  136. {
  137. $err_msg .= $ip.'::'.\Statistics\Config\Config::$ProviderPort . '&nbsp;';
  138. }
  139. }
  140. if(empty(\Statistics\Lib\Cache::$ServerIpList))
  141. {
  142. $notice_msg = <<<EOT
  143. <h4>数据源为空</h4>
  144. 您可以 <a href="/?fn=admin&act=detect_server" class="btn" type="button"><strong>探测数据源</strong></a>或者<a href="/?fn=admin" class="btn" type="button"><strong>添加数据源</strong></a>
  145. EOT;
  146. }
  147. include ST_ROOT . '/Views/header.tpl.php';
  148. include ST_ROOT . '/Views/main.tpl.php';
  149. include ST_ROOT . '/Views/footer.tpl.php';
  150. }
  151. function multiRequestStAndModules($module, $interface, $date)
  152. {
  153. \Statistics\Lib\Cache::$statisticDataCache['statistic'] = '';
  154. $buffer = json_encode(array('cmd'=>'get_statistic','module'=>$module, 'interface'=>$interface, 'date'=>$date))."\n";
  155. $ip_list = (!empty($_GET['ip']) && is_array($_GET['ip'])) ? $_GET['ip'] : \Statistics\Lib\Cache::$ServerIpList;
  156. $reqest_buffer_array = array();
  157. $port = \Statistics\Config\Config::$ProviderPort;;
  158. foreach($ip_list as $ip)
  159. {
  160. $reqest_buffer_array["$ip:$port"] = $buffer;
  161. }
  162. $read_buffer_array = multiRequest($reqest_buffer_array);
  163. foreach($read_buffer_array as $address => $buf)
  164. {
  165. list($ip, $port) = explode(':',$address);
  166. $body_data = json_decode(trim($buf), true);
  167. $statistic_data = isset($body_data['statistic']) ? $body_data['statistic'] : '';
  168. $modules_data = isset($body_data['modules']) ? $body_data['modules'] : array();
  169. // 整理modules
  170. foreach($modules_data as $mod => $interfaces)
  171. {
  172. if(!isset(\Statistics\Lib\Cache::$modulesDataCache[$mod]))
  173. {
  174. \Statistics\Lib\Cache::$modulesDataCache[$mod] = array();
  175. }
  176. foreach($interfaces as $if)
  177. {
  178. \Statistics\Lib\Cache::$modulesDataCache[$mod][$if] = $if;
  179. }
  180. }
  181. \Statistics\Lib\Cache::$statisticDataCache['statistic'][$ip] = $statistic_data;
  182. }
  183. }
  184. function formatSt($str, $date, &$code_map)
  185. {
  186. // time:[suc_count:xx,suc_cost_time:xx,fail_count:xx,fail_cost_time:xx]
  187. $st_data = $code_map = array();
  188. $st_explode = explode("\n", $str);
  189. // 汇总计算
  190. foreach($st_explode as $line)
  191. {
  192. // line = IP time suc_count suc_cost_time fail_count fail_cost_time code_json
  193. $line_data = explode("\t", $line);
  194. if(!isset($line_data[5]))
  195. {
  196. continue;
  197. }
  198. $time_line = $line_data[1];
  199. $time_line = ceil($time_line/300)*300;
  200. $suc_count = $line_data[2];
  201. $suc_cost_time = $line_data[3];
  202. $fail_count = $line_data[4];
  203. $fail_cost_time = $line_data[5];
  204. $tmp_code_map = json_decode($line_data[6], true);
  205. if(!isset($st_data[$time_line]))
  206. {
  207. $st_data[$time_line] = array('suc_count'=>0, 'suc_cost_time'=>0, 'fail_count'=>0, 'fail_cost_time'=>0);
  208. }
  209. $st_data[$time_line]['suc_count'] += $suc_count;
  210. $st_data[$time_line]['suc_cost_time'] += $suc_cost_time;
  211. $st_data[$time_line]['fail_count'] += $fail_count;
  212. $st_data[$time_line]['fail_cost_time'] += $fail_cost_time;
  213. if(is_array($tmp_code_map))
  214. {
  215. foreach($tmp_code_map as $code=>$count)
  216. {
  217. if(!isset($code_map[$code]))
  218. {
  219. $code_map[$code] = 0;
  220. }
  221. $code_map[$code] += $count;
  222. }
  223. }
  224. }
  225. // 按照时间排序
  226. ksort($st_data);
  227. // time => [total_count:xx,suc_count:xx,suc_avg_time:xx,fail_count:xx,fail_avg_time:xx,percent:xx]
  228. $data = array();
  229. // 计算成功率 耗时
  230. foreach($st_data as $time_line=>$item)
  231. {
  232. $data[$time_line] = array(
  233. 'time' => date('Y-m-d H:i:s', $time_line),
  234. 'total_count' => $item['suc_count']+$item['fail_count'],
  235. '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']), 6),
  236. 'suc_count' => $item['suc_count'],
  237. 'suc_avg_time' => $item['suc_count'] == 0 ? $item['suc_count'] : round($item['suc_cost_time']/$item['suc_count'], 6),
  238. 'fail_count' => $item['fail_count'],
  239. 'fail_avg_time' => $item['fail_count'] == 0 ? 0 : round($item['fail_cost_time']/$item['fail_count'], 6),
  240. 'precent' => $item['suc_count']+$item['fail_count'] == 0 ? 0 : round(($item['suc_count']*100/($item['suc_count']+$item['fail_count'])), 4),
  241. );
  242. }
  243. $time_point = strtotime($date);
  244. for($i=0;$i<288;$i++)
  245. {
  246. $data[$time_point] = isset($data[$time_point]) ? $data[$time_point] :
  247. array(
  248. 'time' => date('Y-m-d H:i:s', $time_point),
  249. 'total_count' => 0,
  250. 'total_avg_time'=> 0,
  251. 'suc_count' => 0,
  252. 'suc_avg_time' => 0,
  253. 'fail_count' => 0,
  254. 'fail_avg_time' => 0,
  255. 'precent' => 100,
  256. );
  257. $time_point +=300;
  258. }
  259. ksort($data);
  260. return $data;
  261. }