TableScannerLogic.php 15 KB

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