render.gno
13.44 Kb · 274 lines
1package dokimi
2
3import (
4 "strconv"
5 "strings"
6 "time"
7
8 core "gno.land/p/g1v9mz7rtgkvwkw90ey6496h5fk6pj6cgyawkvft/dokimi/v1"
9 "gno.land/p/nt/markdown/sanitize/v0"
10)
11
12const (
13 // listLimit is how many experiments the home page shows, newest first.
14 listLimit = 30
15
16 // webPath is this realm's address on gnoweb, used to build links. It is
17 // the module path in gnomod.toml without the chain domain.
18 webPath = "/r/g1v9mz7rtgkvwkw90ey6496h5fk6pj6cgyawkvft/dokimi/v1"
19
20 // rulesPath is the import path of the pure package holding the rules.
21 rulesPath = "gno.land/p/g1v9mz7rtgkvwkw90ey6496h5fk6pj6cgyawkvft/dokimi/v1"
22)
23
24// Render draws the registry for gnoweb.
25//
26// "" the home page: the list and the form to lock a new plan
27// "<id>" one experiment: its plan, reports, notes and verdict
28func Render(path string) string {
29 if i := strings.IndexByte(path, '?'); i >= 0 {
30 path = path[:i]
31 }
32 path = strings.Trim(path, "/")
33 if path == "" {
34 return renderHome()
35 }
36 id, err := strconv.ParseUint(path, 10, 64)
37 if err != nil {
38 return "# Not found\n\nThere is no page called " + sanitize.InlineCode(path) + ". [Back to all experiments](" + webPath + ")\n"
39 }
40 exp, ok := experiments.Get(key(id)).(*Experiment)
41 if !ok {
42 return "# Not found\n\nExperiment " + strconv.FormatUint(id, 10) + " does not exist. [Back to all experiments](" + webPath + ")\n"
43 }
44 return renderExperiment(exp)
45}
46
47func renderHome() string {
48 var b strings.Builder
49 b.WriteString("# Dokimi\n\n")
50 b.WriteString("**Say what would count as a win before the test starts. Then prove you did not move the bar.**\n\n")
51 b.WriteString("Dokimi is a public record for experiments such as A/B tests. ")
52 b.WriteString("The plan is locked on-chain first. Results are added later and judged only against that plan.\n\n")
53
54 b.WriteString("## Experiments on record\n\n")
55 if experiments.Size() == 0 {
56 b.WriteString("None yet. Lock the first plan with the form below.\n\n")
57 } else {
58 b.WriteString("| # | Experiment | Target | Sample per arm | Status |\n")
59 b.WriteString("|---|---|---|---|---|\n")
60 experiments.ReverseIterateByOffset(0, listLimit, func(_ string, value any) bool {
61 exp := value.(*Experiment)
62 id := strconv.FormatUint(exp.id, 10)
63 b.WriteString("| " + id)
64 b.WriteString(" | [" + sanitize.TableCell(exp.plan.Title) + "](" + webPath + ":" + id + ")")
65 b.WriteString(" | " + targetLabel(exp.plan))
66 b.WriteString(" | " + core.Commas(exp.plan.MinSample))
67 b.WriteString(" | " + statusLabel(exp) + " |\n")
68 return false
69 })
70 b.WriteString("\n")
71 if experiments.Size() > listLimit {
72 b.WriteString("Showing the newest " + strconv.Itoa(listLimit) + " of " + strconv.Itoa(experiments.Size()) + ". Open any experiment by its number: `" + webPath + ":<number>`.\n\n")
73 }
74 }
75
76 b.WriteString("## Lock a new plan\n\n")
77 b.WriteString("Fill this in before your test starts. After you submit, the plan cannot be changed, by you or by anyone else.\n\n")
78 b.WriteString("<gno-form exec=\"Register\">\n")
79 b.WriteString("<gno-input name=\"title\" type=\"text\" placeholder=\"Title\" description=\"A short name for the experiment\" required=\"true\" />\n")
80 b.WriteString("<gno-input name=\"hypothesis\" type=\"text\" placeholder=\"Hypothesis\" description=\"What you expect to happen, in one or two plain sentences on a single line\" required=\"true\" />\n")
81 b.WriteString("<gno-input name=\"metric\" type=\"text\" placeholder=\"Metric\" description=\"The one event or number that decides it, for example signup_completed\" required=\"true\" />\n")
82 b.WriteString("<gno-input name=\"control\" type=\"text\" placeholder=\"Control name\" description=\"What you have today\" required=\"true\" />\n")
83 b.WriteString("<gno-input name=\"variant\" type=\"text\" placeholder=\"Variant name\" description=\"The new version you are testing\" required=\"true\" />\n")
84 b.WriteString("<gno-select name=\"direction\" value=\"increase\" selected=\"true\" description=\"Which way the metric should move\" />\n")
85 b.WriteString("<gno-select name=\"direction\" value=\"decrease\" />\n")
86 b.WriteString("<gno-input name=\"target\" type=\"text\" placeholder=\"Target in percent\" description=\"The smallest relative change that counts as a win, for example 15\" required=\"true\" />\n")
87 b.WriteString("<gno-input name=\"minSample\" type=\"text\" placeholder=\"Minimum sample per arm\" description=\"How many people each version must reach before the result counts\" required=\"true\" />\n")
88 b.WriteString("</gno-form>\n\n")
89
90 b.WriteString("## How it works\n\n")
91 b.WriteString("1. **Lock.** The plan is stored with the block it was written in and a fingerprint of its text.\n")
92 b.WriteString("2. **Report.** The owner adds running totals. Every report is kept and no total can go down. A mistake is fixed with a correction, which is shown as one and never replaces the report it corrects.\n")
93 b.WriteString("3. **Close.** The last report is judged against the locked plan: **met**, **missed** or **inconclusive**. ")
94 b.WriteString("A test stopped before its planned sample is always inconclusive, however good the numbers look.\n\n")
95 b.WriteString("Other realms can register their own experiments here, or import the rules from `" + rulesPath + "`.\n")
96 return b.String()
97}
98
99func renderExperiment(exp *Experiment) string {
100 var b strings.Builder
101 id := strconv.FormatUint(exp.id, 10)
102 p := exp.plan
103
104 b.WriteString("# " + sanitize.InlineText(p.Title) + "\n\n")
105 b.WriteString("[All experiments](" + webPath + ") · Experiment " + id + " · **" + statusLabel(exp) + "**\n\n")
106 b.WriteString("> " + sanitize.InlineText(p.Hypothesis) + "\n\n")
107
108 b.WriteString("## The locked plan\n\n")
109 b.WriteString("| | |\n|---|---|\n")
110 b.WriteString("| Metric | " + sanitize.TableCell(p.Metric) + " |\n")
111 b.WriteString("| Control | " + sanitize.TableCell(p.Control) + " |\n")
112 b.WriteString("| Variant | " + sanitize.TableCell(p.Variant) + " |\n")
113 b.WriteString("| Target | " + targetLabel(p) + " relative change in the metric |\n")
114 b.WriteString("| Minimum sample | " + core.Commas(p.MinSample) + " exposures for each arm |\n")
115 b.WriteString("| Locked | block " + core.Commas(exp.lockedHeight) + ", " + stamp(exp.lockedAt) + " |\n")
116 b.WriteString("| Owner | " + exp.owner.String() + " |\n")
117 b.WriteString("| Fingerprint | `" + exp.digest + "` |\n\n")
118
119 n := len(exp.reports)
120 if exp.closed {
121 b.WriteString("## Verdict: " + outcomeLabel(exp.verdict.Outcome) + "\n\n")
122 b.WriteString(sanitize.InlineText(exp.verdict.Reason) + "\n\n")
123 b.WriteString("Closed at block " + core.Commas(exp.closedHeight) + ", " + stamp(exp.closedAt) + ".\n\n")
124 } else if n == 0 {
125 b.WriteString("## No results yet\n\n")
126 b.WriteString("The plan is locked and waiting for its first report.\n\n")
127 } else {
128 last := exp.reports[n-1]
129 preview := core.Judge(p, last.Control, last.Variant)
130 b.WriteString("## Still running\n\n")
131 b.WriteString("If it were closed on the latest report, the verdict would be **" + outcomeLabel(preview.Outcome) + "**. ")
132 b.WriteString(sanitize.InlineText(preview.Reason) + "\n\n")
133 }
134
135 if n > 0 {
136 b.WriteString("## Reports\n\n")
137 b.WriteString("Running totals as the owner reported them. Each row is permanent. ")
138 if c := corrections(exp); c > 0 {
139 b.WriteString("**" + strconv.Itoa(c) + " of " + strconv.Itoa(n) + " rows are corrections.** ")
140 }
141 first := exp.reports[0]
142 b.WriteString("The first report came " + core.Commas(first.Height-exp.lockedHeight) + " blocks after the plan was locked (" + span(first.Time.Sub(exp.lockedAt)) + ").\n\n")
143 b.WriteString("| # | Block | " + sanitize.TableCell(p.Control) + " | " + sanitize.TableCell(p.Variant) + " | Lift | Source |\n")
144 b.WriteString("|---|---|---|---|---|---|\n")
145 for i, r := range exp.reports {
146 lift := "n/a"
147 if l, ok := core.LiftBps(r.Control, r.Variant); ok {
148 lift = core.FormatBps(l)
149 }
150 source := sanitize.TableCell(r.Source)
151 if r.Correction {
152 source = "**Correction.** " + sanitize.TableCell(r.Reason)
153 } else if source == "" {
154 source = "not given"
155 }
156 b.WriteString("| " + strconv.Itoa(i+1))
157 b.WriteString(" | " + core.Commas(r.Height))
158 b.WriteString(" | " + tallyLabel(r.Control))
159 b.WriteString(" | " + tallyLabel(r.Variant))
160 b.WriteString(" | " + lift)
161 b.WriteString(" | " + source + " |\n")
162 }
163 b.WriteString("\n")
164 }
165
166 if len(exp.notes) > 0 {
167 b.WriteString("## Notes from the owner\n\n")
168 for _, note := range exp.notes {
169 b.WriteString("- Block " + core.Commas(note.Height) + ", " + stamp(note.Time) + ": " + sanitize.InlineText(note.Text) + "\n")
170 }
171 b.WriteString("\n")
172 }
173
174 b.WriteString("## For the owner\n\n")
175 b.WriteString("These only work from the account that locked the plan.\n\n")
176 if !exp.closed {
177 b.WriteString("**Add a report.** Enter totals since the start of the test, not the change since your last report.\n\n")
178 b.WriteString("<gno-form exec=\"AddReport\">\n")
179 b.WriteString("<gno-input name=\"id\" type=\"text\" value=\"" + id + "\" readonly=\"true\" description=\"Experiment number\" />\n")
180 b.WriteString("<gno-input name=\"controlExposures\" type=\"text\" placeholder=\"Control exposures\" description=\"People who saw the control\" required=\"true\" />\n")
181 b.WriteString("<gno-input name=\"controlConversions\" type=\"text\" placeholder=\"Control conversions\" description=\"Of those, how many did the thing the metric counts\" required=\"true\" />\n")
182 b.WriteString("<gno-input name=\"variantExposures\" type=\"text\" placeholder=\"Variant exposures\" description=\"People who saw the variant\" required=\"true\" />\n")
183 b.WriteString("<gno-input name=\"variantConversions\" type=\"text\" placeholder=\"Variant conversions\" description=\"Of those, how many did the thing the metric counts\" required=\"true\" />\n")
184 b.WriteString("<gno-input name=\"source\" type=\"text\" placeholder=\"Source\" description=\"Where these numbers come from, for example your analytics dashboard\" />\n")
185 b.WriteString("</gno-form>\n\n")
186
187 if n > 0 {
188 b.WriteString("**Correct a mistake.** If a report was wrong, enter the right totals and say what happened. The wrong report stays visible and this one is marked as a correction.\n\n")
189 b.WriteString("<gno-form exec=\"AddCorrection\">\n")
190 b.WriteString("<gno-input name=\"id\" type=\"text\" value=\"" + id + "\" readonly=\"true\" description=\"Experiment number\" />\n")
191 b.WriteString("<gno-input name=\"controlExposures\" type=\"text\" placeholder=\"Control exposures\" description=\"The right total of people who saw the control\" required=\"true\" />\n")
192 b.WriteString("<gno-input name=\"controlConversions\" type=\"text\" placeholder=\"Control conversions\" description=\"The right total of conversions for the control\" required=\"true\" />\n")
193 b.WriteString("<gno-input name=\"variantExposures\" type=\"text\" placeholder=\"Variant exposures\" description=\"The right total of people who saw the variant\" required=\"true\" />\n")
194 b.WriteString("<gno-input name=\"variantConversions\" type=\"text\" placeholder=\"Variant conversions\" description=\"The right total of conversions for the variant\" required=\"true\" />\n")
195 b.WriteString("<gno-input name=\"reason\" type=\"text\" placeholder=\"Reason\" description=\"What was wrong with the earlier report\" required=\"true\" />\n")
196 b.WriteString("</gno-form>\n\n")
197 }
198
199 b.WriteString("**Close the experiment.** This stores the verdict for good. No reports can be added afterwards.\n\n")
200 b.WriteString("<gno-form exec=\"Close\">\n")
201 b.WriteString("<gno-input name=\"id\" type=\"text\" value=\"" + id + "\" readonly=\"true\" description=\"Experiment number\" />\n")
202 b.WriteString("</gno-form>\n\n")
203 }
204 b.WriteString("**Add a note.** Use it to tell readers about anything that changed, such as a test that ended early.\n\n")
205 b.WriteString("<gno-form exec=\"AddNote\">\n")
206 b.WriteString("<gno-input name=\"id\" type=\"text\" value=\"" + id + "\" readonly=\"true\" description=\"Experiment number\" />\n")
207 b.WriteString("<gno-input name=\"text\" type=\"text\" placeholder=\"Note\" description=\"One line, shown publicly with the block it was added in\" required=\"true\" />\n")
208 b.WriteString("</gno-form>\n")
209 return b.String()
210}
211
212// corrections counts the reports that were added as corrections.
213func corrections(exp *Experiment) int {
214 c := 0
215 for _, r := range exp.reports {
216 if r.Correction {
217 c++
218 }
219 }
220 return c
221}
222
223// statusLabel is the one-word state shown in lists and headers.
224func statusLabel(exp *Experiment) string {
225 if !exp.closed {
226 if len(exp.reports) == 0 {
227 return "Locked"
228 }
229 return "Running"
230 }
231 return outcomeLabel(exp.verdict.Outcome)
232}
233
234func outcomeLabel(o core.Outcome) string {
235 switch o {
236 case core.Met:
237 return "Target met"
238 case core.Missed:
239 return "Target missed"
240 }
241 return "Inconclusive"
242}
243
244// targetLabel renders the target with the sign of the expected direction.
245func targetLabel(p core.Plan) string {
246 if p.Direction == core.Decrease {
247 return core.FormatBps(-p.TargetBps)
248 }
249 return core.FormatBps(p.TargetBps)
250}
251
252// tallyLabel renders "268 of 1,530 (17.51%)".
253func tallyLabel(t core.Tally) string {
254 return core.Commas(t.Conversions) + " of " + core.Commas(t.Exposures) + " (" + t.RateText() + ")"
255}
256
257// span renders how long passed between two block times, in plain words.
258func span(d time.Duration) string {
259 minutes := int64(d / time.Minute)
260 switch {
261 case minutes < 1:
262 return "less than a minute"
263 case minutes < 120:
264 return core.Commas(minutes) + " minutes"
265 case minutes < 48*60:
266 return core.Commas(minutes/60) + " hours"
267 }
268 return core.Commas(minutes/(24*60)) + " days"
269}
270
271// stamp renders a block time in UTC.
272func stamp(t time.Time) string {
273 return t.UTC().Format("02 Jan 2006 15:04") + " UTC"
274}