the coolest token pot ever
0

Configure Feed

Select the types of activity you want to include in your feed.

feat: fix stats for other providers

Kieran Klukas (Jun 16, 2026, 1:37 PM EDT) fba23fa9 3937bf38

+123 -4
+24
server/db/queries/prices.sql
··· 35 35 WHERE pkbr.is_duplicate = 0 36 36 GROUP BY pkbr.model 37 37 ORDER BY request_count DESC; 38 + 39 + -- name: ListModelStatsFromRequests :many 40 + -- Per-model aggregate directly from potluck_requests. 41 + -- Covers all providers (including those without billing rows). 42 + -- since scopes the TPS window (48h); token counts are all-time. 43 + SELECT 44 + model, 45 + COUNT(*) AS request_count, 46 + SUM(COALESCE(prompt_tokens, 0)) AS total_input_tokens, 47 + SUM(COALESCE(completion_tokens, 0)) AS total_output_tokens, 48 + AVG( 49 + CASE 50 + WHEN finished_at IS NOT NULL 51 + AND finished_at > started_at 52 + AND started_at >= ? 53 + THEN CAST(COALESCE(completion_tokens, 0) AS REAL) 54 + / (finished_at - started_at) 55 + ELSE NULL 56 + END 57 + ) AS avg_tps 58 + FROM potluck_requests 59 + WHERE status = 'done' 60 + GROUP BY model 61 + ORDER BY request_count DESC;
+33 -4
server/internal/api/web/models.go
··· 23 23 refreshedAt = toInt64(ts) 24 24 } 25 25 26 - // Stats cover the last 48h. 27 - statsRows, _ := s.Q.ListModelStats(r.Context(), time.Now().Add(-48*time.Hour).Unix()) 28 - statsMap := make(map[string]any, len(statsRows)) 29 - for _, st := range statsRows { 26 + // Stats cover the last 48h. Merge billing-row stats (pioneer) with 27 + // potluck_requests stats (all providers). Request-based stats take 28 + // precedence since they use the full prefixed model ID that matches 29 + // the catalog. 30 + since := time.Now().Add(-48 * time.Hour).Unix() 31 + statsMap := make(map[string]any) 32 + 33 + // First: billing-row stats (legacy pioneer, unprefixed model names). 34 + billingRows, _ := s.Q.ListModelStats(r.Context(), since) 35 + for _, st := range billingRows { 36 + var avgTps *float64 37 + if st.AvgTps.Valid { 38 + avgTps = &st.AvgTps.Float64 39 + } 40 + var totalIn, totalOut float64 41 + if st.TotalInputTokens.Valid { 42 + totalIn = st.TotalInputTokens.Float64 43 + } 44 + if st.TotalOutputTokens.Valid { 45 + totalOut = st.TotalOutputTokens.Float64 46 + } 47 + statsMap[st.Model] = map[string]any{ 48 + "request_count": st.RequestCount, 49 + "total_input_tokens": totalIn, 50 + "total_output_tokens": totalOut, 51 + "avg_tps": avgTps, 52 + } 53 + } 54 + 55 + // Second: request-based stats (all providers, prefixed model names). 56 + // These override billing-row stats when keys match. 57 + reqRows, _ := s.Q.ListModelStatsFromRequests(r.Context(), since) 58 + for _, st := range reqRows { 30 59 var avgTps *float64 31 60 if st.AvgTps.Valid { 32 61 avgTps = &st.AvgTps.Float64
+62
server/internal/store/prices.sql.go
··· 120 120 return items, nil 121 121 } 122 122 123 + const listModelStatsFromRequests = `-- name: ListModelStatsFromRequests :many 124 + SELECT 125 + model, 126 + COUNT(*) AS request_count, 127 + SUM(COALESCE(prompt_tokens, 0)) AS total_input_tokens, 128 + SUM(COALESCE(completion_tokens, 0)) AS total_output_tokens, 129 + AVG( 130 + CASE 131 + WHEN finished_at IS NOT NULL 132 + AND finished_at > started_at 133 + AND started_at >= ? 134 + THEN CAST(COALESCE(completion_tokens, 0) AS REAL) 135 + / (finished_at - started_at) 136 + ELSE NULL 137 + END 138 + ) AS avg_tps 139 + FROM potluck_requests 140 + WHERE status = 'done' 141 + GROUP BY model 142 + ORDER BY request_count DESC 143 + ` 144 + 145 + type ListModelStatsFromRequestsRow struct { 146 + Model string `json:"model"` 147 + RequestCount int64 `json:"request_count"` 148 + TotalInputTokens sql.NullFloat64 `json:"total_input_tokens"` 149 + TotalOutputTokens sql.NullFloat64 `json:"total_output_tokens"` 150 + AvgTps sql.NullFloat64 `json:"avg_tps"` 151 + } 152 + 153 + // Per-model aggregate directly from potluck_requests. 154 + // Covers all providers (including those without billing rows). 155 + // since scopes the TPS window (48h); token counts are all-time. 156 + func (q *Queries) ListModelStatsFromRequests(ctx context.Context, startedAt int64) ([]ListModelStatsFromRequestsRow, error) { 157 + rows, err := q.db.QueryContext(ctx, listModelStatsFromRequests, startedAt) 158 + if err != nil { 159 + return nil, err 160 + } 161 + defer rows.Close() 162 + items := []ListModelStatsFromRequestsRow{} 163 + for rows.Next() { 164 + var i ListModelStatsFromRequestsRow 165 + if err := rows.Scan( 166 + &i.Model, 167 + &i.RequestCount, 168 + &i.TotalInputTokens, 169 + &i.TotalOutputTokens, 170 + &i.AvgTps, 171 + ); err != nil { 172 + return nil, err 173 + } 174 + items = append(items, i) 175 + } 176 + if err := rows.Close(); err != nil { 177 + return nil, err 178 + } 179 + if err := rows.Err(); err != nil { 180 + return nil, err 181 + } 182 + return items, nil 183 + } 184 + 123 185 const upsertModelPrice = `-- name: UpsertModelPrice :exec 124 186 INSERT INTO model_prices (model, input_micros_per_1k, output_micros_per_1k, updated_at) 125 187 VALUES (?, ?, ?, ?)
+4
server/internal/store/querier.go
··· 110 110 // Per-model aggregate from billing rows + potluck_requests. 111 111 // since scopes the TPS window (48h); cost/token counts are all-time. 112 112 ListModelStats(ctx context.Context, startedAt int64) ([]ListModelStatsRow, error) 113 + // Per-model aggregate directly from potluck_requests. 114 + // Covers all providers (including those without billing rows). 115 + // since scopes the TPS window (48h); token counts are all-time. 116 + ListModelStatsFromRequests(ctx context.Context, startedAt int64) ([]ListModelStatsFromRequestsRow, error) 113 117 // All users with their pool key stats (if any). 114 118 // Users without keys appear with zero contributions. 115 119 // private_reservation_micros = sum(max_micros - shared_micros) for active keys.