public function getStasticLog($module, $interface, $startTime, $endTime, $count = 10) { $ipList = !empty($_GET['ip']) && is_array($_GET['ip']) ? $_GET['ip'] : Cache::$serverIpList; $offsetList = !empty($_GET['offset']) && is_array($_GET['offset']) ? $_GET['offset'] : []; $port = Config::load('statServer')->get('providerPort'); $requestBufferArray = []; foreach ($ipList as $key => $ip) { $offset = isset($offsetList[$key]) ? $offsetList[$key] : 0; $buffer = ['cmd' => 'get-log', 'module' => $module, 'interface' => $interface, 'start_time' => $startTime, 'end_time' => $endTime, 'offset' => $offset, 'count' => $count]; Base::getLog()->info(__METHOD__ . ' generate buffer fot getting log', $buffer); $requestBufferArray["{$ip}:{$port}"] = json_encode($buffer) . "\n"; } $readBufferArray = StatServer::multiRequest($requestBufferArray); ksort($readBufferArray); foreach ($readBufferArray as $address => $buf) { $bodyData = json_decode(trim($buf), true); $logData = isset($bodyData['data']) ? $bodyData['data'] : ''; $offset = isset($bodyData['offset']) ? $bodyData['offset'] : 0; $readBufferArray[$address] = ['offset' => $offset, 'data' => $logData]; } return $readBufferArray; }
/** * 入口 */ public function actionIndex() { $date = $this->request->query('date'); if (!$date) { $date = date('Y-m-d'); } $errorMsg = $noticeMsg = ''; $module = 'WorkerMan'; $interface = 'Statistics'; $today = date('Y-m-d'); $timeNow = time(); StatServer::multiRequestStAndModules($module, $interface, $date); $allStr = ''; if (is_array(Cache::$statisticData['statistic'])) { foreach (Cache::$statisticData['statistic'] as $ip => $st_str) { $allStr .= $st_str; } } $codeMap = []; $data = StatServer::formatStatLog($allStr, $date, $codeMap); $successSeriesData = $failSeriesData = $successTimeSeriesData = $failTimeSeriesData = []; $totalCount = $fail_count = 0; foreach ($data as $time_point => $item) { if ($item['total_count']) { $successSeriesData[] = "[" . $time_point * 1000 . ",{$item['total_count']}]"; $totalCount += $item['total_count']; } $failSeriesData[] = "[" . $time_point * 1000 . ",{$item['fail_count']}]"; $fail_count += $item['fail_count']; if ($item['total_avg_time']) { $successTimeSeriesData[] = "[" . $time_point * 1000 . ",{$item['total_avg_time']}]"; } $failTimeSeriesData[] = "[" . $time_point * 1000 . ",{$item['fail_avg_time']}]"; } $successSeriesData = implode(',', $successSeriesData); $failSeriesData = implode(',', $failSeriesData); $successTimeSeriesData = implode(',', $successTimeSeriesData); $failTimeSeriesData = implode(',', $failTimeSeriesData); // 总体成功率 $globalRate = $totalCount ? round(($totalCount - $fail_count) / $totalCount * 100, 4) : 100; // 返回码分布 $codePieData = ''; $codePieArray = []; unset($codeMap[0]); if (empty($codeMap)) { $codeMap[0] = $totalCount > 0 ? $totalCount : 1; } if (is_array($codeMap)) { $total_item_count = array_sum($codeMap); foreach ($codeMap as $code => $count) { $codePieArray[] = "[\"{$code}:{$count}个\", " . round($count * 100 / $total_item_count, 4) . "]"; } $codePieData = implode(',', $codePieArray); } unset($_GET['start_time'], $_GET['end_time'], $_GET['date'], $_GET['fn']); $query = http_build_query($_GET); // 删除末尾0的记录 if ($today == $date) { while (!empty($data) && ($item = end($data)) && $item['total_count'] == 0 && ($key = key($data)) && $timeNow < $key) { unset($data[$key]); } } if (Cache::$lastFailedIpArray) { $errorMsg = '<strong>无法从以下数据源获取数据:</strong>'; foreach (Cache::$lastFailedIpArray as $ip) { $errorMsg .= $ip . ':' . Config::load('statServer')->get('providerPort') . ' '; } } $this->template->set('content', View::factory('main/index', ['date' => $date, 'query' => $query, 'interfaceName' => '整体', 'errorMsg' => $errorMsg, 'noticeMsg' => $noticeMsg, 'data' => $data, 'successSeriesData' => $successSeriesData, 'failSeriesData' => $failSeriesData, 'successTimeSeriesData' => $successTimeSeriesData, 'failTimeSeriesData' => $failTimeSeriesData, 'globalRate' => $globalRate, 'codePieData' => $codePieData])); }
/** * 默认动作 */ public function actionIndex() { $module = $this->request->query('module'); $interface = $this->request->query('interface'); $date = $this->request->query('date'); if (!$date) { $date = date('Y-m-d'); } $errorMsg = ''; $today = date('Y-m-d'); $timeNow = time(); StatServer::multiRequestStAndModules($module, $interface, $date); $allStr = ''; if (is_array(Cache::$statisticData['statistic'])) { foreach (Cache::$statisticData['statistic'] as $ip => $st_str) { $allStr .= $st_str; } } $codeMap = []; $data = StatServer::formatStatLog($allStr, $date, $codeMap); $interfaceName = "{$module}::{$interface}"; $successSeriesData = $failSeriesData = $successTimeSeriesData = $failTimeSeriesData = []; $totalCount = $failCount = 0; foreach ($data as $timePoint => $item) { if ($item['total_count']) { $successSeriesData[] = "[" . $timePoint * 1000 . ",{$item['total_count']}]"; $totalCount += $item['total_count']; } $failSeriesData[] = "[" . $timePoint * 1000 . ",{$item['fail_count']}]"; $failCount += $item['fail_count']; if ($item['total_avg_time']) { $successTimeSeriesData[] = "[" . $timePoint * 1000 . ",{$item['total_avg_time']}]"; } $failTimeSeriesData[] = "[" . $timePoint * 1000 . ",{$item['fail_avg_time']}]"; } $successSeriesData = implode(',', $successSeriesData); $failSeriesData = implode(',', $failSeriesData); $successTimeSeriesData = implode(',', $successTimeSeriesData); $failTimeSeriesData = implode(',', $failTimeSeriesData); unset($_GET['start_time'], $_GET['end_time'], $_GET['date'], $_GET['fn']); $query = http_build_query($_GET); // 删除末尾0的记录 if ($today == $date) { while (!empty($data) && ($item = end($data)) && $item['total_count'] == 0 && ($key = key($data)) && $timeNow < $key) { unset($data[$key]); } } $tableData = $htmlClass = ''; if ($data) { $firstLine = true; foreach ($data as $item) { if ($firstLine) { $firstLine = false; if ($item['total_count'] == 0) { continue; } } $htmlClass = 'class="danger"'; if ($item['total_count'] == 0) { $htmlClass = ''; } elseif ($item['percent'] >= 99.98999999999999) { $htmlClass = 'class="success"'; } elseif ($item['percent'] >= 99) { $htmlClass = ''; } elseif ($item['percent'] >= 98) { $htmlClass = 'class="warning"'; } $tableData .= "\n<tr {$htmlClass}>\n <td>{$item['time']}</td>\n <td>{$item['total_count']}</td>\n <td> {$item['total_avg_time']}</td>\n <td>{$item['success_count']}</td>\n <td>{$item['suc_avg_time']}</td>\n <td>" . ($item['fail_count'] > 0 ? "<a href='" . Route::url('stat-web', ['controller' => 'Logger']) . "?{$query}&start_time=" . (strtotime($item['time']) - 300) . "&end_time=" . strtotime($item['time']) . "'>{$item['fail_count']}</a>" : $item['fail_count']) . "</td>\n <td>{$item['fail_avg_time']}</td>\n <td>{$item['percent']}%</td>\n </tr>\n "; } } // date btn $dateBtnStr = ''; for ($i = 13; $i >= 1; $i--) { $theTime = strtotime("-{$i} day"); $theDate = date('Y-m-d', $theTime); $htmlTheDate = $date == $theDate ? "<b>{$theDate}</b>" : $theDate; $dateBtnStr .= '<a href="' . Route::url('stat-web', ['controller' => 'Statistic']) . '?date=' . "{$theDate}&{$query}" . '" class="btn ' . $htmlClass . '" type="button">' . $htmlTheDate . '</a>'; if ($i == 7) { $dateBtnStr .= '</br>'; } } $theDate = date('Y-m-d'); $htmlTheDate = $date == $theDate ? "<b>{$theDate}</b>" : $theDate; $dateBtnStr .= '<a href="' . Route::url('stat-web', ['controller' => 'Statistic']) . '?date=' . "{$theDate}&{$query}" . '" class="btn" type="button">' . $htmlTheDate . '</a>'; $moduleStr = ''; foreach (Cache::$moduleData as $mod => $interfaces) { if ($mod == 'WorkerMan') { continue; } $moduleStr .= '<li><a href="' . Route::url('stat-web', ['controller' => 'Statistic']) . '?module=' . $mod . '">' . $mod . '</a></li>'; if ($module == $mod) { foreach ($interfaces as $if) { $moduleStr .= '<li> <a href="' . Route::url('stat-web', ['controller' => 'Statistic']) . '?module=' . $mod . '&interface=' . $if . '">' . $if . '</a></li>'; } } } if (Cache::$lastFailedIpArray) { $errorMsg = '<strong>无法从以下数据源获取数据:</strong>'; foreach (Cache::$lastFailedIpArray as $ip) { $errorMsg .= $ip . '::' . Config::load('statServer')->get('providerPort') . ' '; } } $this->template->set('content', View::factory('statistic/index', ['errorMsg' => $errorMsg, 'date' => $date, 'module' => $module, 'interface' => $interface, 'interfaceName' => $interfaceName, 'moduleStr' => $moduleStr, 'successSeriesData' => $successSeriesData, 'failSeriesData' => $failSeriesData, 'successTimeSeriesData' => $successTimeSeriesData, 'failTimeSeriesData' => $failTimeSeriesData, 'tableData' => $tableData, 'dateBtnStr' => $dateBtnStr])); }