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

Function usageHandler

core/http/routes/usage_test.go:37–66  ·  view source on GitHub ↗

usageHandler reproduces the /api/usage handler logic from routes/usage.go without going through application.Application, which drags in galleryop, model loaders, etc. Keeping this tight test surface lets the no-auth path (the user-visible feature here) be covered without the auth build tag.

(rec *billing.Recorder, fallback *auth.User)

Source from the content-addressed store, hash-verified

35// surface lets the no-auth path (the user-visible feature here) be
36// covered without the auth build tag.
37func usageHandler(rec *billing.Recorder, fallback *auth.User) echo.HandlerFunc {
38 return func(c echo.Context) error {
39 user := auth.GetUser(c)
40 if user == nil {
41 user = fallback
42 }
43 if user == nil {
44 return c.JSON(http.StatusUnauthorized, map[string]string{"error": "not authenticated"})
45 }
46 period := c.QueryParam("period")
47 if period == "" {
48 period = "month"
49 }
50 buckets, err := rec.Aggregate(c.Request().Context(), billing.AggregateQuery{
51 UserID: user.ID,
52 Period: period,
53 })
54 if err != nil {
55 return c.JSON(http.StatusInternalServerError, map[string]string{"error": "agg failed"})
56 }
57 return c.JSON(http.StatusOK, map[string]any{
58 "usage": buckets,
59 "viewer": map[string]string{
60 "id": user.ID,
61 "name": user.Name,
62 "role": user.Role,
63 },
64 })
65 }
66}
67
68var _ = Describe("Usage endpoint", func() {
69 It("resolves the local user in no-auth mode", func() {

Callers 1

usage_test.goFile · 0.85

Calls 5

GetUserFunction · 0.92
JSONMethod · 0.80
AggregateMethod · 0.65
ContextMethod · 0.65
RequestMethod · 0.65

Tested by

no test coverage detected