code re-org and bugfix hanging hourly snapshot
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2026-01-21 09:12:25 +11:00
parent c7c7fd3dc9
commit fd9cc185ce
6 changed files with 113 additions and 56 deletions

View File

@@ -44,10 +44,11 @@ func boolStringFromInterface(value interface{}) string {
// latestHourlySnapshotBefore finds the most recent hourly snapshot table prior to the given time, skipping empty tables.
func latestHourlySnapshotBefore(ctx context.Context, dbConn *sqlx.DB, cutoff time.Time) (string, error) {
rows, err := dbConn.QueryxContext(ctx, `
SELECT table_name, snapshot_time
SELECT table_name, snapshot_time, snapshot_count
FROM snapshot_registry
WHERE snapshot_type = 'hourly' AND snapshot_time < ?
WHERE snapshot_type = 'hourly' AND snapshot_time < ? AND snapshot_count > 0
ORDER BY snapshot_time DESC
LIMIT 50
`, cutoff.Unix())
if err != nil {
return "", err
@@ -57,17 +58,18 @@ ORDER BY snapshot_time DESC
for rows.Next() {
var name string
var ts int64
if scanErr := rows.Scan(&name, &ts); scanErr != nil {
var count int64
if scanErr := rows.Scan(&name, &ts, &count); scanErr != nil {
continue
}
if err := db.ValidateTableName(name); err != nil {
continue
}
hasRows, err := db.TableHasRows(ctx, dbConn, name)
if err != nil {
continue
// Rely on snapshot_count to avoid costly table scans; fall back to a cheap row check only if count is zero.
if count > 0 {
return name, nil
}
if hasRows {
if hasRows, _ := db.TableHasRows(ctx, dbConn, name); hasRows {
return name, nil
}
}