/** * 缺省动作 */ public function actionIndex() { $module = $this->request->query('module'); $interface = $this->request->query('interface'); $startTime = $this->request->query('start_time'); $endTime = $this->request->query('end_time'); $count = $this->request->query('count'); $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>'; } } } $logDataArray = $this->getStasticLog($module, $interface, $startTime, $endTime, $count); unset($_GET['ip'], $_GET['offset']); $logStr = ''; foreach ($logDataArray as $address => $log_data) { $temp = explode(':', $address); $ip = array_shift($temp); $logStr .= $log_data['data']; $_GET['ip'][] = $ip; $_GET['offset'][] = $log_data['offset']; } $logStr = nl2br(str_replace("\n", "\n\n", $logStr)); $nextPageUrl = http_build_query($_GET); $logStr .= '</br><center><a href="' . Route::url('stat-web', ['controller' => 'Logger']) . '?' . $nextPageUrl . '">下一页</a></center>'; $this->template->set('content', View::factory('logger/index', ['logStr' => $logStr])); }
/** * 获取视图的最终输入 * * @param string $viewFilename 文件名 * @param array $viewData 变量 * @return string * @throws Exception */ protected function capture($viewFilename, array $viewData) { //使用单件模式实例化模板类 $template = Template::getInstance(); $template->setOptions($this->_templateOptions); // 附加模板后缀 $viewFilename .= $this->_templateOptions['template_ext']; // 获取模板解析后的完整路径 $file = $template->getFile($viewFilename); return parent::capture($file, $viewData); }
/** * 初始化,并加载模板对象 */ public function before() { parent::before(); if ($this->autoRender) { Base::getLog()->debug(__METHOD__ . ' enable auto render'); if (!$this->template instanceof ViewInterface) { Base::getLog()->debug(__METHOD__ . ' create template view instance', ['template' => $this->template]); $this->template = View::factory($this->template); } } else { Base::getLog()->debug(__METHOD__ . ' disable auto render'); } }
<?php use tourze\Base\Config; use tourze\Route\Route; use tourze\StatServer\Cache; use tourze\View\View; if (is_file(__DIR__ . '/vendor/autoload.php')) { require_once __DIR__ . '/vendor/autoload.php'; } ini_set('display_errors', 'on'); if (!defined('ROOT_PATH')) { define('ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR); } if (!defined('WEB_PATH')) { define('WEB_PATH', ROOT_PATH . 'web' . DIRECTORY_SEPARATOR); } Config::addPath(ROOT_PATH . 'config' . DIRECTORY_SEPARATOR); View::addPath(ROOT_PATH . 'view' . DIRECTORY_SEPARATOR); Cache::$serverIpList = Config::load('statServer')->get('serverIpList'); // 指定控制器命名空间 Route::$defaultNamespace = '\\tourze\\StatServer\\Controller\\'; Route::set('stat-web', '(<controller>(/<action>(/<id>)))')->defaults(['controller' => 'Main', 'action' => 'index']); // 创建类别名 @class_alias('\\tourze\\StatServer\\Protocol\\Statistic', '\\Workerman\\Protocols\\Statistic');
/** * 入口 */ 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])); }