Files
cburn/internal/pipeline/loader.go
teernisse 892f578565 fix: linter compliance and code quality improvements across codebase
Address golangci-lint findings and improve error handling throughout:

Package doc comments:
- Add canonical "// Package X ..." comments to source, model, config,
  pipeline, cli, store, and main packages for godoc compliance.

Security & correctness:
- Fix directory permissions 0o755 -> 0o750 in store/cache.go Open()
  (gosec G301: restrict group write on cache directory)
- Fix config.Save() to check encoder error before closing file, preventing
  silent data loss on encode failure
- Add //nolint:gosec annotations with justifications on intentional
  patterns (constructed file paths, manual bounds checking, config fields)
- Add //nolint:nilerr on intentional error-swallowing in scanner WalkDir
- Add //nolint:revive on stuttering type names (ModelStats, ModelUsage)
  that would break too many call sites to rename

Performance (perfsprint):
- Replace fmt.Sprintf("%d", n) with strconv.FormatInt(n, 10) in format.go
  FormatTokens() and FormatNumber() hot paths
- Clean up redundant fmt.Sprintf patterns in FormatCost and FormatDelta

Code cleanup:
- Convert if-else chain to switch in parser.go skipJSONString() for clarity
- Remove unused indexedResult struct from pipeline/loader.go
- Add deferred cache.Close() in pipeline/bench_test.go to prevent leaks
- Add deferred cache.Close() in cmd/root.go data loading path
- Fix doc comment alignment in scanner.go decodeProjectName
- Remove trailing blank line in cmd/costs.go
- Fix duplicate "/day" suffix in cmd/summary.go cost-per-day formatting
- Rename shadowed variable 'max' -> 'maxVal' in cli/render.go Sparkline
2026-02-20 16:07:26 -05:00

113 lines
2.3 KiB
Go

package pipeline
import (
"fmt"
"runtime"
"sync"
"sync/atomic"
"cburn/internal/model"
"cburn/internal/source"
)
// LoadResult holds the output of the full data loading pipeline.
type LoadResult struct {
Sessions []model.SessionStats
TotalFiles int
ParsedFiles int
ParseErrors int
FileErrors int
ProjectCount int
}
// ProgressFunc is called during loading to report progress.
// current is the number of files processed so far, total is the total count.
type ProgressFunc func(current, total int)
// Load discovers and parses all session files from the Claude data directory.
// It uses a bounded worker pool for parallel parsing.
func Load(claudeDir string, includeSubagents bool, progressFn ProgressFunc) (*LoadResult, error) {
// Discover files
files, err := source.ScanDir(claudeDir)
if err != nil {
return nil, fmt.Errorf("scanning %s: %w", claudeDir, err)
}
if len(files) == 0 {
return &LoadResult{}, nil
}
// Filter subagents if requested
var toProcess []source.DiscoveredFile
if includeSubagents {
toProcess = files
} else {
for _, f := range files {
if !f.IsSubagent {
toProcess = append(toProcess, f)
}
}
}
result := &LoadResult{
TotalFiles: len(toProcess),
ProjectCount: source.CountProjects(files),
}
if len(toProcess) == 0 {
return result, nil
}
// Parallel parsing with bounded worker pool
numWorkers := runtime.GOMAXPROCS(0)
if numWorkers < 1 {
numWorkers = 4
}
if numWorkers > len(toProcess) {
numWorkers = len(toProcess)
}
work := make(chan int, len(toProcess))
results := make([]source.ParseResult, len(toProcess))
var wg sync.WaitGroup
var processed atomic.Int64
// Feed work
for i := range toProcess {
work <- i
}
close(work)
// Spawn workers
wg.Add(numWorkers)
for w := 0; w < numWorkers; w++ {
go func() {
defer wg.Done()
for idx := range work {
results[idx] = source.ParseFile(toProcess[idx])
n := processed.Add(1)
if progressFn != nil {
progressFn(int(n), len(toProcess))
}
}
}()
}
wg.Wait()
// Collect results
for _, pr := range results {
if pr.Err != nil {
result.FileErrors++
continue
}
result.ParsedFiles++
result.ParseErrors += pr.ParseErrors
if pr.Stats.APICalls > 0 || pr.Stats.UserMessages > 0 {
result.Sessions = append(result.Sessions, pr.Stats)
}
}
return result, nil
}