Model usage was recorded per call and never read, so an org could not see what it spent. Add the read side: - model_usage carries identity_id. Task calls take it from the task row, chat and curator calls from the calling principal carried in the usage record, so an outbox replay keeps its attribution. Existing task rows are backfilled from tasks. - StatsService.UsageTotals sums the caller org's rows by UTC day, source, provider, model and identity. The org comes from the principal, with no org parameter, so cross-org reads do not exist. - GET /api/usage prices tokens at the catalog's list rate, netting cache hits, and reports calls to unpriced models as tokens only. The published catalog view now carries each model's cost. - A new org Usage page behind a read_usage grant: admin and owner only, with a 7, 30 or 90 day range, totals, the daily series, and model, source and identity breakdowns. The org shell keeps it on the caller's own org.
133 lines
3.0 KiB
Go
133 lines
3.0 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: usage.sql
|
|
|
|
package postgresdb
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
const insertModelUsage = `-- name: InsertModelUsage :exec
|
|
INSERT INTO model_usage (
|
|
org_id, source, task_id, attempt, workflow, step, alias, provider, model,
|
|
identity_id, input_tokens, output_tokens, cached_tokens, at
|
|
) VALUES (
|
|
COALESCE((SELECT t.org_id FROM tasks t WHERE t.id = $1 AND $1 <> 0), $2::text),
|
|
$3, $1, $4, $5, $6, $7,
|
|
$8, $9,
|
|
COALESCE((SELECT t.identity FROM tasks t WHERE t.id = $1 AND $1 <> 0), $10::text),
|
|
$11, $12, $13, $14
|
|
)
|
|
`
|
|
|
|
type InsertModelUsageParams struct {
|
|
TaskID int64
|
|
OrgID string
|
|
Source string
|
|
Attempt int64
|
|
Workflow string
|
|
Step string
|
|
Alias string
|
|
Provider string
|
|
Model string
|
|
IdentityID string
|
|
InputTokens int64
|
|
OutputTokens int64
|
|
CachedTokens int64
|
|
At time.Time
|
|
}
|
|
|
|
// A task's usage belongs to the task's org and identity, whatever the caller
|
|
// says; every other call is billed to the caller's org and identity.
|
|
func (q *Queries) InsertModelUsage(ctx context.Context, arg InsertModelUsageParams) error {
|
|
_, err := q.db.Exec(ctx, insertModelUsage,
|
|
arg.TaskID,
|
|
arg.OrgID,
|
|
arg.Source,
|
|
arg.Attempt,
|
|
arg.Workflow,
|
|
arg.Step,
|
|
arg.Alias,
|
|
arg.Provider,
|
|
arg.Model,
|
|
arg.IdentityID,
|
|
arg.InputTokens,
|
|
arg.OutputTokens,
|
|
arg.CachedTokens,
|
|
arg.At,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const listModelUsageTotals = `-- name: ListModelUsageTotals :many
|
|
SELECT
|
|
to_char(at AT TIME ZONE 'UTC', 'YYYY-MM-DD') AS day,
|
|
source,
|
|
provider,
|
|
model,
|
|
identity_id,
|
|
count(*)::bigint AS calls,
|
|
COALESCE(sum(input_tokens), 0)::bigint AS input_tokens,
|
|
COALESCE(sum(output_tokens), 0)::bigint AS output_tokens,
|
|
COALESCE(sum(cached_tokens), 0)::bigint AS cached_tokens
|
|
FROM model_usage
|
|
WHERE org_id = $1
|
|
AND at >= $2::timestamptz
|
|
AND at < $3::timestamptz
|
|
GROUP BY 1, 2, 3, 4, 5
|
|
ORDER BY 1
|
|
`
|
|
|
|
type ListModelUsageTotalsParams struct {
|
|
OrgID string
|
|
FromAt time.Time
|
|
ToAt time.Time
|
|
}
|
|
|
|
type ListModelUsageTotalsRow struct {
|
|
Day string
|
|
Source string
|
|
Provider string
|
|
Model string
|
|
IdentityID string
|
|
Calls int64
|
|
InputTokens int64
|
|
OutputTokens int64
|
|
CachedTokens int64
|
|
}
|
|
|
|
// One row per day, source, provider, model and identity the org used.
|
|
// cached_tokens is a subset of input_tokens; the reader prices them.
|
|
func (q *Queries) ListModelUsageTotals(ctx context.Context, arg ListModelUsageTotalsParams) ([]ListModelUsageTotalsRow, error) {
|
|
rows, err := q.db.Query(ctx, listModelUsageTotals, arg.OrgID, arg.FromAt, arg.ToAt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListModelUsageTotalsRow
|
|
for rows.Next() {
|
|
var i ListModelUsageTotalsRow
|
|
if err := rows.Scan(
|
|
&i.Day,
|
|
&i.Source,
|
|
&i.Provider,
|
|
&i.Model,
|
|
&i.IdentityID,
|
|
&i.Calls,
|
|
&i.InputTokens,
|
|
&i.OutputTokens,
|
|
&i.CachedTokens,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|