// Package estimate derives a cheap total-size estimate for progress display. // // Only bounded MIN/MAX queries are used; COUNT(*) is never attempted. A query // that fails, times out, or whose metric does not apply silently yields no // estimate, leaving the caller to render rows and rate only. package estimate import ( "context" "database/sql" "strconv" "strings" "time" "archer/internal/progress" "archer/internal/row" "archer/internal/schema" ) // Spec describes which metric a run can expose. type Spec struct { Kind string // "day", "key" or "" for none Column string ShardKind schema.ShardKind Unit int64 Loc *time.Location } // RangeQuery returns MIN and MAX of a column as raw text. Implementations must // honor ctx so the bounded timeout is effective. type RangeQuery func(ctx context.Context, column string) (minRaw, maxRaw sql.NullString, err error) // Bounded runs q under a timeout and converts the result into a display // estimate. It returns nil instead of an error on every failure path. func Bounded(ctx context.Context, q RangeQuery, spec Spec, timeout time.Duration) *progress.Estimate { if q == nil || spec.Kind == "" || timeout <= 0 { return nil } qctx, cancel := context.WithTimeout(ctx, timeout) defer cancel() minRaw, maxRaw, err := q(qctx, spec.Column) if err != nil || !minRaw.Valid || !maxRaw.Valid { return nil } var lo, hi float64 var ok bool switch spec.Kind { case "day": lo, hi, ok = dayRange(minRaw.String, maxRaw.String, spec.ShardKind, spec.Unit, spec.Loc) case "key": lo, hi, ok = keyRange(minRaw.String, maxRaw.String) } if !ok || hi <= lo { return nil } return &progress.Estimate{Kind: spec.Kind, Lo: lo, Hi: hi} } // KeyColumn reports the scan key column when it is a single integer column, // which is the only non-date shape a cheap key-range estimate applies to. func KeyColumn(cols, types []string) (string, bool) { if len(cols) != 1 || len(types) != 1 { return "", false } if !row.IsInteger(types[0]) { return "", false } return cols[0], true } func dayRange(minRaw, maxRaw string, kind schema.ShardKind, unit int64, loc *time.Location) (float64, float64, bool) { if loc == nil { loc = time.UTC } switch kind { case schema.ShardDate, schema.ShardDateTime: lo, ok1 := daySeconds(minRaw, loc) hi, ok2 := daySeconds(maxRaw, loc) return lo, hi, ok1 && ok2 case schema.ShardEpoch: minV, err1 := strconv.ParseInt(strings.TrimSpace(minRaw), 10, 64) maxV, err2 := strconv.ParseInt(strings.TrimSpace(maxRaw), 10, 64) if err1 != nil || err2 != nil { return 0, 0, false } lo := dayStart(schema.EpochTime(minV, unit, loc), loc).Unix() hi := dayStart(schema.EpochTime(maxV, unit, loc), loc).Unix() return float64(lo), float64(hi), true } return 0, 0, false } func daySeconds(raw string, loc *time.Location) (float64, bool) { s := strings.TrimSpace(raw) if len(s) < 10 { return 0, false } day := s[:10] if strings.HasPrefix(day, "0000-") { return 0, false } t, err := time.ParseInLocation("2006-01-02", day, loc) if err != nil { return 0, false } return float64(t.Unix()), true } func keyRange(minRaw, maxRaw string) (float64, float64, bool) { minV, err1 := strconv.ParseInt(strings.TrimSpace(minRaw), 10, 64) maxV, err2 := strconv.ParseInt(strings.TrimSpace(maxRaw), 10, 64) if err1 != nil || err2 != nil { return 0, 0, false } return float64(minV), float64(maxV), true } func dayStart(t time.Time, loc *time.Location) time.Time { return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc) }