code wiki / _hdl_build / nx_figures_page.nx

nx_figures_page.nx source

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