| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/rand" |
| 6 | "net/http" |
| 7 | "slices" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/go-chi/chi/v5" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 13 | "amadan.net/oficina/equipo/internal/wire" |
| 14 | ) |
| 15 | |
| 16 | // The team panel: the sidebar's specialist rows open one specialist's |
| 17 | // drawer (GET /team/{specialist}, its own URL, so it also works as a |
| 18 | // page without script and on a phone). It shows the task the specialist |
| 19 | // is on, what it is doing in plain words (the raw command behind a |
| 20 | // disclosure), the files its recent lines touched, the connection and |
| 21 | // model it runs on, a box to ask it about the work where asking works, |
| 22 | // and what it did lately. team-panel.js opens the same page as a drawer |
| 23 | // over the current one and keeps it current from the event stream. |
| 24 | |
| 25 | // teamPanel is one specialist's drawer. |
| 26 | type teamPanel struct { |
| 27 | Key, Icon, CopyKey, Name string |
| 28 | // Task and Step are what it is working on now (nil when idle), Stage |
| 29 | // the task's stage in words; More counts its other running tasks and |
| 30 | // Queue the tasks waiting for it. |
| 31 | Task *wire.Task |
| 32 | Step *wire.Step |
| 33 | Stage string |
| 34 | More int |
| 35 | Queue []wire.Task |
| 36 | // Lines are the step's activity, oldest first; Files what they touched. |
| 37 | Lines []teamLine |
| 38 | Files []teamFile |
| 39 | // Provider and Connection (its shared label) say what it runs on; |
| 40 | // Model is "" until the step reports usage (ModelPending while it |
| 41 | // runs). |
| 42 | Provider, Model string |
| 43 | Connection *chat.ConnLabel |
| 44 | ModelPending bool |
| 45 | Ask *teamAsk |
| 46 | Recent []teamRecent |
| 47 | } |
| 48 | |
| 49 | // teamLine is one activity line: what it means, and the raw subject for |
| 50 | // a command (shown only behind a disclosure). |
| 51 | type teamLine struct { |
| 52 | Text, Command, Ago string |
| 53 | At time.Time |
| 54 | } |
| 55 | |
| 56 | // teamAsk is where a question to this specialist goes: the task, whether |
| 57 | // the answer waits for the running step, and the questions already asked. |
| 58 | type teamAsk struct { |
| 59 | TaskID, Title, Specialist, Name, RequestID string |
| 60 | Waits bool |
| 61 | Questions []threadItem |
| 62 | } |
| 63 | |
| 64 | // teamRecent is one task the specialist finished a step on. |
| 65 | type teamRecent struct { |
| 66 | TaskID, Title, Project, Headline, Ago string |
| 67 | At time.Time |
| 68 | } |
| 69 | |
| 70 | func railEntry(key string) (struct{ specialist, icon, copyKey string }, bool) { |
| 71 | for _, s := range specialistRail { |
| 72 | if s.specialist == key { |
| 73 | return s, true |
| 74 | } |
| 75 | } |
| 76 | return struct{ specialist, icon, copyKey string }{}, false |
| 77 | } |
| 78 | |
| 79 | // teamEntry is the sidebar row key names: a built-in specialist, or an |
| 80 | // active data-defined one (its id). |
| 81 | func (a *application) teamEntry(ctx context.Context, person, key string) (struct{ specialist, icon, copyKey string }, bool) { |
| 82 | if s, ok := railEntry(key); ok { |
| 83 | return s, true |
| 84 | } |
| 85 | added, err := a.store.Chat().Specialists(ctx, person) |
| 86 | if err != nil { |
| 87 | return struct{ specialist, icon, copyKey string }{}, false |
| 88 | } |
| 89 | for _, s := range addedRows(added.Items) { |
| 90 | if s.specialist == key { |
| 91 | return s, true |
| 92 | } |
| 93 | } |
| 94 | return struct{ specialist, icon, copyKey string }{}, false |
| 95 | } |
| 96 | |
| 97 | // teamPage is GET /team/{specialist}: a built-in specialist's drawer, or |
| 98 | // a data-defined one's by its id. |
| 99 | func (a *application) teamPage(w http.ResponseWriter, r *http.Request) { |
| 100 | w.Header().Set("Cache-Control", "no-store") |
| 101 | ctx, address, ok := sessionContext(r) |
| 102 | if !ok { |
| 103 | a.failure(w, r, ErrForbidden) |
| 104 | return |
| 105 | } |
| 106 | s, ok := a.teamEntry(ctx, address, chi.URLParam(r, "specialist")) |
| 107 | if !ok { |
| 108 | a.failure(w, r, ErrNotFound) |
| 109 | return |
| 110 | } |
| 111 | data, err := a.page(r, s.specialist) |
| 112 | if err != nil { |
| 113 | a.failure(w, r, err) |
| 114 | return |
| 115 | } |
| 116 | panel, err := a.teamPanel(ctx, address, s.specialist, s.icon, s.copyKey) |
| 117 | if err != nil { |
| 118 | a.failure(w, r, chatError(err)) |
| 119 | return |
| 120 | } |
| 121 | data.Team, data.Title = panel, s.copyKey |
| 122 | render(w, "team", data) |
| 123 | } |
| 124 | |
| 125 | // teamPanel reads one specialist's drawer from the thin core. |
| 126 | func (a *application) teamPanel(ctx context.Context, person, key, icon, copyKey string) (teamPanel, error) { |
| 127 | c := a.store.Chat() |
| 128 | p := teamPanel{Key: key, Icon: icon, CopyKey: copyKey, Name: copyText(copyKey)} |
| 129 | if _, builtIn := railEntry(key); builtIn { |
| 130 | p.Name = specialistName(key) |
| 131 | } |
| 132 | tasks, err := c.Tasks(ctx, person) |
| 133 | if err != nil { |
| 134 | return p, err |
| 135 | } |
| 136 | now := time.Now() |
| 137 | var running []wire.Task |
| 138 | for _, k := range tasks.Items { |
| 139 | if wire.FinalStage(k.Stage) || len(k.Steps) == 0 { |
| 140 | continue |
| 141 | } |
| 142 | switch { |
| 143 | case keyStep(k, key, wire.StepRunning) != nil: |
| 144 | running = append(running, k) |
| 145 | case keyStep(k, key, wire.StepQueued) != nil: |
| 146 | p.Queue = append(p.Queue, k) |
| 147 | } |
| 148 | } |
| 149 | if len(running) > 0 { |
| 150 | k := running[0] |
| 151 | st := *keyStep(k, key, wire.StepRunning) |
| 152 | p.Task, p.Step, p.More = &k, &st, len(running)-1 |
| 153 | p.Stage = copyText("work.stage." + k.Stage) |
| 154 | for _, l := range st.Activity { |
| 155 | line := teamLine{Text: humaneActivity(l), At: l.At, Ago: ago(now, l.At)} |
| 156 | if l.Kind == wire.ActivityRun { |
| 157 | line.Command = l.Subject |
| 158 | } |
| 159 | p.Lines = append(p.Lines, line) |
| 160 | } |
| 161 | slices.Reverse(p.Lines) |
| 162 | p.Files = activityFiles(st.Activity) |
| 163 | p.Provider = providerName(st.Family) |
| 164 | if conns, labels, err := c.LabelledConnections(ctx, person); err == nil { |
| 165 | for _, cn := range conns { |
| 166 | if cn.LocalID == st.Account && (st.RunnerID == "" || cn.RunnerID == st.RunnerID) { |
| 167 | l := chat.LabelOf(labels, cn) |
| 168 | p.Connection = &l |
| 169 | if p.Provider == "" { |
| 170 | p.Provider = providerName(cn.Provider) |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | models, err := c.StepModels(ctx, person, []string{st.ID}) |
| 176 | if err != nil { |
| 177 | return p, err |
| 178 | } |
| 179 | p.Model, p.ModelPending = models[st.ID], models[st.ID] == "" |
| 180 | if kind, ok := finishedAskable(k, key); ok && k.Creator == person && k.AskOpen { |
| 181 | p.Ask = &teamAsk{TaskID: k.ID, Title: k.Title, Specialist: kind, Waits: true} |
| 182 | } |
| 183 | } |
| 184 | // What it finished lately, newest first, one line per task. |
| 185 | type done struct { |
| 186 | k wire.Task |
| 187 | st wire.Step |
| 188 | } |
| 189 | var finished []done |
| 190 | for _, k := range tasks.Items { |
| 191 | if p.Task != nil && k.ID == p.Task.ID { |
| 192 | continue |
| 193 | } |
| 194 | for i := len(k.Steps) - 1; i >= 0; i-- { |
| 195 | if st := k.Steps[i]; stepKey(st) == key && wire.StepTerminal(st.State) && st.FinishedAt != nil { |
| 196 | finished = append(finished, done{k, st}) |
| 197 | break |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | slices.SortStableFunc(finished, func(x, y done) int { return y.st.FinishedAt.Compare(*x.st.FinishedAt) }) |
| 202 | for i, d := range finished { |
| 203 | if i == 5 { |
| 204 | break |
| 205 | } |
| 206 | headline := d.st.Headline |
| 207 | if headline == "" && d.st.Specialist == wire.SpecialistReview { |
| 208 | headline = map[string]string{wire.VerdictApprove: copyText("work.badge.approved"), wire.VerdictChanges: copyText("work.badge.changes")}[d.st.Verdict] |
| 209 | } |
| 210 | p.Recent = append(p.Recent, teamRecent{TaskID: d.k.ID, Title: d.k.Title, Project: d.k.Project, Headline: headline, At: *d.st.FinishedAt, Ago: ago(now, *d.st.FinishedAt)}) |
| 211 | if kind, ok := finishedAskable(d.k, key); ok && p.Task == nil && p.Ask == nil && d.k.Creator == person && d.k.AskOpen { |
| 212 | k := d.k |
| 213 | p.Ask = &teamAsk{TaskID: k.ID, Title: k.Title, Specialist: kind} |
| 214 | } |
| 215 | } |
| 216 | if p.Ask != nil { |
| 217 | // The question goes to the step kind it resumes (Design asks as |
| 218 | // Engineering's kind), named as the drawer's specialist. |
| 219 | p.Ask.Name, p.Ask.RequestID = p.Name, rand.Text() |
| 220 | for _, k := range tasks.Items { |
| 221 | if k.ID != p.Ask.TaskID { |
| 222 | continue |
| 223 | } |
| 224 | thread := threadOf(k, person, 0) |
| 225 | for _, it := range thread.Items { |
| 226 | if !it.Note && it.Who == p.Name { |
| 227 | p.Ask.Questions = append(p.Ask.Questions, it) |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | if n := len(p.Ask.Questions); n > 2 { |
| 232 | p.Ask.Questions = p.Ask.Questions[n-2:] |
| 233 | } |
| 234 | } |
| 235 | return p, nil |
| 236 | } |
| 237 | |
| 238 | // finishedAskable: a question to this specialist would reach its own |
| 239 | // step. A question goes to the newest finished, askable step of its kind |
| 240 | // (Engineering or Review: chat.askTarget), so this specialist can be |
| 241 | // asked only when that step is its own (by stepKey: Design's, not a later |
| 242 | // Engineering's after a handoff); kind is the kind the question names. |
| 243 | func finishedAskable(k wire.Task, key string) (kind string, ok bool) { |
| 244 | for _, want := range []string{wire.SpecialistEngineering, wire.SpecialistReview} { |
| 245 | for i := len(k.Steps) - 1; i >= 0; i-- { |
| 246 | if s := k.Steps[i]; s.Specialist == want && s.Askable && s.State == wire.StepDone { |
| 247 | if stepKey(s) == key { |
| 248 | return want, true |
| 249 | } |
| 250 | break |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | return "", false |
| 255 | } |
| 256 | |
| 257 | // keyStep is the task's newest step of this specialist (by stepKey) in |
| 258 | // state, or nil: a task can run one specialist's step while another's |
| 259 | // waits after it (Research before a held Engineering step). |
| 260 | func keyStep(k wire.Task, key, state string) *wire.Step { |
| 261 | for i := len(k.Steps) - 1; i >= 0; i-- { |
| 262 | if st := k.Steps[i]; stepKey(st) == key && st.State == state { |
| 263 | return &k.Steps[i] |
| 264 | } |
| 265 | } |
| 266 | return nil |
| 267 | } |
| 268 | |