bugfix hourly totals
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2026-01-28 13:27:05 +11:00
parent 1f2783fc86
commit aa6abb8cb2
2 changed files with 32 additions and 4 deletions

View File

@@ -795,22 +795,37 @@ func SaveTableReport(logger *slog.Logger, Database db.Database, ctx context.Cont
}
func addTotalsChartSheet(logger *slog.Logger, database db.Database, ctx context.Context, xlsx *excelize.File, tableName string) {
if logger == nil {
logger = slog.Default()
}
if strings.HasPrefix(tableName, "inventory_daily_summary_") {
suffix := strings.TrimPrefix(tableName, "inventory_daily_summary_")
dayStart, err := time.ParseInLocation("20060102", suffix, time.Local)
if err != nil {
logger.Debug("hourly totals skip: invalid daily summary suffix", "table", tableName, "suffix", suffix, "error", err)
return
}
dayEnd := dayStart.AddDate(0, 0, 1)
if err := EnsureSnapshotRegistry(ctx, database); err != nil {
logger.Debug("hourly totals skip: snapshot registry unavailable", "table", tableName, "error", err)
return
}
records, err := SnapshotRecordsWithFallback(ctx, database, "hourly", "inventory_hourly_", "epoch", dayStart, dayEnd.Add(2*time.Hour))
if err != nil || len(records) == 0 {
if err != nil {
logger.Debug("hourly totals skip: failed to load hourly snapshots", "table", tableName, "error", err)
return
}
if len(records) == 0 {
logger.Debug("hourly totals skip: no hourly snapshots found", "table", tableName, "window_start", dayStart, "window_end", dayEnd)
return
}
points, err := buildHourlyTotals(ctx, logger, database.DB(), records, dayStart, dayEnd)
if err != nil || len(points) == 0 {
if err != nil {
logger.Debug("hourly totals skip: build failed", "table", tableName, "error", err)
return
}
if len(points) == 0 {
logger.Debug("hourly totals skip: no hourly totals points", "table", tableName, "window_start", dayStart, "window_end", dayEnd)
return
}
writeTotalsChart(logger, xlsx, "Hourly Totals", points)
@@ -821,18 +836,30 @@ func addTotalsChartSheet(logger *slog.Logger, database db.Database, ctx context.
suffix := strings.TrimPrefix(tableName, "inventory_monthly_summary_")
monthStart, err := time.ParseInLocation("200601", suffix, time.Local)
if err != nil {
logger.Debug("daily totals skip: invalid monthly summary suffix", "table", tableName, "suffix", suffix, "error", err)
return
}
monthEnd := monthStart.AddDate(0, 1, 0)
if err := EnsureSnapshotRegistry(ctx, database); err != nil {
logger.Debug("daily totals skip: snapshot registry unavailable", "table", tableName, "error", err)
return
}
records, err := SnapshotRecordsWithFallback(ctx, database, "daily", "inventory_daily_summary_", "20060102", monthStart, monthEnd)
if err != nil || len(records) == 0 {
if err != nil {
logger.Debug("daily totals skip: failed to load daily snapshots", "table", tableName, "error", err)
return
}
if len(records) == 0 {
logger.Debug("daily totals skip: no daily snapshots found", "table", tableName, "window_start", monthStart, "window_end", monthEnd)
return
}
points, err := buildDailyTotals(ctx, database.DB(), records, true)
if err != nil || len(points) == 0 {
if err != nil {
logger.Debug("daily totals skip: build failed", "table", tableName, "error", err)
return
}
if len(points) == 0 {
logger.Debug("daily totals skip: no daily totals points", "table", tableName, "window_start", monthStart, "window_end", monthEnd)
return
}
writeTotalsChart(logger, xlsx, "Daily Totals", points)