| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- namespace App\Module\ThirdParty\Metrics;
- use Dcat\Admin\Widgets\Metrics\Card;
- use Illuminate\Http\Request;
- use App\Module\ThirdParty\Models\ThirdPartyService;
- use App\Module\ThirdParty\Models\ThirdPartyCredential;
- use App\Module\ThirdParty\Models\ThirdPartyLog;
- use App\Module\ThirdParty\Enums\SERVICE_STATUS;
- /**
- * 第三方服务概览统计卡片
- */
- class ServiceOverviewCard extends Card
- {
- /**
- * 卡片高度
- *
- * @var int
- */
- protected $height = 200;
- /**
- * 初始化卡片
- */
- protected function init()
- {
- parent::init();
- $this->title('第三方服务概览');
- $this->dropdown([
- 'services' => '服务统计',
- 'credentials' => '凭证统计',
- 'logs' => '日志统计',
- ]);
- }
- /**
- * 处理请求
- *
- * @param Request $request
- * @return void
- */
- public function handle(Request $request)
- {
- $option = $request->get('option', 'services');
- switch ($option) {
- case 'credentials':
- $this->handleCredentialsStats();
- break;
- case 'logs':
- $this->handleLogsStats();
- break;
- case 'services':
- default:
- $this->handleServicesStats();
- break;
- }
- }
- /**
- * 处理服务统计
- */
- protected function handleServicesStats()
- {
- $totalServices = ThirdPartyService::count();
- $activeServices = ThirdPartyService::where('status', SERVICE_STATUS::ACTIVE->value)->count();
- $healthyServices = ThirdPartyService::where('health_status', 'HEALTHY')->count();
- $inactiveServices = $totalServices - $activeServices;
- $this->withContent([
- ['label' => '总服务数', 'value' => $totalServices, 'color' => 'primary'],
- ['label' => '激活服务', 'value' => $activeServices, 'color' => 'success'],
- ['label' => '健康服务', 'value' => $healthyServices, 'color' => 'info'],
- ['label' => '未激活', 'value' => $inactiveServices, 'color' => 'secondary'],
- ]);
- }
- /**
- * 处理凭证统计
- */
- protected function handleCredentialsStats()
- {
- $totalCredentials = ThirdPartyCredential::count();
- $activeCredentials = ThirdPartyCredential::where('is_active', true)->count();
- $inactiveCredentials = $totalCredentials - $activeCredentials;
- $this->withContent([
- ['label' => '总凭证数', 'value' => $totalCredentials, 'color' => 'primary'],
- ['label' => '激活凭证', 'value' => $activeCredentials, 'color' => 'success'],
- ['label' => '未激活', 'value' => $inactiveCredentials, 'color' => 'secondary'],
- ]);
- }
- /**
- * 处理日志统计
- */
- protected function handleLogsStats()
- {
- $totalLogs = ThirdPartyLog::count();
- $todayLogs = ThirdPartyLog::whereDate('created_at', today())->count();
- $errorLogs = ThirdPartyLog::where('level', 'ERROR')->count();
- $this->withContent([
- ['label' => '总调用次数', 'value' => number_format($totalLogs), 'color' => 'primary'],
- ['label' => '今日调用', 'value' => number_format($todayLogs), 'color' => 'info'],
- ['label' => '错误调用', 'value' => number_format($errorLogs), 'color' => 'danger'],
- ]);
- }
- /**
- * 设置卡片内容
- *
- * @param array $stats
- * @return $this
- */
- public function withContent(array $stats)
- {
- $html = '<div class="row">';
-
- foreach ($stats as $stat) {
- $html .= <<<HTML
- <div class="col-6 mb-3">
- <div class="d-flex align-items-center">
- <div class="flex-grow-1">
- <div class="text-{$stat['color']} font-weight-bold">{$stat['value']}</div>
- <div class="text-muted small">{$stat['label']}</div>
- </div>
- </div>
- </div>
- HTML;
- }
-
- $html .= '</div>';
- return $this->content($html);
- }
- }
|