code wiki / _hdl_build / nx_vizsla_report.nx
nx_vizsla_report.nx source
↩ module page · 270 lines · 10416 B
1// nx_vizsla_report.nx -- NISHI VIZSLA: ACTIVITY ANALYTICS / PORTFOLIO REPORTING (PRM/CRM depth e). Operator
2// 2026-06-23: "i want our prm crm ... s class exceeds and im sure we have none of the depth of real system yet."
3// This is the REPORTING dimension a real CRM has: roll the WHOLE typed-interaction log up into actionable
4// analytics -- total volume, breakdown BY TYPE (call/meeting/email/...), breakdown BY CONTACT (most-active
5// relationships), a RECENT-WINDOW count (momentum), and the date SPAN. Reads the "int:" records that
6// nx_vizsla_log writes on the sovereign CID/seg_store plane; read-only; computed LOCALLY (no cloud, no data
7// sale). Integer-only NO-FLOAT, deterministic (no clock in stdout). Discovers contacts/types from the data
8// itself (single responsibility -- does not depend on the roster).
9// activity <prefix> <today> [window_days] -> per-type + per-contact + window + span; verdict = the rollup.
10// MODEL AS DATA: window default 30 days (refine = arg). Type/contact lists sorted by count descending.
11// license_tier: ORIGINAL
12import "nx_syscalls.nx"
13import "nx_seg_store.nx"
14const K_MAGIC_1970: i64 = 1970
15const K_MAGIC_146097: i64 = 146097
16const K_MAGIC_719468: i64 = 719468
17const K_MAGIC_9000000: i64 = 9000000
18const K_MAGIC_131072: i64 = 131072
19
20func vr_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
21func vr_p(s: *u8) -> i64 { sys_write(1, s, vr_slen(s)); return 0 }
22
23func vr_eq(a: *u8, b: *u8) -> i64 {
24 var i: i64 = 0
25 var go: i64 = 1
26 while go == 1 { if a[i] != b[i] { return 0 } if a[i] == (0 as u8) { return 1 } i = i + 1 }
27 return 0
28}
29
30func vr_dup(s: *u8) -> *u8 {
31 let n: i64 = vr_slen(s)
32 let d: *u8 = sys_mmap(n + 2)
33 var i: i64 = 0
34 while i <= n { d[i] = s[i]; i = i + 1 }
35 return d
36}
37
38func vr_atoi(s: *u8) -> i64 {
39 var v: i64 = 0
40 var i: i64 = 0
41 while s[i] != (0 as u8) { let d: i64 = (s[i] as i64) - 48; if d >= 0 { if d <= 9 { v = v * 10 + d } } i = i + 1 }
42 return v
43}
44
45func vr_cat(dst: *u8, off: i64, s: *u8) -> i64 {
46 var i: i64 = 0
47 while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 }
48 return off + i
49}
50
51func vr_catn(dst: *u8, off: i64, v: i64) -> i64 {
52 var o: i64 = off
53 var m: i64 = v
54 if m < 0 { dst[o] = 45 as u8; o = o + 1; m = 0 - m }
55 let t: *u8 = sys_mmap(28)
56 var k: i64 = 0
57 if m == 0 { t[0] = 48 as u8; k = 1 }
58 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
59 var i: i64 = 0
60 while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 }
61 return o + k
62}
63
64func vr_kmemeq(b: *u8, off: i64, n: i64, s: *u8) -> i64 {
65 if vr_slen(s) != n { return 0 }
66 var i: i64 = 0
67 while i < n { if b[off + i] != s[i] { return 0 } i = i + 1 }
68 return 1
69}
70
71func vr_iskind(b: *u8, koff: i64, kl: i64, pfx: *u8) -> i64 {
72 let pl: i64 = vr_slen(pfx)
73 if kl <= pl { return 0 }
74 return vr_kmemeq(b, koff, pl, pfx)
75}
76
77func vr_field(rec: *u8, rl: i64, want: *u8, out: *u8, cap: i64) -> i64 {
78 if rl < 8 { return 0 }
79 let nf: i64 = ss_r32(rec, 4)
80 var off: i64 = 8
81 var fi: i64 = 0
82 while fi < nf {
83 if off + 8 > rl { return 0 }
84 let kl: i64 = ss_r32(rec, off)
85 let koff: i64 = off + 4
86 let vl: i64 = ss_r32(rec, koff + kl)
87 let voff: i64 = koff + kl + 4
88 if vr_kmemeq(rec, koff, kl, want) == 1 {
89 var t: i64 = 0
90 while t < vl { if t < cap - 1 { out[t] = rec[voff + t] } t = t + 1 }
91 if t > cap - 1 { t = cap - 1 }
92 out[t] = 0 as u8
93 return 1
94 }
95 off = voff + vl
96 fi = fi + 1
97 }
98 return 0
99}
100
101func vr_datedays(s: *u8) -> i64 {
102 let nums: *i64 = sys_mmap(8 * 4) as *i64
103 var nn: i64 = 0
104 var cur: i64 = 0
105 var indig: i64 = 0
106 var i: i64 = 0
107 var go: i64 = 1
108 while go == 1 {
109 let c: i64 = s[i] as i64
110 var isd: i64 = 0
111 if c >= 48 { if c <= 57 { isd = 1 } }
112 if isd == 1 { cur = cur * 10 + (c - 48); indig = 1 }
113 if isd == 0 { if indig == 1 { if nn < 4 { nums[nn] = cur; nn = nn + 1 } cur = 0; indig = 0 } }
114 if c == 0 { go = 0 }
115 i = i + 1
116 }
117 if nn != 3 { return 0 - 1 }
118 var y: i64 = nums[0]
119 let m: i64 = nums[1]
120 let d: i64 = nums[2]
121 if y < K_MAGIC_1970 { return 0 - 1 }
122 if m < 1 { return 0 - 1 }
123 if m > 12 { return 0 - 1 }
124 if d < 1 { return 0 - 1 }
125 if d > 31 { return 0 - 1 }
126 if m <= 2 { y = y - 1 }
127 let era: i64 = y / 400
128 let yoe: i64 = y - era * 400
129 var mp: i64 = m - 3
130 if m <= 2 { mp = m + 9 }
131 let doy: i64 = (153 * mp + 2) / 5 + d - 1
132 let doe: i64 = yoe * 365 + yoe / 4 - yoe / 100 + doy
133 return era * K_MAGIC_146097 + doe - K_MAGIC_719468
134}
135
136// find-or-insert into a (names,counts) table; increments the count. np[0] = current length.
137func vr_bump(names: *i64, cnts: *i64, np: *i64, s: *u8) -> i64 {
138 var i: i64 = 0
139 while i < np[0] { if vr_eq(names[i] as *u8, s) == 1 { cnts[i] = cnts[i] + 1; return 0 } i = i + 1 }
140 if np[0] < 256 { names[np[0]] = vr_dup(s) as i64; cnts[np[0]] = 1; np[0] = np[0] + 1 }
141 return 0
142}
143
144// selection sort (names,counts) by count DESCENDING; strict > keeps ties in first-seen order.
145func vr_sort(names: *i64, cnts: *i64, n: i64) -> i64 {
146 var a: i64 = 0
147 while a < n {
148 var mx: i64 = a
149 var c: i64 = a + 1
150 while c < n { if cnts[c] > cnts[mx] { mx = c } c = c + 1 }
151 if mx != a {
152 let tn: i64 = names[a]; names[a] = names[mx]; names[mx] = tn
153 let tc: i64 = cnts[a]; cnts[a] = cnts[mx]; cnts[mx] = tc
154 }
155 a = a + 1
156 }
157 return 0
158}
159
160// one interaction record -> totals/span/recent. st:[0]=total [1]=recent [2]=dmin [3]=dmax . nesting <=3.
161func vr_record(dnum: i64, today: i64, window: i64, fdt: *u8, st: *i64, fmin: *i64, fmax: *i64) -> i64 {
162 st[0] = st[0] + 1
163 if dnum >= 0 {
164 if dnum < st[2] { st[2] = dnum; fmin[0] = vr_dup(fdt) as i64 }
165 if dnum > st[3] { st[3] = dnum; fmax[0] = vr_dup(fdt) as i64 }
166 let ds: i64 = today - dnum
167 if ds >= 0 { if ds <= window { st[1] = st[1] + 1 } }
168 }
169 return 0
170}
171
172func main(argc: i64, argv: *i64) -> i64 {
173 if argc < 4 { vr_p("VIZSLA-REPORT usage: activity <prefix> <today> [window_days] -- fail loud\n" as *u8); return 1 }
174 let cmd: *u8 = argv[1] as *u8
175 if vr_eq(cmd, "activity" as *u8) == 0 { vr_p("VIZSLA-REPORT unknown command (activity) -- fail loud\n" as *u8); return 1 }
176 let prefix: *u8 = argv[2] as *u8
177 let today: i64 = vr_datedays(argv[3] as *u8)
178 if today < 0 { vr_p("VIZSLA-REPORT bad today date (YYYY-MM-DD) -- fail loud\n" as *u8); return 1 }
179 var window: i64 = 30
180 if argc > 4 { window = vr_atoi(argv[4] as *u8) }
181
182 let ty_name: *i64 = sys_mmap(8 * 256) as *i64
183 let ty_cnt: *i64 = sys_mmap(8 * 256) as *i64
184 let typ: *i64 = sys_mmap(16) as *i64
185 typ[0] = 0
186 let ct_name: *i64 = sys_mmap(8 * 256) as *i64
187 let ct_cnt: *i64 = sys_mmap(8 * 256) as *i64
188 let ctp: *i64 = sys_mmap(16) as *i64
189 ctp[0] = 0
190 let st: *i64 = sys_mmap(8 * 8) as *i64
191 st[0] = 0; st[1] = 0; st[2] = K_MAGIC_9000000; st[3] = 0 - 1
192 let fminp: *i64 = sys_mmap(16) as *i64
193 let fmaxp: *i64 = sys_mmap(16) as *i64
194 fminp[0] = "n/a" as *u8 as i64
195 fmaxp[0] = "n/a" as *u8 as i64
196
197 let segs: *i64 = sys_mmap(8 * 260) as *i64
198 let nseg: i64 = ss_manifest(prefix, segs)
199 let fct: *u8 = sys_mmap(128)
200 let fty: *u8 = sys_mmap(128)
201 let fdt: *u8 = sys_mmap(128)
202 var s: i64 = 0
203 while s < nseg {
204 let path: *u8 = sys_mmap(512)
205 var po: i64 = 0
206 po = vr_cat(path, po, prefix); po = vr_cat(path, po, segs[s] as *u8); po = vr_cat(path, po, ".docs" as *u8)
207 path[po] = 0 as u8
208 let szp: *i64 = sys_mmap(16) as *i64
209 let b: *u8 = ss_readall(path, szp)
210 let sz: i64 = szp[0]
211 var j: i64 = 0
212 while j + 9 <= sz {
213 let kind: i64 = b[j]
214 let kl: i64 = ss_r32(b, j + 1)
215 let koff: i64 = j + 5
216 let vl: i64 = ss_r32(b, koff + kl)
217 let voff: i64 = koff + kl + 4
218 var take: i64 = 0
219 if kind == 1 { take = vr_iskind(b, koff, kl, "int:" as *u8) }
220 if take == 1 {
221 let rec: *u8 = (b as i64 + voff) as *u8
222 if vr_field(rec, vl, "contact" as *u8, fct, 128) == 1 {
223 vr_field(rec, vl, "type" as *u8, fty, 128)
224 vr_field(rec, vl, "date" as *u8, fdt, 128)
225 vr_bump(ty_name, ty_cnt, typ, fty)
226 vr_bump(ct_name, ct_cnt, ctp, fct)
227 vr_record(vr_datedays(fdt), today, window, fdt, st, fminp, fmaxp)
228 }
229 }
230 j = voff + vl
231 }
232 s = s + 1
233 }
234
235 vr_sort(ty_name, ty_cnt, typ[0])
236 vr_sort(ct_name, ct_cnt, ctp[0])
237
238 let rep: *u8 = sys_mmap(K_MAGIC_131072)
239 var o: i64 = 0
240 var ti: i64 = 0
241 while ti < typ[0] {
242 o = vr_cat(rep, o, "VIZSLA-REPORT type=" as *u8); o = vr_cat(rep, o, ty_name[ti] as *u8)
243 o = vr_cat(rep, o, " count=" as *u8); o = vr_catn(rep, o, ty_cnt[ti])
244 o = vr_cat(rep, o, "\n" as *u8)
245 ti = ti + 1
246 }
247 var ci: i64 = 0
248 while ci < ctp[0] {
249 o = vr_cat(rep, o, "VIZSLA-REPORT contact=" as *u8); o = vr_cat(rep, o, ct_name[ci] as *u8)
250 o = vr_cat(rep, o, " interactions=" as *u8); o = vr_catn(rep, o, ct_cnt[ci])
251 o = vr_cat(rep, o, "\n" as *u8)
252 ci = ci + 1
253 }
254 let older: i64 = st[0] - st[1]
255 o = vr_cat(rep, o, "VIZSLA-REPORT window=" as *u8); o = vr_catn(rep, o, window)
256 o = vr_cat(rep, o, "d recent=" as *u8); o = vr_catn(rep, o, st[1])
257 o = vr_cat(rep, o, " older=" as *u8); o = vr_catn(rep, o, older)
258 o = vr_cat(rep, o, "\n" as *u8)
259
260 o = vr_cat(rep, o, "VIZSLA-REPORT-VERDICT interactions=" as *u8); o = vr_catn(rep, o, st[0])
261 o = vr_cat(rep, o, " contacts=" as *u8); o = vr_catn(rep, o, ctp[0])
262 o = vr_cat(rep, o, " types=" as *u8); o = vr_catn(rep, o, typ[0])
263 o = vr_cat(rep, o, " recent=" as *u8); o = vr_catn(rep, o, st[1])
264 o = vr_cat(rep, o, " window=" as *u8); o = vr_catn(rep, o, window)
265 o = vr_cat(rep, o, "d span=" as *u8)
266 o = vr_cat(rep, o, fminp[0] as *u8); o = vr_cat(rep, o, ".." as *u8); o = vr_cat(rep, o, fmaxp[0] as *u8)
267 o = vr_cat(rep, o, "\n" as *u8)
268 sys_write(1, rep, o)
269 return 0
270}