CleanDataCommand.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace App\Module\OpenAPI\Commands;
  3. use App\Module\OpenAPI\Models\OpenApiLog;
  4. use App\Module\OpenAPI\Models\OpenApiRateLimit;
  5. use App\Module\OpenAPI\Models\OpenApiStats;
  6. use App\Module\OpenAPI\Services\RateLimitService;
  7. use Illuminate\Console\Command;
  8. use Carbon\Carbon;
  9. /**
  10. * 清理OpenAPI数据命令
  11. */
  12. class CleanDataCommand extends Command
  13. {
  14. /**
  15. * 命令签名
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'openapi:clean-data
  20. {--logs=30 : 保留日志天数}
  21. {--rate-limits=7 : 保留限流记录天数}
  22. {--stats=365 : 保留统计数据天数}
  23. {--dry-run : 仅显示将要删除的数据,不实际删除}
  24. {--force : 强制删除,不询问确认}';
  25. /**
  26. * 命令描述
  27. *
  28. * @var string
  29. */
  30. protected $description = '清理OpenAPI过期数据';
  31. protected RateLimitService $rateLimitService;
  32. /**
  33. * 构造函数
  34. *
  35. * @param RateLimitService $rateLimitService
  36. */
  37. public function __construct(RateLimitService $rateLimitService)
  38. {
  39. parent::__construct();
  40. $this->rateLimitService = $rateLimitService;
  41. }
  42. /**
  43. * 执行命令
  44. *
  45. * @return int
  46. */
  47. public function handle(): int
  48. {
  49. $logsDays = (int)$this->option('logs');
  50. $rateLimitDays = (int)$this->option('rate-limits');
  51. $statsDays = (int)$this->option('stats');
  52. $dryRun = $this->option('dry-run');
  53. $force = $this->option('force');
  54. $this->info('OpenAPI数据清理工具');
  55. $this->line('');
  56. // 显示清理计划
  57. $this->table(['数据类型', '保留天数', '清理日期之前'], [
  58. ['API调用日志', $logsDays, Carbon::now()->subDays($logsDays)->toDateString()],
  59. ['限流记录', $rateLimitDays, Carbon::now()->subDays($rateLimitDays)->toDateString()],
  60. ['统计数据', $statsDays, Carbon::now()->subDays($statsDays)->toDateString()],
  61. ]);
  62. if ($dryRun) {
  63. $this->warn('这是一次试运行,不会实际删除数据');
  64. }
  65. // 统计将要删除的数据
  66. $logsCount = $this->getExpiredLogsCount($logsDays);
  67. $rateLimitsCount = $this->getExpiredRateLimitsCount($rateLimitDays);
  68. $statsCount = $this->getExpiredStatsCount($statsDays);
  69. $this->line('');
  70. $this->info('将要清理的数据统计:');
  71. $this->table(['数据类型', '记录数'], [
  72. ['API调用日志', number_format($logsCount)],
  73. ['限流记录', number_format($rateLimitsCount)],
  74. ['统计数据', number_format($statsCount)],
  75. ]);
  76. if ($logsCount === 0 && $rateLimitsCount === 0 && $statsCount === 0) {
  77. $this->info('没有需要清理的数据');
  78. return 0;
  79. }
  80. // 确认删除
  81. if (!$dryRun && !$force) {
  82. if (!$this->confirm('确定要删除这些数据吗?此操作不可恢复!')) {
  83. $this->info('操作已取消');
  84. return 0;
  85. }
  86. }
  87. $this->line('');
  88. $this->info('开始清理数据...');
  89. $totalDeleted = 0;
  90. // 清理API调用日志
  91. if ($logsCount > 0) {
  92. $deleted = $this->cleanLogs($logsDays, $dryRun);
  93. $totalDeleted += $deleted;
  94. $this->line("✓ 清理API调用日志: " . number_format($deleted) . " 条记录");
  95. }
  96. // 清理限流记录
  97. if ($rateLimitsCount > 0) {
  98. $deleted = $this->cleanRateLimits($rateLimitDays, $dryRun);
  99. $totalDeleted += $deleted;
  100. $this->line("✓ 清理限流记录: " . number_format($deleted) . " 条记录");
  101. }
  102. // 清理统计数据
  103. if ($statsCount > 0) {
  104. $deleted = $this->cleanStats($statsDays, $dryRun);
  105. $totalDeleted += $deleted;
  106. $this->line("✓ 清理统计数据: " . number_format($deleted) . " 条记录");
  107. }
  108. $this->line('');
  109. if ($dryRun) {
  110. $this->info("试运行完成,共计将删除 " . number_format($totalDeleted) . " 条记录");
  111. } else {
  112. $this->info("数据清理完成,共删除 " . number_format($totalDeleted) . " 条记录");
  113. }
  114. return 0;
  115. }
  116. /**
  117. * 获取过期日志数量
  118. *
  119. * @param int $days
  120. * @return int
  121. */
  122. protected function getExpiredLogsCount(int $days): int
  123. {
  124. $cutoffDate = Carbon::now()->subDays($days);
  125. return OpenApiLog::where('created_at', '<', $cutoffDate)->count();
  126. }
  127. /**
  128. * 获取过期限流记录数量
  129. *
  130. * @param int $days
  131. * @return int
  132. */
  133. protected function getExpiredRateLimitsCount(int $days): int
  134. {
  135. $cutoffDate = Carbon::now()->subDays($days);
  136. return OpenApiRateLimit::where('created_at', '<', $cutoffDate)->count();
  137. }
  138. /**
  139. * 获取过期统计数据数量
  140. *
  141. * @param int $days
  142. * @return int
  143. */
  144. protected function getExpiredStatsCount(int $days): int
  145. {
  146. $cutoffDate = Carbon::now()->subDays($days);
  147. return OpenApiStats::where('created_at', '<', $cutoffDate)->count();
  148. }
  149. /**
  150. * 清理API调用日志
  151. *
  152. * @param int $days
  153. * @param bool $dryRun
  154. * @return int
  155. */
  156. protected function cleanLogs(int $days, bool $dryRun): int
  157. {
  158. $cutoffDate = Carbon::now()->subDays($days);
  159. if ($dryRun) {
  160. return OpenApiLog::where('created_at', '<', $cutoffDate)->count();
  161. }
  162. $deleted = 0;
  163. $batchSize = 1000;
  164. // 分批删除,避免内存溢出
  165. do {
  166. $deletedBatch = OpenApiLog::where('created_at', '<', $cutoffDate)
  167. ->limit($batchSize)
  168. ->delete();
  169. $deleted += $deletedBatch;
  170. if ($deletedBatch > 0) {
  171. $this->line(" 已删除 {$deleted} 条日志记录...");
  172. }
  173. } while ($deletedBatch > 0);
  174. return $deleted;
  175. }
  176. /**
  177. * 清理限流记录
  178. *
  179. * @param int $days
  180. * @param bool $dryRun
  181. * @return int
  182. */
  183. protected function cleanRateLimits(int $days, bool $dryRun): int
  184. {
  185. if ($dryRun) {
  186. $cutoffDate = Carbon::now()->subDays($days);
  187. return OpenApiRateLimit::where('created_at', '<', $cutoffDate)->count();
  188. }
  189. // 使用服务类的清理方法
  190. return $this->rateLimitService->cleanExpiredRecords($days);
  191. }
  192. /**
  193. * 清理统计数据
  194. *
  195. * @param int $days
  196. * @param bool $dryRun
  197. * @return int
  198. */
  199. protected function cleanStats(int $days, bool $dryRun): int
  200. {
  201. $cutoffDate = Carbon::now()->subDays($days);
  202. if ($dryRun) {
  203. return OpenApiStats::where('created_at', '<', $cutoffDate)->count();
  204. }
  205. $deleted = 0;
  206. $batchSize = 1000;
  207. // 分批删除
  208. do {
  209. $deletedBatch = OpenApiStats::where('created_at', '<', $cutoffDate)
  210. ->limit($batchSize)
  211. ->delete();
  212. $deleted += $deletedBatch;
  213. if ($deletedBatch > 0) {
  214. $this->line(" 已删除 {$deleted} 条统计记录...");
  215. }
  216. } while ($deletedBatch > 0);
  217. return $deleted;
  218. }
  219. /**
  220. * 清理缓存数据
  221. *
  222. * @return void
  223. */
  224. protected function cleanCache(): void
  225. {
  226. $this->info('清理缓存数据...');
  227. // 清理限流缓存
  228. $pattern = 'rate_limit:*';
  229. $keys = \Illuminate\Support\Facades\Cache::getRedis()->keys($pattern);
  230. if (!empty($keys)) {
  231. \Illuminate\Support\Facades\Cache::getRedis()->del($keys);
  232. $this->line("✓ 清理限流缓存: " . count($keys) . " 个键");
  233. }
  234. // 清理应用缓存
  235. $pattern = 'openapi_app:*';
  236. $keys = \Illuminate\Support\Facades\Cache::getRedis()->keys($pattern);
  237. if (!empty($keys)) {
  238. \Illuminate\Support\Facades\Cache::getRedis()->del($keys);
  239. $this->line("✓ 清理应用缓存: " . count($keys) . " 个键");
  240. }
  241. // 清理权限缓存
  242. $pattern = 'app_scopes:*';
  243. $keys = \Illuminate\Support\Facades\Cache::getRedis()->keys($pattern);
  244. if (!empty($keys)) {
  245. \Illuminate\Support\Facades\Cache::getRedis()->del($keys);
  246. $this->line("✓ 清理权限缓存: " . count($keys) . " 个键");
  247. }
  248. }
  249. /**
  250. * 优化数据库表
  251. *
  252. * @return void
  253. */
  254. protected function optimizeTables(): void
  255. {
  256. $this->info('优化数据库表...');
  257. $tables = [
  258. 'kku_openapi_logs',
  259. 'kku_openapi_rate_limits',
  260. 'kku_openapi_stats',
  261. ];
  262. foreach ($tables as $table) {
  263. try {
  264. \Illuminate\Support\Facades\DB::statement("OPTIMIZE TABLE {$table}");
  265. $this->line("✓ 优化表: {$table}");
  266. } catch (\Exception $e) {
  267. $this->warn("优化表 {$table} 失败: " . $e->getMessage());
  268. }
  269. }
  270. }
  271. }