ThirdPartyQuotaController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. namespace App\Module\ThirdParty\AdminControllers;
  3. use UCore\DcatAdmin\AdminController;
  4. use App\Module\ThirdParty\Models\ThirdPartyQuota;
  5. use App\Module\ThirdParty\Models\ThirdPartyService;
  6. use App\Module\ThirdParty\Repositorys\ThirdPartyQuotaRepository;
  7. use App\Module\ThirdParty\Enums\QUOTA_TYPE;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Show;
  11. use Spatie\RouteAttributes\Attributes\Resource;
  12. /**
  13. * 第三方服务配额管理控制器
  14. *
  15. * 路由: /admin/thirdparty/quotas
  16. */
  17. #[Resource('thirdparty/quotas', names: 'dcat.admin.thirdparty.quotas')]
  18. class ThirdPartyQuotaController extends AdminController
  19. {
  20. /**
  21. * 页面标题
  22. *
  23. * @var string
  24. */
  25. protected $title = '配额管理';
  26. /**
  27. * 数据仓库
  28. *
  29. * @return string
  30. */
  31. protected function repository()
  32. {
  33. return ThirdPartyQuotaRepository::class;
  34. }
  35. /**
  36. * 列表页面
  37. *
  38. * @return Grid
  39. */
  40. protected function grid(): Grid
  41. {
  42. return Grid::make(new ThirdPartyQuotaRepository(), function (Grid $grid) {
  43. // 基础设置
  44. $grid->column('id', 'ID')->sortable();
  45. // 关联服务
  46. $grid->column('service.name', '服务名称')->sortable();
  47. $grid->column('service.code', '服务代码');
  48. // 配额类型
  49. $grid->column('type', '配额类型')->display(function ($type) {
  50. $quotaType = QUOTA_TYPE::tryFrom($type);
  51. if ($quotaType) {
  52. $label = $quotaType->getLabel();
  53. $color = $quotaType->getColor();
  54. return "<span class='badge badge-{$color}'>{$label}</span>";
  55. }
  56. return $type;
  57. });
  58. // 配额限制
  59. $grid->column('limit_value', '配额限制')->display(function ($limit) {
  60. return number_format($limit);
  61. })->sortable();
  62. // 已使用量
  63. $grid->column('used_value', '已使用')->display(function ($used) {
  64. return number_format($used);
  65. })->sortable();
  66. // 使用率
  67. $grid->column('usage_percentage', '使用率')->display(function () {
  68. $percentage = $this->limit_value > 0 ?
  69. ($this->used_value / $this->limit_value) * 100 : 0;
  70. $color = 'success';
  71. if ($percentage >= 95) {
  72. $color = 'danger';
  73. } elseif ($percentage >= 80) {
  74. $color = 'warning';
  75. } elseif ($percentage >= 60) {
  76. $color = 'info';
  77. }
  78. return "<div class='progress' style='height: 20px;'>
  79. <div class='progress-bar bg-{$color}' style='width: {$percentage}%'>
  80. " . number_format($percentage, 1) . "%
  81. </div>
  82. </div>";
  83. });
  84. // 剩余配额
  85. $grid->column('remaining', '剩余配额')->display(function () {
  86. $remaining = max(0, $this->limit_value - $this->used_value);
  87. $color = $remaining > 0 ? 'text-success' : 'text-danger';
  88. return "<span class='{$color}'>" . number_format($remaining) . "</span>";
  89. });
  90. // 时间窗口
  91. $grid->column('window_start', '窗口开始')->display(function ($time) {
  92. return $time ? \Carbon\Carbon::parse($time)->format('m-d H:i') : '-';
  93. });
  94. $grid->column('window_end', '窗口结束')->display(function ($time) {
  95. return $time ? \Carbon\Carbon::parse($time)->format('m-d H:i') : '-';
  96. });
  97. // 重置时间
  98. $grid->column('reset_at', '下次重置')->display(function ($time) {
  99. if (!$time) {
  100. return '-';
  101. }
  102. $resetTime = \Carbon\Carbon::parse($time);
  103. if ($resetTime->isPast()) {
  104. return '<span class="badge badge-warning">待重置</span>';
  105. } else {
  106. return $resetTime->diffForHumans();
  107. }
  108. });
  109. // 告警阈值
  110. $grid->column('alert_threshold', '告警阈值')->display(function ($threshold) {
  111. return $threshold . '%';
  112. });
  113. // 状态
  114. $grid->column('is_active', '状态')->display(function ($active) {
  115. return $active ?
  116. '<span class="badge badge-success"><i class="fa fa-check"></i> 激活</span>' :
  117. '<span class="badge badge-secondary"><i class="fa fa-times"></i> 未激活</span>';
  118. });
  119. // 超限状态
  120. $grid->column('is_exceeded', '超限状态')->display(function ($exceeded) {
  121. return $exceeded ?
  122. '<span class="badge badge-danger"><i class="fa fa-exclamation-triangle"></i> 已超限</span>' :
  123. '<span class="badge badge-success"><i class="fa fa-check"></i> 正常</span>';
  124. });
  125. // 过滤器
  126. $grid->filter(function (Grid\Filter $filter) {
  127. $filter->equal('service_id', '服务')->select(
  128. ThirdPartyService::pluck('name', 'id')->toArray()
  129. );
  130. $filter->equal('type', '配额类型')->select(QUOTA_TYPE::getOptions());
  131. $filter->equal('is_active', '状态')->select([
  132. 1 => '激活',
  133. 0 => '未激活',
  134. ]);
  135. $filter->equal('is_exceeded', '超限状态')->select([
  136. 1 => '已超限',
  137. 0 => '正常',
  138. ]);
  139. $filter->between('alert_threshold', '告警阈值(%)');
  140. $filter->between('used_value', '已使用量');
  141. });
  142. // 批量操作
  143. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  144. // TODO: 创建批量操作类
  145. // $batch->add('批量重置', new \App\Module\ThirdParty\AdminActions\BatchResetQuotaAction());
  146. // $batch->add('批量激活', new \App\Module\ThirdParty\AdminActions\BatchActivateQuotaAction());
  147. // $batch->add('批量停用', new \App\Module\ThirdParty\AdminActions\BatchDeactivateQuotaAction());
  148. });
  149. // 工具栏
  150. $grid->tools(function (Grid\Tools $tools) {
  151. $tools->append('<a href="/admin/thirdparty/quotas/exceeded" class="btn btn-sm btn-danger"><i class="fa fa-exclamation-triangle"></i> 超限配额</a>');
  152. $tools->append('<a href="/admin/thirdparty/quotas/reset-all" class="btn btn-sm btn-warning"><i class="fa fa-redo"></i> 重置所有</a>');
  153. $tools->append('<a href="/admin/thirdparty/quotas/stats" class="btn btn-sm btn-info"><i class="fa fa-chart-bar"></i> 使用统计</a>');
  154. });
  155. // 行操作
  156. $grid->actions(function (Grid\Displayers\Actions $actions) {
  157. $actions->append('<a href="/admin/thirdparty/quotas/' . $actions->getKey() . '/reset" class="btn btn-xs btn-warning"><i class="fa fa-redo"></i> 重置</a>');
  158. if ($actions->row->is_exceeded) {
  159. $actions->append('<a href="/admin/thirdparty/quotas/' . $actions->getKey() . '/increase" class="btn btn-xs btn-success"><i class="fa fa-plus"></i> 增加配额</a>');
  160. }
  161. $actions->append('<a href="/admin/thirdparty/quotas/' . $actions->getKey() . '/usage" class="btn btn-xs btn-info"><i class="fa fa-chart-line"></i> 使用详情</a>');
  162. });
  163. // 默认排序
  164. $grid->model()->orderBy('service_id')->orderBy('type');
  165. });
  166. }
  167. /**
  168. * 详情页面
  169. *
  170. * @return Show
  171. */
  172. protected function detail($id): Show
  173. {
  174. return Show::make($id, new ThirdPartyQuotaRepository(), function (Show $show) {
  175. $show->field('id', 'ID');
  176. $show->field('service.name', '服务名称');
  177. $show->field('service.code', '服务代码');
  178. $show->field('type', '配额类型')->as(function ($type) {
  179. $quotaType = QUOTA_TYPE::tryFrom($type);
  180. return $quotaType ? $quotaType->getLabel() : $type;
  181. });
  182. $show->field('limit_value', '配额限制')->as(function ($limit) {
  183. return number_format($limit);
  184. });
  185. $show->field('used_value', '已使用量')->as(function ($used) {
  186. return number_format($used);
  187. });
  188. $show->field('usage_percentage', '使用率')->as(function () {
  189. $percentage = $this->limit_value > 0 ?
  190. ($this->used_value / $this->limit_value) * 100 : 0;
  191. return number_format($percentage, 2) . '%';
  192. });
  193. $show->field('remaining', '剩余配额')->as(function () {
  194. $remaining = max(0, $this->limit_value - $this->used_value);
  195. return number_format($remaining);
  196. });
  197. $show->field('window_start', '时间窗口开始');
  198. $show->field('window_end', '时间窗口结束');
  199. $show->field('reset_at', '下次重置时间');
  200. $show->field('alert_threshold', '告警阈值')->as(function ($threshold) {
  201. return $threshold . '%';
  202. });
  203. $show->field('is_active', '状态')->as(function ($active) {
  204. return $active ? '激活' : '未激活';
  205. });
  206. $show->field('is_exceeded', '超限状态')->as(function ($exceeded) {
  207. return $exceeded ? '已超限' : '正常';
  208. });
  209. $show->field('exceeded_at', '超限时间');
  210. $show->field('created_at', '创建时间');
  211. $show->field('updated_at', '更新时间');
  212. });
  213. }
  214. /**
  215. * 表单页面
  216. *
  217. * @return Form
  218. */
  219. protected function form(): Form
  220. {
  221. return Form::make(new ThirdPartyQuotaRepository(), function (Form $form) {
  222. $form->display('id', 'ID');
  223. $form->select('service_id', '所属服务')
  224. ->options(ThirdPartyService::pluck('name', 'id')->toArray())
  225. ->required()
  226. ->help('选择要配置配额的第三方服务');
  227. $form->select('type', '配额类型')
  228. ->options(QUOTA_TYPE::getOptions())
  229. ->required()
  230. ->help('选择配额的时间窗口类型');
  231. $form->number('limit_value', '配额限制')
  232. ->required()
  233. ->min(1)
  234. ->help('在指定时间窗口内允许的最大调用次数');
  235. $form->number('alert_threshold', '告警阈值(%)')
  236. ->default(80)
  237. ->min(1)
  238. ->max(100)
  239. ->help('当使用率达到此百分比时触发告警');
  240. $form->switch('is_active', '是否激活')->default(1);
  241. $form->datetime('window_start', '时间窗口开始')->help('留空自动计算');
  242. $form->datetime('window_end', '时间窗口结束')->help('留空自动计算');
  243. $form->datetime('reset_at', '下次重置时间')->help('留空自动计算');
  244. $form->display('used_value', '已使用量');
  245. $form->display('is_exceeded', '超限状态');
  246. $form->display('exceeded_at', '超限时间');
  247. $form->display('created_at', '创建时间');
  248. $form->display('updated_at', '更新时间');
  249. // 保存前处理
  250. $form->saving(function (Form $form) {
  251. // 自动计算时间窗口
  252. if (empty($form->window_start) || empty($form->window_end)) {
  253. $quotaType = QUOTA_TYPE::from($form->type);
  254. $form->window_start = $quotaType->getCurrentWindowStart()->format('Y-m-d H:i:s');
  255. $form->window_end = $quotaType->getCurrentWindowEnd()->format('Y-m-d H:i:s');
  256. $form->reset_at = $form->window_end;
  257. }
  258. });
  259. });
  260. }
  261. }