CleanupService.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. namespace App\Module\Cleanup\Services;
  3. use App\Module\Cleanup\Logics\TableScannerLogic;
  4. use App\Module\Cleanup\Logics\CleanupPlanLogic;
  5. use App\Module\Cleanup\Logics\CleanupTaskLogic;
  6. use App\Module\Cleanup\Logics\CleanupExecutorLogic;
  7. use App\Module\Cleanup\Logics\BackupLogic;
  8. use App\Module\Cleanup\Models\CleanupPlan;
  9. use App\Module\Cleanup\Models\CleanupTask;
  10. use App\Module\Cleanup\Models\CleanupBackup;
  11. /**
  12. * 清理服务类
  13. *
  14. * 提供对外的清理服务接口
  15. */
  16. class CleanupService
  17. {
  18. /**
  19. * 扫描系统中的所有数据表
  20. *
  21. * @param bool $forceRefresh 是否强制刷新
  22. * @return array 扫描结果
  23. */
  24. public static function scanTables(bool $forceRefresh = false): array
  25. {
  26. return TableScannerLogic::scanAllTables($forceRefresh);
  27. }
  28. /**
  29. * 创建清理计划
  30. *
  31. * @param array $planData 计划数据
  32. * @return array 创建结果
  33. */
  34. public static function createCleanupPlan(array $planData): array
  35. {
  36. return CleanupPlanLogic::createPlan($planData);
  37. }
  38. /**
  39. * 为计划生成内容配置
  40. *
  41. * @param int $planId 计划ID
  42. * @param bool $autoGenerate 是否自动生成
  43. * @return array 生成结果
  44. */
  45. public static function generatePlanContents(int $planId, bool $autoGenerate = true): array
  46. {
  47. return CleanupPlanLogic::generateContents($planId, $autoGenerate);
  48. }
  49. /**
  50. * 基于计划创建清理任务
  51. *
  52. * @param int $planId 计划ID
  53. * @param array $taskOptions 任务选项
  54. * @return array 创建结果
  55. */
  56. public static function createCleanupTask(int $planId, array $taskOptions = []): array
  57. {
  58. return CleanupTaskLogic::createTask($planId, $taskOptions);
  59. }
  60. /**
  61. * 预览计划的清理结果
  62. *
  63. * @param int $planId 计划ID
  64. * @return array 预览结果
  65. */
  66. public static function previewPlanCleanup(int $planId): array
  67. {
  68. return CleanupExecutorLogic::previewPlanCleanup($planId);
  69. }
  70. /**
  71. * 预览任务的清理结果
  72. *
  73. * @param int $taskId 任务ID
  74. * @return array 预览结果
  75. */
  76. public static function previewTaskCleanup(int $taskId): array
  77. {
  78. return CleanupExecutorLogic::previewTaskCleanup($taskId);
  79. }
  80. /**
  81. * 执行清理任务
  82. *
  83. * @param int $taskId 任务ID
  84. * @param bool $dryRun 是否为预演模式
  85. * @return array 执行结果
  86. */
  87. public static function executeCleanupTask(int $taskId, bool $dryRun = false): array
  88. {
  89. return CleanupExecutorLogic::executeTask($taskId, $dryRun);
  90. }
  91. /**
  92. * 为计划创建数据备份
  93. *
  94. * @param int $planId 计划ID
  95. * @param array $backupOptions 备份选项
  96. * @return array 备份结果
  97. */
  98. public static function createPlanBackup(int $planId, array $backupOptions = []): array
  99. {
  100. return BackupLogic::createPlanBackup($planId, $backupOptions);
  101. }
  102. /**
  103. * 为任务创建数据备份
  104. *
  105. * @param int $taskId 任务ID
  106. * @param array $backupOptions 备份选项
  107. * @return array 备份结果
  108. */
  109. public static function createTaskBackup(int $taskId, array $backupOptions = []): array
  110. {
  111. return BackupLogic::createTaskBackup($taskId, $backupOptions);
  112. }
  113. /**
  114. * 恢复数据备份
  115. *
  116. * @param int $backupId 备份ID
  117. * @param array $restoreOptions 恢复选项
  118. * @return array 恢复结果
  119. */
  120. public static function restoreBackup(int $backupId, array $restoreOptions = []): array
  121. {
  122. return BackupLogic::restoreBackup($backupId, $restoreOptions);
  123. }
  124. /**
  125. * 验证备份完整性
  126. *
  127. * @param int $backupId 备份ID
  128. * @return array 验证结果
  129. */
  130. public static function verifyBackup(int $backupId): array
  131. {
  132. return BackupLogic::verifyBackup($backupId);
  133. }
  134. /**
  135. * 获取任务执行进度
  136. *
  137. * @param int $taskId 任务ID
  138. * @return array 进度信息
  139. */
  140. public static function getTaskProgress(int $taskId): array
  141. {
  142. return CleanupTaskLogic::getTaskProgress($taskId);
  143. }
  144. /**
  145. * 停止任务执行
  146. *
  147. * @param int $taskId 任务ID
  148. * @return array 停止结果
  149. */
  150. public static function stopTask(int $taskId): array
  151. {
  152. return CleanupTaskLogic::stopTask($taskId);
  153. }
  154. /**
  155. * 暂停任务执行
  156. *
  157. * @param int $taskId 任务ID
  158. * @return array 暂停结果
  159. */
  160. public static function pauseTask(int $taskId): array
  161. {
  162. return CleanupTaskLogic::pauseTask($taskId);
  163. }
  164. /**
  165. * 恢复任务执行
  166. *
  167. * @param int $taskId 任务ID
  168. * @return array 恢复结果
  169. */
  170. public static function resumeTask(int $taskId): array
  171. {
  172. return CleanupTaskLogic::resumeTask($taskId);
  173. }
  174. /**
  175. * 获取清理统计信息
  176. *
  177. * @param array $filters 筛选条件
  178. * @return array 统计信息
  179. */
  180. public static function getCleanupStats(array $filters = []): array
  181. {
  182. return [
  183. 'plans' => CleanupPlan::getEnabledStats(),
  184. 'tasks' => CleanupTask::getStatusStats(),
  185. 'backups' => CleanupBackup::getStatusStats(),
  186. 'recent_tasks' => CleanupTask::getRecentStats(7),
  187. 'storage' => CleanupBackup::getStorageStats(),
  188. ];
  189. }
  190. /**
  191. * 清理过期备份
  192. *
  193. * @param int $retentionDays 保留天数
  194. * @return array 清理结果
  195. */
  196. public static function cleanExpiredBackups(int $retentionDays = 30): array
  197. {
  198. return BackupLogic::cleanExpiredBackups($retentionDays);
  199. }
  200. /**
  201. * 清理历史日志
  202. *
  203. * @param int $retentionDays 保留天数
  204. * @return array 清理结果
  205. */
  206. public static function cleanHistoryLogs(int $retentionDays = 30): array
  207. {
  208. return CleanupTaskLogic::cleanHistoryLogs($retentionDays);
  209. }
  210. /**
  211. * 获取系统健康状态
  212. *
  213. * @return array 健康状态
  214. */
  215. public static function getSystemHealth(): array
  216. {
  217. $runningTasks = CleanupTask::running()->count();
  218. $failedTasks = CleanupTask::byStatus(\App\Module\Cleanup\Enums\TASK_STATUS::FAILED->value)
  219. ->where('created_at', '>=', now()->subDay())
  220. ->count();
  221. $expiredBackups = CleanupBackup::expired()->count();
  222. $expiringSoonBackups = CleanupBackup::expiringSoon(3)->count();
  223. $health = 'good';
  224. $issues = [];
  225. if ($runningTasks > 5) {
  226. $health = 'warning';
  227. $issues[] = "有 {$runningTasks} 个任务正在运行,可能存在性能问题";
  228. }
  229. if ($failedTasks > 0) {
  230. $health = 'warning';
  231. $issues[] = "最近24小时内有 {$failedTasks} 个任务执行失败";
  232. }
  233. if ($expiredBackups > 0) {
  234. $health = 'warning';
  235. $issues[] = "有 {$expiredBackups} 个备份已过期,建议清理";
  236. }
  237. if ($expiringSoonBackups > 0) {
  238. $issues[] = "有 {$expiringSoonBackups} 个备份即将过期";
  239. }
  240. if ($failedTasks > 5) {
  241. $health = 'critical';
  242. }
  243. return [
  244. 'health' => $health,
  245. 'issues' => $issues,
  246. 'metrics' => [
  247. 'running_tasks' => $runningTasks,
  248. 'failed_tasks_24h' => $failedTasks,
  249. 'expired_backups' => $expiredBackups,
  250. 'expiring_soon_backups' => $expiringSoonBackups,
  251. ],
  252. ];
  253. }
  254. /**
  255. * 获取推荐的清理计划
  256. *
  257. * @return array 推荐计划
  258. */
  259. public static function getRecommendedPlans(): array
  260. {
  261. $recommendations = [];
  262. // 检查是否有大量日志数据
  263. $logTables = \App\Module\Cleanup\Models\CleanupConfig::byCategory(
  264. \App\Module\Cleanup\Enums\DATA_CATEGORY::LOG_DATA->value
  265. )->get();
  266. if ($logTables->count() > 0) {
  267. $recommendations[] = [
  268. 'type' => 'category',
  269. 'title' => '日志数据清理',
  270. 'description' => '清理系统中的日志数据,释放存储空间',
  271. 'config' => [
  272. 'plan_type' => \App\Module\Cleanup\Enums\PLAN_TYPE::CATEGORY->value,
  273. 'target_selection' => [
  274. 'selection_type' => 'category',
  275. 'categories' => [\App\Module\Cleanup\Enums\DATA_CATEGORY::LOG_DATA->value],
  276. ],
  277. ],
  278. 'estimated_tables' => $logTables->count(),
  279. ];
  280. }
  281. // 检查是否有缓存数据
  282. $cacheTables = \App\Module\Cleanup\Models\CleanupConfig::byCategory(
  283. \App\Module\Cleanup\Enums\DATA_CATEGORY::CACHE_DATA->value
  284. )->get();
  285. if ($cacheTables->count() > 0) {
  286. $recommendations[] = [
  287. 'type' => 'category',
  288. 'title' => '缓存数据清理',
  289. 'description' => '清理系统中的缓存数据,提高系统性能',
  290. 'config' => [
  291. 'plan_type' => \App\Module\Cleanup\Enums\PLAN_TYPE::CATEGORY->value,
  292. 'target_selection' => [
  293. 'selection_type' => 'category',
  294. 'categories' => [\App\Module\Cleanup\Enums\DATA_CATEGORY::CACHE_DATA->value],
  295. ],
  296. ],
  297. 'estimated_tables' => $cacheTables->count(),
  298. ];
  299. }
  300. return $recommendations;
  301. }
  302. }