ThirdPartyQuotaController.php 12 KB

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