try to fix pro-rata yet again
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Nathan Coad
2026-01-27 09:09:24 +11:00
parent 73ec80bb6f
commit 35b4a50cf6
4 changed files with 75 additions and 10 deletions

View File

@@ -1532,6 +1532,36 @@ GROUP BY
return insert, nil
}
// UpdateSummaryPresenceByWindow recomputes AvgIsPresent using CreationTime/DeletionTime overlap with the window.
func UpdateSummaryPresenceByWindow(ctx context.Context, dbConn *sqlx.DB, summaryTable string, windowStart, windowEnd int64) error {
if err := ValidateTableName(summaryTable); err != nil {
return err
}
if windowEnd <= windowStart {
return fmt.Errorf("invalid presence window: %d to %d", windowStart, windowEnd)
}
duration := float64(windowEnd - windowStart)
startExpr := `CASE WHEN "CreationTime" IS NOT NULL AND "CreationTime" > 0 AND "CreationTime" > ? THEN "CreationTime" ELSE ? END`
endExpr := `CASE WHEN "DeletionTime" IS NOT NULL AND "DeletionTime" > 0 AND "DeletionTime" < ? THEN "DeletionTime" ELSE ? END`
query := fmt.Sprintf(`
UPDATE %s
SET "AvgIsPresent" = CASE
WHEN %s > %s THEN (CAST((%s - %s) AS REAL) / ?)
ELSE 0
END
`, summaryTable, endExpr, startExpr, endExpr, startExpr)
query = dbConn.Rebind(query)
args := []interface{}{
windowEnd, windowEnd,
windowStart, windowStart,
windowEnd, windowEnd,
windowStart, windowStart,
duration,
}
_, err := execLog(ctx, dbConn, query, args...)
return err
}
// RefineCreationDeletionFromUnion walks all snapshot rows in a period and tightens CreationTime/DeletionTime
// by using the first and last observed samples and the first sample after disappearance.
func RefineCreationDeletionFromUnion(ctx context.Context, dbConn *sqlx.DB, summaryTable, unionQuery string) error {