| 1 | package ownermode |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/kernel" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 6 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 7 | "context" |
| 8 | "encoding/json" |
| 9 | "gorm.io/gorm" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func Status(ctx context.Context, tx *gorm.DB, core *team.Store, b runnerproto.CoordinatorBinding, teamID string, now time.Time, projects any, state string) (map[string]any, error) { |
| 14 | var runners []struct { |
| 15 | ID, Label string |
| 16 | RevokedAt *string |
| 17 | } |
| 18 | if err := tx.Table("runners").Select("id,label,revoked_at").Where("person=? AND instance_id=?", b.Person, teamID).Find(&runners).Error; err != nil { |
| 19 | return nil, err |
| 20 | } |
| 21 | var rows []struct { |
| 22 | ID, RunnerID, LocalID, Label, ReportsJSON string |
| 23 | LastSeen time.Time |
| 24 | } |
| 25 | if err := tx.Table("connections").Select("id,runner_id,local_id,label,reports_json,last_seen").Where("person=? AND revoked_at IS NULL AND runner_id IN (SELECT id FROM runners WHERE person=? AND instance_id=? AND revoked_at IS NULL)", b.Person, b.Person, teamID).Find(&rows).Error; err != nil { |
| 26 | return nil, err |
| 27 | } |
| 28 | type connection struct { |
| 29 | ID, RunnerID, Label, Provider string |
| 30 | Available bool |
| 31 | LastSeen time.Time |
| 32 | Reason string |
| 33 | } |
| 34 | connections := []connection{} |
| 35 | for _, c := range rows { |
| 36 | var reports []runnerproto.ConnectionReport |
| 37 | if err := json.Unmarshal([]byte(c.ReportsJSON), &reports); err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | for _, r := range reports { |
| 41 | connections = append(connections, connection{c.ID, c.RunnerID, c.Label, r.Provider, r.Available && !c.LastSeen.After(now) && now.Sub(c.LastSeen) < 90*time.Second, c.LastSeen, r.UnavailableReason}) |
| 42 | } |
| 43 | } |
| 44 | var budget kernel.BudgetRecord |
| 45 | if err := tx.Where("team_id=?", teamID).Take(&budget).Error; err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | var config kernel.BudgetConfig |
| 49 | if err := json.Unmarshal([]byte(budget.Config), &config); err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | limits, _, err := core.CoordinatorLimits(ctx, team.AccountID(teamID), b.Person) |
| 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | usage, err := core.CoordinatorUsage(ctx, team.AccountID(teamID), b.Person, now) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | |
| 61 | return map[string]any{"runner_id": b.RunnerID, "connection_id": b.ConnectionID, "runners": runners, "connections": connections, "budgets": config, "project_budget_default": config.Team, "coordinator_limits": limits, "coordinator_usage": usage, "projects": projects, "turn_state": state}, nil |
| 62 | } |
| 63 | |