code wiki / _hdl_build / nx_adnet_dash.nx
nx_adnet_dash.nx source
↩ module page · 116 lines · 5936 B
1// nx_adnet_dash.nx -- LIB: the advertiser DASHBOARD for the sovereign ad network. Server-rendered
2// ZERO-JS HTML from the live inventory + the per-AD append-only counters: per-ad SERVED impressions,
3// clicks, CTR (permil) + totals. AGGREGATE ONLY -- the logs carry ad ids, never visitor identity, so
4// the dashboard cannot leak what was never collected. HONEST LABELS: counts are SERVED first-party
5// counts; MRC viewability is the separate gated nx_ad_view lane and is labeled as such.
6// Also emits the /ads.txt transparency artifact: first-party DIRECT, no reseller chain.
7// license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "_hdl_build/nx_adnet_slot.nx"
10
11// BOUNDED per-request file read (rule 21: NEVER sys_read_file per request -- it mmaps 4GB per call).
12// Reads at most cap-1 bytes; absent file -> 0 bytes (an empty log is a valid zero-counter state).
13func and_read_bounded(path: *u8, buf: *u8, cap: i64) -> i64 {
14 let fd: i64 = sys_openat_rd(path)
15 if fd < 0 { return 0 }
16 var total: i64 = 0
17 var more: i64 = 1
18 while more == 1 {
19 if total >= cap - 1 { more = 0 } else {
20 let got: i64 = sys_read(fd, ((buf as i64) + total) as *u8, cap - 1 - total)
21 if got <= 0 { more = 0 } else { total = total + got }
22 }
23 }
24 sys_close(fd)
25 return total
26}
27
28func and_catd(dst: *u8, off: i64, v: i64) -> i64 {
29 var o: i64 = off
30 var m: i64 = v
31 if m < 0 { dst[o] = 45 as u8; o = o + 1; m = 0 - m }
32 let t: *u8 = sys_mmap(24)
33 var k: i64 = 0
34 if m == 0 { t[0] = 48 as u8; k = 1 }
35 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
36 var i: i64 = k - 1
37 while i >= 0 { dst[o] = t[i]; o = o + 1; i = i - 1 }
38 return o
39}
40
41// ads.txt (IAB Authorized Digital Sellers): we are the ad system AND the publisher -- one DIRECT row,
42// zero resellers. The reseller/spoof fraud class is eliminated by construction.
43func and_adstxt(domain: *u8, out: *u8, cap: i64) -> i64 {
44 if cap < 256 { return 0 }
45 var o: i64 = 0
46 o = ad_cat(out, o, domain)
47 o = ad_cat(out, o, ", nishi-adnet, DIRECT" as *u8)
48 out[o] = 10 as u8
49 o = o + 1
50 return o
51}
52
53// the dashboard page. inv = live inventory conf bytes; implog/clklog = per-AD id-per-line counters.
54func and_page(inv: *u8, ilen: i64, implog: *u8, iln: i64, clklog: *u8, cln: i64, out: *u8, cap: i64) -> i64 {
55 if cap < 8192 { return 0 }
56 var o: i64 = 0
57 o = ad_cat(out, o, "<html><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>Adnet Dashboard</title>" as *u8)
58 o = ad_cat(out, o, "<style>body{font-family:system-ui,sans-serif;max-width:860px;margin:4vh auto;padding:0 20px;color:var(--fg,black)}table{border-collapse:collapse;width:100%}td,th{border:1px solid gray;padding:6px 10px;text-align:left;font-size:.95rem}th{background:silver}caption{margin-bottom:8px;font-size:.85rem;color:gray;text-align:left}</style></head><body class=\"nx-ad-optout\">" as *u8)
59 o = ad_cat(out, o, "<h1>Adnet Dashboard</h1><p>Aggregate per-AD counters only. No visitor identity exists in this system: no cookie, no IP, no fingerprint. Counts below are SERVED first-party impressions; MRC viewability is the separate gated nx_ad_view lane.</p>" as *u8)
60 o = ad_cat(out, o, "<table><caption>live inventory + served counters</caption><tr><th>ad</th><th>advertiser</th><th>section</th><th>served</th><th>clicks</th><th>ctr permil</th></tr>" as *u8)
61 let id: *u8 = sys_mmap(128)
62 let adv: *u8 = sys_mmap(256)
63 let sec: *u8 = sys_mmap(128)
64 var timp: i64 = 0
65 var tclk: i64 = 0
66 var nrows: i64 = 0
67 var ls: i64 = 0
68 while ls < ilen {
69 let le: i64 = ad_eol(inv, ls, ilen)
70 if le > ls {
71 if le - ls < 1536 {
72 let row: *u8 = ((inv as i64) + ls) as *u8
73 if row[0] != (64 as u8) {
74 aslot_field_b(row, le - ls, 0, id, 128)
75 aslot_field_b(row, le - ls, 1, adv, 256)
76 aslot_field_b(row, le - ls, 4, sec, 128)
77 if aslot_id_ok(id) == 1 {
78 if o + 1024 >= cap { return 0 }
79 let imps: i64 = ad_event_count(implog, iln, id)
80 let clks: i64 = ad_event_count(clklog, cln, id)
81 var ctr: i64 = 0
82 if imps > 0 { ctr = (clks * 1000) / imps }
83 timp = timp + imps
84 tclk = tclk + clks
85 nrows = nrows + 1
86 o = ad_cat(out, o, "<tr><td>" as *u8)
87 o = ad_cat(out, o, id)
88 o = ad_cat(out, o, "</td><td>" as *u8)
89 o = aslot_cat_esc(out, o, adv, cap)
90 o = ad_cat(out, o, "</td><td>" as *u8)
91 o = aslot_cat_esc(out, o, sec, cap)
92 o = ad_cat(out, o, "</td><td>" as *u8)
93 o = and_catd(out, o, imps)
94 o = ad_cat(out, o, "</td><td>" as *u8)
95 o = and_catd(out, o, clks)
96 o = ad_cat(out, o, "</td><td>" as *u8)
97 o = and_catd(out, o, ctr)
98 o = ad_cat(out, o, "</td></tr>" as *u8)
99 }
100 }
101 }
102 }
103 ls = le + 1
104 }
105 o = ad_cat(out, o, "<tr><th>TOTAL</th><th></th><th></th><th>" as *u8)
106 o = and_catd(out, o, timp)
107 o = ad_cat(out, o, "</th><th>" as *u8)
108 o = and_catd(out, o, tclk)
109 o = ad_cat(out, o, "</th><th></th></tr></table>" as *u8)
110 o = ad_cat(out, o, "<p>ads=" as *u8)
111 o = and_catd(out, o, nrows)
112 o = ad_cat(out, o, " · first-party serving · DIRECT single-seller (see /ads.txt) · zero third-party JS by construction.</p></body></html>" as *u8)
113 if o + 2 >= cap { return 0 }
114 out[o] = 0 as u8
115 return o
116}