| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/url" |
| 6 | "time" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // A Research step on the task page (Research spec §3.6 and §5, slice |
| 12 | // R5): where it is while it waits, in plain words, and once done what it |
| 13 | // found. Everything here is text the page escapes; the only things it |
| 14 | // loads are the kept images, from this instance. |
| 15 | |
| 16 | // researchView is a done Research step's findings as the task page shows |
| 17 | // them: the summary, each fact with the pages it came from, what it could |
| 18 | // not find, the images it kept and its fetch log. |
| 19 | type researchView struct { |
| 20 | At *time.Time |
| 21 | StepID string |
| 22 | Summary string |
| 23 | Facts []researchFact |
| 24 | Gaps []string |
| 25 | Images []researchImage |
| 26 | Fetches []wire.ResearchFetch |
| 27 | } |
| 28 | |
| 29 | // researchFact is one fact and the pages it came from. |
| 30 | type researchFact struct { |
| 31 | Label, Value string |
| 32 | Sources []researchSource |
| 33 | } |
| 34 | |
| 35 | // researchSource is a source a fact cites: its id and the URL the fetch |
| 36 | // log holds for it ("" when the log has none). |
| 37 | type researchSource struct{ ID, URL string } |
| 38 | |
| 39 | // researchImage is a kept image: Src is the instance's own route for it. |
| 40 | type researchImage struct { |
| 41 | wire.StepResearchImage |
| 42 | Src, RoleWord string |
| 43 | } |
| 44 | |
| 45 | // researchItem is a Research step's timeline entry. title is its name |
| 46 | // (with its attempt when there was more than one). |
| 47 | func researchItem(t wire.Task, s wire.Step, v taskLine, title string) timelineItem { |
| 48 | name := stepName(s) |
| 49 | it := timelineItem{Specialist: s.Specialist} |
| 50 | switch s.State { |
| 51 | case wire.StepQueued: |
| 52 | it.Kind, it.What, it.Headline = "queued", fmt.Sprintf(copyText("work.chip.waiting_for"), name), researchWaiting(s, v) |
| 53 | case wire.StepRunning: |
| 54 | it.Kind, it.Live, it.What, it.Since = "step", true, title, s.StartedAt |
| 55 | for _, a := range s.Activity { |
| 56 | it.Lines = append(it.Lines, activityText(a)) |
| 57 | } |
| 58 | if len(it.Lines) == 0 { |
| 59 | it.Lines = []string{copyText("work.research.running")} |
| 60 | } |
| 61 | case wire.StepFailed: |
| 62 | it.Kind, it.What, it.Headline, it.At = "stopped", fmt.Sprintf(copyText("work.tl.stopped"), title), reasonLine(s.Reason, "work.reason.other"), s.FinishedAt |
| 63 | if s.LogTail != "" { |
| 64 | it.Markdown = "```\n" + s.LogTail + "\n```" |
| 65 | } |
| 66 | if s.Research != nil { |
| 67 | // What it fetched before it stopped: the fetch log alone. |
| 68 | it.Research = researchOf(t.ID, *s.Research) |
| 69 | it.Research.StepID = s.ID |
| 70 | it.Research.At = s.FinishedAt |
| 71 | } |
| 72 | case wire.StepCancelled: |
| 73 | // Cancelled with a reason of its own (Research left the team, the |
| 74 | // primary is too old): said in words. |
| 75 | it.Kind, it.What, it.At = "stopped", fmt.Sprintf(copyText("work.tl.cancelled"), title), s.FinishedAt |
| 76 | if s.Reason != "cancelled" { |
| 77 | it.Headline = reasonLine(s.Reason, "") |
| 78 | } |
| 79 | if s.Research != nil { |
| 80 | it.Research = researchOf(t.ID, *s.Research) |
| 81 | it.Research.StepID = s.ID |
| 82 | it.Research.At = s.FinishedAt |
| 83 | } |
| 84 | default: |
| 85 | it.Kind, it.What, it.Took, it.At = "step", title, took(s), s.FinishedAt |
| 86 | if s.Research == nil { |
| 87 | // Ended without running (nothing to read): its reason. |
| 88 | if it.Headline = reasonLine(s.Reason, ""); it.Headline == "" { |
| 89 | it.Headline = headline("", s.Summary) |
| 90 | } |
| 91 | break |
| 92 | } |
| 93 | it.Research = researchOf(t.ID, *s.Research) |
| 94 | it.Research.StepID = s.ID |
| 95 | it.Research.At = s.FinishedAt |
| 96 | } |
| 97 | return it |
| 98 | } |
| 99 | |
| 100 | // reasonLine is a step's reason as a sentence, or fallback's words, or "". |
| 101 | func reasonLine(reason, fallback string) string { |
| 102 | words := copyText("work.reason." + reason) |
| 103 | if words == "" && fallback != "" { |
| 104 | words = copyText(fallback) |
| 105 | } |
| 106 | if words == "" { |
| 107 | return "" |
| 108 | } |
| 109 | return sentenceCase(words) + "." |
| 110 | } |
| 111 | |
| 112 | // researchWaiting is what a waiting Research step waits for. |
| 113 | func researchWaiting(s wire.Step, v taskLine) string { |
| 114 | switch s.Placement { |
| 115 | case wire.PlacementBoundary: |
| 116 | return copyText("work.research.waiting_boundary") |
| 117 | case wire.PlacementSources: |
| 118 | if v.Mine { |
| 119 | return copyText("work.research.waiting_sources") |
| 120 | } |
| 121 | return fmt.Sprintf(copyText("work.research.waiting_sources_other"), v.Creator.Name) |
| 122 | case wire.PlacementInventory: |
| 123 | return copyText("work.research.waiting_inventory") |
| 124 | case wire.PlacementWaiting: |
| 125 | return copyText("work.research.waiting_runner") |
| 126 | } |
| 127 | return "" |
| 128 | } |
| 129 | |
| 130 | // researchOf is a Research step's result as the page shows it (a |
| 131 | // stopped one's is its fetch log alone). A fact's |
| 132 | // sources are named by the URL the fetch log holds for each id. |
| 133 | func researchOf(taskID string, r wire.StepResearch) *researchView { |
| 134 | urls := map[string]string{} |
| 135 | for _, f := range r.Fetches { |
| 136 | if _, ok := urls[f.SourceID]; !ok { |
| 137 | urls[f.SourceID] = f.URL |
| 138 | } |
| 139 | } |
| 140 | v := &researchView{Summary: r.Findings.Summary, Gaps: r.Findings.Gaps, Fetches: r.Fetches} |
| 141 | for _, f := range r.Findings.Facts { |
| 142 | fact := researchFact{Label: f.Label, Value: f.Value} |
| 143 | for _, id := range f.Sources { |
| 144 | fact.Sources = append(fact.Sources, researchSource{ID: id, URL: urls[id]}) |
| 145 | } |
| 146 | v.Facts = append(v.Facts, fact) |
| 147 | } |
| 148 | for _, img := range r.Images { |
| 149 | role := copyText("work.research.role." + img.Role) |
| 150 | if role == "" { |
| 151 | role = copyText("work.research.role.other") |
| 152 | } |
| 153 | v.Images = append(v.Images, researchImage{StepResearchImage: img, Src: "/tasks/" + taskID + "/files/" + img.ID, RoleWord: role}) |
| 154 | } |
| 155 | return v |
| 156 | } |
| 157 | |
| 158 | func (s researchSource) Domain() string { |
| 159 | u, err := url.Parse(s.URL) |
| 160 | if err != nil || u.Hostname() == "" { |
| 161 | return s.ID |
| 162 | } |
| 163 | return u.Hostname() |
| 164 | } |
| 165 | |