| 1 | package planning |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/equipo/kernel" |
| 8 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 9 | "amadan.net/oficina/equipo/internal/taskplan" |
| 10 | "gorm.io/gorm" |
| 11 | "gorm.io/gorm/clause" |
| 12 | ) |
| 13 | |
| 14 | // SelectionRecord is a revocable pre-start choice. Assignments retain actuals. |
| 15 | type SelectionRecord struct { |
| 16 | TaskID string `gorm:"primaryKey"` |
| 17 | StepKey string `gorm:"primaryKey"` |
| 18 | PlanRevision int64 |
| 19 | Confirmed bool |
| 20 | Selection string |
| 21 | } |
| 22 | |
| 23 | func (SelectionRecord) TableName() string { return "plan_selections" } |
| 24 | |
| 25 | func ActualSelection(tx *gorm.DB, t Task, key string) (runnerproto.Selection, bool, error) { |
| 26 | var row struct{ Selection string } |
| 27 | err := tx.Table("runner_assignments").Select("selection").Where("task_id=? AND job_id=? AND step_key=? AND claim_request<>''", t.ID, t.JobID, key).Order("id").Take(&row).Error |
| 28 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 29 | return runnerproto.Selection{}, false, nil |
| 30 | } |
| 31 | if err != nil { |
| 32 | return runnerproto.Selection{}, false, err |
| 33 | } |
| 34 | var sel runnerproto.Selection |
| 35 | err = json.Unmarshal([]byte(row.Selection), &sel) |
| 36 | return sel, true, err |
| 37 | } |
| 38 | |
| 39 | func (s *Store) Select(actor kernel.Actor, id, key, connection, model string, confirm bool, m kernel.Mutation) (runnerproto.Selection, error) { |
| 40 | var result runnerproto.Selection |
| 41 | t, err := s.requesterTask(actor, id) |
| 42 | if err != nil { |
| 43 | return result, err |
| 44 | } |
| 45 | if t.Requester != actor.Address || s.coordinator != "" { |
| 46 | return result, s.deps.Forbidden |
| 47 | } |
| 48 | p, err := s.Plan(t) |
| 49 | if err != nil { |
| 50 | return result, err |
| 51 | } |
| 52 | var step taskplan.Step |
| 53 | for _, st := range p.Steps { |
| 54 | if st.Key == key { |
| 55 | step = st |
| 56 | } |
| 57 | } |
| 58 | if step.Specialist == "" || !m.Valid() || confirm && (connection == "" || model == "") { |
| 59 | return result, s.deps.Invalid |
| 60 | } |
| 61 | op := "plan.select" |
| 62 | if confirm { |
| 63 | op = "plan.confirm_selection" |
| 64 | } |
| 65 | q, replay, err := s.request(actor, op, id+"/"+key, m, struct { |
| 66 | Connection, Model string |
| 67 | Revision int64 |
| 68 | }{connection, model, m.Revision}, &result) |
| 69 | if err != nil { |
| 70 | return result, err |
| 71 | } |
| 72 | actual, started, err := ActualSelection(s.tx, t, key) |
| 73 | if err != nil { |
| 74 | return result, err |
| 75 | } |
| 76 | if started { |
| 77 | if confirm && (connection != actual.ConnectionID || model != actual.Model) { |
| 78 | return result, s.deps.Conflict |
| 79 | } |
| 80 | if replay { |
| 81 | return result, nil |
| 82 | } |
| 83 | if auditErr := s.deps.Audit(s.tx, actor, op, id+"/"+key); auditErr != nil { |
| 84 | return result, auditErr |
| 85 | } |
| 86 | return actual, s.save(q, actual) |
| 87 | } |
| 88 | if err != nil || replay { |
| 89 | return result, err |
| 90 | } |
| 91 | if m.Revision != p.Revision { |
| 92 | return result, s.deps.Conflict |
| 93 | } |
| 94 | if confirm { |
| 95 | step.ConnectionID = connection |
| 96 | step.Model = model |
| 97 | } |
| 98 | result, err = s.deps.Choose(s.tx, t, p, step, confirm) |
| 99 | if err != nil { |
| 100 | return result, err |
| 101 | } |
| 102 | raw, err := json.Marshal(result) |
| 103 | if err != nil { |
| 104 | return result, err |
| 105 | } |
| 106 | row := SelectionRecord{TaskID: id, StepKey: key, PlanRevision: p.Revision, Confirmed: confirm, Selection: string(raw)} |
| 107 | // Reading a previously confirmed choice must not revoke its consent. |
| 108 | var old SelectionRecord |
| 109 | err = s.tx.First(&old, "task_id=? AND step_key=?", id, key).Error |
| 110 | if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
| 111 | return result, err |
| 112 | } |
| 113 | if err == nil && old.PlanRevision == row.PlanRevision && old.Confirmed { |
| 114 | var previous runnerproto.Selection |
| 115 | if err = json.Unmarshal([]byte(old.Selection), &previous); err != nil { |
| 116 | return result, err |
| 117 | } |
| 118 | if previous.ConnectionID == result.ConnectionID && previous.Model == result.Model && previous.Revision == result.Revision && previous.ChoiceRevision == result.ChoiceRevision && previous.SpecialistRevision == result.SpecialistRevision { |
| 119 | row.Confirmed = true |
| 120 | } |
| 121 | } |
| 122 | err = s.tx.Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "task_id"}, {Name: "step_key"}}, DoUpdates: clause.AssignmentColumns([]string{"plan_revision", "confirmed", "selection"})}).Create(&row).Error |
| 123 | if err != nil { |
| 124 | return result, err |
| 125 | } |
| 126 | if err = s.deps.Fence(s.tx, id); err != nil { |
| 127 | return result, err |
| 128 | } |
| 129 | if err = s.deps.Audit(s.tx, actor, op, id+"/"+key); err != nil { |
| 130 | return result, err |
| 131 | } |
| 132 | if err = s.deps.Card(s.tx, t); err != nil { |
| 133 | return result, err |
| 134 | } |
| 135 | return result, s.save(q, result) |
| 136 | } |
| 137 | |
| 138 | // SelectionView excludes routing, account identity hashes and credential data. |
| 139 | type SelectionView struct { |
| 140 | ConnectionID, Model, Account, Reason string |
| 141 | AllowanceRemaining *int64 |
| 142 | Actual, Confirmed, Stale bool |
| 143 | } |
| 144 | |
| 145 | func SavedSelection(tx *gorm.DB, t Task, key string) (runnerproto.Selection, bool, bool, error) { |
| 146 | sel, actual, err := ActualSelection(tx, t, key) |
| 147 | if err != nil || actual { |
| 148 | return sel, actual, true, err |
| 149 | } |
| 150 | var row SelectionRecord |
| 151 | err = tx.First(&row, "task_id=? AND step_key=? AND plan_revision=?", t.ID, key, t.PlanRevision).Error |
| 152 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 153 | return sel, false, false, nil |
| 154 | } |
| 155 | if err != nil { |
| 156 | return sel, false, false, err |
| 157 | } |
| 158 | err = json.Unmarshal([]byte(row.Selection), &sel) |
| 159 | return sel, false, row.Confirmed, err |
| 160 | } |
| 161 | func (s *Store) selectionView(a kernel.Actor, t Task, key string) (*SelectionView, error) { |
| 162 | sel, actual, confirmed, err := SavedSelection(s.tx, t, key) |
| 163 | if err != nil || sel.ConnectionID == "" { |
| 164 | return nil, err |
| 165 | } |
| 166 | v := &SelectionView{Model: sel.Model, Reason: sel.Reason, Actual: actual, Confirmed: confirmed} |
| 167 | if !actual { |
| 168 | p, e := s.Plan(t) |
| 169 | if e != nil { |
| 170 | return nil, e |
| 171 | } |
| 172 | specialist := "" |
| 173 | for _, st := range p.Steps { |
| 174 | if st.Key == key { |
| 175 | specialist = st.Specialist |
| 176 | } |
| 177 | } |
| 178 | var n int64 |
| 179 | e = s.tx.Table("connections c").Joins("JOIN connection_choices choice ON choice.person=c.person AND choice.project_id=?", t.ProjectID).Joins("JOIN specialists sp ON sp.key=?", specialist).Where("c.id=? AND c.person=? AND c.revision=? AND choice.revision=? AND sp.revision=?", sel.ConnectionID, t.Requester, sel.Revision, sel.ChoiceRevision, sel.SpecialistRevision).Count(&n).Error |
| 180 | if e != nil { |
| 181 | return nil, e |
| 182 | } |
| 183 | v.Stale = n != 1 |
| 184 | } |
| 185 | if a.Address == t.Requester { |
| 186 | v.ConnectionID = sel.ConnectionID |
| 187 | v.Account = sel.AccountLabel |
| 188 | if sel.AllowanceKnown { |
| 189 | n := sel.AllowanceRemaining |
| 190 | v.AllowanceRemaining = &n |
| 191 | } |
| 192 | } |
| 193 | return v, nil |
| 194 | } |
| 195 | |