| 1 | package ownermode |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/planning" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 6 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 7 | "errors" |
| 8 | "gorm.io/gorm" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | func OwnConnections(tx *gorm.DB, person, teamID string) ([]planning.ConnectionRecord, string, error) { |
| 13 | var n int64 |
| 14 | if err := tx.Table("runners").Where("person=? AND instance_id=? AND revoked_at IS NULL", person, teamID).Count(&n).Error; err != nil { |
| 15 | return nil, "", err |
| 16 | } |
| 17 | if n == 0 { |
| 18 | return nil, "coordinator.setup_runner", nil |
| 19 | } |
| 20 | var cs []planning.ConnectionRecord |
| 21 | if err := tx.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)", person, person, teamID).Order("id").Find(&cs).Error; err != nil { |
| 22 | return nil, "", err |
| 23 | } |
| 24 | if len(cs) == 0 { |
| 25 | return nil, "coordinator.setup_connection", nil |
| 26 | } |
| 27 | return cs, "", nil |
| 28 | } |
| 29 | func Eligible(tx *gorm.DB, c planning.ConnectionRecord, r runnerproto.ConnectionReport, now time.Time, confined func(runnerproto.ConnectionReport) bool, owner func(*gorm.DB, string) (string, error), forbidden error) (bool, error) { |
| 30 | if c.RevokedAt != nil || c.LastSeen.After(now) || now.Sub(c.LastSeen) >= 90*time.Second || !r.Available || !RoutingValid(r) || !confined(r) { |
| 31 | return false, nil |
| 32 | } |
| 33 | person, err := owner(tx, c.RunnerID) |
| 34 | if errors.Is(err, forbidden) { |
| 35 | return false, nil |
| 36 | } |
| 37 | if err == nil && r.OwnerMode && person != c.Person { |
| 38 | return false, nil |
| 39 | } |
| 40 | return err == nil, err |
| 41 | } |
| 42 | func Snapshot(tx *gorm.DB, c planning.ConnectionRecord, r runnerproto.ConnectionReport, confined func(runnerproto.ConnectionReport) bool, owner func(*gorm.DB, string) (string, error), forbidden error) (team.IsolationSnapshot, error) { |
| 43 | status := "unverified" |
| 44 | if confined(r) { |
| 45 | status = "confined" |
| 46 | } |
| 47 | mode := "" |
| 48 | if r.OwnerMode { |
| 49 | person, err := owner(tx, c.RunnerID) |
| 50 | if err != nil { |
| 51 | return team.IsolationSnapshot{}, err |
| 52 | } |
| 53 | if person != c.Person { |
| 54 | return team.IsolationSnapshot{}, forbidden |
| 55 | } |
| 56 | mode = "owner mode" |
| 57 | } |
| 58 | return team.IsolationSnapshot{Mode: mode, Status: status, ObservedTuples: []team.IsolationTuple{Tuple(r)}}, nil |
| 59 | } |
| 60 | |