Download this file
| 1 | package personapi |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/kernel" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "gorm.io/gorm" |
| 9 | ) |
| 10 | |
| 11 | type PeriodLimits struct { |
| 12 | Calls int64 `json:"calls"` |
| 13 | InputTokens int64 `json:"input_tokens"` |
| 14 | OutputTokens int64 `json:"output_tokens"` |
| 15 | Seconds int64 `json:"seconds"` |
| 16 | } |
| 17 | type BudgetsInput struct { |
| 18 | Team PeriodLimits `json:"team"` |
| 19 | Projects map[string]PeriodLimits `json:"projects"` |
| 20 | } |
| 21 | type Budgets struct { |
| 22 | BudgetsInput |
| 23 | Revision int64 `json:"revision"` |
| 24 | } |
| 25 | |
| 26 | func ReadBudgetsAt(tx *gorm.DB, allowUnset bool, unavailable error) (config kernel.BudgetConfig, err error) { |
| 27 | err = func() error { |
| 28 | var instance kernel.Team |
| 29 | if err := tx.First(&instance).Error; err != nil { |
| 30 | return err |
| 31 | } |
| 32 | var row kernel.BudgetRecord |
| 33 | if err := tx.Where("team_id = ?", instance.ID).First(&row).Error; err != nil { |
| 34 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 35 | if allowUnset { |
| 36 | return nil |
| 37 | } |
| 38 | return unavailable |
| 39 | } |
| 40 | return err |
| 41 | } |
| 42 | if err := json.Unmarshal([]byte(row.Config), &config); err != nil { |
| 43 | return unavailable |
| 44 | } |
| 45 | if config.Revision != row.Revision || config.Revision <= 0 { |
| 46 | return unavailable |
| 47 | } |
| 48 | if err := (team.BudgetSet{Revision: config.Revision, Team: config.Team, Projects: config.Projects}).Validate(); err != nil { |
| 49 | return unavailable |
| 50 | } |
| 51 | return nil |
| 52 | }() |
| 53 | return config, err |
| 54 | } |
| 55 | |