|
| 1 | +package adminapi |
| 2 | + |
| 3 | +import "strings" |
| 4 | + |
| 5 | +type modelDisplay struct { |
| 6 | + StatusLabel string |
| 7 | + PolicyLabel string |
| 8 | + PrimaryReason string |
| 9 | + SecondaryReasons []string |
| 10 | +} |
| 11 | + |
| 12 | +func decorateModelDisplay(models []uiModel, role string) { |
| 13 | + for i := range models { |
| 14 | + applyModelDisplay(&models[i], role, false) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +func applyModelDisplay(m *uiModel, role string, current bool) { |
| 19 | + d := modelDisplayFor(*m, role, current) |
| 20 | + m.StatusLabel = d.StatusLabel |
| 21 | + m.PolicyLabel = d.PolicyLabel |
| 22 | + m.PrimaryReason = d.PrimaryReason |
| 23 | + m.SecondaryReasons = d.SecondaryReasons |
| 24 | +} |
| 25 | + |
| 26 | +func modelDisplayFor(m uiModel, role string, current bool) modelDisplay { |
| 27 | + status := modelStatusLabel(m, current) |
| 28 | + policy := modelPolicyLabel(m, status) |
| 29 | + primary := modelPrimaryReason(m, role, status) |
| 30 | + secondary := modelSecondaryReasons(m.Reasons, primary) |
| 31 | + return modelDisplay{ |
| 32 | + StatusLabel: status, |
| 33 | + PolicyLabel: policy, |
| 34 | + PrimaryReason: primary, |
| 35 | + SecondaryReasons: secondary, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func modelStatusLabel(m uiModel, current bool) string { |
| 40 | + if current { |
| 41 | + return "Current" |
| 42 | + } |
| 43 | + switch modelSectionKey(m) { |
| 44 | + case "recommended": |
| 45 | + return "Recommended" |
| 46 | + case "interesting": |
| 47 | + return "Interesting" |
| 48 | + case "untested": |
| 49 | + return "Untested" |
| 50 | + case "blocked": |
| 51 | + return "Blocked" |
| 52 | + default: |
| 53 | + return "Candidate" |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func modelPolicyLabel(m uiModel, status string) string { |
| 58 | + switch m.Policy { |
| 59 | + case "", "candidate", "recommended": |
| 60 | + return "" |
| 61 | + case "manual_allow": |
| 62 | + if status == "Interesting" { |
| 63 | + return "Manual allow" |
| 64 | + } |
| 65 | + return "Manual allow" |
| 66 | + case "manual_deny": |
| 67 | + if status == "Blocked" { |
| 68 | + return "Manual deny" |
| 69 | + } |
| 70 | + return "Manual deny" |
| 71 | + case "free_unverified": |
| 72 | + if status == "Untested" { |
| 73 | + return "Free unverified" |
| 74 | + } |
| 75 | + return "Free unverified" |
| 76 | + case "free_verified": |
| 77 | + return "Free verified" |
| 78 | + case "free_degraded": |
| 79 | + return "Free degraded" |
| 80 | + case "free_blocked": |
| 81 | + if status == "Blocked" { |
| 82 | + return "Free blocked" |
| 83 | + } |
| 84 | + return "Free blocked" |
| 85 | + default: |
| 86 | + label := strings.ReplaceAll(m.Policy, "_", " ") |
| 87 | + return strings.ToUpper(label[:1]) + label[1:] |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func modelPrimaryReason(m uiModel, role, status string) string { |
| 92 | + switch { |
| 93 | + case m.Recommended: |
| 94 | + if role != "" { |
| 95 | + return "Pareto frontier for " + role |
| 96 | + } |
| 97 | + return "Pareto frontier" |
| 98 | + case m.Policy == "manual_allow": |
| 99 | + return firstNonEmpty("Manual allow", m.OverrideNote) |
| 100 | + case m.Policy == "manual_deny": |
| 101 | + return firstNonEmpty("Manual deny", m.OverrideNote) |
| 102 | + case m.Policy == "free_blocked": |
| 103 | + return "Free endpoint is blocked" |
| 104 | + case m.Policy == "free_degraded": |
| 105 | + return "Free endpoint degraded" |
| 106 | + case m.Policy == "free_verified": |
| 107 | + return "Free endpoint verified" |
| 108 | + case m.Policy == "free_unverified": |
| 109 | + return "Free endpoint needs a check" |
| 110 | + case m.Source == "near_frontier": |
| 111 | + if role != "" { |
| 112 | + return "Near frontier for " + role |
| 113 | + } |
| 114 | + return "Near recommendation frontier" |
| 115 | + case status == "Untested": |
| 116 | + return "Compatible but missing role evidence" |
| 117 | + } |
| 118 | + if len(m.Reasons) > 0 { |
| 119 | + return m.Reasons[0] |
| 120 | + } |
| 121 | + return "" |
| 122 | +} |
| 123 | + |
| 124 | +func modelSecondaryReasons(reasons []string, primary string) []string { |
| 125 | + out := make([]string, 0, len(reasons)) |
| 126 | + for _, reason := range uniqueStrings(reasons) { |
| 127 | + if reason == "" || reason == primary { |
| 128 | + continue |
| 129 | + } |
| 130 | + out = append(out, reason) |
| 131 | + } |
| 132 | + return out |
| 133 | +} |
| 134 | + |
| 135 | +func firstNonEmpty(fallback, value string) string { |
| 136 | + if strings.TrimSpace(value) != "" { |
| 137 | + return fallback + ": " + strings.TrimSpace(value) |
| 138 | + } |
| 139 | + return fallback |
| 140 | +} |
0 commit comments