| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- <?php
- namespace App\Module\Cleanup\Services;
- use App\Module\Cleanup\Logics\TableScannerLogic;
- use App\Module\Cleanup\Logics\CleanupPlanLogic;
- use App\Module\Cleanup\Logics\CleanupTaskLogic;
- use App\Module\Cleanup\Logics\CleanupExecutorLogic;
- use App\Module\Cleanup\Logics\BackupLogic;
- use App\Module\Cleanup\Models\CleanupPlan;
- use App\Module\Cleanup\Models\CleanupTask;
- use App\Module\Cleanup\Models\CleanupBackup;
- /**
- * 清理服务类
- *
- * 提供对外的清理服务接口
- */
- class CleanupService
- {
- /**
- * 扫描系统中的所有数据表
- *
- * @param bool $forceRefresh 是否强制刷新
- * @return array 扫描结果
- */
- public static function scanTables(bool $forceRefresh = false): array
- {
- return TableScannerLogic::scanAllTables($forceRefresh);
- }
- /**
- * 创建清理计划
- *
- * @param array $planData 计划数据
- * @return array 创建结果
- */
- public static function createCleanupPlan(array $planData): array
- {
- return CleanupPlanLogic::createPlan($planData);
- }
- /**
- * 为计划生成内容配置
- *
- * @param int $planId 计划ID
- * @param bool $autoGenerate 是否自动生成
- * @return array 生成结果
- */
- public static function generatePlanContents(int $planId, bool $autoGenerate = true): array
- {
- return CleanupPlanLogic::generateContents($planId, $autoGenerate);
- }
- /**
- * 基于计划创建清理任务
- *
- * @param int $planId 计划ID
- * @param array $taskOptions 任务选项
- * @return array 创建结果
- */
- public static function createCleanupTask(int $planId, array $taskOptions = []): array
- {
- return CleanupTaskLogic::createTask($planId, $taskOptions);
- }
- /**
- * 预览计划的清理结果
- *
- * @param int $planId 计划ID
- * @return array 预览结果
- */
- public static function previewPlanCleanup(int $planId): array
- {
- return CleanupExecutorLogic::previewPlanCleanup($planId);
- }
- /**
- * 预览任务的清理结果
- *
- * @param int $taskId 任务ID
- * @return array 预览结果
- */
- public static function previewTaskCleanup(int $taskId): array
- {
- return CleanupExecutorLogic::previewTaskCleanup($taskId);
- }
- /**
- * 执行清理任务
- *
- * @param int $taskId 任务ID
- * @param bool $dryRun 是否为预演模式
- * @return array 执行结果
- */
- public static function executeCleanupTask(int $taskId, bool $dryRun = false): array
- {
- return CleanupExecutorLogic::executeTask($taskId, $dryRun);
- }
- /**
- * 为计划创建数据备份
- *
- * @param int $planId 计划ID
- * @param array $backupOptions 备份选项
- * @return array 备份结果
- */
- public static function createPlanBackup(int $planId, array $backupOptions = []): array
- {
- return BackupLogic::createPlanBackup($planId, $backupOptions);
- }
- /**
- * 为任务创建数据备份
- *
- * @param int $taskId 任务ID
- * @param array $backupOptions 备份选项
- * @return array 备份结果
- */
- public static function createTaskBackup(int $taskId, array $backupOptions = []): array
- {
- return BackupLogic::createTaskBackup($taskId, $backupOptions);
- }
- /**
- * 恢复数据备份
- *
- * @param int $backupId 备份ID
- * @param array $restoreOptions 恢复选项
- * @return array 恢复结果
- */
- public static function restoreBackup(int $backupId, array $restoreOptions = []): array
- {
- return BackupLogic::restoreBackup($backupId, $restoreOptions);
- }
- /**
- * 验证备份完整性
- *
- * @param int $backupId 备份ID
- * @return array 验证结果
- */
- public static function verifyBackup(int $backupId): array
- {
- return BackupLogic::verifyBackup($backupId);
- }
- /**
- * 获取任务执行进度
- *
- * @param int $taskId 任务ID
- * @return array 进度信息
- */
- public static function getTaskProgress(int $taskId): array
- {
- return CleanupTaskLogic::getTaskProgress($taskId);
- }
- /**
- * 停止任务执行
- *
- * @param int $taskId 任务ID
- * @return array 停止结果
- */
- public static function stopTask(int $taskId): array
- {
- return CleanupTaskLogic::stopTask($taskId);
- }
- /**
- * 暂停任务执行
- *
- * @param int $taskId 任务ID
- * @return array 暂停结果
- */
- public static function pauseTask(int $taskId): array
- {
- return CleanupTaskLogic::pauseTask($taskId);
- }
- /**
- * 恢复任务执行
- *
- * @param int $taskId 任务ID
- * @return array 恢复结果
- */
- public static function resumeTask(int $taskId): array
- {
- return CleanupTaskLogic::resumeTask($taskId);
- }
- /**
- * 获取清理统计信息
- *
- * @param array $filters 筛选条件
- * @return array 统计信息
- */
- public static function getCleanupStats(array $filters = []): array
- {
- return [
- 'plans' => CleanupPlan::getEnabledStats(),
- 'tasks' => CleanupTask::getStatusStats(),
- 'backups' => CleanupBackup::getStatusStats(),
- 'recent_tasks' => CleanupTask::getRecentStats(7),
- 'storage' => CleanupBackup::getStorageStats(),
- ];
- }
- /**
- * 清理过期备份
- *
- * @param int $retentionDays 保留天数
- * @return array 清理结果
- */
- public static function cleanExpiredBackups(int $retentionDays = 30): array
- {
- return BackupLogic::cleanExpiredBackups($retentionDays);
- }
- /**
- * 清理历史日志
- *
- * @param int $retentionDays 保留天数
- * @return array 清理结果
- */
- public static function cleanHistoryLogs(int $retentionDays = 30): array
- {
- return CleanupTaskLogic::cleanHistoryLogs($retentionDays);
- }
- /**
- * 获取系统健康状态
- *
- * @return array 健康状态
- */
- public static function getSystemHealth(): array
- {
- $runningTasks = CleanupTask::running()->count();
- $failedTasks = CleanupTask::byStatus(\App\Module\Cleanup\Enums\TASK_STATUS::FAILED->value)
- ->where('created_at', '>=', now()->subDay())
- ->count();
- $expiredBackups = CleanupBackup::expired()->count();
- $expiringSoonBackups = CleanupBackup::expiringSoon(3)->count();
- $health = 'good';
- $issues = [];
- if ($runningTasks > 5) {
- $health = 'warning';
- $issues[] = "有 {$runningTasks} 个任务正在运行,可能存在性能问题";
- }
- if ($failedTasks > 0) {
- $health = 'warning';
- $issues[] = "最近24小时内有 {$failedTasks} 个任务执行失败";
- }
- if ($expiredBackups > 0) {
- $health = 'warning';
- $issues[] = "有 {$expiredBackups} 个备份已过期,建议清理";
- }
- if ($expiringSoonBackups > 0) {
- $issues[] = "有 {$expiringSoonBackups} 个备份即将过期";
- }
- if ($failedTasks > 5) {
- $health = 'critical';
- }
- return [
- 'health' => $health,
- 'issues' => $issues,
- 'metrics' => [
- 'running_tasks' => $runningTasks,
- 'failed_tasks_24h' => $failedTasks,
- 'expired_backups' => $expiredBackups,
- 'expiring_soon_backups' => $expiringSoonBackups,
- ],
- ];
- }
- /**
- * 获取推荐的清理计划
- *
- * @return array 推荐计划
- */
- public static function getRecommendedPlans(): array
- {
- $recommendations = [];
- // 检查是否有大量日志数据
- $logTables = \App\Module\Cleanup\Models\CleanupConfig::byCategory(
- \App\Module\Cleanup\Enums\DATA_CATEGORY::LOG_DATA->value
- )->get();
- if ($logTables->count() > 0) {
- $recommendations[] = [
- 'type' => 'category',
- 'title' => '日志数据清理',
- 'description' => '清理系统中的日志数据,释放存储空间',
- 'config' => [
- 'plan_type' => \App\Module\Cleanup\Enums\PLAN_TYPE::CATEGORY->value,
- 'target_selection' => [
- 'selection_type' => 'category',
- 'categories' => [\App\Module\Cleanup\Enums\DATA_CATEGORY::LOG_DATA->value],
- ],
- ],
- 'estimated_tables' => $logTables->count(),
- ];
- }
- // 检查是否有缓存数据
- $cacheTables = \App\Module\Cleanup\Models\CleanupConfig::byCategory(
- \App\Module\Cleanup\Enums\DATA_CATEGORY::CACHE_DATA->value
- )->get();
- if ($cacheTables->count() > 0) {
- $recommendations[] = [
- 'type' => 'category',
- 'title' => '缓存数据清理',
- 'description' => '清理系统中的缓存数据,提高系统性能',
- 'config' => [
- 'plan_type' => \App\Module\Cleanup\Enums\PLAN_TYPE::CATEGORY->value,
- 'target_selection' => [
- 'selection_type' => 'category',
- 'categories' => [\App\Module\Cleanup\Enums\DATA_CATEGORY::CACHE_DATA->value],
- ],
- ],
- 'estimated_tables' => $cacheTables->count(),
- ];
- }
- return $recommendations;
- }
- }
|