Skip to content

Commit 8886845

Browse files
committed
feat(balances): measure usage alerts per basis and fire on usage limits
Move alert firing into balances/usageAlerts/check as resolve → measure → wasThresholdCrossed → send. Balance bases read the breakdown for their denominator; usage_limit alerts read the usage-window counter of the cap that shares their feature and filter, both sides at one now so a rolled window measures 0 before the event. The webhook carries basis, filter and a balance or usage_limit block, and the idempotency key includes basis, filter and window start. Entity subjects keep customer-scope usage window rows after deduction so a cap inherited from the customer can fire on an entity track.
1 parent 580a09d commit 8886845

33 files changed

Lines changed: 2174 additions & 408 deletions

server/src/internal/balances/trackWebhooks/checkUsageAlerts.ts

Lines changed: 0 additions & 305 deletions
This file was deleted.

server/src/internal/balances/trackWebhooks/fireTrackWebhooks.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import {
66
} from "@autumn/shared";
77
import type { AutumnContext } from "@/honoUtils/HonoEnv.js";
88
import { buildEvaluationSubject } from "@/internal/balances/check/buildEvaluationSubject.js";
9+
import { checkUsageAlerts } from "@/internal/balances/usageAlerts/check/checkUsageAlerts.js";
910
import { getApiCustomerBase } from "@/internal/customers/cusUtils/apiCusUtils/getApiCustomerBase.js";
1011
import { getApiEntityBase } from "@/internal/entities/entityUtils/apiEntityUtils/getApiEntityBase.js";
1112
import { checkLimitReached } from "./checkLimitReached.js";
12-
import { checkUsageAlerts } from "./checkUsageAlerts.js";
1313
import { handleThresholdReached } from "./handleThresholdReached.js";
1414

1515
export const fireTrackWebhooks = ({
@@ -22,6 +22,7 @@ export const fireTrackWebhooks = ({
2222
entityId,
2323
featuresFromMutationLogs,
2424
eventProperties,
25+
now = Date.now(),
2526
}: {
2627
ctx: AutumnContext;
2728
oldFullSubject?: FullSubject;
@@ -32,6 +33,7 @@ export const fireTrackWebhooks = ({
3233
entityId?: string;
3334
featuresFromMutationLogs?: Feature[];
3435
eventProperties?: Record<string, unknown> | null;
36+
now?: number;
3537
}) => {
3638
const oldFullCus = oldFullSubject
3739
? fullSubjectToFullCustomer({ fullSubject: oldFullSubject })
@@ -61,8 +63,11 @@ export const fireTrackWebhooks = ({
6163
ctx,
6264
oldFullCus,
6365
newFullCus,
66+
oldFullSubject,
67+
newFullSubject,
6468
feature: affectedFeature,
6569
entityId,
70+
now,
6671
}).catch((error) => {
6772
ctx.logger.error(`[fireTrackWebhooks] checkUsageAlerts: ${error}`);
6873
});

0 commit comments

Comments
 (0)