import { type Database } from "./database.js"; /** * Site platform usage (migration 0790): gateway requests, Site SQL rows read * and written, and the Site SQL plus Site Data storage level, metered per * workspace and month apart from model spend, plus deployment-global * per-month totals in eight shards and the Opengeni-wide monthly brake. * * The global tables are reached through the platform lane: a transaction * sets `opengeni.site_platform_lane` first (the only policy on them). They * hold counters and the brake state, no tenant content. Writers lock the * workspace row before a shard row. */ export type SitePlatformScope = { accountId: string; workspaceId: string; }; export type SitePlatformUsageRecord = { periodStart: string; requests: number; sqlRowsRead: number; sqlRowsWritten: number; storageBytes: number; storagePeakBytes: number; chargedMicros: number; shadowMicros: number; chargeSeq: number; }; export type SitePlatformTotals = { periodStart: string; requests: number; sqlRowsRead: number; sqlRowsWritten: number; storageBytes: number; }; export type SitePlatformBrakeRecord = { periodStart: string; alertedAt: Date | null; engagedAt: Date | null; engagedBy: "auto" | "operator" | null; engagedReason: string | null; liftedAt: Date | null; liftedBy: string | null; liftReason: string | null; lastEstimateMicros: number; }; export declare const SITE_PLATFORM_SHARDS = 8; /** The shard of a workspace's global counters (stable per workspace). */ export declare function sitePlatformShard(workspaceId: string): number; /** The first day of the UTC month of `now`, YYYY-MM-DD. */ export declare function sitePlatformPeriod(now?: Date): string; export type SitePlatformUsageChange = { requests?: number; sqlRowsRead?: number; sqlRowsWritten?: number; /** Recompute the workspace's storage level (after a Site SQL or Site Data write). */ refreshStorage?: boolean; now?: Date; }; /** * Add platform usage for the transaction's workspace (which must already run * in that workspace's RLS context): lock and roll the workspace row, add the * counters, recompute the storage level when asked, then move the global * shard totals and storage level by the same amounts. Returns the row after. */ export declare function recordSitePlatformUsageInTransaction(tx: Database, scope: SitePlatformScope, change: SitePlatformUsageChange): Promise; /** Record platform usage for one workspace in its own transaction. */ export declare function recordSitePlatformUsage(db: Database, scope: SitePlatformScope, change: SitePlatformUsageChange): Promise; /** * Count gateway requests of one report once: the usage event keyed by * `idempotencyKey` is inserted with the counters, and a replayed report * (same key) changes nothing. Returns whether the requests were counted. */ export declare function recordSitePlatformRequests(db: Database, scope: SitePlatformScope, input: { siteId: string; requests: number; idempotencyKey: string; now?: Date; }): Promise; /** This month's platform usage of a workspace (zero counters, current storage, when unused). */ export declare function readSitePlatformUsage(db: Database, scope: SitePlatformScope, now?: Date): Promise; /** * Claim a charge of this month's usage: when `billableMicros` exceeds what * was charged by at least `minimumMicros`, raise charged_micros to it and * bump the charge sequence, and run `debit` for the delta in the same * transaction (a failed debit rolls the claim back, so the next call retries * it; the sequence keys the debit, so a replay never charges twice). Returns * the claim, or null when there is nothing to charge. Shadow mode only * records shadow_micros. */ export declare function claimSitePlatformCharge(db: Database, scope: SitePlatformScope, input: { billableMicros: number; minimumMicros: number; mode: "shadow" | "credits"; debit?: (tx: Database, claim: { deltaMicros: number; seq: number; periodStart: string; }) => Promise; now?: Date; }): Promise<{ deltaMicros: number; seq: number; periodStart: string; } | null>; /** Opengeni-wide totals of this month and the current storage level. */ export declare function readSitePlatformTotals(db: Database, now?: Date): Promise; /** The brake state of the month of `now` (a new month starts released). */ export declare function readSitePlatformBrake(db: Database, now?: Date): Promise; /** * Record an evaluation of this month's estimate: the first one at or past * the alert threshold sets alerted_at, and one at or past the limit engages * the brake automatically unless it is engaged already or an operator lifted * it this month. Returns the state after and what changed. */ export declare function evaluateSitePlatformBrake(db: Database, input: { estimateMicros: number; alert: boolean; limit: boolean; now?: Date; }): Promise<{ brake: SitePlatformBrakeRecord; alerted: boolean; engaged: boolean; }>; /** Engage the brake by hand for this month (clears a lift). */ export declare function engageSitePlatformBrake(db: Database, input: { reason: string; now?: Date; }): Promise; /** * Lift the brake for the rest of this month: it stays released, and is not * engaged automatically again, until the month ends or an operator engages it. */ export declare function liftSitePlatformBrake(db: Database, input: { by: string; reason: string; now?: Date; }): Promise;