-
Notifications
You must be signed in to change notification settings - Fork 30
feat(billing): threshold notification service with persisted dedupe #2351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
k11kirky
wants to merge
1
commit into
posthog-code/usage-sidebar-reset-time
from
posthog-code/usage-threshold-monitor
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| export const USAGE_THRESHOLDS = [50, 75, 90, 100] as const; | ||
| export type UsageThreshold = (typeof USAGE_THRESHOLDS)[number]; | ||
|
|
||
| export const thresholdCrossedEvent = z.object({ | ||
| bucket: z.enum(["burst", "sustained"]), | ||
| threshold: z.union([ | ||
| z.literal(50), | ||
| z.literal(75), | ||
| z.literal(90), | ||
| z.literal(100), | ||
| ]), | ||
| usedPercent: z.number(), | ||
| resetAt: z.string().datetime().nullable(), | ||
| resetsInSeconds: z.number(), | ||
| isPro: z.boolean(), | ||
| }); | ||
|
|
||
| export type ThresholdCrossedEvent = z.infer<typeof thresholdCrossedEvent>; | ||
|
|
||
| export const UsageMonitorEvent = { | ||
| ThresholdCrossed: "threshold-crossed", | ||
| } as const; | ||
|
|
||
| export interface UsageMonitorEvents { | ||
| [UsageMonitorEvent.ThresholdCrossed]: ThresholdCrossedEvent; | ||
| } |
182 changes: 182 additions & 0 deletions
182
apps/code/src/main/services/usage-monitor/service.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import type { UsageOutput } from "../llm-gateway/schemas"; | ||
| import { UsageMonitorEvent } from "./schemas"; | ||
|
|
||
| const mockStoreGet = vi.hoisted(() => vi.fn()); | ||
| const mockStoreSet = vi.hoisted(() => vi.fn()); | ||
|
|
||
| vi.mock("./store", () => ({ | ||
| usageMonitorStore: { | ||
| get: mockStoreGet, | ||
| set: mockStoreSet, | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock("../../utils/logger.js", () => ({ | ||
| logger: { | ||
| scope: () => ({ | ||
| info: vi.fn(), | ||
| error: vi.fn(), | ||
| warn: vi.fn(), | ||
| debug: vi.fn(), | ||
| }), | ||
| }, | ||
| })); | ||
|
|
||
| import { LlmGatewayService } from "../llm-gateway/service"; | ||
| import { UsageMonitorService } from "./service"; | ||
|
|
||
| function makeUsage(overrides?: { | ||
| burstPercent?: number; | ||
| sustainedPercent?: number; | ||
| billingPeriodEnd?: string | null; | ||
| burstResetAt?: string; | ||
| sustainedResetAt?: string; | ||
| }): UsageOutput { | ||
| return { | ||
| product: "posthog_code", | ||
| user_id: 42, | ||
| is_rate_limited: false, | ||
| billing_period_end: | ||
| overrides?.billingPeriodEnd === undefined | ||
| ? null | ||
| : overrides.billingPeriodEnd, | ||
| burst: { | ||
| used_percent: overrides?.burstPercent ?? 0, | ||
| resets_in_seconds: 3600, | ||
| reset_at: overrides?.burstResetAt ?? "2026-05-25T16:00:00.000Z", | ||
| exceeded: false, | ||
| }, | ||
| sustained: { | ||
| used_percent: overrides?.sustainedPercent ?? 0, | ||
| resets_in_seconds: 86400, | ||
| reset_at: overrides?.sustainedResetAt ?? "2026-06-01T00:00:00.000Z", | ||
| exceeded: false, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function mockGateway(usage: UsageOutput | null): LlmGatewayService { | ||
| return { | ||
| fetchUsage: vi.fn().mockResolvedValue(usage), | ||
| } as unknown as LlmGatewayService; | ||
| } | ||
|
|
||
| describe("UsageMonitorService", () => { | ||
| let service: UsageMonitorService; | ||
| let persisted: Record<string, string>; | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| vi.setSystemTime(new Date("2026-05-25T12:00:00.000Z")); | ||
| persisted = {}; | ||
| mockStoreGet.mockImplementation((_key: string, fallback: unknown) => ({ | ||
| ...persisted, | ||
| ...(fallback as Record<string, string>), | ||
| })); | ||
| mockStoreSet.mockImplementation( | ||
| (_key: string, value: Record<string, string>) => { | ||
| persisted = { ...value }; | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| service?.stop(); | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it("emits at 75% but not again on the next poll for the same anchor", async () => { | ||
| const events: unknown[] = []; | ||
| const gateway = mockGateway(makeUsage({ burstPercent: 78 })); | ||
| service = new UsageMonitorService(gateway); | ||
| service.on(UsageMonitorEvent.ThresholdCrossed, (e) => events.push(e)); | ||
|
|
||
| await service.pollOnce(); | ||
| expect(events).toHaveLength(1); | ||
| expect(events[0]).toMatchObject({ | ||
| bucket: "burst", | ||
| threshold: 75, | ||
| usedPercent: 78, | ||
| }); | ||
|
|
||
| await service.pollOnce(); | ||
| expect(events).toHaveLength(1); | ||
| }); | ||
|
|
||
| it("only emits the highest threshold a bucket has crossed", async () => { | ||
| const events: unknown[] = []; | ||
| const gateway = mockGateway(makeUsage({ burstPercent: 95 })); | ||
| service = new UsageMonitorService(gateway); | ||
| service.on(UsageMonitorEvent.ThresholdCrossed, (e) => events.push(e)); | ||
|
|
||
| await service.pollOnce(); | ||
| expect(events).toHaveLength(1); | ||
| expect(events[0]).toMatchObject({ threshold: 90 }); | ||
| }); | ||
|
|
||
| it("doesn't re-emit after a relaunch with persisted dedupe", async () => { | ||
| const events: unknown[] = []; | ||
| const gateway = mockGateway(makeUsage({ burstPercent: 55 })); | ||
| service = new UsageMonitorService(gateway); | ||
| service.on(UsageMonitorEvent.ThresholdCrossed, (e) => events.push(e)); | ||
| await service.pollOnce(); | ||
| expect(events).toHaveLength(1); | ||
| service.stop(); | ||
|
|
||
| // Simulate relaunch | ||
| service = new UsageMonitorService(gateway); | ||
| service.on(UsageMonitorEvent.ThresholdCrossed, (e) => events.push(e)); | ||
| await service.pollOnce(); | ||
| expect(events).toHaveLength(1); | ||
| }); | ||
|
|
||
| it("tracks burst and sustained as independent buckets", async () => { | ||
| const events: unknown[] = []; | ||
| const gateway = mockGateway( | ||
| makeUsage({ | ||
| burstPercent: 55, | ||
| sustainedPercent: 80, | ||
| billingPeriodEnd: "2026-06-01T00:00:00.000Z", | ||
| }), | ||
| ); | ||
| service = new UsageMonitorService(gateway); | ||
| service.on(UsageMonitorEvent.ThresholdCrossed, (e) => events.push(e)); | ||
|
|
||
| await service.pollOnce(); | ||
| expect(events).toHaveLength(2); | ||
| expect(events.map((e) => (e as { bucket: string }).bucket).sort()).toEqual([ | ||
| "burst", | ||
| "sustained", | ||
| ]); | ||
| }); | ||
|
|
||
| it("marks events with isPro when billing_period_end is set", async () => { | ||
| const events: { isPro: boolean }[] = []; | ||
| const gateway = mockGateway( | ||
| makeUsage({ | ||
| sustainedPercent: 60, | ||
| billingPeriodEnd: "2026-06-01T00:00:00.000Z", | ||
| }), | ||
| ); | ||
| service = new UsageMonitorService(gateway); | ||
| service.on(UsageMonitorEvent.ThresholdCrossed, (e) => | ||
| events.push(e as { isPro: boolean }), | ||
| ); | ||
|
|
||
| await service.pollOnce(); | ||
| expect(events[0]?.isPro).toBe(true); | ||
| }); | ||
|
|
||
| it("silently skips polls when the gateway throws", async () => { | ||
| const events: unknown[] = []; | ||
| const gateway = { | ||
| fetchUsage: vi.fn().mockRejectedValue(new Error("not authenticated")), | ||
| } as unknown as LlmGatewayService; | ||
| service = new UsageMonitorService(gateway); | ||
| service.on(UsageMonitorEvent.ThresholdCrossed, (e) => events.push(e)); | ||
|
|
||
| await expect(service.pollOnce()).resolves.toBeNull(); | ||
| expect(events).toHaveLength(0); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No test covers the scenario where usage crosses a lower threshold (e.g. 55% → 50% fires) and then rises to a higher one (85% → 75% fires) within the same anchor window. This is the most important in-window state-machine transition and it's currently untested.
Separately, the threshold detection cases ("emits at 75%" and "only emits the highest threshold") share the same structure and are good candidates for a parameterised test per the team's convention — e.g.
it.each([[78, 75], [95, 90], [100, 100], [49, null]])("threshold at %i% fires %i", ...).Prompt To Fix With AI