statistics = $statistics; } /** * 渲染小部件 * * @return string */ public function render(): string { $stats = [ [ 'title' => '在线用户', 'value' => $this->statistics['users_online'] ?? 0, 'icon' => 'fa-users', 'color' => 'primary', 'description' => '当前在线用户数量', ], [ 'title' => '总请求数', 'value' => number_format($this->statistics['total_requests'] ?? 0), 'icon' => 'fa-chart-bar', 'color' => 'success', 'description' => '今日总请求数量', ], [ 'title' => '错误率', 'value' => number_format($this->statistics['error_rate'] ?? 0, 2) . '%', 'icon' => 'fa-exclamation-triangle', 'color' => $this->getErrorRateColor($this->statistics['error_rate'] ?? 0), 'description' => '今日错误率', ], [ 'title' => '响应时间', 'value' => number_format($this->statistics['response_time'] ?? 0, 0) . 'ms', 'icon' => 'fa-clock', 'color' => $this->getResponseTimeColor($this->statistics['response_time'] ?? 0), 'description' => '平均响应时间', ], ]; $html = '
'; foreach ($stats as $stat) { $html .= $this->buildStatCard($stat); } $html .= '
'; return $html; } /** * 构建统计卡片 * * @param array $stat * @return string */ protected function buildStatCard(array $stat): string { $html = '
'; $html .= '
'; $html .= '
'; $html .= ''; $html .= '
'; $html .= '
'; $html .= '
'; $html .= '

' . $stat['title'] . '

'; $html .= '
'; $html .= '
'; $html .= '
'; $html .= '
'; $html .= '

' . $stat['value'] . '

'; $html .= '' . $stat['description'] . ''; $html .= '
'; $html .= '
'; $html .= '
'; $html .= '
'; $html .= '
'; $html .= '
'; return $html; } /** * 根据错误率获取颜色 * * @param float $errorRate * @return string */ protected function getErrorRateColor(float $errorRate): string { if ($errorRate < 1) { return 'success'; } elseif ($errorRate < 5) { return 'warning'; } else { return 'danger'; } } /** * 根据响应时间获取颜色 * * @param float $responseTime * @return string */ protected function getResponseTimeColor(float $responseTime): string { if ($responseTime < 500) { return 'success'; } elseif ($responseTime < 1000) { return 'warning'; } else { return 'danger'; } } }