Skip to content

πŸ—ƒοΈ refactor(cache): dedupe IOTokenBuckets/CostBuckets sub-day pair via subDayBuckets genericΒ #422

@martinciu

Description

@martinciu

πŸ” Problem

IOTokenBuckets (pkg/cache/cache.go:704-758) and CostBuckets (cache.go:859-915) are ~55-line near-clones: identical bucket-align preamble, identical strftime bucket-epoch SQL (differing in one line β€” the SUM expression), identical map-fill and bucket-fill loops, identical slog tail. Only the value type (int64 vs float64) and built struct differ.

#338 already solved this exact problem for the daily path with the generic dailyBuckets[V, T] (cache.go:775-830) β€” the sub-day path never got the same treatment.

πŸͺ¦ Drift is proven: the COALESCE defensiveness is present in IOTokenBuckets (line ~720) but absent in CostBuckets (line ~875). The comment on CostBuckets literally says "Mirrors IOTokenBuckets exactly" β€” a manual sync contract.

πŸ› οΈ Suggested shape

Extract a subDayBuckets[V int64 | float64, T any](ctx, c, aggExpr string, dur, from, to time.Time, build func(time.Time, V) T) mirroring dailyBuckets, leaving both public methods as ~6-line dispatchers.

⚠️ Notes

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestgoPull requests that update go codeimportance: lowPolish, refactor, or nice-to-havesize: sSmall β€” about an hour

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions