code wiki / _hdl_build / nx_figures_page.nx
nx_figures_page.nx source
↩ module page · 150 lines · 9752 B
1// nx_figures_page.nx -- IMS Thrust D R2 LIVE page: emit /wiki/figures.html with a NATIVE SVG bar chart
2// inline + working DOWNLOAD links to the SAME dataset in CSV / JSON / TSV. EVERY artifact is computed at
3// render time by the sovereign organs (no hand-typed SVG, no hand-typed data files):
4// - chart_svg (nx_chart) -> the inline <svg> bar chart.
5// - dx_emit_csv/json/tsv (nx_dataset_export) -> the 3 downloadable data files (written to web_assets/).
6// - ims_page (nx_ims_page) -> the walkable typed wiki page (head/nav/walk-furniture/footer).
7//
8// REAL DATASET (not invented): the live wiki's own page-count growth across the IMS rungs (8 -> 12 -> 13
9// -> 20 -> 21 -> 22 with this page). The chart visualises it; the 3 files let a consumer download it.
10//
11// Output: web_assets/figures.html + web_assets/figures_data.{csv,json,tsv}. Publishing (vpub for the page,
12// nx_aw_push for the 3 data files) is the SEPARATE step nx_figures_publish. license_tier: ORIGINAL
13import "nx_syscalls.nx"
14import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
15import "nx_chart.nx"
16import "nx_dataset_export.nx"
17import "nx_ims_page.nx"
18const FP_MAGIC_16384: i64 = 16384
19
20const FP_BODYCAP: i64 = 65536
21const FP_MODE_0644: i64 = 0x1a4
22
23func fp_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64 = off; var i: i64 = 0; while s[i] != 0 as u8 { dst[o] = s[i]; o = o + 1; i = i + 1 } return o }
24func fp_catn(dst: *u8, off: i64, v: i64) -> i64 {
25 var o: i64 = off; var n: i64 = v
26 if n < 0 { dst[o] = 45 as u8; o = o + 1; n = 0 - n }
27 let t: *u8 = sys_mmap(32); var k: i64 = 0
28 if n == 0 { t[0] = 48 as u8; k = 1 }
29 while n > 0 { t[k] = (48 + (n % 10)) as u8; n = n / 10; k = k + 1 }
30 var j: i64 = k - 1; while j >= 0 { dst[o] = t[j]; o = o + 1; j = j - 1 }
31 return o
32}
33func fp_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
34// small stdout decimal (kept separate so the body assembler's fp_catn stays buffer-only).
35// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
36// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
37// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
38// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
39func fp_catn_stdout(v: i64) -> i64 { nxi_out(v); return 0 }
40
41// write a NUL-terminated-known-length buffer to web_assets/<name>.
42func fp_write(name: *u8, buf: *u8, len: i64) -> i64 {
43 let path: *u8 = sys_mmap(256)
44 var po: i64 = 0
45 po = fp_cat(path, po, "web_assets/" as *u8)
46 po = fp_cat(path, po, name)
47 path[po] = 0 as u8
48 let fd: i64 = sys_openat_wr(path, FP_MODE_0644)
49 if fd < 0 { return 0 - 1 }
50 sys_write(fd, buf, len)
51 sys_close(fd)
52 return 0
53}
54
55func main() -> i64 {
56 // ---------- build the real dataset ----------
57 let ds: *NxDataset = sys_mmap(128) as *NxDataset
58 dx_alloc(ds, 2, 5)
59 dx_set_hdr(ds, 0, "rung" as *u8)
60 dx_set_hdr(ds, 1, "pages" as *u8)
61 dx_set_cell_z(ds, 0, 0, "thrustA" as *u8); dx_set_cell_z(ds, 0, 1, "8" as *u8)
62 dx_set_cell_z(ds, 1, 0, "thrustB" as *u8); dx_set_cell_z(ds, 1, 1, "12" as *u8)
63 dx_set_cell_z(ds, 2, 0, "thrustC" as *u8); dx_set_cell_z(ds, 2, 1, "13" as *u8)
64 dx_set_cell_z(ds, 3, 0, "substrate" as *u8); dx_set_cell_z(ds, 3, 1, "20" as *u8)
65 dx_set_cell_z(ds, 4, 0, "figures" as *u8); dx_set_cell_z(ds, 4, 1, "22" as *u8)
66
67 // ---------- write the 3 download files (computed, not hand-typed) ----------
68 let cbuf: *u8 = sys_mmap(FP_MAGIC_16384); let cn: i64 = dx_emit_csv(ds, cbuf, FP_MAGIC_16384)
69 let jbuf: *u8 = sys_mmap(FP_MAGIC_16384); let jn: i64 = dx_emit_json(ds, jbuf, FP_MAGIC_16384)
70 let tbuf: *u8 = sys_mmap(FP_MAGIC_16384); let tn: i64 = dx_emit_tsv(ds, tbuf, FP_MAGIC_16384)
71 if cn <= 0 { fp_w("figures: CSV emit failed\n"); sys_exit(2); return 2 }
72 if jn <= 0 { fp_w("figures: JSON emit failed\n"); sys_exit(2); return 2 }
73 if tn <= 0 { fp_w("figures: TSV emit failed\n"); sys_exit(2); return 2 }
74 if fp_write("figures_data.csv" as *u8, cbuf, cn) != 0 { fp_w("figures: write csv failed\n"); sys_exit(3); return 3 }
75 if fp_write("figures_data.json" as *u8, jbuf, jn) != 0 { fp_w("figures: write json failed\n"); sys_exit(3); return 3 }
76 if fp_write("figures_data.tsv" as *u8, tbuf, tn) != 0 { fp_w("figures: write tsv failed\n"); sys_exit(3); return 3 }
77
78 // ---------- compute the inline SVG chart over the same values ----------
79 let N: i64 = 5
80 let lp: *i64 = sys_mmap(64) as *i64
81 let ll: *i64 = sys_mmap(64) as *i64
82 let vv: *i64 = sys_mmap(64) as *i64
83 var ci: i64 = 0
84 while ci < N {
85 lp[ci] = dx_cell_ptr(ds, ci, 0) as i64
86 ll[ci] = dx_cell_len(ds, ci, 0)
87 let pp: *u8 = dx_cell_ptr(ds, ci, 1); let pnn: i64 = dx_cell_len(ds, ci, 1)
88 var v: i64 = 0; var z: i64 = 0
89 while z < pnn { v = v * 10 + ((pp[z] as i64) - 48); z = z + 1 }
90 vv[ci] = v
91 ci = ci + 1
92 }
93 let svg: *u8 = sys_mmap(FP_MAGIC_16384); let svgu: *i64 = sys_mmap(16) as *i64
94 let crc: i64 = chart_svg(lp, ll, vv, N, "Nishi wiki page-count growth across IMS rungs" as *u8, svg, FP_MAGIC_16384, svgu)
95 if crc != CH_OK { fp_w("figures: chart_svg failed\n"); sys_exit(4); return 4 }
96 let svgn: i64 = svgu[0]
97
98 // ---------- assemble body_html: chart + download links + table ----------
99 let body: *u8 = sys_mmap(FP_BODYCAP)
100 var w: i64 = 0
101 w = fp_cat(body, w, "<p>This page is the live read-out of <strong>Thrust D</strong> of the Nishi IMS arc — the “image → native graphs → dataset in many formats → download” half. The bar chart below is a <strong>native SVG</strong> emitted by the sovereign organ <code>nx_chart</code> (composing the bits-up viz primitives <code>nx_viz_scale</code>/<code>nx_viz_axis</code>); there is no <code><script></code>, no d3.js and no CDN. The dataset is the wiki’s own page-count growth across the build rungs, offered for download in three byte-faithful formats below.</p>\n" as *u8)
102
103 // the inline chart, in a figure
104 w = fp_cat(body, w, "<figure style=\"margin:1.2rem 0;overflow-x:auto\">\n" as *u8)
105 var si: i64 = 0
106 while si < svgn { body[w] = svg[si]; w = w + 1; si = si + 1 }
107 w = fp_cat(body, w, "\n<figcaption style=\"color:#555;font-size:.9rem;margin-top:.4rem\">Pages published to the live wiki after each thrust (computed at render time by <code>nx_chart</code>).</figcaption>\n</figure>\n" as *u8)
108
109 // download links -- the three formats published to the doc-root alongside this page.
110 w = fp_cat(body, w, "<h2>Download the dataset</h2>\n" as *u8)
111 w = fp_cat(body, w, "<p>The same records, emitted by <code>nx_dataset_export</code> and proven byte-faithful (CSV↔JSON↔TSV all parse back to identical rows by the <code>nx_figure_gate</code> referee, 10/10):</p>\n" as *u8)
112 w = fp_cat(body, w, "<ul>\n" as *u8)
113 w = fp_cat(body, w, "<li><a href=\"/wiki/figures_data.csv\" download>figures_data.csv</a> — RFC 4180 (" as *u8); w = fp_catn(body, w, cn); w = fp_cat(body, w, " bytes)</li>\n" as *u8)
114 w = fp_cat(body, w, "<li><a href=\"/wiki/figures_data.json\" download>figures_data.json</a> — RFC 8259 array-of-objects (" as *u8); w = fp_catn(body, w, jn); w = fp_cat(body, w, " bytes)</li>\n" as *u8)
115 w = fp_cat(body, w, "<li><a href=\"/wiki/figures_data.tsv\" download>figures_data.tsv</a> — tab-separated (" as *u8); w = fp_catn(body, w, tn); w = fp_cat(body, w, " bytes)</li>\n" as *u8)
116 w = fp_cat(body, w, "</ul>\n" as *u8)
117
118 // a plain table mirroring the data (so the page is readable even without downloading).
119 w = fp_cat(body, w, "<h2>The data</h2>\n<table>\n<thead><tr><th>rung</th><th>pages</th></tr></thead>\n<tbody>\n" as *u8)
120 var r: i64 = 0
121 while r < 5 {
122 w = fp_cat(body, w, "<tr><td>" as *u8)
123 let rp: *u8 = dx_cell_ptr(ds, r, 0); let rn: i64 = dx_cell_len(ds, r, 0)
124 var a: i64 = 0; while a < rn { body[w] = rp[a]; w = w + 1; a = a + 1 }
125 w = fp_cat(body, w, "</td><td>" as *u8)
126 let vp: *u8 = dx_cell_ptr(ds, r, 1); let vn: i64 = dx_cell_len(ds, r, 1)
127 var b2: i64 = 0; while b2 < vn { body[w] = vp[b2]; w = w + 1; b2 = b2 + 1 }
128 w = fp_cat(body, w, "</td></tr>\n" as *u8)
129 r = r + 1
130 }
131 w = fp_cat(body, w, "</tbody>\n</table>\n" as *u8)
132 w = fp_cat(body, w, "<p style=\"color:#555;font-size:.9rem\">Built sovereign on the Nishi stack: <code>nx_chart</code> (SVG) + <code>nx_dataset_export</code> (CSV/JSON/TSV) + <code>nx_ims_page</code> (this page) + <code>nx_aw_push</code> (publish). Gated by <code>nx_figure_gate</code>.</p>\n" as *u8)
133 body[w] = 0 as u8
134
135 // ---------- render the walkable page ----------
136 let pw: i64 = ims_page("figures" as *u8, "reference" as *u8,
137 "Figures — native SVG charts + multi-format dataset export" as *u8, body)
138 if pw < 0 { fp_w("figures: ims_page failed rc="); fp_catn(body, 0, pw); sys_exit(5); return 5 }
139
140 fp_w("[nx_figures_page] wrote web_assets/figures.html + figures_data.{csv,json,tsv} | page_bytes=")
141 let nb: *u8 = sys_mmap(28); var m: i64 = pw; let t: *u8 = sys_mmap(28); var k: i64 = 0
142 if m == 0 { t[0] = 48 as u8; k = 1 }
143 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
144 var i: i64 = 0; while i < k { nb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, nb, k)
145 fp_w(" svg_bytes="); fp_catn_stdout(svgn)
146 fp_w(" csv="); fp_catn_stdout(cn); fp_w(" json="); fp_catn_stdout(jn); fp_w(" tsv="); fp_catn_stdout(tn)
147 fp_w("\n")
148 sys_exit(0)
149 return 0
150}