MCPcopy
hub / github.com/mudler/LocalAI / GetUserUsageBySource

Function GetUserUsageBySource

core/http/auth/usage.go:266–293  ·  view source on GitHub ↗

GetUserUsageBySource returns per-source aggregated usage for one user. Legacy is excluded by design (visible to admins only via the admin variant).

(db *gorm.DB, userID, period string)

Source from the content-addressed store, hash-verified

264// GetUserUsageBySource returns per-source aggregated usage for one user. Legacy
265// is excluded by design (visible to admins only via the admin variant).
266func GetUserUsageBySource(db *gorm.DB, userID, period string) ([]UsageBucket, SourceTotals, error) {
267 sqlite := isSQLiteDB(db)
268 since, dateFmt := periodToWindow(period, sqlite)
269 bucketExpr := fmt.Sprintf("%s as bucket", dateFmt)
270
271 query := db.Model(&UsageRecord{}).
272 Select(bucketExpr+", source, COALESCE(api_key_id, '') as api_key_id, api_key_name, "+
273 "SUM(prompt_tokens) as prompt_tokens, "+
274 "SUM(completion_tokens) as completion_tokens, "+
275 "SUM(total_tokens) as total_tokens, "+
276 "COUNT(*) as request_count").
277 Where("user_id = ?", userID).
278 Where("source <> ?", UsageSourceLegacy).
279 Group("bucket, source, api_key_id, api_key_name").
280 Order("bucket ASC")
281
282 if !since.IsZero() {
283 query = query.Where("created_at >= ?", since)
284 }
285
286 var buckets []UsageBucket
287 if err := query.Find(&buckets).Error; err != nil {
288 return nil, SourceTotals{}, err
289 }
290
291 totals := computeSourceTotals(db, userID, "", since, false)
292 return buckets, totals, nil
293}
294
295// computeSourceTotals rolls up by_source / by_key / grand_total.
296// userID/apiKeyID are optional filters. includeLegacy controls whether the

Callers 3

usage_test.goFile · 0.92
RegisterAuthRoutesFunction · 0.92
newTestAuthAppFunction · 0.92

Calls 5

isSQLiteDBFunction · 0.85
periodToWindowFunction · 0.85
computeSourceTotalsFunction · 0.85
SelectMethod · 0.80
FindMethod · 0.45

Tested by 1

newTestAuthAppFunction · 0.74