Something went wrong. Try again.
the coolest token pot ever
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344// Code generated by sqlc. DO NOT EDIT.// versions:// sqlc v1.31.1// source: billing_rows.sql
package store
import ( "context" "database/sql")
const getUserLiveSpendToday = `-- name: GetUserLiveSpendToday :oneSELECT COALESCE(SUM(CASE WHEN pk.user_id = b.attributed_user_id AND pk.max_micros > pk.shared_micros THEN b.cost_micros ELSE 0 END), 0) AS private_spent_micros, COALESCE(SUM(CASE WHEN NOT (pk.user_id = b.attributed_user_id AND pk.max_micros > pk.shared_micros) THEN b.cost_micros ELSE 0 END), 0) AS shared_spent_microsFROM pool_key_billing_rows bJOIN pool_keys pk ON pk.id = b.pool_key_idWHERE b.attributed_user_id = ? AND b.is_duplicate = 0 AND b.pioneer_created_at >= ?`
type GetUserLiveSpendTodayParams struct { AttributedUserID sql.NullString `json:"attributed_user_id"` PioneerCreatedAt int64 `json:"pioneer_created_at"`}
type GetUserLiveSpendTodayRow struct { PrivateSpentMicros interface{} `json:"private_spent_micros"` SharedSpentMicros interface{} `json:"shared_spent_micros"`}
// Live spend for a user since the start of the current UTC day, read// directly from billing rows. Used by PoolGate for accurate pre-flight// checks without waiting for the reconciler to flush user_daily_spend.// Splits into shared vs private: private = rows where the user owns the// key and the key has a non-zero private reservation (max > shared).func (q *Queries) GetUserLiveSpendToday(ctx context.Context, arg GetUserLiveSpendTodayParams) (GetUserLiveSpendTodayRow, error) { row := q.db.QueryRowContext(ctx, getUserLiveSpendToday, arg.AttributedUserID, arg.PioneerCreatedAt) var i GetUserLiveSpendTodayRow err := row.Scan(&i.PrivateSpentMicros, &i.SharedSpentMicros) return i, err}
const latestBillingRowTime = `-- name: LatestBillingRowTime :oneSELECT COALESCE(SUM(pioneer_created_at * 0) + MAX(pioneer_created_at), 0)FROM pool_key_billing_rowsWHERE pool_key_id = ?`
// Most recent pioneer_created_at for a key.// COALESCE(SUM(pioneer_created_at*0) + MAX(...)) is a workaround for// sqlc's sqlite parser rejecting bare COALESCE(MAX(...), 0).func (q *Queries) LatestBillingRowTime(ctx context.Context, poolKeyID string) (interface{}, error) { row := q.db.QueryRowContext(ctx, latestBillingRowTime, poolKeyID) var coalesce interface{} err := row.Scan(&coalesce) return coalesce, err}
const listAllUsersLiveSpendToday = `-- name: ListAllUsersLiveSpendToday :manySELECT b.attributed_user_id AS user_id, COALESCE(SUM(CASE WHEN pk.user_id = b.attributed_user_id AND pk.max_micros > pk.shared_micros THEN b.cost_micros ELSE 0 END), 0) AS private_spent_micros, COALESCE(SUM(CASE WHEN NOT (pk.user_id = b.attributed_user_id AND pk.max_micros > pk.shared_micros) THEN b.cost_micros ELSE 0 END), 0) AS shared_spent_microsFROM pool_key_billing_rows bJOIN pool_keys pk ON pk.id = b.pool_key_idWHERE b.is_duplicate = 0 AND b.pioneer_created_at >= ? AND b.attributed_user_id IS NOT NULLGROUP BY b.attributed_user_id`
type ListAllUsersLiveSpendTodayRow struct { UserID sql.NullString `json:"user_id"` PrivateSpentMicros interface{} `json:"private_spent_micros"` SharedSpentMicros interface{} `json:"shared_spent_micros"`}
// Live spend for ALL users since dayStart, grouped by user.// Used by RunSmartAllocation so it doesn't depend on the stale// user_daily_spend cache.func (q *Queries) ListAllUsersLiveSpendToday(ctx context.Context, pioneerCreatedAt int64) ([]ListAllUsersLiveSpendTodayRow, error) { rows, err := q.db.QueryContext(ctx, listAllUsersLiveSpendToday, pioneerCreatedAt) if err != nil { return nil, err } defer rows.Close() items := []ListAllUsersLiveSpendTodayRow{} for rows.Next() { var i ListAllUsersLiveSpendTodayRow if err := rows.Scan(&i.UserID, &i.PrivateSpentMicros, &i.SharedSpentMicros); err != nil { return nil, err } items = append(items, i) } if err := rows.Close(); err != nil { return nil, err } if err := rows.Err(); err != nil { return nil, err } return items, nil}
const listBillingRowsForKeyAfter = `-- name: ListBillingRowsForKeyAfter :manySELECT id, pool_key_id, pioneer_created_at, credit_micros, cost_micros, token_usage, model, endpoint, attributed_user_id, attribution, is_duplicate, matched_request_id, ingested_at FROM pool_key_billing_rowsWHERE pool_key_id = ? AND pioneer_created_at > ?ORDER BY pioneer_created_at ASC`
type ListBillingRowsForKeyAfterParams struct { PoolKeyID string `json:"pool_key_id"` PioneerCreatedAt int64 `json:"pioneer_created_at"`}
// All billing rows for a key after a given timestamp.func (q *Queries) ListBillingRowsForKeyAfter(ctx context.Context, arg ListBillingRowsForKeyAfterParams) ([]PoolKeyBillingRow, error) { rows, err := q.db.QueryContext(ctx, listBillingRowsForKeyAfter, arg.PoolKeyID, arg.PioneerCreatedAt) if err != nil { return nil, err } defer rows.Close() items := []PoolKeyBillingRow{} for rows.Next() { var i PoolKeyBillingRow if err := rows.Scan( &i.ID, &i.PoolKeyID, &i.PioneerCreatedAt, &i.CreditMicros, &i.CostMicros, &i.TokenUsage, &i.Model, &i.Endpoint, &i.AttributedUserID, &i.Attribution, &i.IsDuplicate, &i.MatchedRequestID, &i.IngestedAt, ); err != nil { return nil, err } items = append(items, i) } if err := rows.Close(); err != nil { return nil, err } if err := rows.Err(); err != nil { return nil, err } return items, nil}
const listBillingRowsForUserBetween = `-- name: ListBillingRowsForUserBetween :manySELECT id, pool_key_id, pioneer_created_at, credit_micros, cost_micros, token_usage, model, endpoint, attributed_user_id, attribution, is_duplicate, matched_request_id, ingested_at FROM pool_key_billing_rowsWHERE attributed_user_id = ? AND pioneer_created_at >= ? AND pioneer_created_at < ?ORDER BY pioneer_created_at ASC`
type ListBillingRowsForUserBetweenParams struct { AttributedUserID sql.NullString `json:"attributed_user_id"` PioneerCreatedAt int64 `json:"pioneer_created_at"` PioneerCreatedAt_2 int64 `json:"pioneer_created_at_2"`}
// Billing rows attributed to a user in a time window.// Caller filters duplicates and sums in Go.func (q *Queries) ListBillingRowsForUserBetween(ctx context.Context, arg ListBillingRowsForUserBetweenParams) ([]PoolKeyBillingRow, error) { rows, err := q.db.QueryContext(ctx, listBillingRowsForUserBetween, arg.AttributedUserID, arg.PioneerCreatedAt, arg.PioneerCreatedAt_2) if err != nil { return nil, err } defer rows.Close() items := []PoolKeyBillingRow{} for rows.Next() { var i PoolKeyBillingRow if err := rows.Scan( &i.ID, &i.PoolKeyID, &i.PioneerCreatedAt, &i.CreditMicros, &i.CostMicros, &i.TokenUsage, &i.Model, &i.Endpoint, &i.AttributedUserID, &i.Attribution, &i.IsDuplicate, &i.MatchedRequestID, &i.IngestedAt, ); err != nil { return nil, err } items = append(items, i) } if err := rows.Close(); err != nil { return nil, err } if err := rows.Err(); err != nil { return nil, err } return items, nil}
const sumBillingRowsForKey = `-- name: SumBillingRowsForKey :oneSELECT COALESCE(SUM(cost_micros * 0) + SUM(cost_micros), 0)FROM pool_key_billing_rowsWHERE pool_key_id = ? AND is_duplicate = 0`
// Total non-duplicate cost for a key, all time. Used to keep// pool_keys.total_micros accurate after reconciliation.func (q *Queries) SumBillingRowsForKey(ctx context.Context, poolKeyID string) (interface{}, error) { row := q.db.QueryRowContext(ctx, sumBillingRowsForKey, poolKeyID) var coalesce interface{} err := row.Scan(&coalesce) return coalesce, err}
const upsertBillingRow = `-- name: UpsertBillingRow :exec
INSERT INTO pool_key_billing_rows ( id, pool_key_id, pioneer_created_at, credit_micros, cost_micros, token_usage, model, endpoint, attributed_user_id, attribution, is_duplicate, matched_request_id, ingested_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)ON CONFLICT(id) DO NOTHING`
type UpsertBillingRowParams struct { ID string `json:"id"` PoolKeyID string `json:"pool_key_id"` PioneerCreatedAt int64 `json:"pioneer_created_at"` CreditMicros int64 `json:"credit_micros"` CostMicros int64 `json:"cost_micros"` TokenUsage int64 `json:"token_usage"` Model string `json:"model"` Endpoint string `json:"endpoint"` AttributedUserID sql.NullString `json:"attributed_user_id"` Attribution int64 `json:"attribution"` IsDuplicate int64 `json:"is_duplicate"` MatchedRequestID sql.NullString `json:"matched_request_id"` IngestedAt int64 `json:"ingested_at"`}
// pool_key_billing_rows: ingested pioneer billing log entries.//// attribution integer enum://// 0 = matched (joined to a potluck_requests row)// 1 = judge_paired (/llmaj/judge paired to preceding opus call)// 2 = owner_fallback (no match, charged to key owner)// 3 = duplicate (double-logged by pioneer, not charged)func (q *Queries) UpsertBillingRow(ctx context.Context, arg UpsertBillingRowParams) error { _, err := q.db.ExecContext(ctx, upsertBillingRow, arg.ID, arg.PoolKeyID, arg.PioneerCreatedAt, arg.CreditMicros, arg.CostMicros, arg.TokenUsage, arg.Model, arg.Endpoint, arg.AttributedUserID, arg.Attribution, arg.IsDuplicate, arg.MatchedRequestID, arg.IngestedAt, ) return err}
const userHistoryProfile = `-- name: UserHistoryProfile :manySELECT attributed_user_id AS user_id, COALESCE(SUM(cost_micros), 0) AS total_spend_micros, COUNT(DISTINCT pioneer_created_at / 86400) AS days_with_spendFROM pool_key_billing_rowsWHERE is_duplicate = 0 AND attributed_user_id IS NOT NULL AND pioneer_created_at >= ? AND pioneer_created_at < ?GROUP BY attributed_user_id`
type UserHistoryProfileParams struct { PioneerCreatedAt int64 `json:"pioneer_created_at"` PioneerCreatedAt_2 int64 `json:"pioneer_created_at_2"`}
type UserHistoryProfileRow struct { UserID sql.NullString `json:"user_id"` TotalSpendMicros interface{} `json:"total_spend_micros"` DaysWithSpend int64 `json:"days_with_spend"`}
// Per-user historical spend profile over a time window.// Returns one row per user, with total non-duplicate spend and the count// of distinct UTC days that had any spend. Used by smartAllocate for// demand prediction.//// ?1 = window start (unix seconds, inclusive)// ?2 = window end (unix seconds, exclusive)func (q *Queries) UserHistoryProfile(ctx context.Context, arg UserHistoryProfileParams) ([]UserHistoryProfileRow, error) { rows, err := q.db.QueryContext(ctx, userHistoryProfile, arg.PioneerCreatedAt, arg.PioneerCreatedAt_2) if err != nil { return nil, err } defer rows.Close() items := []UserHistoryProfileRow{} for rows.Next() { var i UserHistoryProfileRow if err := rows.Scan(&i.UserID, &i.TotalSpendMicros, &i.DaysWithSpend); err != nil { return nil, err } items = append(items, i) } if err := rows.Close(); err != nil { return nil, err } if err := rows.Err(); err != nil { return nil, err } return items, nil}