Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
feat(metabase): add tenant-filtered revenue dashboard (#7224)
r33drichardsc5c0fbb0a4e1
diff --git a/libs/fleet/backend/database/migrations/000008_metabase_hourly_reservation_usage_excluding_tenants.sql b/libs/fleet/backend/database/migrations/000008_metabase_hourly_reservation_usage_excluding_tenants.sqlnew file mode 100644index 0000000000..1093388418--- /dev/null+++ b/libs/fleet/backend/database/migrations/000008_metabase_hourly_reservation_usage_excluding_tenants.sql@@ -0,0 +1,33 @@+SET LOCAL ROLE k8s_reporting_owner;++CREATE VIEW k8s_reporting.hourly_reservation_usage_excluding_tenants+WITH (security_barrier = true, security_invoker = false) AS+SELECT+ collection.cluster_id,+ collection.hour_start,+ collection.hour_end,+ collection.covered_seconds,+ count(DISTINCT fact.sandbox_uid)::integer AS 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+ AND fact.capsule_tenant NOT IN (+ 'user-f039fe89-9b5f-43dc-8ccd-d100ae732246',+ 'user-30a53246-881d-4f1a-8005-979f2a07933e'+ )+GROUP BY+ collection.cluster_id,+ collection.hour_start,+ collection.hour_end,+ collection.covered_seconds;++REVOKE ALL ON k8s_reporting.hourly_reservation_usage_excluding_tenants FROM PUBLIC;+GRANT SELECT ON k8s_reporting.hourly_reservation_usage_excluding_tenants TO k8s_metabase;++RESET ROLE;diff --git a/libs/fleet/backend/database/migrator.go b/libs/fleet/backend/database/migrator.goindex 3060da2cf3..f9de1c205a 100644--- a/libs/fleet/backend/database/migrator.go+++ b/libs/fleet/backend/database/migrator.go@@ -647,7 +647,7 @@ func validateReportingBoundary(ctx context.Context, transaction pgx.Tx) error {return fmt.Errorf("reporting schema owner is %s, want k8s_reporting_owner", schemaOwner)}- for _, relationName := range []string{"current_resources", "hourly_reservation_usage"} {+ for _, relationName := range []string{"current_resources", "hourly_reservation_usage", "hourly_reservation_usage_excluding_tenants"} {var relationKind, viewOwner stringif err := transaction.QueryRow(ctx, `select relation.relkind::text, relation.relowner::regrole::text@@ -708,7 +708,7 @@ func readReportingACLs(ctx context.Context, transaction pgx.Tx) ([]reportingACL,and acl.grantee <> relation.relownerand 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 in ('current_resources', 'hourly_reservation_usage') and acl.grantee <> relation.relowner))+ or (namespace.nspname = 'k8s_reporting' and relation.relname in ('current_resources', 'hourly_reservation_usage', 'hourly_reservation_usage_excluding_tenants') and acl.grantee <> relation.relowner))union allselect'routine'::text,@@ -793,6 +793,7 @@ func expectedReportingACLs() []reportingACL {{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: reportingObjectRelation, schema: "k8s_reporting", name: "hourly_reservation_usage_excluding_tenants"}, 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.goindex c54793596c..a7d9333702 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 != 7 || upgrade.Pending != 6 || upgrade.Applied != 6 || upgrade.Skipped != 1 || upgrade.Result != "success" {+ if upgrade.Current != 1 || upgrade.Target != 8 || upgrade.Pending != 7 || upgrade.Applied != 7 || 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 != 7 || noOp.Target != 7 || noOp.Pending != 0 || noOp.Applied != 0 || noOp.Skipped != 7 || noOp.Result != "success" {+ if noOp.Current != 8 || noOp.Target != 8 || noOp.Pending != 0 || noOp.Applied != 0 || noOp.Skipped != 8 || 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) []lif err := rows.Err(); err != nil {t.Fatal("iterate migration ledger")}- if len(ledger) != 7 {- t.Fatalf("migration ledger row count = %d, want 7", len(ledger))+ if len(ledger) != 8 {+ t.Fatalf("migration ledger row count = %d, want 8", len(ledger))}for index, want := range []struct {version int64@@ -1606,6 +1606,7 @@ func migrationLedgerRows(t *testing.T, ctx context.Context, adminURL string) []l{5, "000005_hourly_reservation_meter.sql"},{6, "000006_chat_conversations.sql"},{7, "000007_metabase_hourly_reservation_usage.sql"},+ {8, "000008_metabase_hourly_reservation_usage_excluding_tenants.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)@@ -1851,6 +1852,7 @@ func assertOwnershipAndPublicACLs(t *testing.T, ctx context.Context, adminURL, m{"k8s_api", "current_resources", "k8s_state_owner"},{"k8s_reporting", "current_resources", "k8s_reporting_owner"},{"k8s_reporting", "hourly_reservation_usage", "k8s_reporting_owner"},+ {"k8s_reporting", "hourly_reservation_usage_excluding_tenants", "k8s_reporting_owner"},} {var owner stringif 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 {@@ -1878,7 +1880,7 @@ func assertOwnershipAndPublicACLs(t *testing.T, ctx context.Context, adminURL, mfor _, 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", "k8s_reporting.hourly_reservation_usage", "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", "k8s_reporting.hourly_reservation_usage_excluding_tenants", "cyclops_migrations.applied_migrations"} {assertNoPublicTablePrivilege(t, ctx, connection, relation)}}@@ -1939,7 +1941,7 @@ func assertRuntimeLedgerAccess(t *testing.T, ctx context.Context, credentials Crvar count interr := connection.QueryRow(ctx, `select count(*) from cyclops_migrations.applied_migrations`).Scan(&count)connection.Close(ctx)- if err != nil || count != 7 {+ if err != nil || count != 8 {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')`)@@ -2222,6 +2224,9 @@ func assertMetabaseBoundary(t *testing.T, ctx context.Context, inspectionURL, meif 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)}+ if err := connection.QueryRow(ctx, `select count(*) from k8s_reporting.hourly_reservation_usage_excluding_tenants`).Scan(&count); err != nil {+ t.Errorf("metabase cannot read filtered hourly reservation usage view: err=%v", err)+ }var readOnly stringif 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)@@ -2245,6 +2250,7 @@ func assertMetabaseBoundary(t *testing.T, ctx context.Context, inspectionURL, me`select 1 from public.github_trust_policies limit 1`,`delete from k8s_reporting.current_resources`,`delete from k8s_reporting.hourly_reservation_usage`,+ `delete from k8s_reporting.hourly_reservation_usage_excluding_tenants`,} {assertStatementFails(t, ctx, metabaseURL, statement)}@@ -2386,6 +2392,7 @@ func assertExactReportingACLContract(t *testing.T, ctx context.Context, connecti('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),+ ('relation:k8s_reporting.hourly_reservation_usage_excluding_tenants', '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),@@ -2416,7 +2423,7 @@ func assertExactReportingACLContract(t *testing.T, ctx context.Context, connectifrom pg_class as relationjoin pg_namespace as namespace on namespace.oid = relation.relnamespacejoin lateral aclexplode(relation.relacl) as acl on true- where namespace.nspname = 'k8s_reporting' and relation.relname in ('current_resources', 'hourly_reservation_usage')+ where namespace.nspname = 'k8s_reporting' and relation.relname in ('current_resources', 'hourly_reservation_usage', 'hourly_reservation_usage_excluding_tenants')and acl.grantee <> relation.relownerand (acl.grantee <> 'k8s_metabase'::regrole or acl.privilege_type <> 'SELECT' or acl.is_grantable)union alldiff --git a/libs/fleet/backend/database/migrator_test.go b/libs/fleet/backend/database/migrator_test.goindex 21c7e8005b..38d49fff63 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) != 7 {- t.Fatalf("expected exactly seven migrations, got %d", len(files))+ if len(files) != 8 {+ t.Fatalf("expected exactly eight migrations, got %d", len(files))}initial := files[0]if initial.Version != 1 || initial.Name != "000001_initial_schema.sql" {@@ -660,6 +660,22 @@ func TestEmbeddedMigrationsAreOrderedAndImmutable(t *testing.T) {t.Errorf("Metabase usage migration is missing contract %q", expected)}}++ filteredMetabaseUsage := files[7]+ if filteredMetabaseUsage.Version != 8 || filteredMetabaseUsage.Name != "000008_metabase_hourly_reservation_usage_excluding_tenants.sql" {+ t.Fatalf("expected version 8 filtered Metabase usage migration, got version=%d name=%q", filteredMetabaseUsage.Version, filteredMetabaseUsage.Name)+ }+ for _, expected := range []string{+ "CREATE VIEW k8s_reporting.hourly_reservation_usage_excluding_tenants",+ "fact.capsule_tenant NOT IN",+ "user-f039fe89-9b5f-43dc-8ccd-d100ae732246",+ "user-30a53246-881d-4f1a-8005-979f2a07933e",+ "GRANT SELECT ON k8s_reporting.hourly_reservation_usage_excluding_tenants TO k8s_metabase",+ } {+ if !strings.Contains(filteredMetabaseUsage.SQL, expected) {+ t.Errorf("filtered Metabase usage migration is missing contract %q", expected)+ }+ }}func TestReservationMeterMigrationRetainsRecordedChecksum(t *testing.T) {