code wiki / _hdl_build / nx_xlsx_chart_gate.nx
nx_xlsx_chart_gate.nx source
↩ module page · 146 lines · 7892 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"
8
9func cg_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8) { n=n+1 } return n }
10func cg_p(s: *u8) -> i64 { sys_write(1, s, cg_slen(s)); return 0 }
11func 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 }
12func cg_catn(dst: *u8, off: i64, v: i64) -> i64 {
13 var o: i64=off; var m: i64=v; if m<0 { dst[o]=45 as u8; o=o+1; m=0-m }
14 let t: *u8=sys_mmap(28); var k: i64=0; if m==0 { t[0]=48 as u8; k=1 }
15 while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 }
16 var i: i64=0; while i<k { dst[o+i]=t[k-1-i]; i=i+1 } return o+k
17}
18func cg_write(path: *u8, content: *u8) -> i64 {
19 let fd: i64=sys_openat_wr(path, 0x1a4); if fd<0 { return 0-1 }
20 sys_write(fd, content, cg_slen(content)); sys_close(fd); return 0
21}
22func cg_readall(path: *u8, szout: *i64) -> *u8 {
23 let fd: i64=sys_openat_rd(path); if fd<0 { szout[0]=0-1; return 0 as *u8 }
24 let buf: *u8=sys_mmap(4194304); var got: i64=0; var n: i64=1
25 while n>0 { n=sys_read(fd,(buf as i64+got) as *u8,131072); if n>0 { got=got+n } }
26 sys_close(fd); szout[0]=got; return buf
27}
28func cg_runv(elf: *u8, args: *i64, outpath: *u8) -> i64 {
29 let pid: i64=sys_fork()
30 if pid==0 {
31 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) } }
32 let argv: *i64=sys_mmap(128) as *i64
33 argv[0]=elf as i64; var i: i64=0; var go: i64=1
34 while go==1 { if args[i]==0 { go=0 } else { argv[i+1]=args[i]; i=i+1 } }
35 argv[i+1]=0
36 let envp: *i64=sys_mmap(16) as *i64; envp[0]=0
37 sys_execve(elf, argv, envp); sys_exit(127)
38 }
39 let st: *i64=sys_mmap(16) as *i64; sys_wait4(pid, st, 0)
40 let sig: i64=st[0]&0x7f; if sig!=0 { return 128+sig }
41 return (st[0]>>8)&0xff
42}
43func cg_has(path: *u8, needle: *u8) -> i64 {
44 let szp: *i64=sys_mmap(16) as *i64; let b: *u8=cg_readall(path, szp); let sz: i64=szp[0]
45 let n: i64=cg_slen(needle); if sz<n { return 0 }
46 var i: i64=0
47 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 }
48 return 0
49}
50func cg_fileeq(p1: *u8, p2: *u8) -> i64 {
51 let s1: *i64=sys_mmap(16) as *i64
52 let s2: *i64=sys_mmap(16) as *i64
53 let b1: *u8=cg_readall(p1, s1)
54 let b2: *u8=cg_readall(p2, s2)
55 if s1[0] != s2[0] { return 0 }
56 if s1[0]<=0 { return 0 }
57 var i: i64=0
58 while i<s1[0] { if b1[i] != b2[i] { return 0 } i=i+1 }
59 return 1
60}
61func cg_args4(a1: *u8, a2: *u8, a3: *u8, a4: *u8) -> *i64 {
62 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
63}
64func cg_row(name: *u8, pass: i64) -> i64 {
65 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
66}
67
68func main(argc: i64, argv: *i64) -> i64 {
69 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)
70 let xl: *u8="/tmp/nx_xlsx.sov.elf" as *u8
71 let pr: i64=sys_openat_rd(xl)
72 if pr>=0 { sys_close(pr) } else {
73 cg_p(" instrument missing -> rebuilding\n" as *u8)
74 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)
75 }
76
77 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)
78 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)
79
80 var pass: i64=0
81 var r: i64=0
82
83 // row 1: chart present with PINNED integer-percent widths (350/1030.25=33%, 480.25=46%, 200=19%)
84 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)
85 r=0
86 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 {
87 if cg_has("/tmp/cg_v.html" as *u8, "style=\"width:46%\">480.25</div>" as *u8)==1 {
88 if cg_has("/tmp/cg_v.html" as *u8, "style=\"width:19%\">200</div>" as *u8)==1 { r=1 }
89 }
90 } }
91 pass=pass+cg_row("chart-pinned-widths" as *u8, r)
92
93 // row 2: the =SUM total charts at its COMPUTED value as the 100% bar
94 r=cg_has("/tmp/cg_v.html" as *u8, "style=\"width:100%\">1030.25</div>" as *u8)
95 pass=pass+cg_row("formula-bar-100pct" as *u8, r)
96
97 // row 3: a sheet with NO numeric column gets NO chart (graceful), table still renders
98 cg_write("/tmp/cg_text.tsv" as *u8, "Name\x09Role\nAlice\x09Lead\nBob\x09Eng\n" as *u8)
99 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)
100 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)
101 r=0
102 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 } }
103 pass=pass+cg_row("no-numeric-no-chart" as *u8, r)
104
105 // row 4: registry dispatch output includes the chart
106 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)
107 r=0
108 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 } } }
109 pass=pass+cg_row("dispatch-includes-chart" as *u8, r)
110
111 // row 5: 0-JS
112 r=1
113 if cg_has("/tmp/cg_v.html" as *u8, "<script" as *u8)==1 { r=0 }
114 if cg_has("/tmp/cg_dv.html" as *u8, "<script" as *u8)==1 { r=0 }
115 pass=pass+cg_row("outputs-0js" as *u8, r)
116
117 // row 6: determinism
118 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)
119 r=cg_fileeq("/tmp/cg_v.html" as *u8, "/tmp/cg_v3.html" as *u8)
120 pass=pass+cg_row("determinism" as *u8, r)
121
122 // row 7 (neg): missing workbook loud-fails
123 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)
124 r=0
125 if rc7==1 { if cg_has("/tmp/cg_r7.txt" as *u8, "XLSX-HTML-FAIL" as *u8)==1 { r=1 } }
126 pass=pass+cg_row("loud-fail-missing" as *u8, r)
127
128 let permil: i64=(pass*1000)/7
129 let logfd: i64=sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4)
130 var fdi: i64=0
131 while fdi<2 {
132 var fd: i64=1; if fdi==1 { fd=logfd }
133 if fd>0 {
134 let line: *u8=sys_mmap(256); var o: i64=0
135 o=cg_cat(line,o,"XLSX-CHART-GATE epoch=" as *u8); o=cg_catn(line,o,sys_now_realtime_sec())
136 o=cg_cat(line,o," rows=7 pass=" as *u8); o=cg_catn(line,o,pass)
137 o=cg_cat(line,o," permil=" as *u8); o=cg_catn(line,o,permil)
138 if pass==7 { o=cg_cat(line,o," verdict=GREEN\n" as *u8) } else { o=cg_cat(line,o," verdict=RED\n" as *u8) }
139 sys_write(fd, line, o)
140 }
141 fdi=fdi+1
142 }
143 if logfd>0 { sys_close(logfd) }
144 if pass==7 { return 0 }
145 return 1
146}