render.gno
11.62 Kb · 246 lines
1package dokimi
2
3import (
4 "strconv"
5 "strings"
6 "time"
7
8 core "gno.land/p/g1v9mz7rtgkvwkw90ey6496h5fk6pj6cgyawkvft/dokimi/v0"
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/v0"
19
20 // rulesPath is the import path of the pure package holding the rules.
21 rulesPath = "gno.land/p/g1v9mz7rtgkvwkw90ey6496h5fk6pj6cgyawkvft/dokimi/v0"
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=\"number\" 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.\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 plan, locked before the test\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 first := exp.reports[0]
139 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")
140 b.WriteString("| # | Block | " + sanitize.TableCell(p.Control) + " | " + sanitize.TableCell(p.Variant) + " | Lift | Source |\n")
141 b.WriteString("|---|---|---|---|---|---|\n")
142 for i, r := range exp.reports {
143 lift := "n/a"
144 if l, ok := core.LiftBps(r.Control, r.Variant); ok {
145 lift = core.FormatBps(l)
146 }
147 source := sanitize.TableCell(r.Source)
148 if source == "" {
149 source = "not given"
150 }
151 b.WriteString("| " + strconv.Itoa(i+1))
152 b.WriteString(" | " + core.Commas(r.Height))
153 b.WriteString(" | " + tallyLabel(r.Control))
154 b.WriteString(" | " + tallyLabel(r.Variant))
155 b.WriteString(" | " + lift)
156 b.WriteString(" | " + source + " |\n")
157 }
158 b.WriteString("\n")
159 }
160
161 if len(exp.notes) > 0 {
162 b.WriteString("## Notes from the owner\n\n")
163 for _, note := range exp.notes {
164 b.WriteString("- Block " + core.Commas(note.Height) + ", " + stamp(note.Time) + ": " + sanitize.InlineText(note.Text) + "\n")
165 }
166 b.WriteString("\n")
167 }
168
169 b.WriteString("## For the owner\n\n")
170 b.WriteString("These only work from the account that locked the plan.\n\n")
171 if !exp.closed {
172 b.WriteString("**Add a report.** Enter totals since the start of the test, not the change since your last report.\n\n")
173 b.WriteString("<gno-form exec=\"AddReport\">\n")
174 b.WriteString("<gno-input name=\"id\" type=\"number\" value=\"" + id + "\" readonly=\"true\" description=\"Experiment number\" />\n")
175 b.WriteString("<gno-input name=\"controlExposures\" type=\"number\" placeholder=\"Control exposures\" description=\"People who saw the control\" required=\"true\" />\n")
176 b.WriteString("<gno-input name=\"controlConversions\" type=\"number\" placeholder=\"Control conversions\" description=\"Of those, how many did the thing the metric counts\" required=\"true\" />\n")
177 b.WriteString("<gno-input name=\"variantExposures\" type=\"number\" placeholder=\"Variant exposures\" description=\"People who saw the variant\" required=\"true\" />\n")
178 b.WriteString("<gno-input name=\"variantConversions\" type=\"number\" placeholder=\"Variant conversions\" description=\"Of those, how many did the thing the metric counts\" required=\"true\" />\n")
179 b.WriteString("<gno-input name=\"source\" type=\"text\" placeholder=\"Source\" description=\"Where these numbers come from, for example your analytics dashboard\" />\n")
180 b.WriteString("</gno-form>\n\n")
181
182 b.WriteString("**Close the experiment.** This stores the verdict for good. No reports can be added afterwards.\n\n")
183 b.WriteString("<gno-form exec=\"Close\">\n")
184 b.WriteString("<gno-input name=\"id\" type=\"number\" value=\"" + id + "\" readonly=\"true\" description=\"Experiment number\" />\n")
185 b.WriteString("</gno-form>\n\n")
186 }
187 b.WriteString("**Add a note.** Use it to tell readers about anything that changed, such as a test that ended early.\n\n")
188 b.WriteString("<gno-form exec=\"AddNote\">\n")
189 b.WriteString("<gno-input name=\"id\" type=\"number\" value=\"" + id + "\" readonly=\"true\" description=\"Experiment number\" />\n")
190 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")
191 b.WriteString("</gno-form>\n")
192 return b.String()
193}
194
195// statusLabel is the one-word state shown in lists and headers.
196func statusLabel(exp *Experiment) string {
197 if !exp.closed {
198 if len(exp.reports) == 0 {
199 return "Locked"
200 }
201 return "Running"
202 }
203 return outcomeLabel(exp.verdict.Outcome)
204}
205
206func outcomeLabel(o core.Outcome) string {
207 switch o {
208 case core.Met:
209 return "Target met"
210 case core.Missed:
211 return "Target missed"
212 }
213 return "Inconclusive"
214}
215
216// targetLabel renders the target with the sign of the expected direction.
217func targetLabel(p core.Plan) string {
218 if p.Direction == core.Decrease {
219 return core.FormatBps(-p.TargetBps)
220 }
221 return core.FormatBps(p.TargetBps)
222}
223
224// tallyLabel renders "268 of 1,530 (17.51%)".
225func tallyLabel(t core.Tally) string {
226 return core.Commas(t.Conversions) + " of " + core.Commas(t.Exposures) + " (" + t.RateText() + ")"
227}
228
229// span renders how long passed between two block times, in plain words.
230func span(d time.Duration) string {
231 minutes := int64(d / time.Minute)
232 switch {
233 case minutes < 1:
234 return "less than a minute"
235 case minutes < 120:
236 return core.Commas(minutes) + " minutes"
237 case minutes < 48*60:
238 return core.Commas(minutes/60) + " hours"
239 }
240 return core.Commas(minutes/(24*60)) + " days"
241}
242
243// stamp renders a block time in UTC.
244func stamp(t time.Time) string {
245 return t.UTC().Format("02 Jan 2006 15:04") + " UTC"
246}