ServiceOverviewCard.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace App\Module\ThirdParty\Metrics;
  3. use Dcat\Admin\Widgets\Metrics\Card;
  4. use Illuminate\Http\Request;
  5. use App\Module\ThirdParty\Models\ThirdPartyService;
  6. use App\Module\ThirdParty\Models\ThirdPartyCredential;
  7. use App\Module\ThirdParty\Models\ThirdPartyLog;
  8. use App\Module\ThirdParty\Enums\SERVICE_STATUS;
  9. /**
  10. * 第三方服务概览统计卡片
  11. */
  12. class ServiceOverviewCard extends Card
  13. {
  14. /**
  15. * 卡片高度
  16. *
  17. * @var int
  18. */
  19. protected $height = 200;
  20. /**
  21. * 初始化卡片
  22. */
  23. protected function init()
  24. {
  25. parent::init();
  26. $this->title('第三方服务概览');
  27. $this->dropdown([
  28. 'services' => '服务统计',
  29. 'credentials' => '凭证统计',
  30. 'logs' => '日志统计',
  31. ]);
  32. }
  33. /**
  34. * 处理请求
  35. *
  36. * @param Request $request
  37. * @return void
  38. */
  39. public function handle(Request $request)
  40. {
  41. $option = $request->get('option', 'services');
  42. switch ($option) {
  43. case 'credentials':
  44. $this->handleCredentialsStats();
  45. break;
  46. case 'logs':
  47. $this->handleLogsStats();
  48. break;
  49. case 'services':
  50. default:
  51. $this->handleServicesStats();
  52. break;
  53. }
  54. }
  55. /**
  56. * 处理服务统计
  57. */
  58. protected function handleServicesStats()
  59. {
  60. $totalServices = ThirdPartyService::count();
  61. $activeServices = ThirdPartyService::where('status', SERVICE_STATUS::ACTIVE->value)->count();
  62. $healthyServices = ThirdPartyService::where('health_status', 'HEALTHY')->count();
  63. $inactiveServices = $totalServices - $activeServices;
  64. $this->withContent([
  65. ['label' => '总服务数', 'value' => $totalServices, 'color' => 'primary'],
  66. ['label' => '激活服务', 'value' => $activeServices, 'color' => 'success'],
  67. ['label' => '健康服务', 'value' => $healthyServices, 'color' => 'info'],
  68. ['label' => '未激活', 'value' => $inactiveServices, 'color' => 'secondary'],
  69. ]);
  70. }
  71. /**
  72. * 处理凭证统计
  73. */
  74. protected function handleCredentialsStats()
  75. {
  76. $totalCredentials = ThirdPartyCredential::count();
  77. $activeCredentials = ThirdPartyCredential::where('is_active', true)->count();
  78. $inactiveCredentials = $totalCredentials - $activeCredentials;
  79. $this->withContent([
  80. ['label' => '总凭证数', 'value' => $totalCredentials, 'color' => 'primary'],
  81. ['label' => '激活凭证', 'value' => $activeCredentials, 'color' => 'success'],
  82. ['label' => '未激活', 'value' => $inactiveCredentials, 'color' => 'secondary'],
  83. ]);
  84. }
  85. /**
  86. * 处理日志统计
  87. */
  88. protected function handleLogsStats()
  89. {
  90. $totalLogs = ThirdPartyLog::count();
  91. $todayLogs = ThirdPartyLog::whereDate('created_at', today())->count();
  92. $errorLogs = ThirdPartyLog::where('level', 'ERROR')->count();
  93. $this->withContent([
  94. ['label' => '总调用次数', 'value' => number_format($totalLogs), 'color' => 'primary'],
  95. ['label' => '今日调用', 'value' => number_format($todayLogs), 'color' => 'info'],
  96. ['label' => '错误调用', 'value' => number_format($errorLogs), 'color' => 'danger'],
  97. ]);
  98. }
  99. /**
  100. * 设置卡片内容
  101. *
  102. * @param array $stats
  103. * @return $this
  104. */
  105. public function withContent(array $stats)
  106. {
  107. $html = '<div class="row">';
  108. foreach ($stats as $stat) {
  109. $html .= <<<HTML
  110. <div class="col-6 mb-3">
  111. <div class="d-flex align-items-center">
  112. <div class="flex-grow-1">
  113. <div class="text-{$stat['color']} font-weight-bold">{$stat['value']}</div>
  114. <div class="text-muted small">{$stat['label']}</div>
  115. </div>
  116. </div>
  117. </div>
  118. HTML;
  119. }
  120. $html .= '</div>';
  121. return $this->content($html);
  122. }
  123. }