Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
fix(billing): show live usage on per-unit product dimensions
Tre Dubravaf5a67dd958de
diff --git a/backend/src/ee/services/license-v2/license-v2-service.ts b/backend/src/ee/services/license-v2/license-v2-service.tsindex 1fd245336a..607fb88d9d 100644--- a/backend/src/ee/services/license-v2/license-v2-service.ts+++ b/backend/src/ee/services/license-v2/license-v2-service.ts@@ -13,6 +13,7 @@ import {TSubscriptionPreviewPayload,TSubscriptionResponse} from "@app/services/license-client/license-client-types";+import { TMeteredFeature } from "@app/services/license-client/usage";import { TOrgDALFactory } from "@app/services/org/org-dal";import { isV2SelfHostedLicenseKey } from "../license/license-fns";@@ -45,6 +46,9 @@ type TLicenseV2ServiceFactoryDep = {orgDAL: Pick<TOrgDALFactory, "findById" | "countAllOrgMembers">;identityOrgMembershipDAL: Pick<TIdentityOrgDALFactory, "countAllOrgIdentities">;permissionService: Pick<TPermissionServiceFactory, "getOrgPermission">;+ // Feature-keyed live-count fns (the same source the usage meter uses), so per-unit dimensions can+ // show real utilization instead of the zero the license server reports for non-metered dimensions.+ meteredFeatures: TMeteredFeature[];licenseClient: Pick<TLicenseClientFactory,| "getEntitlements"@@ -303,7 +307,8 @@ export const licenseV2ServiceFactory = ({orgDAL,identityOrgMembershipDAL,permissionService,- licenseClient+ licenseClient,+ meteredFeatures}: TLicenseV2ServiceFactoryDep) => {// A self-hosted v2 license is managed out-of-band: the billing surface is read-only. Self-serve// mutations don't need an explicit guard here, the self-hosted license client rejects them itself.@@ -336,6 +341,46 @@ export const licenseV2ServiceFactory = ({);};+ const counterByFeatureKey = new Map(meteredFeatures.map((metered) => [metered.feature.key, metered.count]));++ // Live per-org usage for the subscription's non-metered dimensions, keyed by feature key. Only the+ // feature keys that have a counter and appear as a non-metered dimension are counted, once each,+ // in parallel. A count failure is logged and dropped so the overview still renders.+ const resolveLiveUsage = async (+ orgId: string,+ subscription: TSubscriptionResponse | null+ ): Promise<Map<string, number>> => {+ const usage = new Map<string, number>();+ if (!subscription) {+ return usage;+ }++ const featureKeys = new Set<string>();+ subscription.items.forEach((item) => {+ item.dimensions.forEach((dimension) => {+ if (!dimension.metered && dimension.featureKey && counterByFeatureKey.has(dimension.featureKey)) {+ featureKeys.add(dimension.featureKey);+ }+ });+ });++ await Promise.all(+ [...featureKeys].map(async (featureKey) => {+ const count = counterByFeatureKey.get(featureKey);+ if (!count) {+ return;+ }+ try {+ usage.set(featureKey, await count(orgId));+ } catch (error) {+ logger.error(error, `billing-v2: failed to count live usage [orgId=${orgId}, feature=${featureKey}]`);+ }+ })+ );++ return usage;+ };+// Fold the subscription items and the entitlement features sourced from a product into a single// per-product entitlement map, keyed by the server's product ids.const buildEntitlements = async (org: TEntitlementOrg, subscription: TSubscriptionResponse | null) => {@@ -362,6 +407,12 @@ export const licenseV2ServiceFactory = ({}}+ // The license server only tracks usage for metered dimensions, so per-unit ones come back with+ // used=0. Overlay live counts from the same feature-keyed counters the usage meter uses, matched+ // on the stable feature key (dimension keys drift). Metered dimensions keep the server's period+ // peak, which is the billed figure. Counts are fetched once per distinct feature key.+ const liveUsageByFeatureKey = await resolveLiveUsage(org.id, subscription);+if (subscription) {subscription.items.forEach((item) => {// Resolve the pinned per-dimension contract into a display-ready list: label/noun from the@@ -373,13 +424,18 @@ export const licenseV2ServiceFactory = ({const metered = dimension.metered ?? false;const hasRate = dimension.rateCents !== null && dimension.rateCents !== undefined;const hasFreeBand = dimension.freeBand !== null && dimension.freeBand !== undefined;+ let used = dimension.used ?? 0;+ const liveUsed = dimension.featureKey ? liveUsageByFeatureKey.get(dimension.featureKey) : undefined;+ if (!metered && liveUsed !== undefined) {+ used = liveUsed;+ }return {key: dimension.key,label: labelByDimension.get(catalogKey) ?? dimension.key,noun: nounByDimension.get(catalogKey) ?? dimension.unit ?? dimension.key,unit: dimension.unit ?? nounByDimension.get(catalogKey) ?? dimension.key,metered,- used: dimension.used ?? 0,+ used,limit: dimension.limit ?? null,...(metered && hasFreeBand ? { freeBand: dimension.freeBand as number } : {}),...(metered && hasRate ? { rate: centsToDollars(dimension.rateCents) } : {})diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.tsindex d3045cdc59..72e4ebf81e 100644--- a/backend/src/server/routes/index.ts+++ b/backend/src/server/routes/index.ts@@ -829,7 +829,8 @@ export const registerRoutes = async (orgDAL,identityOrgMembershipDAL,permissionService,- licenseClient+ licenseClient,+ meteredFeatures});// Project events SSE service (for clients to subscribe to secret mutation events)diff --git a/backend/src/services/license-client/license-client-types.ts b/backend/src/services/license-client/license-client-types.tsindex a7b9f90435..2910bb25a7 100644--- a/backend/src/services/license-client/license-client-types.ts+++ b/backend/src/services/license-client/license-client-types.ts@@ -91,6 +91,9 @@ export const catalogResponseSchema = zconst subscriptionItemDimensionSchema = z.object({key: z.string(),+ // Stable feature key the dimension caps against; used to overlay live per-org usage. Dimension+ // keys drift (e.g. a display rename), so usage is matched on this, not on key.+ featureKey: z.string().nullish(),unit: z.string().optional(),metered: z.boolean().default(false),aggregation: z.string().optional(),