Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat(billing): enhance billing preview and usage breakdown functionality

Victor Hugo dos Santose35ebfb0c654
diff --git a/backend/src/ee/services/license-v2/license-v2-service.ts b/backend/src/ee/services/license-v2/license-v2-service.ts
index c9b397ddb3..ce205cecbd 100644
--- a/backend/src/ee/services/license-v2/license-v2-service.ts
+++ b/backend/src/ee/services/license-v2/license-v2-service.ts
@@ -948,7 +948,8 @@ export const licenseV2ServiceFactory = ({
const buildIdentityBreakdown = async (
counts: { users: number; identities: number },
rows: { orgId: string; projectId: string | null; count: number }[],
- withProjectDetail: boolean
+ withProjectDetail: boolean,
+ unit: string
): Promise<BillingV2UsageBreakdown> => {
const attributed = withProjectDetail
? rows
@@ -965,7 +966,7 @@ export const licenseV2ServiceFactory = ({
userCount: counts.users,
scopedCount: counts.identities,
hasProjectDetail: withProjectDetail,
- unit: "machine identity",
+ unit,
scopes: $buildScopes(orgs, projectsById, attributed)
};
};
@@ -986,12 +987,12 @@ export const licenseV2ServiceFactory = ({
licenseDAL.countBillableOrgActors(rootOrgId),
licenseDAL.getBillableIdentityOwnershipBreakdown(rootOrgId)
]);
- return { breakdown: await buildIdentityBreakdown(counts, rows, true) };
+ return { breakdown: await buildIdentityBreakdown(counts, rows, true, "machine identity") };
}
case BillingV2BreakdownDimension.SecretIdentities:
case BillingV2BreakdownDimension.PamIdentities: {
- const projectType =
- dimensionKey === BillingV2BreakdownDimension.SecretIdentities ? ProjectType.SecretManager : ProjectType.PAM;
+ const isSecretIdentities = dimensionKey === BillingV2BreakdownDimension.SecretIdentities;
+ const projectType = isSecretIdentities ? ProjectType.SecretManager : ProjectType.PAM;
const [counts, rows] = await Promise.all([
usageCounterDAL.countProjectIdentitiesByKindFor(projectType, scopeOrgId),
usageCounterDAL.getProjectIdentityBreakdown(projectType, scopeOrgId)
@@ -1000,7 +1001,8 @@ export const licenseV2ServiceFactory = ({
breakdown: await buildIdentityBreakdown(
counts,
rows,
- dimensionKey === BillingV2BreakdownDimension.SecretIdentities
+ isSecretIdentities,
+ isSecretIdentities ? "secret identity" : "PAM identity"
)
};
}
diff --git a/frontend/src/pages/organization/BillingV2Page/BillingV2Page.tsx b/frontend/src/pages/organization/BillingV2Page/BillingV2Page.tsx
index 0731459607..f9ee29d41e 100644
--- a/frontend/src/pages/organization/BillingV2Page/BillingV2Page.tsx
+++ b/frontend/src/pages/organization/BillingV2Page/BillingV2Page.tsx
@@ -33,8 +33,9 @@ import {
asCloudOverview,
asManagedCloudOverview,
PREVIEW_CATALOG,
- PREVIEW_ENTITLEMENTS,
- PREVIEW_MODE
+ PREVIEW_MODE,
+ previewEntitlements,
+ usePreviewUsage
} from "./billing-v2-local-preview";
import { BillingV2RenderState } from "./billing-v2-view-types";
@@ -98,17 +99,21 @@ export const BillingV2Page = () => {
// PREVIEW_MODE in billing-v2-local-preview.ts.
const usePreview = PREVIEW_MODE !== "off" && realCatalog.length === 0;
const catalog = usePreview ? PREVIEW_CATALOG : realCatalog;
+ // Real figures for the faked products, read off this instance's own database through the breakdown
+ // endpoint, so picking an organization moves the meters the way it will in production.
+ const previewUsage = usePreviewUsage(selectedOrgId, breakdownScope);
+ const previewedEntitlements = previewEntitlements(previewUsage);
// Shadows the real overview for the whole component, so every branch keyed off it (subState, the
// managed/self-serve copy, the payment and invoice cards) sees the faked shape too. Restore to
// `const { data: overview, ... } = useGetBillingV2Overview(selectedOrgId)` when removing the preview.
let overview = serverOverview;
if (usePreview && serverOverview) {
if (PREVIEW_MODE === "cloud") {
- overview = asCloudOverview(serverOverview);
+ overview = asCloudOverview(serverOverview, previewedEntitlements);
} else if (PREVIEW_MODE === "cloud-managed") {
- overview = asManagedCloudOverview(serverOverview);
+ overview = asManagedCloudOverview(serverOverview, previewedEntitlements);
} else {
- overview = { ...serverOverview, entitlements: PREVIEW_ENTITLEMENTS };
+ overview = { ...serverOverview, entitlements: previewedEntitlements };
}
}
// The preview fakes the deployment, so it has to fake this endpoint's answer too. A dev stack is
@@ -259,7 +264,7 @@ export const BillingV2Page = () => {
onSetCommitment={onSetCommitment}
onViewBreakdown={onViewBreakdown}
rootOrgs={pickerOrgs}
- rootOrgCount={rootOrgCount}
+ rootOrgCount={orgPage?.totalCount ?? rootOrgCount}
isRootOrgsLoading={isOrgSearchPending}
isReloading={isReloading}
selectedOrgId={breakdownScope === "instance" ? ALL_ORGS_VALUE : selectedOrgId}
diff --git a/frontend/src/pages/organization/BillingV2Page/billing-v2-local-preview.ts b/frontend/src/pages/organization/BillingV2Page/billing-v2-local-preview.ts
index 7dc1ca7966..ba0fd06d65 100644
--- a/frontend/src/pages/organization/BillingV2Page/billing-v2-local-preview.ts
+++ b/frontend/src/pages/organization/BillingV2Page/billing-v2-local-preview.ts
@@ -14,11 +14,17 @@
export type PreviewMode = "cloud" | "cloud-managed" | "self-hosted" | "offline" | "off";
export const PREVIEW_MODE: PreviewMode = "self-hosted";
+import { useMemo } from "react";
+import { keepPreviousData, useQueries } from "@tanstack/react-query";
+
+import { apiRequest } from "@app/config/request";
import {
+ BillingV2BreakdownScopeKind,
BillingV2CatalogProduct,
BillingV2Entitlement,
BillingV2EntitlementDim,
- BillingV2Overview
+ BillingV2Overview,
+ BillingV2UsageBreakdown
} from "@app/hooks/api";
const dim = (key: string, label: string, noun: string, used: number): BillingV2EntitlementDim => ({
@@ -120,7 +126,27 @@ export const PREVIEW_CATALOG: BillingV2CatalogProduct[] = [
)
];
-export const PREVIEW_ENTITLEMENTS: Record<string, BillingV2Entitlement> = {
+// The licence server is what a dev stack lacks; the breakdown endpoint is local, scope-aware and
+// reads the same tables the meters do. Pointing the cards at it makes every figure real and makes the
+// organization picker move them: "All organizations" counts the whole database, one organization
+// counts its tree. The numbers below are the fallback for a dimension whose request has not landed.
+export const PREVIEW_DIMENSION_KEYS = [
+ "identities",
+ "user_identities",
+ "secret_identities",
+ "pam_identities",
+ "internal_cas",
+ "active_certs",
+ "wildcard_certs"
+] as const;
+
+export type PreviewUsage = Partial<Record<(typeof PREVIEW_DIMENSION_KEYS)[number], number>>;
+
+// Off and offline fake nothing, so nothing is fetched for them either.
+const INERT_MODES: PreviewMode[] = ["off", "offline"];
+const IS_PREVIEW_ACTIVE = !INERT_MODES.includes(PREVIEW_MODE);
+
+export const previewEntitlements = (usage: PreviewUsage = {}): Record<string, BillingV2Entitlement> => ({
// Two dimensions, as production shows: the combined identity meter plus the human-seat one.
legacy_secrets: {
entitled: true,
@@ -128,15 +154,15 @@ export const PREVIEW_ENTITLEMENTS: Record<string, BillingV2Entitlement> = {
cadence: "annual",
renewsOn: "November 3, 2026",
dimensions: [
- dim("identities", "Identities", "identity", 25),
- dim("user_identities", "User Identities", "user identity", 10)
+ dim("identities", "Identities", "identity", usage.identities ?? 25),
+ dim("user_identities", "User Identities", "user identity", usage.user_identities ?? 10)
]
},
secrets: {
entitled: true,
planTier: "enterprise",
cadence: "monthly",
- dimensions: [dim("secret_identities", "Secret Identities", "secret identity", 15)]
+ dimensions: [dim("secret_identities", "Secret Identities", "secret identity", usage.secret_identities ?? 15)]
},
// Several breakdownable dimensions, so the sheet renders its unit tabs. internal_cas is committable
// here, which is also what makes this the card that shows the breakdown strip and the commit nudge
@@ -146,17 +172,53 @@ export const PREVIEW_ENTITLEMENTS: Record<string, BillingV2Entitlement> = {
planTier: "enterprise",
cadence: "monthly",
dimensions: [
- commitDim("internal_cas", "Internal CAs", "internal CA", 23),
- dim("active_certs", "Active Certificates", "active certificate", 134),
- dim("wildcard_certs", "Wildcard Certificates", "wildcard certificate", 23)
+ commitDim("internal_cas", "Internal CAs", "internal CA", usage.internal_cas ?? 23),
+ dim("active_certs", "Active Certificates", "active certificate", usage.active_certs ?? 134),
+ dim("wildcard_certs", "Wildcard Certificates", "wildcard certificate", usage.wildcard_certs ?? 23)
]
},
pam: {
entitled: true,
planTier: "enterprise",
cadence: "monthly",
- dimensions: [dim("pam_identities", "PAM Identities", "PAM identity", 54)]
+ dimensions: [dim("pam_identities", "PAM Identities", "PAM identity", usage.pam_identities ?? 54)]
}
+});
+
+export const PREVIEW_ENTITLEMENTS = previewEntitlements();
+
+// One request per previewed dimension, keyed on the picked organization and scope, so switching the
+// picker refetches and the cards move with it.
+export const usePreviewUsage = (orgId: string, scope: BillingV2BreakdownScopeKind): PreviewUsage => {
+ const results = useQueries({
+ queries: PREVIEW_DIMENSION_KEYS.map((key) => ({
+ queryKey: ["billing-v2-preview-usage", orgId, key, scope] as const,
+ queryFn: async () => {
+ const {
+ data: { breakdown }
+ } = await apiRequest.get<{ breakdown: BillingV2UsageBreakdown }>(
+ `/api/v1/organizations/${orgId}/billing/v2/breakdowns/${key}`,
+ { params: { scope } }
+ );
+ return breakdown.total;
+ },
+ enabled: Boolean(orgId) && IS_PREVIEW_ACTIVE,
+ placeholderData: keepPreviousData,
+ retry: false
+ }))
+ });
+
+ const totals = results.map((result) => result.data);
+ return useMemo(
+ () =>
+ Object.fromEntries(
+ PREVIEW_DIMENSION_KEYS.map((key, index) => [key, totals[index]]).filter(
+ ([, total]) => total !== undefined
+ )
+ ),
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ totals
+ );
};
// Two real cloud shapes, because they render very differently and both are worth reviewing.
@@ -167,13 +229,16 @@ const CLOUD_BASE = {
isCloud: true as const,
mode: "self-serve" as const,
checkoutFrozen: false,
- planName: "Enterprise",
- entitlements: PREVIEW_ENTITLEMENTS
+ planName: "Enterprise"
};
-export const asCloudOverview = (overview: BillingV2Overview): BillingV2Overview => ({
+export const asCloudOverview = (
+ overview: BillingV2Overview,
+ entitlements: Record<string, BillingV2Entitlement>
+): BillingV2Overview => ({
...overview,
...CLOUD_BASE,
+ entitlements,
subState: "active",
selfServe: true,
billing: {
@@ -230,9 +295,13 @@ export const asCloudOverview = (overview: BillingV2Overview): BillingV2Overview
// One thing this cannot reproduce: production also shows a Manage button on the active product. On this
// branch that needs overview.selfServe true, the same flag the Managed Billing notice needs false, so
// the two cannot both render. Production is on an older build where they could.
-export const asManagedCloudOverview = (overview: BillingV2Overview): BillingV2Overview => ({
+export const asManagedCloudOverview = (
+ overview: BillingV2Overview,
+ entitlements: Record<string, BillingV2Entitlement>
+): BillingV2Overview => ({
...overview,
...CLOUD_BASE,
+ entitlements,
subState: "no-subscription",
selfServe: false,
billing: { monthlyRecurring: 0, annualCommitted: 0, activeProductCount: 1, nextCharge: null },
diff --git a/frontend/src/pages/organization/BillingV2Page/components/UsageBreakdownSheet.tsx b/frontend/src/pages/organization/BillingV2Page/components/UsageBreakdownSheet.tsx
index 37bbd4d915..d405cd5c24 100644
--- a/frontend/src/pages/organization/BillingV2Page/components/UsageBreakdownSheet.tsx
+++ b/frontend/src/pages/organization/BillingV2Page/components/UsageBreakdownSheet.tsx
@@ -322,7 +322,11 @@ const BreakdownBody = ({
<EmptyHeader>
<EmptyTitle>Nothing counted yet</EmptyTitle>
<EmptyDescription>
- No {unitLabel} have been created in this organization or its sub-organizations.
+ No {unitLabel} have been created{" "}
+ {isInstanceScope
+ ? "anywhere on this instance"
+ : "in this organization or its sub-organizations"}
+ .
</EmptyDescription>
</EmptyHeader>
</Empty>