aggregateLogFiles is a generic helper that aggregates multiple log files It handles file discovery, parsing, domain deduplication, and sorting
( logsDir string, globPattern string, verbose bool, parser LogParser[T], newAnalysis func() T, )
| 39 | // aggregateLogFiles is a generic helper that aggregates multiple log files |
| 40 | // It handles file discovery, parsing, domain deduplication, and sorting |
| 41 | func aggregateLogFiles[T MutableLogAnalysis]( |
| 42 | logsDir string, |
| 43 | globPattern string, |
| 44 | verbose bool, |
| 45 | parser LogParser[T], |
| 46 | newAnalysis func() T, |
| 47 | ) (T, error) { |
| 48 | logAggregationLog.Printf("Aggregating log files: dir=%s, pattern=%s", logsDir, globPattern) |
| 49 | var zero T |
| 50 | |
| 51 | // Find log files matching the pattern |
| 52 | files, err := filepath.Glob(filepath.Join(logsDir, globPattern)) |
| 53 | if err != nil { |
| 54 | logAggregationLog.Printf("Failed to find log files with pattern '%s': %v", globPattern, err) |
| 55 | return zero, fmt.Errorf("failed to find log files: %w", err) |
| 56 | } |
| 57 | |
| 58 | if len(files) == 0 { |
| 59 | logAggregationLog.Printf("No log files found matching pattern '%s' in %s", globPattern, logsDir) |
| 60 | if verbose { |
| 61 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage("No log files found in "+logsDir)) |
| 62 | } |
| 63 | return zero, nil |
| 64 | } |
| 65 | |
| 66 | logAggregationLog.Printf("Found %d log files to aggregate", len(files)) |
| 67 | |
| 68 | if verbose { |
| 69 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Analyzing %d log files from %s", len(files), logsDir))) |
| 70 | } |
| 71 | |
| 72 | // Initialize aggregated analysis |
| 73 | aggregated := newAnalysis() |
| 74 | |
| 75 | // Track unique domains across all files |
| 76 | allAllowedDomains := make(map[string]struct { |
| 77 | }) |
| 78 | allBlockedDomains := make(map[string]struct { |
| 79 | }) |
| 80 | |
| 81 | // Parse each file and aggregate results |
| 82 | for _, file := range files { |
| 83 | if verbose { |
| 84 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Parsing "+filepath.Base(file))) |
| 85 | } |
| 86 | |
| 87 | analysis, err := parser(file, verbose) |
| 88 | if err != nil { |
| 89 | if verbose { |
| 90 | fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("Failed to parse %s: %v", filepath.Base(file), err))) |
| 91 | } |
| 92 | continue |
| 93 | } |
| 94 | |
| 95 | // Aggregate metrics |
| 96 | aggregated.AddMetrics(analysis) |
| 97 | |
| 98 | // Collect unique domains |