Something went wrong. Try again.
the coolest token pot ever
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101-- Pool key queries.---- Pool keys are pioneer.ai API keys contributed by users to the shared pool.-- The picker selects the active key with the least spend today that is still-- under its daily cap. Spend counters are maintained here; exact accounting-- lives in the spends table.
-- name: CreatePoolKey :oneINSERT INTO pool_keys ( id, user_id, label, key_ciphertext, key_fingerprint, active, daily_limit_micros, today_date, today_micros, total_micros, request_count, created_at, provider_id) VALUES (?, ?, ?, ?, ?, 1, ?, 0, 0, 0, 0, ?, ?)RETURNING *;
-- name: GetPoolKey :oneSELECT * FROM pool_keys WHERE id = ?;
-- name: ListPoolKeys :many-- All keys (for the pool management page). Includes inactive and other users' keys.SELECT pk.*, u.display_name AS owner_name, u.email AS owner_emailFROM pool_keys pkJOIN users u ON u.id = pk.user_idORDER BY pk.active DESC, pk.today_micros ASC;
-- name: ListPoolKeysForUser :manySELECT * FROM pool_keys WHERE user_id = ? ORDER BY created_at DESC;
-- name: PickPoolKey :one-- Select the best key to use for a request: active, under daily cap,-- least spend today. Resets stale today_* counters are handled in Go-- (compare today_date to current UTC day and update if stale).-- ?1 = current UTC day (unix / 86400)SELECT * FROM pool_keysWHERE active = 1 AND (today_date < ?1 OR today_micros < daily_limit_micros)ORDER BY CASE WHEN today_date < ?1 THEN 0 ELSE today_micros END ASC, RANDOM() -- tiebreakLIMIT 1;
-- name: SetPoolKeyActive :execUPDATE pool_keys SET active = ? WHERE id = ? AND user_id = ?;
-- name: DeletePoolKey :execDELETE FROM pool_keys WHERE id = ? AND user_id = ?;
-- name: UpdatePoolKeyLabel :execUPDATE pool_keys SET label = ? WHERE id = ? AND user_id = ?;
-- name: UpdatePoolKeyLimit :execUPDATE pool_keys SET daily_limit_micros = ? WHERE id = ? AND user_id = ?;
-- name: SyncTodaySpend :exec-- Called after fetching real spend from pioneer's billing API.-- Overwrites today_micros with the authoritative value.-- If the key is over its daily limit, also marks it inactive.UPDATE pool_keysSET today_date = ?1, today_micros = ?2, active = CASE WHEN ?2 >= daily_limit_micros THEN 0 ELSE active ENDWHERE id = ?3;
-- name: RecordPoolKeySpend :exec-- Called after a request settles. Resets daily counter if the day rolled over.-- today_day is the current UTC day (unix / 86400).UPDATE pool_keysSET today_date = CASE WHEN today_date < ?1 THEN ?1 ELSE today_date END, today_micros = CASE WHEN today_date < ?1 THEN ?2 ELSE today_micros + ?2 END, total_micros = total_micros + ?2, request_count = request_count + 1, last_used_at = ?3WHERE id = ?4;
-- name: ListPoolAllocations :many-- All users with their pool key stats (if any).-- Users without keys appear with zero contributions.-- private_reservation_micros = sum(max_micros - shared_micros) for active keys.-- today_micros capped at shared_micros: a key whose credit limit dropped (e.g.-- $1000->$50) can't report more pool spend than its current contribution.SELECT u.id AS user_id, u.display_name, u.email, COUNT(pk.id) AS key_count, COALESCE(SUM(CASE WHEN pk.active = 1 THEN pk.shared_micros ELSE 0 END), 0) AS daily_limit_micros, COALESCE(SUM(CASE WHEN pk.active = 1 THEN pk.max_micros - pk.shared_micros ELSE 0 END), 0) AS private_reservation_micros, COALESCE(SUM(CASE WHEN pk.active = 1 THEN MIN(pk.today_micros, pk.shared_micros) ELSE 0 END), 0) AS today_micros, COALESCE(SUM(pk.total_micros), 0) AS total_micros, COALESCE(SUM(pk.request_count), 0) AS request_countFROM users uLEFT JOIN pool_keys pk ON pk.user_id = u.id AND pk.revoked_at IS NULLWHERE u.status = 'active'GROUP BY u.id, u.display_name, u.emailORDER BY daily_limit_micros DESC, u.display_name ASC;