StatisticsWidget.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Module\Admin\AdminWidgets;
  3. use Dcat\Admin\Widgets\Widget;
  4. /**
  5. * 统计信息小部件
  6. */
  7. class StatisticsWidget extends Widget
  8. {
  9. /**
  10. * @var array
  11. */
  12. protected array $statistics;
  13. public function __construct(array $statistics)
  14. {
  15. $this->statistics = $statistics;
  16. }
  17. /**
  18. * 渲染小部件
  19. *
  20. * @return string
  21. */
  22. public function render(): string
  23. {
  24. $stats = [
  25. [
  26. 'title' => '在线用户',
  27. 'value' => $this->statistics['users_online'] ?? 0,
  28. 'icon' => 'fa-users',
  29. 'color' => 'primary',
  30. 'description' => '当前在线用户数量',
  31. ],
  32. [
  33. 'title' => '总请求数',
  34. 'value' => number_format($this->statistics['total_requests'] ?? 0),
  35. 'icon' => 'fa-chart-bar',
  36. 'color' => 'success',
  37. 'description' => '今日总请求数量',
  38. ],
  39. [
  40. 'title' => '错误率',
  41. 'value' => number_format($this->statistics['error_rate'] ?? 0, 2) . '%',
  42. 'icon' => 'fa-exclamation-triangle',
  43. 'color' => $this->getErrorRateColor($this->statistics['error_rate'] ?? 0),
  44. 'description' => '今日错误率',
  45. ],
  46. [
  47. 'title' => '响应时间',
  48. 'value' => number_format($this->statistics['response_time'] ?? 0, 0) . 'ms',
  49. 'icon' => 'fa-clock',
  50. 'color' => $this->getResponseTimeColor($this->statistics['response_time'] ?? 0),
  51. 'description' => '平均响应时间',
  52. ],
  53. ];
  54. $html = '<div class="row">';
  55. foreach ($stats as $stat) {
  56. $html .= $this->buildStatCard($stat);
  57. }
  58. $html .= '</div>';
  59. return $html;
  60. }
  61. /**
  62. * 构建统计卡片
  63. *
  64. * @param array $stat
  65. * @return string
  66. */
  67. protected function buildStatCard(array $stat): string
  68. {
  69. $html = '<div class="col-lg-3 col-md-6 col-sm-6 col-12">';
  70. $html .= '<div class="card card-statistic-1">';
  71. $html .= '<div class="card-icon bg-' . $stat['color'] . '">';
  72. $html .= '<i class="fa ' . $stat['icon'] . '"></i>';
  73. $html .= '</div>';
  74. $html .= '<div class="card-wrap">';
  75. $html .= '<div class="card-header">';
  76. $html .= '<h4>' . $stat['title'] . '</h4>';
  77. $html .= '</div>';
  78. $html .= '<div class="card-body">';
  79. $html .= '<div class="d-flex justify-content-between align-items-center">';
  80. $html .= '<div>';
  81. $html .= '<h2 class="mb-0">' . $stat['value'] . '</h2>';
  82. $html .= '<small class="text-muted">' . $stat['description'] . '</small>';
  83. $html .= '</div>';
  84. $html .= '</div>';
  85. $html .= '</div>';
  86. $html .= '</div>';
  87. $html .= '</div>';
  88. $html .= '</div>';
  89. return $html;
  90. }
  91. /**
  92. * 根据错误率获取颜色
  93. *
  94. * @param float $errorRate
  95. * @return string
  96. */
  97. protected function getErrorRateColor(float $errorRate): string
  98. {
  99. if ($errorRate < 1) {
  100. return 'success';
  101. } elseif ($errorRate < 5) {
  102. return 'warning';
  103. } else {
  104. return 'danger';
  105. }
  106. }
  107. /**
  108. * 根据响应时间获取颜色
  109. *
  110. * @param float $responseTime
  111. * @return string
  112. */
  113. protected function getResponseTimeColor(float $responseTime): string
  114. {
  115. if ($responseTime < 500) {
  116. return 'success';
  117. } elseif ($responseTime < 1000) {
  118. return 'warning';
  119. } else {
  120. return 'danger';
  121. }
  122. }
  123. }