main.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. function main($module, $interface, $date, $date, $start_time, $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. $data = formatSt($all_st_str, $date);
  16. include ST_ROOT . '/Views/header.tpl.php';
  17. include ST_ROOT . '/Views/main.tpl.php';
  18. include ST_ROOT . '/Views/footer.tpl.php';
  19. }
  20. function multiRequestStAndModules($module, $interface, $date)
  21. {
  22. Cache::$statisticDataCache['statistic'] = '';
  23. $buffer = json_encode(array('module'=>$module, 'interface'=>$interface, 'date'=>$date))."\n";
  24. $ip_list = (!empty($_GET['server_ip']) && is_array($_GET['server_ip'])) ? $_GET['server_ip'] : Cache::$ServerIpList;
  25. $reqest_buffer_array = array();
  26. $port = \Man\Core\Lib\Config::get('StatisticWorker.port') ? \Man\Core\Lib\Config::get('StatisticWorker.port') : 55656;
  27. foreach($ip_list as $ip)
  28. {
  29. $reqest_buffer_array["$ip:$port"] = $buffer;
  30. }
  31. $read_buffer_array = multiRequest($reqest_buffer_array);
  32. foreach($read_buffer_array as $address => $buf)
  33. {
  34. list($ip, $port) = explode(':',$address);
  35. $body_data = json_decode(trim($buf), true);
  36. $statistic_data = isset($body_data['statistic']) ? $body_data['statistic'] : '';
  37. $modules_data = isset($body_data['modules']) ? $body_data['modules'] : array();
  38. // 整理modules
  39. foreach($modules_data as $mod => $interfaces)
  40. {
  41. if(!isset(Cache::$modulesDataCache[$mod]))
  42. {
  43. Cache::$modulesDataCache[$mod] = array();
  44. }
  45. foreach($interfaces as $if)
  46. {
  47. Cache::$modulesDataCache[$mod][$if] = $if;
  48. }
  49. }
  50. Cache::$statisticDataCache['statistic'][$ip] = $statistic_data;
  51. }
  52. }
  53. function formatSt($str, $date)
  54. {
  55. // time:[suc_count:xx,suc_cost_time:xx,fail_count:xx,fail_cost_time:xx]
  56. $st_data = array();
  57. $st_explode = explode("\n", $str);
  58. // 汇总计算
  59. foreach($st_explode as $line)
  60. {
  61. // line = IP time suc_count suc_cost_time fail_count fail_cost_time code_json
  62. $line_data = explode("\t", $line);
  63. if(!isset($line_data[5]))
  64. {
  65. continue;
  66. }
  67. $time_line = $line_data[1];
  68. $suc_count = $line_data[2];
  69. $suc_cost_time = $line_data[3];
  70. $fail_count = $line_data[4];
  71. $fail_cost_time = $line_data[5];
  72. if(!isset($st_data[$time_line]))
  73. {
  74. $st_data[$time_line] = array('suc_count'=>0, 'suc_cost_time'=>0, 'fail_count'=>0, 'fail_cost_time'=>0);
  75. }
  76. $st_data[$time_line]['suc_count'] += $suc_count;
  77. $st_data[$time_line]['suc_cost_time'] += $suc_cost_time;
  78. $st_data[$time_line]['fail_count'] += $fail_count;
  79. $st_data[$time_line]['fail_cost_time'] += $fail_cost_time;
  80. }
  81. // 按照时间排序
  82. ksort($st_data);
  83. // time => [total_count:xx,suc_count:xx,suc_avg_time:xx,fail_count:xx,fail_avg_time:xx,percent:xx]
  84. $data = array();
  85. // 计算成功率 耗时
  86. foreach($st_data as $time_line=>$item)
  87. {
  88. $data[$time_line] = array(
  89. 'time' => date('Y-m-d H:i:s', $time_line),
  90. 'total_count' => $item['suc_count']+$item['fail_count'],
  91. '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),
  92. 'suc_count' => $item['suc_count'],
  93. 'suc_avg_time' => $item['suc_count'] == 0 ? $item['suc_count'] : round($item['suc_cost_time']/$item['suc_count'], 4),
  94. 'fail_count' => $item['fail_count'],
  95. 'fail_avg_time' => $item['fail_count'] == 0 ? 0 : round($item['fail_cost_time']/$item['fail_count'], 4),
  96. 'precent' => $item['suc_count']+$item['fail_count'] == 0 ? 0 : round(($item['suc_count']*100/($item['suc_count']+$item['fail_count'])), 4),
  97. );
  98. }
  99. $time_point = strtotime($date);
  100. for($i=0;$i<288;$i++)
  101. {
  102. $data[$time_point] = isset($data[$time_point]) ? $data[$time_point] :
  103. array(
  104. 'time' => date('Y-m-d H:i:s', $time_point),
  105. 'total_count' => 0,
  106. 'total_avg_time'=> 0,
  107. 'suc_count' => 0,
  108. 'suc_avg_time' => 0,
  109. 'fail_count' => 0,
  110. 'fail_avg_time' => 0,
  111. 'precent' => 100,
  112. );
  113. $time_point +=300;
  114. }
  115. ksort($data);
  116. return $data;
  117. }