SystemStatusWidget.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace App\Module\Admin\AdminWidgets;
  3. use Dcat\Admin\Widgets\Card;
  4. /**
  5. * 系统状态小部件
  6. */
  7. class SystemStatusWidget extends Card
  8. {
  9. /**
  10. * @var array
  11. */
  12. protected array $systemData;
  13. public function __construct(array $systemData)
  14. {
  15. $this->systemData = $systemData;
  16. parent::__construct('系统状态', $this->buildContent());
  17. }
  18. /**
  19. * 构建内容
  20. *
  21. * @return string
  22. */
  23. protected function buildContent(): string
  24. {
  25. $html = '<div class="row">';
  26. // 服务器信息
  27. $html .= $this->buildServerInfo();
  28. // 性能指标
  29. $html .= $this->buildPerformanceMetrics();
  30. $html .= '</div>';
  31. return $html;
  32. }
  33. /**
  34. * 构建服务器信息
  35. *
  36. * @return string
  37. */
  38. protected function buildServerInfo(): string
  39. {
  40. $serverInfo = $this->systemData['server_info'] ?? [];
  41. $html = '<div class="col-md-6">';
  42. $html .= '<h6>服务器信息</h6>';
  43. $html .= '<table class="table table-sm table-borderless">';
  44. $items = [
  45. '操作系统' => $serverInfo['os'] ?? 'Unknown',
  46. '主机名' => $serverInfo['hostname'] ?? 'Unknown',
  47. '架构' => $serverInfo['architecture'] ?? 'Unknown',
  48. 'Web服务器' => $serverInfo['server_software'] ?? 'Unknown',
  49. ];
  50. foreach ($items as $label => $value) {
  51. $html .= "<tr><td class=\"text-muted\">{$label}:</td><td>{$value}</td></tr>";
  52. }
  53. $html .= '</table>';
  54. $html .= '</div>';
  55. return $html;
  56. }
  57. /**
  58. * 构建性能指标
  59. *
  60. * @return string
  61. */
  62. protected function buildPerformanceMetrics(): string
  63. {
  64. $performance = $this->systemData['performance'] ?? [];
  65. $html = '<div class="col-md-6">';
  66. $html .= '<h6>性能指标</h6>';
  67. // 内存使用率
  68. if (isset($performance['memory'])) {
  69. $memory = $performance['memory'];
  70. $percentage = $memory['percentage'];
  71. $progressClass = $this->getProgressClass($percentage);
  72. $html .= '<div class="mb-2">';
  73. $html .= '<small class="text-muted">内存使用率</small>';
  74. $html .= "<div class=\"progress\" style=\"height: 8px;\">";
  75. $html .= "<div class=\"progress-bar bg-{$progressClass}\" style=\"width: {$percentage}%\"></div>";
  76. $html .= "</div>";
  77. $html .= "<small>{$memory['current']} / {$memory['limit']} ({$percentage}%)</small>";
  78. $html .= '</div>';
  79. }
  80. // 磁盘使用率
  81. if (isset($performance['disk'])) {
  82. $disk = $performance['disk'];
  83. $percentage = $disk['percentage'];
  84. $progressClass = $this->getProgressClass($percentage);
  85. $html .= '<div class="mb-2">';
  86. $html .= '<small class="text-muted">磁盘使用率</small>';
  87. $html .= "<div class=\"progress\" style=\"height: 8px;\">";
  88. $html .= "<div class=\"progress-bar bg-{$progressClass}\" style=\"width: {$percentage}%\"></div>";
  89. $html .= "</div>";
  90. $html .= "<small>{$disk['used']} / {$disk['total']} ({$percentage}%)</small>";
  91. $html .= '</div>';
  92. }
  93. // CPU使用率
  94. if (isset($performance['cpu_usage'])) {
  95. $cpuUsage = $performance['cpu_usage'];
  96. $progressClass = $this->getProgressClass($cpuUsage);
  97. $html .= '<div class="mb-2">';
  98. $html .= '<small class="text-muted">CPU使用率</small>';
  99. $html .= "<div class=\"progress\" style=\"height: 8px;\">";
  100. $html .= "<div class=\"progress-bar bg-{$progressClass}\" style=\"width: {$cpuUsage}%\"></div>";
  101. $html .= "</div>";
  102. $html .= "<small>{$cpuUsage}%</small>";
  103. $html .= '</div>';
  104. }
  105. // 负载平均值
  106. if (isset($performance['load_average'])) {
  107. $load = $performance['load_average'];
  108. $html .= '<div class="mb-2">';
  109. $html .= '<small class="text-muted">负载平均值</small><br>';
  110. $html .= "<small>1分钟: {$load['1min']} | 5分钟: {$load['5min']} | 15分钟: {$load['15min']}</small>";
  111. $html .= '</div>';
  112. }
  113. // 数据库状态
  114. if (isset($performance['database'])) {
  115. $database = $performance['database'];
  116. $statusClass = $database['status'] === 'healthy' ? 'success' : 'danger';
  117. $statusText = $database['status'] === 'healthy' ? '正常' : '异常';
  118. $html .= '<div class="mb-2">';
  119. $html .= '<small class="text-muted">数据库状态</small><br>';
  120. $html .= "<span class=\"badge badge-{$statusClass}\">{$statusText}</span>";
  121. if (isset($database['response_time'])) {
  122. $html .= " <small class=\"text-muted\">响应时间: {$database['response_time']}</small>";
  123. }
  124. $html .= '</div>';
  125. }
  126. $html .= '</div>';
  127. return $html;
  128. }
  129. /**
  130. * 根据百分比获取进度条颜色类
  131. *
  132. * @param float $percentage
  133. * @return string
  134. */
  135. protected function getProgressClass(float $percentage): string
  136. {
  137. if ($percentage < 50) {
  138. return 'success';
  139. } elseif ($percentage < 80) {
  140. return 'warning';
  141. } else {
  142. return 'danger';
  143. }
  144. }
  145. }