Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(usage): bind reservation status query parameters (#7159)

r33drichardsf5a5726aa420
diff --git a/libs/fleet/backend/usage/postgres.go b/libs/fleet/backend/usage/postgres.go
index 0b6e389ba8..1297cbac7e 100644
--- a/libs/fleet/backend/usage/postgres.go
+++ b/libs/fleet/backend/usage/postgres.go
@@ -100,13 +100,16 @@ const reservationFactsQuery = `select namespace, sandbox_uid, sandbox_name, pool
ready_seconds, covered_seconds
from k8s_reporting.reservation_hour_facts($1, $2, $3)`
+const reservationStatusQuery = `select data_as_of, complete
+from k8s_reporting.reservation_meter_status($1, $2, $3)`
+
func (s *PostgresEventStore) Reservations(ctx context.Context, tenant, cluster string, start, end time.Time) ([]ReservationFact, time.Time, bool, error) {
if strings.TrimSpace(tenant) == "" || strings.TrimSpace(cluster) == "" || start.IsZero() || end.IsZero() || !end.After(start) {
return nil, time.Time{}, false, fmt.Errorf("invalid reservation query")
}
var asOf time.Time
var complete bool
- if err := s.pool.QueryRow(ctx, `select data_as_of, complete from k8s_reporting.reservation_meter_status(, , )`, cluster, start, end).Scan(&asOf, &complete); err != nil {
+ if err := s.pool.QueryRow(ctx, reservationStatusQuery, cluster, start, end).Scan(&asOf, &complete); err != nil {
return nil, time.Time{}, false, fmt.Errorf("query reservation meter status: %w", err)
}
rows, err := s.pool.Query(ctx, reservationFactsQuery, tenant, start, end)
diff --git a/libs/fleet/backend/usage/postgres_test.go b/libs/fleet/backend/usage/postgres_test.go
index aada4101f0..949376c5af 100644
--- a/libs/fleet/backend/usage/postgres_test.go
+++ b/libs/fleet/backend/usage/postgres_test.go
@@ -21,6 +21,18 @@ func TestUsageSandboxEventsQueryIsFixed(t *testing.T) {
}
}
+func TestReservationQueriesAreFixed(t *testing.T) {
+ t.Parallel()
+ const wantStatus = "select data_as_of, complete\nfrom k8s_reporting.reservation_meter_status($1, $2, $3)"
+ if reservationStatusQuery != wantStatus {
+ t.Fatalf("unexpected status query: %q", reservationStatusQuery)
+ }
+ const wantFacts = "select namespace, sandbox_uid, sandbox_name, pool_name, runtime,\n hour_start, hour_end, virtual_cpu_core_seconds, virtual_memory_byte_seconds,\n ready_seconds, covered_seconds\nfrom k8s_reporting.reservation_hour_facts($1, $2, $3)"
+ if reservationFactsQuery != wantFacts {
+ t.Fatalf("unexpected facts query: %q", reservationFactsQuery)
+ }
+}
+
func TestNewPostgresEventStoreAcceptsUsageReaderAndCloses(t *testing.T) {
t.Parallel()
store, err := NewPostgresEventStore(context.Background(), "postgres://cyclops_usage_reader:[email protected]:1/cyclops")