| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "math" |
| 6 | "strconv" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // spendLine is one step's usage on the task page. |
| 13 | type spendLine struct { |
| 14 | Step, Line string |
| 15 | } |
| 16 | |
| 17 | // spendOf is what a task used, in words: the task's quiet line and, in |
| 18 | // step order, one line per step that recorded usage (then the questions |
| 19 | // asked about it). Nothing when no usage was recorded. note says rows |
| 20 | // were left out because they overcount (an older runner's answers). |
| 21 | func spendOf(t wire.Task, sp chat.TaskSpend) (total, note string, lines []spendLine) { |
| 22 | if sp.LeftOut { |
| 23 | note = copyText("work.spend_left_out") |
| 24 | } |
| 25 | if sp.Rows == 0 { |
| 26 | return "", note, nil |
| 27 | } |
| 28 | for _, s := range t.Steps { |
| 29 | one, ok := sp.Steps[s.ID] |
| 30 | if !ok { |
| 31 | continue |
| 32 | } |
| 33 | name := stepName(s) |
| 34 | if s.Round > 0 && s.Specialist == wire.SpecialistEngineering && !s.Rebase { |
| 35 | name += ", " + fmt.Sprintf(copyText("work.round"), s.Round) |
| 36 | } |
| 37 | lines = append(lines, spendLine{Step: s.ID, Line: spendWords(one, name)}) |
| 38 | } |
| 39 | if sp.Questions != (chat.Spend{}) { |
| 40 | lines = append(lines, spendLine{Step: "questions", Line: spendWords(sp.Questions, copyText("work.spend_questions"))}) |
| 41 | } |
| 42 | return spendWords(sp.Total, ""), note, lines |
| 43 | } |
| 44 | |
| 45 | // spendWords is one sum in words: the task's line when name is empty, |
| 46 | // a step's otherwise; "at least" when a row lacked a figure. |
| 47 | func spendWords(s chat.Spend, name string) string { |
| 48 | n := tokenCount(s.Tokens) |
| 49 | if s.Partial { |
| 50 | n = fmt.Sprintf(copyText("work.spend_at_least"), n) |
| 51 | } |
| 52 | switch { |
| 53 | case name == "" && s.Cached > 0: |
| 54 | return fmt.Sprintf(copyText("work.spend"), n, tokenCount(s.Cached)) |
| 55 | case name == "": |
| 56 | return fmt.Sprintf(copyText("work.spend_uncached"), n) |
| 57 | case s.Cached > 0: |
| 58 | return fmt.Sprintf(copyText("work.spend_step"), name, n, tokenCount(s.Cached)) |
| 59 | } |
| 60 | return fmt.Sprintf(copyText("work.spend_step_uncached"), name, n) |
| 61 | } |
| 62 | |
| 63 | // tokenCount is a token figure as people read it: 840, 1.5k, 84k, 1.2M. |
| 64 | func tokenCount(n int64) string { |
| 65 | trim := func(v float64, unit string) string { |
| 66 | return strconv.FormatFloat(math.Round(v*10)/10, 'f', -1, 64) + unit |
| 67 | } |
| 68 | switch { |
| 69 | case n < 1000: |
| 70 | return strconv.FormatInt(n, 10) |
| 71 | case n < 10_000: |
| 72 | return trim(float64(n)/1000, "k") |
| 73 | case n < 999_500: |
| 74 | return strconv.FormatInt(int64(math.Round(float64(n)/1000)), 10) + "k" |
| 75 | } |
| 76 | return trim(float64(n)/1e6, "M") |
| 77 | } |
| 78 | |