oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package ownermode
2
3import (
4 "amadan.net/oficina/equipo/internal/equipo/planning"
5 "gorm.io/gorm"
6 "sort"
7 "time"
8)
9
10func RankConnections(tx *gorm.DB, connections []planning.ConnectionRecord, now time.Time, decode func(*planning.ConnectionRecord) error) error {
11 busy := map[string]int64{}
12 healthy := map[string]bool{}
13 for i := range connections {
14 c := &connections[i]
15 if err := decode(c); err != nil {
16 return err
17 }
18 for _, r := range c.Reports {
19 healthy[c.ID] = healthy[c.ID] || r.Available && !c.LastSeen.After(now) && now.Sub(c.LastSeen) < 90*time.Second
20 }
21 var n int64
22 if err := tx.Table("runner_assignments").Where("person=? AND state IN ('claimed','reconcile','fenced') AND attempt_id IN (SELECT id FROM attempts WHERE containment='pending') AND json_extract(selection,'$.ConnectionID')=?", c.Person, c.ID).Count(&n).Error; err != nil {
23 return err
24 }
25 busy[c.ID] = n
26 }
27 sort.SliceStable(connections, func(i, j int) bool {
28 a, b := connections[i], connections[j]
29 if healthy[a.ID] != healthy[b.ID] {
30 return healthy[a.ID]
31 }
32 if busy[a.ID] != busy[b.ID] {
33 return busy[a.ID] < busy[b.ID]
34 }
35 return a.LastSeen.After(b.LastSeen)
36 })
37 return nil
38}
39