-
Notifications
You must be signed in to change notification settings - Fork 138
feat(usage): add box usage metering ledger (Phase 1 Usage) #969
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
Open
law-chain-hot
wants to merge
2
commits into
main
Choose a base branch
from
codex/billing-v3-pr1-usage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
52 changes: 52 additions & 0 deletions
52
apps/api/src/migrations/pre-deploy/1782700000000-migration.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,52 @@ | ||
| import { MigrationInterface, QueryRunner } from 'typeorm' | ||
|
|
||
| export class Migration1782700000000 implements MigrationInterface { | ||
| name = 'Migration1782700000000' | ||
|
|
||
| public async up(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query( | ||
| `CREATE TABLE "box_usage_period" ( | ||
| "id" uuid NOT NULL DEFAULT uuid_generate_v4(), | ||
| "boxId" character varying NOT NULL, | ||
| "organizationId" character varying NOT NULL, | ||
| "startAt" TIMESTAMP WITH TIME ZONE NOT NULL, | ||
| "endAt" TIMESTAMP WITH TIME ZONE, | ||
| "cpu" double precision NOT NULL, | ||
| "gpu" double precision NOT NULL, | ||
| "mem" double precision NOT NULL, | ||
| "disk" double precision NOT NULL, | ||
| "region" character varying NOT NULL, | ||
| "boxClass" character varying NOT NULL DEFAULT 'small', | ||
| "regionType" character varying NOT NULL DEFAULT 'shared', | ||
| CONSTRAINT "box_usage_period_id_pk" PRIMARY KEY ("id") | ||
| )`, | ||
| ) | ||
| await queryRunner.query(`CREATE INDEX "box_usage_period_box_end_idx" ON "box_usage_period" ("boxId", "endAt")`) | ||
| await queryRunner.query( | ||
| `CREATE UNIQUE INDEX "box_usage_period_one_open_per_box_idx" ON "box_usage_period" ("boxId") WHERE "endAt" IS NULL`, | ||
| ) | ||
|
|
||
| await queryRunner.query( | ||
| `CREATE TABLE "box_usage_period_archive" ( | ||
| "id" uuid NOT NULL DEFAULT uuid_generate_v4(), | ||
| "boxId" character varying NOT NULL, | ||
| "organizationId" character varying NOT NULL, | ||
| "startAt" TIMESTAMP WITH TIME ZONE NOT NULL, | ||
| "endAt" TIMESTAMP WITH TIME ZONE NOT NULL, | ||
| "cpu" double precision NOT NULL, | ||
| "gpu" double precision NOT NULL, | ||
| "mem" double precision NOT NULL, | ||
| "disk" double precision NOT NULL, | ||
| "region" character varying NOT NULL, | ||
| "boxClass" character varying NOT NULL DEFAULT 'small', | ||
| "regionType" character varying NOT NULL DEFAULT 'shared', | ||
| CONSTRAINT "box_usage_period_archive_id_pk" PRIMARY KEY ("id") | ||
| )`, | ||
| ) | ||
| } | ||
|
|
||
| public async down(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query(`DROP TABLE "box_usage_period_archive"`) | ||
| await queryRunner.query(`DROP TABLE "box_usage_period"`) | ||
| } | ||
| } |
66 changes: 66 additions & 0 deletions
66
apps/api/src/usage/entities/box-usage-period-archive.entity.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,66 @@ | ||
| /* | ||
| * Copyright BoxLite AI, 2026 | ||
| * SPDX-License-Identifier: AGPL-3.0 | ||
| */ | ||
|
|
||
| import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm' | ||
| import { BoxClass } from '../../box/enums/box-class.enum' | ||
| import { RegionType } from '../../region/enums/region-type.enum' | ||
| import { BoxUsagePeriod } from './box-usage-period.entity' | ||
|
|
||
| // Duplicate of BoxUsagePeriod. It only contains closed periods and keeps the active table lightweight. | ||
| @Entity('box_usage_period_archive') | ||
| export class BoxUsagePeriodArchive { | ||
| @PrimaryGeneratedColumn('uuid') | ||
| id: string | ||
|
|
||
| @Column() | ||
| boxId: string | ||
|
|
||
| @Column() | ||
| // Redundant property to optimize billing queries. | ||
| organizationId: string | ||
|
|
||
| @Column({ type: 'timestamp with time zone' }) | ||
| startAt: Date | ||
|
|
||
| @Column({ type: 'timestamp with time zone' }) | ||
| endAt: Date | ||
|
|
||
| @Column({ type: 'float' }) | ||
| cpu: number | ||
|
|
||
| @Column({ type: 'float' }) | ||
| gpu: number | ||
|
|
||
| @Column({ type: 'float' }) | ||
| mem: number | ||
|
|
||
| @Column({ type: 'float' }) | ||
| disk: number | ||
|
|
||
| @Column() | ||
| region: string | ||
|
|
||
| @Column({ type: 'character varying', default: BoxClass.SMALL }) | ||
| boxClass: BoxClass = BoxClass.SMALL | ||
|
|
||
| @Column({ type: 'character varying', default: RegionType.SHARED }) | ||
| regionType: string = RegionType.SHARED | ||
|
|
||
| public static fromBoxUsagePeriod(usagePeriod: BoxUsagePeriod) { | ||
| const usagePeriodEntity = new BoxUsagePeriodArchive() | ||
| usagePeriodEntity.boxId = usagePeriod.boxId | ||
| usagePeriodEntity.organizationId = usagePeriod.organizationId | ||
| usagePeriodEntity.startAt = usagePeriod.startAt | ||
| usagePeriodEntity.endAt = usagePeriod.endAt as Date | ||
| usagePeriodEntity.cpu = usagePeriod.cpu | ||
| usagePeriodEntity.gpu = usagePeriod.gpu | ||
| usagePeriodEntity.mem = usagePeriod.mem | ||
| usagePeriodEntity.disk = usagePeriod.disk | ||
| usagePeriodEntity.region = usagePeriod.region | ||
| usagePeriodEntity.boxClass = usagePeriod.boxClass | ||
| usagePeriodEntity.regionType = usagePeriod.regionType | ||
| return usagePeriodEntity | ||
| } | ||
| } | ||
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,66 @@ | ||
| /* | ||
| * Copyright BoxLite AI, 2026 | ||
| * SPDX-License-Identifier: AGPL-3.0 | ||
| */ | ||
|
|
||
| import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm' | ||
| import { BoxClass } from '../../box/enums/box-class.enum' | ||
| import { RegionType } from '../../region/enums/region-type.enum' | ||
|
|
||
| @Entity('box_usage_period') | ||
| @Index('box_usage_period_box_end_idx', ['boxId', 'endAt']) | ||
| @Index('box_usage_period_one_open_per_box_idx', ['boxId'], { unique: true, where: '"endAt" IS NULL' }) | ||
| export class BoxUsagePeriod { | ||
| @PrimaryGeneratedColumn('uuid') | ||
| id: string | ||
|
|
||
| @Column() | ||
| boxId: string | ||
|
|
||
| @Column() | ||
| // Redundant property to optimize billing queries. | ||
| organizationId: string | ||
|
|
||
| @Column({ type: 'timestamp with time zone' }) | ||
| startAt: Date | ||
|
|
||
| @Column({ type: 'timestamp with time zone', nullable: true }) | ||
| endAt: Date | null | ||
|
|
||
| @Column({ type: 'float' }) | ||
| cpu: number | ||
|
|
||
| @Column({ type: 'float' }) | ||
| gpu: number | ||
|
|
||
| @Column({ type: 'float' }) | ||
| mem: number | ||
|
|
||
| @Column({ type: 'float' }) | ||
| disk: number | ||
|
|
||
| @Column() | ||
| region: string | ||
|
|
||
| @Column({ type: 'character varying', default: BoxClass.SMALL }) | ||
| boxClass: BoxClass = BoxClass.SMALL | ||
|
|
||
| @Column({ type: 'character varying', default: RegionType.SHARED }) | ||
| regionType: string = RegionType.SHARED | ||
|
|
||
| public static fromBoxUsagePeriod(usagePeriod: BoxUsagePeriod) { | ||
| const usagePeriodEntity = new BoxUsagePeriod() | ||
| usagePeriodEntity.boxId = usagePeriod.boxId | ||
| usagePeriodEntity.organizationId = usagePeriod.organizationId | ||
| usagePeriodEntity.startAt = usagePeriod.startAt | ||
| usagePeriodEntity.endAt = usagePeriod.endAt | ||
| usagePeriodEntity.cpu = usagePeriod.cpu | ||
| usagePeriodEntity.gpu = usagePeriod.gpu | ||
| usagePeriodEntity.mem = usagePeriod.mem | ||
| usagePeriodEntity.disk = usagePeriod.disk | ||
| usagePeriodEntity.region = usagePeriod.region | ||
| usagePeriodEntity.boxClass = usagePeriod.boxClass | ||
| usagePeriodEntity.regionType = usagePeriod.regionType | ||
| return usagePeriodEntity | ||
| } | ||
| } |
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.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
Unsafe
as Datecast onendAtmasks null values.usagePeriod.endAtisDate | null, but the archive column isNOT NULL. Theas Datecast silences the compiler; if an open period is accidentally passed, it will fail at the DB level with a constraint violation rather than being caught at compile time. Consider a runtime guard or explicit error.🛡️ Proposed fix
public static fromUsagePeriod(usagePeriod: UsagePeriod) { + if (!usagePeriod.endAt) { + throw new Error('Cannot archive a usage period with null endAt') + } const usagePeriodEntity = new UsagePeriodArchive() usagePeriodEntity.boxId = usagePeriod.boxId usagePeriodEntity.organizationId = usagePeriod.organizationId usagePeriodEntity.startAt = usagePeriod.startAt - usagePeriodEntity.endAt = usagePeriod.endAt as Date + usagePeriodEntity.endAt = usagePeriod.endAt📝 Committable suggestion
🤖 Prompt for AI Agents