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.

feat(cyclops): add Metabase revenue forecasting dashboard (#7220)

r33drichards9ce27aff3a88
diff --git a/libs/fleet/backend/database/migrations/000007_metabase_hourly_reservation_usage.sql b/libs/fleet/backend/database/migrations/000007_metabase_hourly_reservation_usage.sql
new file mode 100644
index 0000000000..03dc696fa4
--- /dev/null
+++ b/libs/fleet/backend/database/migrations/000007_metabase_hourly_reservation_usage.sql
@@ -0,0 +1,30 @@
+SET LOCAL ROLE k8s_reporting_owner;
+
+CREATE VIEW k8s_reporting.hourly_reservation_usage
+WITH (security_barrier = true, security_invoker = false) AS
+SELECT
+ collection.cluster_id,
+ collection.hour_start,
+ collection.hour_end,
+ collection.covered_seconds,
+ collection.discovered_sandboxes,
+ count(fact.fact_id)::bigint AS reservation_fact_count,
+ coalesce(sum(fact.virtual_cpu_core_seconds), 0)::numeric AS virtual_cpu_core_seconds,
+ coalesce(sum(fact.virtual_memory_byte_seconds), 0)::numeric AS virtual_memory_byte_seconds,
+ coalesce(sum(fact.ready_seconds), 0)::numeric AS ready_seconds
+FROM billing_meter.reservation_hour_collection_current AS collection
+LEFT JOIN billing_meter.reservation_hour_current AS fact
+ ON fact.cluster_id = collection.cluster_id
+ AND fact.hour_start = collection.hour_start
+ AND fact.hour_end = collection.hour_end
+GROUP BY
+ collection.cluster_id,
+ collection.hour_start,
+ collection.hour_end,
+ collection.covered_seconds,
+ collection.discovered_sandboxes;
+
+REVOKE ALL ON k8s_reporting.hourly_reservation_usage FROM PUBLIC;
+GRANT SELECT ON k8s_reporting.hourly_reservation_usage TO k8s_metabase;
+
+RESET ROLE;
diff --git a/libs/fleet/backend/database/migrator.go b/libs/fleet/backend/database/migrator.go
index 6886d482d8..db48d2fcfc 100644
--- a/libs/fleet/backend/database/migrator.go
+++ b/libs/fleet/backend/database/migrator.go
@@ -647,19 +647,21 @@ func validateReportingBoundary(ctx context.Context, transaction pgx.Tx) error {
return fmt.Errorf("reporting schema owner is %s, want k8s_reporting_owner", schemaOwner)
}
- var relationKind, viewOwner string
- if err := transaction.QueryRow(ctx, `
- select relation.relkind::text, relation.relowner::regrole::text
- from pg_class as relation
- join pg_namespace as namespace on namespace.oid = relation.relnamespace
- where namespace.nspname = 'k8s_reporting' and relation.relname = 'current_resources'`).Scan(&relationKind, &viewOwner); err != nil {
- return fmt.Errorf("read reporting relation: %w", err)
- }
- if relationKind != "v" {
- return fmt.Errorf("reporting relation current_resources has kind %s, want view", relationKind)
- }
- if viewOwner != "k8s_reporting_owner" {
- return fmt.Errorf("reporting view owner is %s, want k8s_reporting_owner", viewOwner)
+ for _, relationName := range []string{"current_resources", "hourly_reservation_usage"} {
+ var relationKind, viewOwner string
+ if err := transaction.QueryRow(ctx, `
+ select relation.relkind::text, relation.relowner::regrole::text
+ from pg_class as relation
+ join pg_namespace as namespace on namespace.oid = relation.relnamespace
+ where namespace.nspname = 'k8s_reporting' and relation.relname = $1`, relationName).Scan(&relationKind, &viewOwner); err != nil {
+ return fmt.Errorf("read reporting relation %s: %w", relationName, err)
+ }
+ if relationKind != "v" {
+ return fmt.Errorf("reporting relation %s has kind %s, want view", relationName, relationKind)
+ }
+ if viewOwner != "k8s_reporting_owner" {
+ return fmt.Errorf("reporting view %s owner is %s, want k8s_reporting_owner", relationName, viewOwner)
+ }
}
return nil
}
@@ -706,7 +708,7 @@ func readReportingACLs(ctx context.Context, transaction pgx.Tx) ([]reportingACL,
and acl.grantee <> relation.relowner
and relation.relkind in ('r', 'p', 'v', 'm', 'f', 'S')
and (acl.grantee in ('k8s_reporting_owner'::regrole, 'k8s_metabase'::regrole, 'cyclops_usage_reader'::regrole)
- or (namespace.nspname = 'k8s_reporting' and relation.relname = 'current_resources' and acl.grantee <> relation.relowner))
+ or (namespace.nspname = 'k8s_reporting' and relation.relname in ('current_resources', 'hourly_reservation_usage') and acl.grantee <> relation.relowner))
union all
select
'routine'::text,
@@ -790,6 +792,7 @@ func expectedReportingACLs() []reportingACL {
{object: reportingObject{kind: reportingObjectRelation, schema: "billing_meter", name: "reservation_hour_collection_current"}, owner: "billing_meter_owner", privilege: reportingPrivilegeSelect, grantee: "k8s_reporting_owner", grantor: "billing_meter_owner"},
{object: reportingObject{kind: reportingObjectSchema, schema: "k8s_reporting"}, owner: "k8s_reporting_owner", privilege: reportingPrivilegeUsage, grantee: "k8s_metabase", grantor: "k8s_reporting_owner"},
{object: reportingObject{kind: reportingObjectRelation, schema: "k8s_reporting", name: "current_resources"}, owner: "k8s_reporting_owner", privilege: reportingPrivilegeSelect, grantee: "k8s_metabase", grantor: "k8s_reporting_owner"},
+ {object: reportingObject{kind: reportingObjectRelation, schema: "k8s_reporting", name: "hourly_reservation_usage"}, owner: "k8s_reporting_owner", privilege: reportingPrivilegeSelect, grantee: "k8s_metabase", grantor: "k8s_reporting_owner"},
{object: reportingObject{kind: reportingObjectSchema, schema: "k8s_reporting"}, owner: "k8s_reporting_owner", privilege: reportingPrivilegeUsage, grantee: "cyclops_usage_reader", grantor: "k8s_reporting_owner"},
{object: reportingObject{kind: reportingObjectRoutine}, routineIdentity: "usage_sandbox_events", owner: "k8s_reporting_owner", privilege: reportingPrivilegeExecute, grantee: "cyclops_usage_reader", grantor: "k8s_reporting_owner"},
{object: reportingObject{kind: reportingObjectRoutine}, routineIdentity: "reservation_hour_facts", owner: "k8s_reporting_owner", privilege: reportingPrivilegeExecute, grantee: "cyclops_usage_reader", grantor: "k8s_reporting_owner"},
diff --git a/libs/fleet/backend/database/migrator_pg_test.go b/libs/fleet/backend/database/migrator_pg_test.go
index 9444f7c995..c54793596c 100644
--- a/libs/fleet/backend/database/migrator_pg_test.go
+++ b/libs/fleet/backend/database/migrator_pg_test.go
@@ -189,14 +189,14 @@ func TestRunUpgradesVersionOneAndThenNoOps(t *testing.T) {
upgrade := captureRunSummary(t, func() error {
return Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials})
})
- if upgrade.Current != 1 || upgrade.Target != 6 || upgrade.Pending != 5 || upgrade.Applied != 5 || upgrade.Skipped != 1 || upgrade.Result != "success" {
+ if upgrade.Current != 1 || upgrade.Target != 7 || upgrade.Pending != 6 || upgrade.Applied != 6 || upgrade.Skipped != 1 || upgrade.Result != "success" {
t.Fatalf("version-one upgrade summary = %+v", upgrade)
}
noOp := captureRunSummary(t, func() error {
return Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials})
})
- if noOp.Current != 6 || noOp.Target != 6 || noOp.Pending != 0 || noOp.Applied != 0 || noOp.Skipped != 6 || noOp.Result != "success" {
+ if noOp.Current != 7 || noOp.Target != 7 || noOp.Pending != 0 || noOp.Applied != 0 || noOp.Skipped != 7 || noOp.Result != "success" {
t.Fatalf("post-upgrade no-op summary = %+v", noOp)
}
}
@@ -1592,8 +1592,8 @@ func migrationLedgerRows(t *testing.T, ctx context.Context, adminURL string) []l
if err := rows.Err(); err != nil {
t.Fatal("iterate migration ledger")
}
- if len(ledger) != 6 {
- t.Fatalf("migration ledger row count = %d, want 6", len(ledger))
+ if len(ledger) != 7 {
+ t.Fatalf("migration ledger row count = %d, want 7", len(ledger))
}
for index, want := range []struct {
version int64
@@ -1605,6 +1605,7 @@ func migrationLedgerRows(t *testing.T, ctx context.Context, adminURL string) []l
{4, "000004_filter_invalid_usage_sandbox_events.sql"},
{5, "000005_hourly_reservation_meter.sql"},
{6, "000006_chat_conversations.sql"},
+ {7, "000007_metabase_hourly_reservation_usage.sql"},
} {
if ledger[index].Version != want.version || ledger[index].ApplicationOrder != int64(index+1) || ledger[index].Filename != want.filename {
t.Fatalf("migration ledger row %d = version:%d order:%d filename:%q", index, ledger[index].Version, ledger[index].ApplicationOrder, ledger[index].Filename)
@@ -1849,6 +1850,7 @@ func assertOwnershipAndPublicACLs(t *testing.T, ctx context.Context, adminURL, m
{"k8s_state", "resource_event_outbox_usage_lookup_idx", "k8s_state_owner"},
{"k8s_api", "current_resources", "k8s_state_owner"},
{"k8s_reporting", "current_resources", "k8s_reporting_owner"},
+ {"k8s_reporting", "hourly_reservation_usage", "k8s_reporting_owner"},
} {
var owner string
if err := connection.QueryRow(ctx, `select relation.relowner::regrole::text from pg_class relation join pg_namespace namespace on namespace.oid = relation.relnamespace where namespace.nspname = $1 and relation.relname = $2`, want.schema, want.name).Scan(&owner); err != nil {
@@ -1876,7 +1878,7 @@ func assertOwnershipAndPublicACLs(t *testing.T, ctx context.Context, adminURL, m
for _, schema := range []string{"k8s_state", "k8s_api", "k8s_reporting", "cyclops_migrations"} {
assertNoPublicSchemaPrivilege(t, ctx, connection, schema, "USAGE")
}
- for _, relation := range []string{"public.github_trust_policies", "public.chat_conversations", "k8s_state.resource_state", "k8s_state.resource_event_outbox", "k8s_api.current_resources", "k8s_reporting.current_resources", "cyclops_migrations.applied_migrations"} {
+ for _, relation := range []string{"public.github_trust_policies", "public.chat_conversations", "k8s_state.resource_state", "k8s_state.resource_event_outbox", "k8s_api.current_resources", "k8s_reporting.current_resources", "k8s_reporting.hourly_reservation_usage", "cyclops_migrations.applied_migrations"} {
assertNoPublicTablePrivilege(t, ctx, connection, relation)
}
}
@@ -1937,7 +1939,7 @@ func assertRuntimeLedgerAccess(t *testing.T, ctx context.Context, credentials Cr
var count int
err := connection.QueryRow(ctx, `select count(*) from cyclops_migrations.applied_migrations`).Scan(&count)
connection.Close(ctx)
- if err != nil || count != 6 {
+ if err != nil || count != 7 {
t.Errorf("%s ledger select = count:%d err:%v", role, count, err)
}
assertStatementFails(t, ctx, databaseURL, `insert into cyclops_migrations.applied_migrations (version, filename, sha256) values (99, 'invalid.sql', 'invalid')`)
@@ -2217,6 +2219,9 @@ func assertMetabaseBoundary(t *testing.T, ctx context.Context, inspectionURL, me
if err := connection.QueryRow(ctx, `select count(*) from k8s_reporting.current_resources where cluster_id = 'migration-test'`).Scan(&count); err != nil || count != 4 {
t.Errorf("metabase cannot read reporting view: count=%d err=%v", count, err)
}
+ if err := connection.QueryRow(ctx, `select count(*) from k8s_reporting.hourly_reservation_usage`).Scan(&count); err != nil {
+ t.Errorf("metabase cannot read hourly reservation usage view: err=%v", err)
+ }
var readOnly string
if err := connection.QueryRow(ctx, `show default_transaction_read_only`).Scan(&readOnly); err != nil || readOnly != "on" {
t.Errorf("metabase default_transaction_read_only = %q err=%v, want on", readOnly, err)
@@ -2239,6 +2244,7 @@ func assertMetabaseBoundary(t *testing.T, ctx context.Context, inspectionURL, me
`select 1 from k8s_api.current_resources limit 1`,
`select 1 from public.github_trust_policies limit 1`,
`delete from k8s_reporting.current_resources`,
+ `delete from k8s_reporting.hourly_reservation_usage`,
} {
assertStatementFails(t, ctx, metabaseURL, statement)
}
@@ -2379,6 +2385,7 @@ func assertExactReportingACLContract(t *testing.T, ctx context.Context, connecti
('relation:billing_meter.reservation_hour_collection_current', 'SELECT', 'k8s_reporting_owner', 'billing_meter_owner', false),
('schema:k8s_reporting', 'USAGE', 'k8s_metabase', 'k8s_reporting_owner', false),
('relation:k8s_reporting.current_resources', 'SELECT', 'k8s_metabase', 'k8s_reporting_owner', false),
+ ('relation:k8s_reporting.hourly_reservation_usage', 'SELECT', 'k8s_metabase', 'k8s_reporting_owner', false),
('schema:k8s_reporting', 'USAGE', 'cyclops_usage_reader', 'k8s_reporting_owner', false),
('routine:k8s_reporting.usage_sandbox_events(text,timestamp with time zone,timestamp with time zone)', 'EXECUTE', 'cyclops_usage_reader', 'k8s_reporting_owner', false),
('routine:k8s_reporting.reservation_hour_facts(text,timestamp with time zone,timestamp with time zone)', 'EXECUTE', 'cyclops_usage_reader', 'k8s_reporting_owner', false),
@@ -2409,7 +2416,7 @@ func assertExactReportingACLContract(t *testing.T, ctx context.Context, connecti
from pg_class as relation
join pg_namespace as namespace on namespace.oid = relation.relnamespace
join lateral aclexplode(relation.relacl) as acl on true
- where namespace.nspname = 'k8s_reporting' and relation.relname = 'current_resources'
+ where namespace.nspname = 'k8s_reporting' and relation.relname in ('current_resources', 'hourly_reservation_usage')
and acl.grantee <> relation.relowner
and (acl.grantee <> 'k8s_metabase'::regrole or acl.privilege_type <> 'SELECT' or acl.is_grantable)
union all
diff --git a/libs/fleet/backend/database/migrator_test.go b/libs/fleet/backend/database/migrator_test.go
index cabd3d9ae3..21c7e8005b 100644
--- a/libs/fleet/backend/database/migrator_test.go
+++ b/libs/fleet/backend/database/migrator_test.go
@@ -520,8 +520,8 @@ func TestEmbeddedMigrationsAreOrderedAndImmutable(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- if len(files) != 6 {
- t.Fatalf("expected exactly six migrations, got %d", len(files))
+ if len(files) != 7 {
+ t.Fatalf("expected exactly seven migrations, got %d", len(files))
}
initial := files[0]
if initial.Version != 1 || initial.Name != "000001_initial_schema.sql" {
@@ -645,6 +645,21 @@ func TestEmbeddedMigrationsAreOrderedAndImmutable(t *testing.T) {
t.Errorf("chat conversation migration is missing contract %q", expected)
}
}
+
+ metabaseUsage := files[6]
+ if metabaseUsage.Version != 7 || metabaseUsage.Name != "000007_metabase_hourly_reservation_usage.sql" {
+ t.Fatalf("expected version 7 Metabase usage migration, got version=%d name=%q", metabaseUsage.Version, metabaseUsage.Name)
+ }
+ for _, expected := range []string{
+ "CREATE VIEW k8s_reporting.hourly_reservation_usage",
+ "FROM billing_meter.reservation_hour_collection_current AS collection",
+ "LEFT JOIN billing_meter.reservation_hour_current AS fact",
+ "GRANT SELECT ON k8s_reporting.hourly_reservation_usage TO k8s_metabase",
+ } {
+ if !strings.Contains(metabaseUsage.SQL, expected) {
+ t.Errorf("Metabase usage migration is missing contract %q", expected)
+ }
+ }
}
func TestReservationMeterMigrationRetainsRecordedChecksum(t *testing.T) {