| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- <?php
- namespace App\Module\ThirdParty\AdminControllers;
- use UCore\DcatAdmin\AdminController;
- use App\Module\ThirdParty\Models\ThirdPartyQuota;
- use App\Module\ThirdParty\Models\ThirdPartyService;
- use App\Module\ThirdParty\Repositorys\ThirdPartyQuotaRepository;
- use App\Module\ThirdParty\Enums\QUOTA_TYPE;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- /**
- * 第三方服务配额管理控制器
- *
- * 路由: /admin/thirdparty/quotas
- */
- class ThirdPartyQuotaController extends AdminController
- {
- /**
- * 页面标题
- *
- * @var string
- */
- protected $title = '配额管理';
- /**
- * 数据仓库
- *
- * @return string
- */
- protected function repository()
- {
- return ThirdPartyQuotaRepository::class;
- }
- /**
- * 列表页面
- *
- * @return Grid
- */
- protected function grid(): Grid
- {
- return Grid::make(new ThirdPartyQuotaRepository(), function (Grid $grid) {
- // 基础设置
- $grid->column('id', 'ID')->sortable();
-
- // 关联服务
- $grid->column('service.name', '服务名称')->sortable();
- $grid->column('service.code', '服务代码');
-
- // 配额类型
- $grid->column('type', '配额类型')->display(function ($type) {
- $quotaType = QUOTA_TYPE::tryFrom($type);
- if ($quotaType) {
- $label = $quotaType->getLabel();
- $color = $quotaType->getColor();
- return "<span class='badge badge-{$color}'>{$label}</span>";
- }
- return $type;
- });
- // 配额限制
- $grid->column('limit_value', '配额限制')->display(function ($limit) {
- return number_format($limit);
- })->sortable();
- // 已使用量
- $grid->column('used_value', '已使用')->display(function ($used) {
- return number_format($used);
- })->sortable();
- // 使用率
- $grid->column('usage_percentage', '使用率')->display(function () {
- $percentage = $this->limit_value > 0 ?
- ($this->used_value / $this->limit_value) * 100 : 0;
-
- $color = 'success';
- if ($percentage >= 95) {
- $color = 'danger';
- } elseif ($percentage >= 80) {
- $color = 'warning';
- } elseif ($percentage >= 60) {
- $color = 'info';
- }
-
- return "<div class='progress' style='height: 20px;'>
- <div class='progress-bar bg-{$color}' style='width: {$percentage}%'>
- " . number_format($percentage, 1) . "%
- </div>
- </div>";
- });
- // 剩余配额
- $grid->column('remaining', '剩余配额')->display(function () {
- $remaining = max(0, $this->limit_value - $this->used_value);
- $color = $remaining > 0 ? 'text-success' : 'text-danger';
- return "<span class='{$color}'>" . number_format($remaining) . "</span>";
- });
- // 时间窗口
- $grid->column('window_start', '窗口开始')->display(function ($time) {
- return $time ? \Carbon\Carbon::parse($time)->format('m-d H:i') : '-';
- });
-
- $grid->column('window_end', '窗口结束')->display(function ($time) {
- return $time ? \Carbon\Carbon::parse($time)->format('m-d H:i') : '-';
- });
- // 重置时间
- $grid->column('reset_at', '下次重置')->display(function ($time) {
- if (!$time) {
- return '-';
- }
-
- $resetTime = \Carbon\Carbon::parse($time);
- if ($resetTime->isPast()) {
- return '<span class="badge badge-warning">待重置</span>';
- } else {
- return $resetTime->diffForHumans();
- }
- });
- // 告警阈值
- $grid->column('alert_threshold', '告警阈值')->display(function ($threshold) {
- return $threshold . '%';
- });
- // 状态
- $grid->column('is_active', '状态')->display(function ($active) {
- return $active ?
- '<span class="badge badge-success"><i class="fa fa-check"></i> 激活</span>' :
- '<span class="badge badge-secondary"><i class="fa fa-times"></i> 未激活</span>';
- });
- // 超限状态
- $grid->column('is_exceeded', '超限状态')->display(function ($exceeded) {
- return $exceeded ?
- '<span class="badge badge-danger"><i class="fa fa-exclamation-triangle"></i> 已超限</span>' :
- '<span class="badge badge-success"><i class="fa fa-check"></i> 正常</span>';
- });
- // 过滤器
- $grid->filter(function (Grid\Filter $filter) {
- $filter->equal('service_id', '服务')->select(
- ThirdPartyService::pluck('name', 'id')->toArray()
- );
- $filter->equal('type', '配额类型')->select(QUOTA_TYPE::getOptions());
- $filter->equal('is_active', '状态')->select([
- 1 => '激活',
- 0 => '未激活',
- ]);
- $filter->equal('is_exceeded', '超限状态')->select([
- 1 => '已超限',
- 0 => '正常',
- ]);
- $filter->between('alert_threshold', '告警阈值(%)');
- $filter->between('used_value', '已使用量');
- });
- // 批量操作
- $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
- // TODO: 创建批量操作类
- // $batch->add('批量重置', new \App\Module\ThirdParty\AdminActions\BatchResetQuotaAction());
- // $batch->add('批量激活', new \App\Module\ThirdParty\AdminActions\BatchActivateQuotaAction());
- // $batch->add('批量停用', new \App\Module\ThirdParty\AdminActions\BatchDeactivateQuotaAction());
- });
- // 工具栏
- $grid->tools(function (Grid\Tools $tools) {
- $tools->append('<a href="/admin/thirdparty/quotas/exceeded" class="btn btn-sm btn-danger"><i class="fa fa-exclamation-triangle"></i> 超限配额</a>');
- $tools->append('<a href="/admin/thirdparty/quotas/reset-all" class="btn btn-sm btn-warning"><i class="fa fa-redo"></i> 重置所有</a>');
- $tools->append('<a href="/admin/thirdparty/quotas/stats" class="btn btn-sm btn-info"><i class="fa fa-chart-bar"></i> 使用统计</a>');
- });
- // 行操作
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $actions->append('<a href="/admin/thirdparty/quotas/' . $actions->getKey() . '/reset" class="btn btn-xs btn-warning"><i class="fa fa-redo"></i> 重置</a>');
-
- if ($actions->row->is_exceeded) {
- $actions->append('<a href="/admin/thirdparty/quotas/' . $actions->getKey() . '/increase" class="btn btn-xs btn-success"><i class="fa fa-plus"></i> 增加配额</a>');
- }
-
- $actions->append('<a href="/admin/thirdparty/quotas/' . $actions->getKey() . '/usage" class="btn btn-xs btn-info"><i class="fa fa-chart-line"></i> 使用详情</a>');
- });
- // 默认排序
- $grid->model()->orderBy('service_id')->orderBy('type');
- });
- }
- /**
- * 详情页面
- *
- * @return Show
- */
- protected function detail($id): Show
- {
- return Show::make($id, new ThirdPartyQuotaRepository(), function (Show $show) {
- $show->field('id', 'ID');
-
- $show->field('service.name', '服务名称');
- $show->field('service.code', '服务代码');
-
- $show->field('type', '配额类型')->as(function ($type) {
- $quotaType = QUOTA_TYPE::tryFrom($type);
- return $quotaType ? $quotaType->getLabel() : $type;
- });
- $show->field('limit_value', '配额限制')->as(function ($limit) {
- return number_format($limit);
- });
- $show->field('used_value', '已使用量')->as(function ($used) {
- return number_format($used);
- });
- $show->field('usage_percentage', '使用率')->as(function () {
- $percentage = $this->limit_value > 0 ?
- ($this->used_value / $this->limit_value) * 100 : 0;
- return number_format($percentage, 2) . '%';
- });
- $show->field('remaining', '剩余配额')->as(function () {
- $remaining = max(0, $this->limit_value - $this->used_value);
- return number_format($remaining);
- });
- $show->field('window_start', '时间窗口开始');
- $show->field('window_end', '时间窗口结束');
- $show->field('reset_at', '下次重置时间');
- $show->field('alert_threshold', '告警阈值')->as(function ($threshold) {
- return $threshold . '%';
- });
- $show->field('is_active', '状态')->as(function ($active) {
- return $active ? '激活' : '未激活';
- });
- $show->field('is_exceeded', '超限状态')->as(function ($exceeded) {
- return $exceeded ? '已超限' : '正常';
- });
- $show->field('exceeded_at', '超限时间');
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- });
- }
- /**
- * 表单页面
- *
- * @return Form
- */
- protected function form(): Form
- {
- return Form::make(new ThirdPartyQuotaRepository(), function (Form $form) {
- $form->display('id', 'ID');
- $form->select('service_id', '所属服务')
- ->options(ThirdPartyService::pluck('name', 'id')->toArray())
- ->required()
- ->help('选择要配置配额的第三方服务');
- $form->select('type', '配额类型')
- ->options(QUOTA_TYPE::getOptions())
- ->required()
- ->help('选择配额的时间窗口类型');
- $form->number('limit_value', '配额限制')
- ->required()
- ->min(1)
- ->help('在指定时间窗口内允许的最大调用次数');
- $form->number('alert_threshold', '告警阈值(%)')
- ->default(80)
- ->min(1)
- ->max(100)
- ->help('当使用率达到此百分比时触发告警');
- $form->switch('is_active', '是否激活')->default(1);
- $form->datetime('window_start', '时间窗口开始')->help('留空自动计算');
- $form->datetime('window_end', '时间窗口结束')->help('留空自动计算');
- $form->datetime('reset_at', '下次重置时间')->help('留空自动计算');
- $form->display('used_value', '已使用量');
- $form->display('is_exceeded', '超限状态');
- $form->display('exceeded_at', '超限时间');
- $form->display('created_at', '创建时间');
- $form->display('updated_at', '更新时间');
- // 保存前处理
- $form->saving(function (Form $form) {
- // 自动计算时间窗口
- if (empty($form->window_start) || empty($form->window_end)) {
- $quotaType = QUOTA_TYPE::from($form->type);
- $form->window_start = $quotaType->getCurrentWindowStart()->format('Y-m-d H:i:s');
- $form->window_end = $quotaType->getCurrentWindowEnd()->format('Y-m-d H:i:s');
- $form->reset_at = $form->window_end;
- }
- });
- });
- }
- }
|