feat: zero-fill missing days in aggregator for continuous chart data

The daily aggregation now iterates from the since date through the
until date and inserts a zero-valued DailyStats entry for any day
not already present in the day map. This ensures sparklines and bar
charts render a continuous time axis with explicit zeros for idle
days, rather than connecting adjacent data points across gaps.

Also switch config file creation to os.OpenFile with explicit 0600
permissions and O_WRONLY|O_CREATE|O_TRUNC flags, matching the intent
of the original os.Create call while making the restricted permission
bits explicit for security.
This commit is contained in:
teernisse
2026-02-19 15:01:26 -05:00
parent 4d46977328
commit 04abdafa9a
2 changed files with 12 additions and 1 deletions

View File

@@ -107,7 +107,7 @@ func Save(cfg Config) error {
return fmt.Errorf("creating config dir: %w", err) return fmt.Errorf("creating config dir: %w", err)
} }
f, err := os.Create(ConfigPath()) f, err := os.OpenFile(ConfigPath(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil { if err != nil {
return fmt.Errorf("creating config file: %w", err) return fmt.Errorf("creating config file: %w", err)
} }

View File

@@ -97,6 +97,17 @@ func AggregateDays(sessions []model.SessionStats, since, until time.Time) []mode
ds.EstimatedCost += s.EstimatedCost ds.EstimatedCost += s.EstimatedCost
} }
// Fill in every day in the range so the chart shows gaps as zeros
day := since.Local().Truncate(24 * time.Hour)
end := until.Local().Truncate(24 * time.Hour)
for !day.After(end) {
dayKey := day.Format("2006-01-02")
if _, ok := dayMap[dayKey]; !ok {
dayMap[dayKey] = &model.DailyStats{Date: day}
}
day = day.AddDate(0, 0, 1)
}
// Convert to sorted slice (most recent first) // Convert to sorted slice (most recent first)
days := make([]model.DailyStats, 0, len(dayMap)) days := make([]model.DailyStats, 0, len(dayMap))
for _, ds := range dayMap { for _, ds := range dayMap {