ThirdPartyQuotaController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. $batch->add('批量重置', new \App\Module\ThirdParty\AdminActions\BatchResetQuotaAction());
  143. $batch->add('批量激活', new \App\Module\ThirdParty\AdminActions\BatchActivateQuotaAction());
  144. $batch->add('批量停用', new \App\Module\ThirdParty\AdminActions\BatchDeactivateQuotaAction());
  145. });
  146. // 工具栏
  147. $grid->tools(function (Grid\Tools $tools) {
  148. $tools->append('<a href="/admin/thirdparty/quotas/exceeded" class="btn btn-sm btn-danger"><i class="fa fa-exclamation-triangle"></i> 超限配额</a>');
  149. $tools->append('<a href="/admin/thirdparty/quotas/reset-all" class="btn btn-sm btn-warning"><i class="fa fa-redo"></i> 重置所有</a>');
  150. $tools->append('<a href="/admin/thirdparty/quotas/stats" class="btn btn-sm btn-info"><i class="fa fa-chart-bar"></i> 使用统计</a>');
  151. });
  152. // 行操作
  153. $grid->actions(function (Grid\Displayers\Actions $actions) {
  154. $actions->append('<a href="/admin/thirdparty/quotas/' . $actions->getKey() . '/reset" class="btn btn-xs btn-warning"><i class="fa fa-redo"></i> 重置</a>');
  155. if ($actions->row->is_exceeded) {
  156. $actions->append('<a href="/admin/thirdparty/quotas/' . $actions->getKey() . '/increase" class="btn btn-xs btn-success"><i class="fa fa-plus"></i> 增加配额</a>');
  157. }
  158. $actions->append('<a href="/admin/thirdparty/quotas/' . $actions->getKey() . '/usage" class="btn btn-xs btn-info"><i class="fa fa-chart-line"></i> 使用详情</a>');
  159. });
  160. // 默认排序
  161. $grid->model()->orderBy('service_id')->orderBy('type');
  162. });
  163. }
  164. /**
  165. * 详情页面
  166. *
  167. * @return Show
  168. */
  169. protected function detail($id): Show
  170. {
  171. return Show::make($id, new ThirdPartyQuotaRepository(), function (Show $show) {
  172. $show->field('id', 'ID');
  173. $show->field('service.name', '服务名称');
  174. $show->field('service.code', '服务代码');
  175. $show->field('type', '配额类型')->as(function ($type) {
  176. $quotaType = QUOTA_TYPE::tryFrom($type);
  177. return $quotaType ? $quotaType->getLabel() : $type;
  178. });
  179. $show->field('limit_value', '配额限制')->as(function ($limit) {
  180. return number_format($limit);
  181. });
  182. $show->field('used_value', '已使用量')->as(function ($used) {
  183. return number_format($used);
  184. });
  185. $show->field('usage_percentage', '使用率')->as(function () {
  186. $percentage = $this->limit_value > 0 ?
  187. ($this->used_value / $this->limit_value) * 100 : 0;
  188. return number_format($percentage, 2) . '%';
  189. });
  190. $show->field('remaining', '剩余配额')->as(function () {
  191. $remaining = max(0, $this->limit_value - $this->used_value);
  192. return number_format($remaining);
  193. });
  194. $show->field('window_start', '时间窗口开始');
  195. $show->field('window_end', '时间窗口结束');
  196. $show->field('reset_at', '下次重置时间');
  197. $show->field('alert_threshold', '告警阈值')->as(function ($threshold) {
  198. return $threshold . '%';
  199. });
  200. $show->field('is_active', '状态')->as(function ($active) {
  201. return $active ? '激活' : '未激活';
  202. });
  203. $show->field('is_exceeded', '超限状态')->as(function ($exceeded) {
  204. return $exceeded ? '已超限' : '正常';
  205. });
  206. $show->field('exceeded_at', '超限时间');
  207. $show->field('created_at', '创建时间');
  208. $show->field('updated_at', '更新时间');
  209. });
  210. }
  211. /**
  212. * 表单页面
  213. *
  214. * @return Form
  215. */
  216. protected function form(): Form
  217. {
  218. return Form::make(new ThirdPartyQuotaRepository(), function (Form $form) {
  219. $form->display('id', 'ID');
  220. $form->select('service_id', '所属服务')
  221. ->options(ThirdPartyService::pluck('name', 'id')->toArray())
  222. ->required()
  223. ->help('选择要配置配额的第三方服务');
  224. $form->select('type', '配额类型')
  225. ->options(QUOTA_TYPE::getOptions())
  226. ->required()
  227. ->help('选择配额的时间窗口类型');
  228. $form->number('limit_value', '配额限制')
  229. ->required()
  230. ->min(1)
  231. ->help('在指定时间窗口内允许的最大调用次数');
  232. $form->number('alert_threshold', '告警阈值(%)')
  233. ->default(80)
  234. ->min(1)
  235. ->max(100)
  236. ->help('当使用率达到此百分比时触发告警');
  237. $form->switch('is_active', '是否激活')->default(1);
  238. $form->datetime('window_start', '时间窗口开始')->help('留空自动计算');
  239. $form->datetime('window_end', '时间窗口结束')->help('留空自动计算');
  240. $form->datetime('reset_at', '下次重置时间')->help('留空自动计算');
  241. $form->display('used_value', '已使用量');
  242. $form->display('is_exceeded', '超限状态');
  243. $form->display('exceeded_at', '超限时间');
  244. $form->display('created_at', '创建时间');
  245. $form->display('updated_at', '更新时间');
  246. // 保存前处理
  247. $form->saving(function (Form $form) {
  248. // 自动计算时间窗口
  249. if (empty($form->window_start) || empty($form->window_end)) {
  250. $quotaType = QUOTA_TYPE::from($form->type);
  251. $form->window_start = $quotaType->getCurrentWindowStart()->format('Y-m-d H:i:s');
  252. $form->window_end = $quotaType->getCurrentWindowEnd()->format('Y-m-d H:i:s');
  253. $form->reset_at = $form->window_end;
  254. }
  255. });
  256. });
  257. }
  258. }