oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package planning
2
3import (
4 "reflect"
5 "sort"
6)
7
8func RankSelections(candidates []Selection, coordinator string, rankHeadroom bool, pin, projectPin string) Selection {
9 sort.SliceStable(candidates, func(i, j int) bool {
10 a, b := candidates[i], candidates[j]
11 if (a.ConnectionID == coordinator) != (b.ConnectionID == coordinator) {
12 return a.ConnectionID != coordinator
13 }
14 // Unknown observations are not zero. Compare headroom only when
15 // both observations explicitly report a known remaining amount.
16 if rankHeadroom && a.AllowanceKnown != b.AllowanceKnown {
17 return a.AllowanceKnown
18 }
19 if rankHeadroom && a.AllowanceKnown && b.AllowanceKnown && a.AllowanceRemaining != b.AllowanceRemaining {
20 return a.AllowanceRemaining > b.AllowanceRemaining
21 }
22 if a.ConnectionID != b.ConnectionID {
23 return a.ConnectionID < b.ConnectionID
24 }
25 if a.Provider != b.Provider {
26 return a.Provider < b.Provider
27 }
28 return a.Model < b.Model
29 })
30 result := candidates[0]
31 if rankHeadroom {
32 result.Reason = "connections.reason_best_fit"
33 if result.AllowanceKnown {
34 result.Reason = "connections.reason_headroom"
35 }
36 if coordinator != "" && result.ConnectionID != coordinator {
37 result.Reason = "connections.reason_alternative"
38 }
39 if pin != "" {
40 result.Reason = "connections.reason_proposed"
41 }
42 if pin != "" && pin == projectPin {
43 result.Reason = "connections.reason_pinned"
44 }
45 }
46 return result
47}
48
49func SamePreparationSelection(a, b Selection) bool {
50 // Heartbeat, allowance and the human account label do not change authority.
51 a.AllowanceKnown = b.AllowanceKnown
52 a.AllowanceRemaining = b.AllowanceRemaining
53 a.AccountLabel = b.AccountLabel
54 a.Isolation = b.Isolation
55 a.Reason = b.Reason
56 return reflect.DeepEqual(a, b)
57}
58