Files
mtusi2026lab6/internal/adapter/postgres/queue_stats.go
T

32 lines
644 B
Go

package postgres
import (
"context"
"fmt"
"time"
)
func (s *PostgreSQLStore) AttachmentQueueStats(
ctx context.Context,
now time.Time,
) (int64, float64, error) {
queryContext, cancel := s.withTimeout(ctx)
defer cancel()
var depth int64
var oldestAge float64
err := s.pool.QueryRow(queryContext, `
SELECT
COUNT(*),
COALESCE(EXTRACT(EPOCH FROM ($1::timestamptz - MIN(created_at))), 0)
FROM attachment_jobs
WHERE status = 'queued'
`, now).Scan(&depth, &oldestAge)
if err != nil {
return 0, 0, fmt.Errorf("read attachment queue stats: %w", err)
}
if oldestAge < 0 {
oldestAge = 0
}
return depth, oldestAge, nil
}