package dokimi import ( "strconv" "strings" "time" core "gno.land/p/g1v9mz7rtgkvwkw90ey6496h5fk6pj6cgyawkvft/dokimi/v1" "gno.land/p/nt/markdown/sanitize/v0" ) const ( // listLimit is how many experiments the home page shows, newest first. listLimit = 30 // webPath is this realm's address on gnoweb, used to build links. It is // the module path in gnomod.toml without the chain domain. webPath = "/r/g1v9mz7rtgkvwkw90ey6496h5fk6pj6cgyawkvft/dokimi/v1" // rulesPath is the import path of the pure package holding the rules. rulesPath = "gno.land/p/g1v9mz7rtgkvwkw90ey6496h5fk6pj6cgyawkvft/dokimi/v1" ) // Render draws the registry for gnoweb. // // "" the home page: the list and the form to lock a new plan // "" one experiment: its plan, reports, notes and verdict func Render(path string) string { if i := strings.IndexByte(path, '?'); i >= 0 { path = path[:i] } path = strings.Trim(path, "/") if path == "" { return renderHome() } id, err := strconv.ParseUint(path, 10, 64) if err != nil { return "# Not found\n\nThere is no page called " + sanitize.InlineCode(path) + ". [Back to all experiments](" + webPath + ")\n" } exp, ok := experiments.Get(key(id)).(*Experiment) if !ok { return "# Not found\n\nExperiment " + strconv.FormatUint(id, 10) + " does not exist. [Back to all experiments](" + webPath + ")\n" } return renderExperiment(exp) } func renderHome() string { var b strings.Builder b.WriteString("# Dokimi\n\n") b.WriteString("**Say what would count as a win before the test starts. Then prove you did not move the bar.**\n\n") b.WriteString("Dokimi is a public record for experiments such as A/B tests. ") b.WriteString("The plan is locked on-chain first. Results are added later and judged only against that plan.\n\n") b.WriteString("## Experiments on record\n\n") if experiments.Size() == 0 { b.WriteString("None yet. Lock the first plan with the form below.\n\n") } else { b.WriteString("| # | Experiment | Target | Sample per arm | Status |\n") b.WriteString("|---|---|---|---|---|\n") experiments.ReverseIterateByOffset(0, listLimit, func(_ string, value any) bool { exp := value.(*Experiment) id := strconv.FormatUint(exp.id, 10) b.WriteString("| " + id) b.WriteString(" | [" + sanitize.TableCell(exp.plan.Title) + "](" + webPath + ":" + id + ")") b.WriteString(" | " + targetLabel(exp.plan)) b.WriteString(" | " + core.Commas(exp.plan.MinSample)) b.WriteString(" | " + statusLabel(exp) + " |\n") return false }) b.WriteString("\n") if experiments.Size() > listLimit { b.WriteString("Showing the newest " + strconv.Itoa(listLimit) + " of " + strconv.Itoa(experiments.Size()) + ". Open any experiment by its number: `" + webPath + ":`.\n\n") } } b.WriteString("## Lock a new plan\n\n") 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") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n\n") b.WriteString("## How it works\n\n") b.WriteString("1. **Lock.** The plan is stored with the block it was written in and a fingerprint of its text.\n") 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") b.WriteString("3. **Close.** The last report is judged against the locked plan: **met**, **missed** or **inconclusive**. ") b.WriteString("A test stopped before its planned sample is always inconclusive, however good the numbers look.\n\n") b.WriteString("Other realms can register their own experiments here, or import the rules from `" + rulesPath + "`.\n") return b.String() } func renderExperiment(exp *Experiment) string { var b strings.Builder id := strconv.FormatUint(exp.id, 10) p := exp.plan b.WriteString("# " + sanitize.InlineText(p.Title) + "\n\n") b.WriteString("[All experiments](" + webPath + ") · Experiment " + id + " · **" + statusLabel(exp) + "**\n\n") b.WriteString("> " + sanitize.InlineText(p.Hypothesis) + "\n\n") b.WriteString("## The locked plan\n\n") b.WriteString("| | |\n|---|---|\n") b.WriteString("| Metric | " + sanitize.TableCell(p.Metric) + " |\n") b.WriteString("| Control | " + sanitize.TableCell(p.Control) + " |\n") b.WriteString("| Variant | " + sanitize.TableCell(p.Variant) + " |\n") b.WriteString("| Target | " + targetLabel(p) + " relative change in the metric |\n") b.WriteString("| Minimum sample | " + core.Commas(p.MinSample) + " exposures for each arm |\n") b.WriteString("| Locked | block " + core.Commas(exp.lockedHeight) + ", " + stamp(exp.lockedAt) + " |\n") b.WriteString("| Owner | " + exp.owner.String() + " |\n") b.WriteString("| Fingerprint | `" + exp.digest + "` |\n\n") n := len(exp.reports) if exp.closed { b.WriteString("## Verdict: " + outcomeLabel(exp.verdict.Outcome) + "\n\n") b.WriteString(sanitize.InlineText(exp.verdict.Reason) + "\n\n") b.WriteString("Closed at block " + core.Commas(exp.closedHeight) + ", " + stamp(exp.closedAt) + ".\n\n") } else if n == 0 { b.WriteString("## No results yet\n\n") b.WriteString("The plan is locked and waiting for its first report.\n\n") } else { last := exp.reports[n-1] preview := core.Judge(p, last.Control, last.Variant) b.WriteString("## Still running\n\n") b.WriteString("If it were closed on the latest report, the verdict would be **" + outcomeLabel(preview.Outcome) + "**. ") b.WriteString(sanitize.InlineText(preview.Reason) + "\n\n") } if n > 0 { b.WriteString("## Reports\n\n") b.WriteString("Running totals as the owner reported them. Each row is permanent. ") if c := corrections(exp); c > 0 { b.WriteString("**" + strconv.Itoa(c) + " of " + strconv.Itoa(n) + " rows are corrections.** ") } first := exp.reports[0] 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") b.WriteString("| # | Block | " + sanitize.TableCell(p.Control) + " | " + sanitize.TableCell(p.Variant) + " | Lift | Source |\n") b.WriteString("|---|---|---|---|---|---|\n") for i, r := range exp.reports { lift := "n/a" if l, ok := core.LiftBps(r.Control, r.Variant); ok { lift = core.FormatBps(l) } source := sanitize.TableCell(r.Source) if r.Correction { source = "**Correction.** " + sanitize.TableCell(r.Reason) } else if source == "" { source = "not given" } b.WriteString("| " + strconv.Itoa(i+1)) b.WriteString(" | " + core.Commas(r.Height)) b.WriteString(" | " + tallyLabel(r.Control)) b.WriteString(" | " + tallyLabel(r.Variant)) b.WriteString(" | " + lift) b.WriteString(" | " + source + " |\n") } b.WriteString("\n") } if len(exp.notes) > 0 { b.WriteString("## Notes from the owner\n\n") for _, note := range exp.notes { b.WriteString("- Block " + core.Commas(note.Height) + ", " + stamp(note.Time) + ": " + sanitize.InlineText(note.Text) + "\n") } b.WriteString("\n") } b.WriteString("## For the owner\n\n") b.WriteString("These only work from the account that locked the plan.\n\n") if !exp.closed { b.WriteString("**Add a report.** Enter totals since the start of the test, not the change since your last report.\n\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n\n") if n > 0 { 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") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n\n") } b.WriteString("**Close the experiment.** This stores the verdict for good. No reports can be added afterwards.\n\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n\n") } b.WriteString("**Add a note.** Use it to tell readers about anything that changed, such as a test that ended early.\n\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") b.WriteString("\n") return b.String() } // corrections counts the reports that were added as corrections. func corrections(exp *Experiment) int { c := 0 for _, r := range exp.reports { if r.Correction { c++ } } return c } // statusLabel is the one-word state shown in lists and headers. func statusLabel(exp *Experiment) string { if !exp.closed { if len(exp.reports) == 0 { return "Locked" } return "Running" } return outcomeLabel(exp.verdict.Outcome) } func outcomeLabel(o core.Outcome) string { switch o { case core.Met: return "Target met" case core.Missed: return "Target missed" } return "Inconclusive" } // targetLabel renders the target with the sign of the expected direction. func targetLabel(p core.Plan) string { if p.Direction == core.Decrease { return core.FormatBps(-p.TargetBps) } return core.FormatBps(p.TargetBps) } // tallyLabel renders "268 of 1,530 (17.51%)". func tallyLabel(t core.Tally) string { return core.Commas(t.Conversions) + " of " + core.Commas(t.Exposures) + " (" + t.RateText() + ")" } // span renders how long passed between two block times, in plain words. func span(d time.Duration) string { minutes := int64(d / time.Minute) switch { case minutes < 1: return "less than a minute" case minutes < 120: return core.Commas(minutes) + " minutes" case minutes < 48*60: return core.Commas(minutes/60) + " hours" } return core.Commas(minutes/(24*60)) + " days" } // stamp renders a block time in UTC. func stamp(t time.Time) string { return t.UTC().Format("02 Jan 2006 15:04") + " UTC" }