package storage import ( "context" "gitea.privcloud.tech/mtusi2026/internal/service/observation" "github.com/google/uuid" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" "time" ) func (r *AttachmentsRepo) AcceptSLO(ctx context.Context, tx pgx.Tx, id uuid.UUID, accepted, deadline time.Time) error { _, e := tx.Exec(ctx, `INSERT INTO job_slo(job_id,accepted_at,deadline_at)VALUES($1,$2,$3)`, id, accepted, deadline) return e } func (r *AttachmentsRepo) ReadySLO(ctx context.Context, tx pgx.Tx, id uuid.UUID, ready time.Time) error { _, e := tx.Exec(ctx, `UPDATE job_slo SET ready_at=COALESCE(ready_at,$2) WHERE job_id=$1`, id, ready) return e } type ObservationRepo struct{ Pool *pgxpool.Pool } func (r *ObservationRepo) Pending(ctx context.Context) ([]observation.Sample, error) { rows, e := r.Pool.Query(ctx, `SELECT job_id,accepted_at,deadline_at,ready_at,failed_at FROM job_slo WHERE outcome IS NULL ORDER BY deadline_at LIMIT 500`) if e != nil { return nil, e } defer rows.Close() out := []observation.Sample{} for rows.Next() { var x observation.Sample if e = rows.Scan(&x.ID, &x.Accepted, &x.Deadline, &x.Ready, &x.Failed); e != nil { return nil, e } out = append(out, x) } return out, rows.Err() } func (r *ObservationRepo) Decide(ctx context.Context, id uuid.UUID, outcome string, at time.Time) error { _, e := r.Pool.Exec(ctx, `UPDATE job_slo SET outcome=$2,evaluated_at=$3 WHERE job_id=$1 AND outcome IS NULL`, id, outcome, at) return e } func (r *ObservationRepo) Snapshot(ctx context.Context) (x observation.Snapshot, e error) { e = r.Pool.QueryRow(ctx, `SELECT (SELECT count(*) FROM attachment_jobs WHERE state IN ('queued','processing')), (SELECT count(*) FROM outbox WHERE status IN ('pending','processing','failed')), COALESCE((SELECT EXTRACT(EPOCH FROM clock_timestamp()-min(created_at)) FROM attachment_jobs WHERE state IN ('queued','processing')),0), (SELECT count(*) FROM job_slo WHERE outcome='good'), (SELECT count(*) FROM job_slo WHERE outcome='bad'), (SELECT count(*) FROM pg_stat_activity WHERE wait_event_type='Lock')`).Scan(&x.Queued, &x.Outbox, &x.Oldest, &x.Good, &x.Bad, &x.Locked) return } func (r *AttachmentsRepo) FailSLO(ctx context.Context, tx pgx.Tx, id uuid.UUID, at time.Time) error { _, e := tx.Exec(ctx, `UPDATE job_slo SET failed_at=COALESCE(failed_at,$2) WHERE job_id=$1`, id, at) return e }