@prismshadow/penguin-library-observability · library/observability/src/usage-service.ts:122

UsageService

component

No JSDoc comment in source yet.

API

@Component()
export class UsageService implements UsageQueries {
  @Use() private readonly usage!: UsageStore;
  @Use() private readonly errors!: ErrorLog;
  @Use() private readonly projectConfig!: ProjectConfigStore;
  @Use() private readonly clock!: UsageClock;
  async costBySession(projectId: string, sessionIds: readonly string[], fromTs: string, toTs: string): Promise<{ bySession: Map<string, number>; unpriced: boolean }>;
  async dailyCostForSessions(projectId: string, sessionIds: readonly string[], fromTs: string, toTs: string): Promise<Array<{ date: string; cost: number }>>;
  async query(projectId: string, q: UsageQuery): Promise<UsageResponse>;
  queryErrors(projectId: string, q: UsageErrorsQuery): UsageErrorsPage;
  clearErrors(projectId: string, q: UsageErrorsClearQuery): number;
  modelTotals(projectId: string): UsageModelTotals;
}

Dependencies & Wiring

Extracted from class decorators
implementsinterfaces
@Userequires (4)
usage: UsageStore · errors: ErrorLog · projectConfig: ProjectConfigStore · clock: UsageClock

Methods (6)

costBySession

async costBySession(projectId: string, sessionIds: readonly string[], fromTs: string, toTs: string): Promise<{ bySession: Map<string, number>; unpriced: boolean }>

Period cost per session, for company mode's attribution by the sessions an organization owns: sums by paired reference priced at the current rates (each half at the tier its own records ran in). unpriced says some usage ran on a model without pricing, so the numbers are a lower bound.

@paramprojectIdstring
@paramsessionIdsreadonly string[]
@paramfromTsstring
@paramtoTsstring
@returnsPromise<{ bySession: Map<string, number>; unpriced: boolean }>

dailyCostForSessions

async dailyCostForSessions(projectId: string, sessionIds: readonly string[], fromTs: string, toTs: string): Promise<Array<{ date: string; cost: number }>>

Daily cost of a set of sessions over a window (the organization finance trend).

@paramprojectIdstring
@paramsessionIdsreadonly string[]
@paramfromTsstring
@paramtoTsstring
@returnsPromise<Array<{ date: string; cost: number }>>

query

async query(projectId: string, q: UsageQuery): Promise<UsageResponse>
@paramprojectIdstring
@paramqUsageQuery
@returnsPromise<UsageResponse>

queryErrors

queryErrors(projectId: string, q: UsageErrorsQuery): UsageErrorsPage

One page of the error detail table, newest first. The dashboard's own response already carries the first page (errors.recent); this serves the "show me earlier ones" paging, where refetching the whole aggregate to move one page would be wasteful. rows is the filtered row count, so the caller knows when it has reached the end; total counts the records behind those rows, the occurrences the badge reports.

Takes the same filter the dashboard applies — date + agent, and admin-only visibility of unattributed errors — so a page never widens what the summary above it counted.

Paging is by offset into a newest-first table that grows at its head, so errors recorded between two page requests shift every row down and a page can re-show rows already seen. Accepted deliberately: this is a diagnostic table read at a moment in time, not a feed to be walked exhaustively, and keying off the newest row's id would cost a cursor the caller has no other use for.

@paramprojectIdstring
@paramqUsageErrorsQuery
@returnsUsageErrorsPage

clearErrors

clearErrors(projectId: string, q: UsageErrorsClearQuery): number

Empties the error table for the filter the panel is showing, and answers how many rows went.

The filter is the same range (dates, and the trailing window when one is on) and Agent the dashboard and the paged route take, read with the same admin visibility, so a clear removes exactly the set the caller was looking at and never a row outside it: a reader who has narrowed to one Agent and one week does not lose the rest of the year to a button that said "clear", and an admin whose panel showed unattributed rows is not left holding them after clearing it (see ErrorsRepo.deleteFiltered).

@paramprojectIdstring
@paramqUsageErrorsClearQuery
@returnsnumber

modelTotals

modelTotals(projectId: string): UsageModelTotals

Lifetime Token totals per Model — one grouped scan of usage_records, no filters. The models page shows each configured model what it has actually spent, and that figure is not scoped to a range the way the cost center's is; a model with no records is simply absent, so the caller renders nothing rather than a zero.

@paramprojectIdstring
@returnsUsageModelTotals