option('info')) {
$this->showCollectorsInfo($manager);
return 0;
}
// 重置收集器进度
if ($this->option('reset')) {
$this->resetCollectors($manager);
return 0;
}
// 显示统计信息
if ($this->option('stats')) {
$this->showStats($manager);
return 0;
}
// 执行日志收集
return $this->executeCollection($manager);
}
/**
* 执行日志收集
*
* @param UserLogCollectorManager $manager
* @return int
*/
private function executeCollection(UserLogCollectorManager $manager): int
{
$collectorName = $this->option('collector');
try {
if ($collectorName) {
// 执行指定收集器
$this->info("执行收集器: {$collectorName}");
$result = $manager->collectByName($collectorName);
$this->displaySingleResult($result);
} else {
// 执行所有收集器
$this->info("执行所有收集器...");
$results = $manager->collectAll();
$this->displayAllResults($results);
}
return 0;
} catch (\Exception $e) {
$this->error("日志收集失败: {$e->getMessage()}");
return 1;
}
}
/**
* 显示收集器信息
*
* @param UserLogCollectorManager $manager
* @return void
*/
private function showCollectorsInfo(UserLogCollectorManager $manager): void
{
$this->info("注册的收集器信息:");
$this->line("");
$collectorsInfo = $manager->getCollectorsInfo();
foreach ($collectorsInfo as $info) {
$this->line("收集器: {$info['name']}");
$this->line(" 类名: {$info['class']}");
$this->line(" 源表: {$info['source_table']}");
$this->line(" 类型: {$info['source_type']}");
$this->line("");
}
}
/**
* 重置收集器进度
*
* @param UserLogCollectorManager $manager
* @return void
*/
private function resetCollectors(UserLogCollectorManager $manager): void
{
$collectorName = $this->option('collector');
if ($collectorName) {
if (!$manager->hasCollector($collectorName)) {
$this->error("收集器 {$collectorName} 不存在");
return;
}
$manager->resetCollector($collectorName);
$this->info("已重置收集器 {$collectorName} 的进度");
} else {
if ($this->confirm('确定要重置所有收集器的进度吗?这将从头开始收集所有日志。')) {
$manager->resetAllCollectors();
$this->info("已重置所有收集器的进度");
}
}
}
/**
* 显示统计信息
*
* @param UserLogCollectorManager $manager
* @return void
*/
private function showStats(UserLogCollectorManager $manager): void
{
$this->info("收集器统计信息:");
$this->line("");
// 这里可以添加更详细的统计信息
// 比如每个收集器的处理进度、最后处理时间等
$collectorsInfo = $manager->getCollectorsInfo();
foreach ($collectorsInfo as $info) {
$this->line("收集器: {$info['name']}");
// 获取最后处理的ID
$cacheKey = "user_log_collector:last_processed_id:{$info['source_table']}";
$lastProcessedId = \Illuminate\Support\Facades\Cache::get($cacheKey, 0);
$this->line(" 最后处理ID: {$lastProcessedId}");
$this->line("");
}
}
/**
* 显示单个收集器结果
*
* @param array $result
* @return void
*/
private function displaySingleResult(array $result): void
{
if ($result['status'] === 'success') {
$this->info("收集完成:");
$this->line(" 处理记录数: {$result['processed_count']}");
$this->line(" 执行时间: {$result['execution_time']}ms");
} else {
$this->error("收集失败:");
$this->line(" 错误信息: {$result['error']}");
}
}
/**
* 显示所有收集器结果
*
* @param array $results
* @return void
*/
private function displayAllResults(array $results): void
{
$this->info("收集完成:");
$this->line(" 总处理记录数: {$results['total_processed']}");
$this->line(" 总执行时间: {$results['total_execution_time']}ms");
$this->line("");
$this->info("各收集器详情:");
foreach ($results['collectors'] as $name => $result) {
$status = $result['status'] === 'success' ? '成功' : '失败';
$this->line(" {$name}: {$status} - 处理{$result['processed_count']}条 - {$result['execution_time']}ms");
if ($result['status'] === 'error') {
$this->line(" 错误: {$result['error']}");
}
}
}
}