TableScannerLogic.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. namespace App\Module\Cleanup\Logics;
  3. use App\Module\Cleanup\Models\CleanupConfig;
  4. use App\Module\Cleanup\Models\CleanupTableStats;
  5. use App\Module\Cleanup\Enums\DATA_CATEGORY;
  6. use App\Module\Cleanup\Enums\CLEANUP_TYPE;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Schema;
  9. /**
  10. * 表扫描逻辑类
  11. *
  12. * 负责扫描系统中的数据表并生成清理配置
  13. */
  14. class TableScannerLogic
  15. {
  16. /**
  17. * 扫描所有数据表
  18. *
  19. * @param bool $forceRefresh 是否强制刷新
  20. * @return array 扫描结果
  21. */
  22. public static function scanAllTables(bool $forceRefresh = false): array
  23. {
  24. $startTime = microtime(true);
  25. // 获取所有以 kku_ 开头的表
  26. $tables = static::getAllTables();
  27. $result = [
  28. 'total_tables' => count($tables),
  29. 'scanned_tables' => 0,
  30. 'new_tables' => 0,
  31. 'updated_tables' => 0,
  32. 'tables' => [],
  33. 'scan_time' => 0,
  34. ];
  35. foreach ($tables as $tableName) {
  36. try {
  37. $tableInfo = static::scanTable($tableName, $forceRefresh);
  38. $result['tables'][] = $tableInfo;
  39. $result['scanned_tables']++;
  40. if ($tableInfo['is_new']) {
  41. $result['new_tables']++;
  42. } elseif ($tableInfo['is_updated']) {
  43. $result['updated_tables']++;
  44. }
  45. } catch (\Exception $e) {
  46. \Log::error("扫描表 {$tableName} 失败: " . $e->getMessage());
  47. }
  48. }
  49. $result['scan_time'] = round(microtime(true) - $startTime, 3);
  50. return $result;
  51. }
  52. /**
  53. * 扫描单个表
  54. *
  55. * @param string $tableName 表名
  56. * @param bool $forceRefresh 是否强制刷新
  57. * @return array 表信息
  58. */
  59. public static function scanTable(string $tableName, bool $forceRefresh = false): array
  60. {
  61. // 检查是否已存在配置
  62. $existingConfig = CleanupConfig::where('table_name', $tableName)->first();
  63. $isNew = !$existingConfig;
  64. $isUpdated = false;
  65. // 获取表的基本信息
  66. $tableInfo = static::getTableInfo($tableName);
  67. // 分析表结构
  68. $structure = static::analyzeTableStructure($tableName);
  69. // 自动识别数据分类
  70. $dataCategory = DATA_CATEGORY::detectFromTableName($tableName);
  71. // 识别模块名称
  72. $moduleName = static::detectModuleName($tableName);
  73. // 生成默认清理配置
  74. $defaultConfig = static::generateDefaultConfig($dataCategory, $structure);
  75. if ($isNew || $forceRefresh) {
  76. // 创建或更新配置
  77. $configData = [
  78. 'table_name' => $tableName,
  79. 'module_name' => $moduleName,
  80. 'data_category' => $dataCategory->value,
  81. 'default_cleanup_type' => $defaultConfig['cleanup_type'],
  82. 'default_conditions' => $defaultConfig['conditions'],
  83. 'is_enabled' => $defaultConfig['is_enabled'],
  84. 'priority' => $defaultConfig['priority'],
  85. 'batch_size' => $defaultConfig['batch_size'],
  86. 'description' => $defaultConfig['description'],
  87. ];
  88. if ($isNew) {
  89. CleanupConfig::create($configData);
  90. } else {
  91. $existingConfig->update($configData);
  92. $isUpdated = true;
  93. }
  94. }
  95. // 更新表统计信息
  96. static::updateTableStats($tableName, $tableInfo, $structure);
  97. return [
  98. 'table_name' => $tableName,
  99. 'module_name' => $moduleName,
  100. 'data_category' => $dataCategory->value,
  101. 'data_category_name' => $dataCategory->getDescription(),
  102. 'record_count' => $tableInfo['record_count'],
  103. 'table_size_mb' => $tableInfo['table_size_mb'],
  104. 'has_time_field' => !empty($structure['time_fields']),
  105. 'time_fields' => $structure['time_fields'],
  106. 'has_user_field' => !empty($structure['user_fields']),
  107. 'user_fields' => $structure['user_fields'],
  108. 'has_status_field' => !empty($structure['status_fields']),
  109. 'status_fields' => $structure['status_fields'],
  110. 'default_cleanup_type' => $defaultConfig['cleanup_type'],
  111. 'default_priority' => $defaultConfig['priority'],
  112. 'is_new' => $isNew,
  113. 'is_updated' => $isUpdated,
  114. ];
  115. }
  116. /**
  117. * 获取所有数据表列表
  118. *
  119. * @return array 表名列表
  120. */
  121. private static function getAllTables(): array
  122. {
  123. $tables = DB::select("SHOW TABLES LIKE 'kku_%'");
  124. $tableColumn = 'Tables_in_' . config('database.connections.mysql.database');
  125. return array_map(function($table) use ($tableColumn) {
  126. return $table->$tableColumn;
  127. }, $tables);
  128. }
  129. /**
  130. * 获取表的基本信息
  131. *
  132. * @param string $tableName 表名
  133. * @return array 表信息
  134. */
  135. private static function getTableInfo(string $tableName): array
  136. {
  137. // 获取表状态信息
  138. $tableStatus = DB::select("SHOW TABLE STATUS LIKE ?", [$tableName]);
  139. if (empty($tableStatus)) {
  140. throw new \Exception("表 {$tableName} 不存在");
  141. }
  142. $status = $tableStatus[0];
  143. // 获取记录数量
  144. $recordCount = DB::table($tableName)->count();
  145. return [
  146. 'record_count' => $recordCount,
  147. 'table_size_mb' => round(($status->Data_length + $status->Index_length) / 1024 / 1024, 2),
  148. 'index_size_mb' => round($status->Index_length / 1024 / 1024, 2),
  149. 'data_free_mb' => round($status->Data_free / 1024 / 1024, 2),
  150. 'avg_row_length' => $status->Avg_row_length,
  151. 'auto_increment' => $status->Auto_increment,
  152. 'engine' => $status->Engine,
  153. 'collation' => $status->Collation,
  154. ];
  155. }
  156. /**
  157. * 分析表结构
  158. *
  159. * @param string $tableName 表名
  160. * @return array 结构信息
  161. */
  162. private static function analyzeTableStructure(string $tableName): array
  163. {
  164. $columns = Schema::getColumnListing($tableName);
  165. $timeFields = [];
  166. $userFields = [];
  167. $statusFields = [];
  168. $primaryKey = null;
  169. foreach ($columns as $column) {
  170. $columnInfo = DB::select("SHOW COLUMNS FROM {$tableName} LIKE ?", [$column]);
  171. if (!empty($columnInfo)) {
  172. $info = $columnInfo[0];
  173. // 检查主键
  174. if ($info->Key === 'PRI') {
  175. $primaryKey = $column;
  176. }
  177. // 检查时间字段
  178. if (static::isTimeField($column, $info->Type)) {
  179. $timeFields[] = $column;
  180. }
  181. // 检查用户字段
  182. if (static::isUserField($column)) {
  183. $userFields[] = $column;
  184. }
  185. // 检查状态字段
  186. if (static::isStatusField($column)) {
  187. $statusFields[] = $column;
  188. }
  189. }
  190. }
  191. // 获取时间范围
  192. $timeRange = static::getTimeRange($tableName, $timeFields);
  193. return [
  194. 'columns' => $columns,
  195. 'primary_key' => $primaryKey,
  196. 'time_fields' => $timeFields,
  197. 'user_fields' => $userFields,
  198. 'status_fields' => $statusFields,
  199. 'time_range' => $timeRange,
  200. ];
  201. }
  202. /**
  203. * 判断是否为时间字段
  204. *
  205. * @param string $columnName 字段名
  206. * @param string $columnType 字段类型
  207. * @return bool
  208. */
  209. private static function isTimeField(string $columnName, string $columnType): bool
  210. {
  211. // 按字段名判断
  212. $timeFieldNames = [
  213. 'created_at', 'updated_at', 'deleted_at',
  214. 'start_time', 'end_time', 'expire_time',
  215. 'login_time', 'logout_time', 'last_login',
  216. 'published_at', 'completed_at', 'processed_at'
  217. ];
  218. if (in_array($columnName, $timeFieldNames)) {
  219. return true;
  220. }
  221. // 按字段名模式判断
  222. if (preg_match('/(time|date|at)$/i', $columnName)) {
  223. return true;
  224. }
  225. // 按字段类型判断
  226. if (preg_match('/^(datetime|timestamp|date)/i', $columnType)) {
  227. return true;
  228. }
  229. return false;
  230. }
  231. /**
  232. * 判断是否为用户字段
  233. *
  234. * @param string $columnName 字段名
  235. * @return bool
  236. */
  237. private static function isUserField(string $columnName): bool
  238. {
  239. $userFieldNames = [
  240. 'user_id', 'urs_user_id', 'created_by', 'updated_by',
  241. 'owner_id', 'author_id', 'operator_id'
  242. ];
  243. return in_array($columnName, $userFieldNames);
  244. }
  245. /**
  246. * 判断是否为状态字段
  247. *
  248. * @param string $columnName 字段名
  249. * @return bool
  250. */
  251. private static function isStatusField(string $columnName): bool
  252. {
  253. $statusFieldNames = [
  254. 'status', 'state', 'is_active', 'is_enabled',
  255. 'is_deleted', 'is_published', 'is_completed'
  256. ];
  257. return in_array($columnName, $statusFieldNames);
  258. }
  259. /**
  260. * 获取时间范围
  261. *
  262. * @param string $tableName 表名
  263. * @param array $timeFields 时间字段
  264. * @return array 时间范围
  265. */
  266. private static function getTimeRange(string $tableName, array $timeFields): array
  267. {
  268. if (empty($timeFields)) {
  269. return [];
  270. }
  271. $timeField = $timeFields[0]; // 使用第一个时间字段
  272. try {
  273. $result = DB::table($tableName)
  274. ->selectRaw("MIN({$timeField}) as min_time, MAX({$timeField}) as max_time")
  275. ->whereNotNull($timeField)
  276. ->first();
  277. return [
  278. 'field' => $timeField,
  279. 'min_time' => $result->min_time,
  280. 'max_time' => $result->max_time,
  281. ];
  282. } catch (\Exception $e) {
  283. return [];
  284. }
  285. }
  286. /**
  287. * 识别模块名称
  288. *
  289. * @param string $tableName 表名
  290. * @return string 模块名称
  291. */
  292. private static function detectModuleName(string $tableName): string
  293. {
  294. // 移除表前缀
  295. $name = preg_replace('/^kku_/', '', $tableName);
  296. // 常见模块映射
  297. $moduleMap = [
  298. 'farm' => 'Farm',
  299. 'item' => 'GameItems',
  300. 'pet' => 'Pet',
  301. 'user' => 'User',
  302. 'fund' => 'Fund',
  303. 'mex' => 'Mex',
  304. 'promotion' => 'Promotion',
  305. 'config' => 'Config',
  306. 'log' => 'Log',
  307. 'cache' => 'Cache',
  308. 'session' => 'Session',
  309. ];
  310. foreach ($moduleMap as $prefix => $module) {
  311. if (str_starts_with($name, $prefix . '_')) {
  312. return $module;
  313. }
  314. }
  315. // 尝试从表名中提取模块名
  316. $parts = explode('_', $name);
  317. if (!empty($parts)) {
  318. return ucfirst($parts[0]);
  319. }
  320. return 'Unknown';
  321. }
  322. /**
  323. * 生成默认清理配置
  324. *
  325. * @param DATA_CATEGORY $dataCategory 数据分类
  326. * @param array $structure 表结构
  327. * @return array 默认配置
  328. */
  329. private static function generateDefaultConfig(DATA_CATEGORY $dataCategory, array $structure): array
  330. {
  331. $cleanupType = $dataCategory->getDefaultCleanupType();
  332. $priority = $dataCategory->getDefaultPriority();
  333. $isEnabled = $dataCategory->isDefaultEnabled();
  334. $conditions = null;
  335. $batchSize = 1000;
  336. // 根据清理类型生成默认条件
  337. if ($cleanupType === CLEANUP_TYPE::DELETE_BY_TIME && !empty($structure['time_fields'])) {
  338. $conditions = [
  339. 'time_field' => $structure['time_fields'][0],
  340. 'time_condition' => 'older_than',
  341. 'time_value' => 30,
  342. 'time_unit' => 'days'
  343. ];
  344. }
  345. // 根据数据分类调整批处理大小
  346. if ($dataCategory === DATA_CATEGORY::LOG_DATA) {
  347. $batchSize = 5000; // 日志数据可以使用更大的批处理
  348. } elseif ($dataCategory === DATA_CATEGORY::CACHE_DATA) {
  349. $batchSize = 10000; // 缓存数据可以使用更大的批处理
  350. }
  351. $description = static::generateDescription($dataCategory, $cleanupType, $conditions);
  352. return [
  353. 'cleanup_type' => $cleanupType->value,
  354. 'conditions' => $conditions,
  355. 'is_enabled' => $isEnabled,
  356. 'priority' => $priority,
  357. 'batch_size' => $batchSize,
  358. 'description' => $description,
  359. ];
  360. }
  361. /**
  362. * 生成配置描述
  363. *
  364. * @param DATA_CATEGORY $dataCategory 数据分类
  365. * @param CLEANUP_TYPE $cleanupType 清理类型
  366. * @param array|null $conditions 清理条件
  367. * @return string 描述
  368. */
  369. private static function generateDescription(DATA_CATEGORY $dataCategory, CLEANUP_TYPE $cleanupType, ?array $conditions): string
  370. {
  371. $description = $dataCategory->getDescription() . ' - ' . $cleanupType->getDescription();
  372. if ($conditions && $cleanupType === CLEANUP_TYPE::DELETE_BY_TIME) {
  373. $description .= ",保留{$conditions['time_value']}{$conditions['time_unit']}内的数据";
  374. }
  375. return $description;
  376. }
  377. /**
  378. * 更新表统计信息
  379. *
  380. * @param string $tableName 表名
  381. * @param array $tableInfo 表信息
  382. * @param array $structure 表结构
  383. * @return void
  384. */
  385. private static function updateTableStats(string $tableName, array $tableInfo, array $structure): void
  386. {
  387. $statsData = [
  388. 'table_name' => $tableName,
  389. 'record_count' => $tableInfo['record_count'],
  390. 'table_size_mb' => $tableInfo['table_size_mb'],
  391. 'index_size_mb' => $tableInfo['index_size_mb'],
  392. 'data_free_mb' => $tableInfo['data_free_mb'],
  393. 'avg_row_length' => $tableInfo['avg_row_length'],
  394. 'auto_increment' => $tableInfo['auto_increment'],
  395. 'oldest_record_time' => $structure['time_range']['min_time'] ?? null,
  396. 'newest_record_time' => $structure['time_range']['max_time'] ?? null,
  397. 'scan_time' => now(),
  398. ];
  399. // 这里应该创建 CleanupTableStats 模型,暂时跳过
  400. // CleanupTableStats::create($statsData);
  401. }
  402. }