BaseLogCollector.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. namespace App\Module\Game\Logics\UserLogCollectors;
  3. use App\Module\Game\Services\UserLogService;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Support\Facades\Log;
  6. /**
  7. * 用户日志收集器基类
  8. *
  9. * 为各个模块的日志收集器提供基础功能
  10. */
  11. abstract class BaseLogCollector
  12. {
  13. /**
  14. * 收集器名称
  15. *
  16. * @var string
  17. */
  18. protected string $collectorName;
  19. /**
  20. * 源表名
  21. *
  22. * @var string
  23. */
  24. protected string $sourceTable;
  25. /**
  26. * 源类型
  27. *
  28. * @var string
  29. */
  30. protected string $sourceType;
  31. /**
  32. * 最大处理记录数
  33. *
  34. * @var int
  35. */
  36. protected int $maxRecords = 1000;
  37. /**
  38. * 构造函数
  39. */
  40. public function __construct()
  41. {
  42. $this->collectorName = static::class;
  43. }
  44. /**
  45. * 收集日志
  46. *
  47. * @return int 处理的记录数
  48. */
  49. public function collect(): int
  50. {
  51. try {
  52. // 优先使用时间线处理,如果没有时间戳记录则回退到ID处理
  53. $lastProcessedTimestamp = $this->getLastProcessedTimestamp();
  54. if ($lastProcessedTimestamp > 0) {
  55. return $this->collectByTimeline();
  56. } else {
  57. return $this->collectByIdWithTimelineUpgrade();
  58. }
  59. } catch (\Exception $e) {
  60. Log::error("日志收集失败", [
  61. 'collector' => $this->collectorName,
  62. 'error' => $e->getMessage(),
  63. 'trace' => $e->getTraceAsString()
  64. ]);
  65. return 0;
  66. }
  67. }
  68. /**
  69. * 按时间线收集日志
  70. *
  71. * @return int 处理的记录数
  72. */
  73. private function collectByTimeline(): int
  74. {
  75. $lastProcessedTimestamp = $this->getLastProcessedTimestamp();
  76. $records = $this->getNewRecordsByTime($lastProcessedTimestamp);
  77. if ($records->isEmpty()) {
  78. return 0;
  79. }
  80. $processedCount = 0;
  81. $userLogs = [];
  82. $maxTimestamp = $lastProcessedTimestamp;
  83. foreach ($records as $record) {
  84. try {
  85. $userLogData = $this->convertToUserLog($record);
  86. if ($userLogData) {
  87. $userLogs[] = $userLogData;
  88. $processedCount++;
  89. }
  90. $recordTimestamp = $this->getRecordTimestamp($record);
  91. if ($recordTimestamp > $maxTimestamp) {
  92. $maxTimestamp = $recordTimestamp;
  93. }
  94. } catch (\Exception $e) {
  95. Log::error("转换日志记录失败", [
  96. 'collector' => $this->collectorName,
  97. 'record_id' => $record->id ?? null,
  98. 'error' => $e->getMessage()
  99. ]);
  100. }
  101. }
  102. if (!empty($userLogs)) {
  103. UserLogService::batchLog($userLogs);
  104. }
  105. // 不再需要手动更新时间戳,进度通过user_logs表自动追踪
  106. Log::info("时间线日志收集完成", [
  107. 'collector' => $this->collectorName,
  108. 'processed_count' => $processedCount,
  109. 'last_timestamp' => $maxTimestamp
  110. ]);
  111. return $processedCount;
  112. }
  113. /**
  114. * 按ID收集日志并升级到时间线处理
  115. *
  116. * @return int 处理的记录数
  117. */
  118. private function collectByIdWithTimelineUpgrade(): int
  119. {
  120. $lastProcessedId = $this->getLastProcessedId();
  121. $records = $this->getNewRecords($lastProcessedId);
  122. if ($records->isEmpty()) {
  123. return 0;
  124. }
  125. $processedCount = 0;
  126. $userLogs = [];
  127. $maxId = $lastProcessedId;
  128. $maxTimestamp = 0;
  129. foreach ($records as $record) {
  130. try {
  131. $userLogData = $this->convertToUserLog($record);
  132. if ($userLogData) {
  133. $userLogs[] = $userLogData;
  134. $processedCount++;
  135. }
  136. if ($record->id > $maxId) {
  137. $maxId = $record->id;
  138. }
  139. $recordTimestamp = $this->getRecordTimestamp($record);
  140. if ($recordTimestamp > $maxTimestamp) {
  141. $maxTimestamp = $recordTimestamp;
  142. }
  143. } catch (\Exception $e) {
  144. Log::error("转换日志记录失败", [
  145. 'collector' => $this->collectorName,
  146. 'record_id' => $record->id ?? null,
  147. 'error' => $e->getMessage()
  148. ]);
  149. }
  150. }
  151. if (!empty($userLogs)) {
  152. UserLogService::batchLog($userLogs);
  153. }
  154. // 不再需要手动更新进度,进度通过user_logs表自动追踪
  155. Log::info("ID日志收集完成并升级到时间线", [
  156. 'collector' => $this->collectorName,
  157. 'processed_count' => $processedCount,
  158. 'last_id' => $maxId,
  159. 'last_timestamp' => $maxTimestamp
  160. ]);
  161. return $processedCount;
  162. }
  163. /**
  164. * 获取新的记录(子类实现)
  165. *
  166. * @param int $lastProcessedId 上次处理的最大ID
  167. * @return \Illuminate\Database\Eloquent\Collection
  168. */
  169. abstract protected function getNewRecords(int $lastProcessedId);
  170. /**
  171. * 按时间获取新的记录(子类实现)
  172. *
  173. * @param int $lastProcessedTimestamp 上次处理的最大时间戳
  174. * @return \Illuminate\Database\Eloquent\Collection
  175. */
  176. abstract protected function getNewRecordsByTime(int $lastProcessedTimestamp);
  177. /**
  178. * 获取记录的时间戳(子类实现)
  179. *
  180. * @param mixed $record 原始记录
  181. * @return int 时间戳
  182. */
  183. abstract protected function getRecordTimestamp($record): int;
  184. /**
  185. * 根据记录ID获取原始记录的时间戳(子类实现)
  186. *
  187. * @param int $recordId 记录ID
  188. * @return int 时间戳
  189. */
  190. abstract protected function getOriginalRecordTimestamp(int $recordId): int;
  191. /**
  192. * 公共方法:按时间获取新的记录
  193. *
  194. * @param int $lastProcessedTimestamp 上次处理的最大时间戳
  195. * @return \Illuminate\Database\Eloquent\Collection
  196. */
  197. public function getNewRecordsByTimePublic(int $lastProcessedTimestamp)
  198. {
  199. return $this->getNewRecordsByTime($lastProcessedTimestamp);
  200. }
  201. /**
  202. * 公共方法:获取记录的时间戳
  203. *
  204. * @param mixed $record 原始记录
  205. * @return int 时间戳
  206. */
  207. public function getRecordTimestampPublic($record): int
  208. {
  209. return $this->getRecordTimestamp($record);
  210. }
  211. /**
  212. * 公共方法:转换记录为用户日志数据
  213. *
  214. * @param mixed $record 原始记录
  215. * @return array|null 用户日志数据,null表示跳过
  216. */
  217. public function convertToUserLogPublic($record): ?array
  218. {
  219. return $this->convertToUserLog($record);
  220. }
  221. /**
  222. * 转换记录为用户日志数据(子类实现)
  223. *
  224. * @param mixed $record 原始记录
  225. * @return array|null 用户日志数据,null表示跳过
  226. */
  227. abstract protected function convertToUserLog($record): ?array;
  228. /**
  229. * 获取上次处理的最大ID
  230. * 从user_logs表中查询该收集器最后处理的记录ID
  231. *
  232. * @return int
  233. */
  234. protected function getLastProcessedId(): int
  235. {
  236. try {
  237. $lastLog = \App\Module\Game\Models\UserLog::where('source_table', $this->sourceTable)
  238. ->where('source_type', $this->sourceType)
  239. ->orderBy('source_id', 'desc')
  240. ->first();
  241. return $lastLog ? $lastLog->source_id : 0;
  242. } catch (\Exception $e) {
  243. Log::error("获取最后处理ID失败", [
  244. 'collector' => $this->collectorName,
  245. 'source_table' => $this->sourceTable,
  246. 'error' => $e->getMessage()
  247. ]);
  248. return 0;
  249. }
  250. }
  251. /**
  252. * 更新最后处理的ID
  253. * 不再需要手动更新,因为进度通过user_logs表自动追踪
  254. *
  255. * @param int $id
  256. * @return void
  257. */
  258. protected function updateLastProcessedId(int $id): void
  259. {
  260. // 不再需要手动更新,进度通过user_logs表自动追踪
  261. // 这个方法保留是为了兼容性
  262. }
  263. /**
  264. * 获取上次处理的最大时间戳
  265. * 从user_logs表中查询该收集器最后处理的记录时间戳
  266. *
  267. * @return int
  268. */
  269. protected function getLastProcessedTimestamp(): int
  270. {
  271. try {
  272. $lastLog = \App\Module\Game\Models\UserLog::where('source_table', $this->sourceTable)
  273. ->where('source_type', $this->sourceType)
  274. ->orderBy('created_at', 'desc')
  275. ->first();
  276. if (!$lastLog) {
  277. return 0;
  278. }
  279. // 获取原始记录的时间戳
  280. return $this->getOriginalRecordTimestamp($lastLog->source_id);
  281. } catch (\Exception $e) {
  282. Log::error("获取最后处理时间戳失败", [
  283. 'collector' => $this->collectorName,
  284. 'source_table' => $this->sourceTable,
  285. 'error' => $e->getMessage()
  286. ]);
  287. return 0;
  288. }
  289. }
  290. /**
  291. * 更新最后处理的时间戳
  292. * 不再需要手动更新,因为进度通过user_logs表自动追踪
  293. *
  294. * @param int $timestamp
  295. * @return void
  296. */
  297. protected function updateLastProcessedTimestamp(int $timestamp): void
  298. {
  299. // 不再需要手动更新,进度通过user_logs表自动追踪
  300. // 这个方法保留是为了兼容性
  301. }
  302. /**
  303. * 获取最后处理ID的缓存键
  304. *
  305. * @return string
  306. */
  307. protected function getLastProcessedIdCacheKey(): string
  308. {
  309. return "user_log_collector:last_processed_id:" . $this->sourceTable;
  310. }
  311. /**
  312. * 获取最后处理时间戳的缓存键
  313. *
  314. * @return string
  315. */
  316. protected function getLastProcessedTimestampCacheKey(): string
  317. {
  318. return "user_log_collector:last_processed_timestamp:" . $this->sourceTable;
  319. }
  320. /**
  321. * 创建用户日志数据数组
  322. *
  323. * @param int $userId 用户ID
  324. * @param string $message 日志消息
  325. * @param int $sourceId 来源记录ID
  326. * @param string|null $createdAt 创建时间,null则使用当前时间
  327. * @return array
  328. */
  329. protected function createUserLogData(int $userId, string $message, int $sourceId, ?string $createdAt = null): array
  330. {
  331. return [
  332. 'user_id' => $userId,
  333. 'message' => $message,
  334. 'source_type' => $this->sourceType,
  335. 'source_id' => $sourceId,
  336. 'source_table' => $this->sourceTable,
  337. 'created_at' => $createdAt ?? now()->toDateTimeString(),
  338. ];
  339. }
  340. /**
  341. * 获取收集器名称
  342. *
  343. * @return string
  344. */
  345. public function getCollectorName(): string
  346. {
  347. return $this->collectorName;
  348. }
  349. /**
  350. * 获取源表名
  351. *
  352. * @return string
  353. */
  354. public function getSourceTable(): string
  355. {
  356. return $this->sourceTable;
  357. }
  358. /**
  359. * 获取源类型
  360. *
  361. * @return string
  362. */
  363. public function getSourceType(): string
  364. {
  365. return $this->sourceType;
  366. }
  367. /**
  368. * 重置最后处理的ID(用于重新处理)
  369. *
  370. * @return void
  371. */
  372. public function resetLastProcessedId(): void
  373. {
  374. $cacheKey = $this->getLastProcessedIdCacheKey();
  375. Cache::forget($cacheKey);
  376. }
  377. /**
  378. * 重置最后处理的时间戳(用于重新处理)
  379. *
  380. * @return void
  381. */
  382. public function resetLastProcessedTimestamp(): void
  383. {
  384. $cacheKey = $this->getLastProcessedTimestampCacheKey();
  385. Cache::forget($cacheKey);
  386. }
  387. /**
  388. * 重置所有处理进度
  389. *
  390. * @return void
  391. */
  392. public function resetAllProgress(): void
  393. {
  394. $this->resetLastProcessedId();
  395. $this->resetLastProcessedTimestamp();
  396. }
  397. }