code wiki / _hdl_build / nx_xlsx_chart_gate.nx
nx_xlsx_chart_gate.nx source
↩ module page · 154 lines · 8442 B
1// nx_xlsx_chart_gate.nx -- gate for CHARTS v1 (office census OF-S4, native-first): the .xlsx preview renders
2// a 0-JS BAR CHART from sheet data -- labels col A, values = first >=60%-numeric column, bar widths = integer
3// percents of the max, and FORMULA cells chart at their COMPUTED values (the =SUM total is the 100% bar).
4// In-file DrawingML c:chart = named follow-up, NOT claimed. Rows: 1 chart+pinned-widths 2 formula-bar-100pct
5// 3 no-numeric-sheet-no-chart 4 registry-dispatch-includes-chart 5 outputs-0js 6 determinism 7 loud-fail[neg]
6// license_tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_gate_verdict.nx"
9
10func cg_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8) { n=n+1 } return n }
11func cg_p(s: *u8) -> i64 { sys_write(1, s, cg_slen(s)); return 0 }
12func cg_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8) { dst[off+i]=s[i]; i=i+1 } return off+i }
13func cg_catn(dst: *u8, off: i64, v: i64) -> i64 {
14 var o: i64=off; var m: i64=v; if m<0 { dst[o]=45 as u8; o=o+1; m=0-m }
15 let t: *u8=sys_mmap(28); var k: i64=0; if m==0 { t[0]=48 as u8; k=1 }
16 while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 }
17 var i: i64=0; while i<k { dst[o+i]=t[k-1-i]; i=i+1 } return o+k
18}
19func cg_write(path: *u8, content: *u8) -> i64 {
20 let fd: i64=sys_openat_wr(path, 0x1a4); if fd<0 { return 0-1 }
21 sys_write(fd, content, cg_slen(content)); sys_close(fd); return 0
22}
23func cg_readall(path: *u8, szout: *i64) -> *u8 {
24 let fd: i64=sys_openat_rd(path); if fd<0 { szout[0]=0-1; return 0 as *u8 }
25 let buf: *u8=sys_mmap(4194304); var got: i64=0; var n: i64=1
26 while n>0 { n=sys_read(fd,(buf as i64+got) as *u8,131072); if n>0 { got=got+n } }
27 sys_close(fd); szout[0]=got; return buf
28}
29func cg_runv(elf: *u8, args: *i64, outpath: *u8) -> i64 {
30 let pid: i64=sys_fork()
31 if pid==0 {
32 if (outpath as i64)!=0 { let ofd: i64=sys_openat_wr(outpath,0x1a4); if ofd>=0 { sys_dup3(ofd,1,0); sys_dup3(ofd,2,0) } }
33 let argv: *i64=sys_mmap(128) as *i64
34 argv[0]=elf as i64; var i: i64=0; var go: i64=1
35 while go==1 { if args[i]==0 { go=0 } else { argv[i+1]=args[i]; i=i+1 } }
36 argv[i+1]=0
37 let envp: *i64=sys_mmap(16) as *i64; envp[0]=0
38 sys_execve(elf, argv, envp); sys_exit(127)
39 }
40 let st: *i64=sys_mmap(16) as *i64; sys_wait4(pid, st, 0)
41 let sig: i64=st[0]&0x7f; if sig!=0 { return 128+sig }
42 return (st[0]>>8)&0xff
43}
44func cg_has(path: *u8, needle: *u8) -> i64 {
45 let szp: *i64=sys_mmap(16) as *i64; let b: *u8=cg_readall(path, szp); let sz: i64=szp[0]
46 let n: i64=cg_slen(needle); if sz<n { return 0 }
47 var i: i64=0
48 while i+n<=sz { var ok: i64=1; var j: i64=0; while j<n { if b[i+j]!=needle[j] { ok=0; j=n } else { j=j+1 } } if ok==1 { return 1 } i=i+1 }
49 return 0
50}
51func cg_fileeq(p1: *u8, p2: *u8) -> i64 {
52 let s1: *i64=sys_mmap(16) as *i64
53 let s2: *i64=sys_mmap(16) as *i64
54 let b1: *u8=cg_readall(p1, s1)
55 let b2: *u8=cg_readall(p2, s2)
56 if s1[0] != s2[0] { return 0 }
57 if s1[0]<=0 { return 0 }
58 var i: i64=0
59 while i<s1[0] { if b1[i] != b2[i] { return 0 } i=i+1 }
60 return 1
61}
62func cg_args4(a1: *u8, a2: *u8, a3: *u8, a4: *u8) -> *i64 {
63 let a: *i64=sys_mmap(64) as *i64; a[0]=a1 as i64; a[1]=a2 as i64; a[2]=a3 as i64; a[3]=a4 as i64; a[4]=0; return a
64}
65func cg_row(name: *u8, pass: i64) -> i64 {
66 cg_p("ROW " as *u8); cg_p(name); if pass==1 { cg_p(" PASS\n" as *u8) } else { cg_p(" FAIL\n" as *u8) } return pass
67}
68
69func main(argc: i64, argv: *i64) -> i64 {
70 cg_p("=== XLSX CHART GATE (OF-S4 native-first): 0-JS bar chart from sheet data; formula totals chart at computed values ===\n" as *u8)
71 let xl: *u8="buildroot/_build/nx_xlsx.sov.elf" as *u8
72 let pr: i64=sys_openat_rd(xl)
73 if pr>=0 { sys_close(pr) } else {
74 cg_p(" instrument missing -> rebuilding\n" as *u8)
75 cg_runv("_offc/nx_sov_build_run.elf" as *u8, cg_args4("nx_xlsx" as *u8, "--build-only" as *u8, 0 as *u8, 0 as *u8), "/tmp/cg_rebuild.out" as *u8)
76 }
77
78 cg_write("/tmp/cg_budget.tsv" as *u8, "Item\x09Cost\nVenue\x09350\nCatering\x09480.25\nMusic\x09200\nTotal\x09=SUM(B2:B4)\n" as *u8)
79 cg_runv(xl, cg_args4("fromtsv" as *u8, "/tmp/cg_budget.tsv" as *u8, "/tmp/cg_budget.xlsx" as *u8, 0 as *u8), "/tmp/cg_w.txt" as *u8)
80
81 var pass: i64=0
82 var r: i64=0
83
84 // row 1: chart present with PINNED integer-percent widths (350/1030.25=33%, 480.25=46%, 200=19%)
85 let rc1: i64=cg_runv(xl, cg_args4("html" as *u8, "/tmp/cg_budget.xlsx" as *u8, "/tmp/cg_v.html" as *u8, 0 as *u8), "/tmp/cg_r1.txt" as *u8)
86 r=0
87 if rc1==0 { if cg_has("/tmp/cg_v.html" as *u8, "<span class=bl>Venue</span><div class=bar style=\"width:33%\">350</div>" as *u8)==1 {
88 if cg_has("/tmp/cg_v.html" as *u8, "style=\"width:46%\">480.25</div>" as *u8)==1 {
89 if cg_has("/tmp/cg_v.html" as *u8, "style=\"width:19%\">200</div>" as *u8)==1 { r=1 }
90 }
91 } }
92 pass=pass+cg_row("chart-pinned-widths" as *u8, r)
93
94 // row 2: the =SUM total charts at its COMPUTED value as the 100% bar
95 r=cg_has("/tmp/cg_v.html" as *u8, "style=\"width:100%\">1030.25</div>" as *u8)
96 pass=pass+cg_row("formula-bar-100pct" as *u8, r)
97
98 // row 3: a sheet with NO numeric column gets NO chart (graceful), table still renders
99 cg_write("/tmp/cg_text.tsv" as *u8, "Name\x09Role\nAlice\x09Lead\nBob\x09Eng\n" as *u8)
100 cg_runv(xl, cg_args4("fromtsv" as *u8, "/tmp/cg_text.tsv" as *u8, "/tmp/cg_text.xlsx" as *u8, 0 as *u8), "/tmp/cg_w2.txt" as *u8)
101 cg_runv(xl, cg_args4("html" as *u8, "/tmp/cg_text.xlsx" as *u8, "/tmp/cg_v2.html" as *u8, 0 as *u8), "/tmp/cg_r3.txt" as *u8)
102 r=0
103 if cg_has("/tmp/cg_v2.html" as *u8, "class=ct" as *u8)==0 { if cg_has("/tmp/cg_v2.html" as *u8, "<th>Name</th>" as *u8)==1 { r=1 } }
104 pass=pass+cg_row("no-numeric-no-chart" as *u8, r)
105
106 // row 4: registry dispatch output includes the chart
107 let rc4: i64=cg_runv("_offc/nx_doc_view.elf" as *u8, cg_args4("/tmp/cg_budget.xlsx" as *u8, "/tmp/cg_dv.html" as *u8, "/sample.xlsx" as *u8, 0 as *u8), "/tmp/cg_r4.txt" as *u8)
108 r=0
109 if rc4==0 { if cg_has("/tmp/cg_r4.txt" as *u8, "DOC-VIEW-OK ext=xlsx" as *u8)==1 { if cg_has("/tmp/cg_dv.html" as *u8, "class=ct" as *u8)==1 { r=1 } } }
110 pass=pass+cg_row("dispatch-includes-chart" as *u8, r)
111
112 // row 5: 0-JS
113 r=1
114 if cg_has("/tmp/cg_v.html" as *u8, "<script" as *u8)==1 { r=0 }
115 if cg_has("/tmp/cg_dv.html" as *u8, "<script" as *u8)==1 { r=0 }
116 pass=pass+cg_row("outputs-0js" as *u8, r)
117
118 // row 6: determinism
119 cg_runv(xl, cg_args4("html" as *u8, "/tmp/cg_budget.xlsx" as *u8, "/tmp/cg_v3.html" as *u8, 0 as *u8), "/tmp/cg_r6.txt" as *u8)
120 r=cg_fileeq("/tmp/cg_v.html" as *u8, "/tmp/cg_v3.html" as *u8)
121 pass=pass+cg_row("determinism" as *u8, r)
122
123 // row 7 (neg): missing workbook loud-fails
124 let rc7: i64=cg_runv(xl, cg_args4("html" as *u8, "/tmp/cg_nope.xlsx" as *u8, "/tmp/cg_x.html" as *u8, 0 as *u8), "/tmp/cg_r7.txt" as *u8)
125 r=0
126 if rc7==1 { if cg_has("/tmp/cg_r7.txt" as *u8, "XLSX-HTML-FAIL" as *u8)==1 { r=1 } }
127 pass=pass+cg_row("loud-fail-missing" as *u8, r)
128
129 let permil: i64=(pass*1000)/7
130 let logfd: i64=sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4)
131 var fdi: i64=0
132 while fdi<2 {
133 var fd: i64=1; if fdi==1 { fd=logfd }
134 if fd>0 {
135 let line: *u8=sys_mmap(256); var o: i64=0
136 o=cg_cat(line,o,"XLSX-CHART-GATE epoch=" as *u8); o=cg_catn(line,o,sys_now_realtime_sec())
137 o=cg_cat(line,o," rows=7 pass=" as *u8); o=cg_catn(line,o,pass)
138 o=cg_cat(line,o," permil=" as *u8); o=cg_catn(line,o,permil)
139 if pass==7 { o=cg_cat(line,o," verdict=GREEN\n" as *u8) } else { o=cg_cat(line,o," verdict=RED\n" as *u8) }
140 sys_write(fd, line, o)
141 }
142 fdi=fdi+1
143 }
144 if logfd>0 { sys_close(logfd) }
145 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
146 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
147 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
148 let ctr__dry: *i64 = gv_ctr()
149 ctr__dry[0] = pass
150 ctr__dry[1] = 7
151 let rc__dry: i64 = gv_verdict("XLSX-CHART-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
152 sys_exit(rc__dry)
153 return rc__dry
154}