MCPcopy Index your code
hub / github.com/coder/coder / getChatUsageLimitConfig

Method getChatUsageLimitConfig

coderd/exp_chats.go:1557–1636  ·  view source on GitHub ↗

@Summary Get chat usage limit config @x-apidocgen {"skip": true} EXPERIMENTAL: this endpoint is experimental and is subject to change. nolint:revive // HTTP handler writes to ResponseWriter.

(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

1555//
1556//nolint:revive // HTTP handler writes to ResponseWriter.
1557func (api *API) getChatUsageLimitConfig(rw http.ResponseWriter, r *http.Request) {
1558 ctx := r.Context()
1559
1560 if !api.Authorize(r, policy.ActionRead, rbac.ResourceDeploymentConfig) {
1561 httpapi.Forbidden(rw)
1562 return
1563 }
1564
1565 config, configErr := api.Database.GetChatUsageLimitConfig(ctx)
1566 if configErr != nil && !errors.Is(configErr, sql.ErrNoRows) {
1567 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
1568 Message: "Failed to get chat usage limit config.",
1569 Detail: configErr.Error(),
1570 })
1571 return
1572 }
1573
1574 overrideRows, err := api.Database.ListChatUsageLimitOverrides(ctx)
1575 if err != nil {
1576 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
1577 Message: "Failed to list chat usage limit overrides.",
1578 Detail: err.Error(),
1579 })
1580 return
1581 }
1582
1583 groupOverrides, err := api.Database.ListChatUsageLimitGroupOverrides(ctx)
1584 if err != nil {
1585 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
1586 Message: "Failed to list group usage limit overrides.",
1587 Detail: err.Error(),
1588 })
1589 return
1590 }
1591
1592 unpricedModelCount, err := api.Database.CountEnabledModelsWithoutPricing(ctx)
1593 if err != nil {
1594 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
1595 Message: "Failed to count unpriced chat models.",
1596 Detail: err.Error(),
1597 })
1598 return
1599 }
1600
1601 response := codersdk.ChatUsageLimitConfigResponse{
1602 ChatUsageLimitConfig: codersdk.ChatUsageLimitConfig{},
1603 UnpricedModelCount: unpricedModelCount,
1604 Overrides: make([]codersdk.ChatUsageLimitOverride, 0, len(overrideRows)),
1605 GroupOverrides: make([]codersdk.ChatUsageLimitGroupOverride, 0, len(groupOverrides)),
1606 }
1607 if configErr == nil {
1608 response.Period = codersdk.ChatUsageLimitPeriod(config.Period)
1609 response.UpdatedAt = config.UpdatedAt
1610 if config.Enabled {
1611 response.SpendLimitMicros = ptr.Ref(config.DefaultLimitMicros)
1612 }
1613 }
1614

Callers 1

chatUsageLimitConfigFunction · 0.80

Calls 13

AuthorizeMethod · 0.95
ForbiddenFunction · 0.92
WriteFunction · 0.92
ChatUsageLimitPeriodTypeAlias · 0.92
RefFunction · 0.92
nullInt64PtrFunction · 0.70
ContextMethod · 0.65
IsMethod · 0.45

Tested by

no test coverage detected