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

Function GetAllUsage

core/http/auth/usage.go:193–221  ·  view source on GitHub ↗

GetAllUsage returns aggregated usage for all users (admin). Optional userID filter.

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

Source from the content-addressed store, hash-verified

191
192// GetAllUsage returns aggregated usage for all users (admin). Optional userID filter.
193func GetAllUsage(db *gorm.DB, period, userID string) ([]UsageBucket, error) {
194 sqlite := isSQLiteDB(db)
195 since, dateFmt := periodToWindow(period, sqlite)
196
197 bucketExpr := fmt.Sprintf("%s as bucket", dateFmt)
198
199 query := db.Model(&UsageRecord{}).
200 Select(bucketExpr + ", model, user_id, user_name, " +
201 "SUM(prompt_tokens) as prompt_tokens, " +
202 "SUM(completion_tokens) as completion_tokens, " +
203 "SUM(total_tokens) as total_tokens, " +
204 "COUNT(*) as request_count").
205 Group("bucket, model, user_id, user_name").
206 Order("bucket ASC")
207
208 if !since.IsZero() {
209 query = query.Where("created_at >= ?", since)
210 }
211
212 if userID != "" {
213 query = query.Where("user_id = ?", userID)
214 }
215
216 var buckets []UsageBucket
217 if err := query.Find(&buckets).Error; err != nil {
218 return nil, err
219 }
220 return buckets, nil
221}
222
223// TotalsEntry is a token+request roll-up.
224type TotalsEntry struct {

Callers 3

usage_test.goFile · 0.92
RegisterAuthRoutesFunction · 0.92
AggregateMethod · 0.92

Calls 4

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

Tested by

no test coverage detected