code wiki / _hdl_build / nx_adnet_ops.nx

nx_adnet_ops.nx source

↩ module page · 123 lines · 5662 B

1// nx_adnet_ops.nx -- LIB: the OPERATOR VIEW. "What in the ad network needs a human right now?" 2// WHY THIS EXISTS: today's work added a lot of REFUSALS -- an ad with no rate row is refused at invoice 3// time, a campaign with a real budget and no spend field refuses to serve, a forged viewable count 4// refuses to bill. Every one of those is CORRECT and every one of them is INVISIBLE. A refusal nobody 5// can see is indistinguishable from the thing simply not working, and the operator finds out when an 6// advertiser emails to ask why their campaign stopped. 7// 8// So this organ answers exactly one question, from the same files the serving and billing paths read: 9// which rows are in a state that a human must resolve? 10// 11// ★ DERIVED, NEVER AUTHORED. It computes from inventory + rates + campaigns + applications. There is no 12// ops state to drift out of sync, because there is no ops state. 13// ★ NAMES THE REMEDY, NOT JUST THE PROBLEM. "unbilled inventory" is a complaint; "ha-video is serving 14// with no rate row -- add one to adnet_rates.conf or it invoices REFUSED" is a work item. 15// ★ SILENT ON A HEALTHY PLANE. An operator report that always prints something trains people to ignore 16// it. Zero findings must produce zero lines. 17// license_tier: ORIGINAL 18import "nx_syscalls.nx" 19import "_hdl_build/nx_adnet_invoice.nx" 20import "_hdl_build/nx_adnet_campaign.nx" 21 22func aop_catd(dst: *u8, off: i64, v: i64) -> i64 { 23 var o: i64 = off 24 var m: i64 = v 25 if m < 0 { dst[o] = 45 as u8; o = o + 1; m = 0 - m } 26 let t: *u8 = sys_mmap(24) 27 var k: i64 = 0 28 if m == 0 { t[0] = 48 as u8; k = 1 } 29 while m > 0 { t[k] = (48 + (m - (m / 10) * 10)) as u8; m = m / 10; k = k + 1 } 30 var i: i64 = k - 1 31 while i >= 0 { dst[o] = t[i]; o = o + 1; i = i - 1 } 32 return o 33} 34 35// count non-comment, non-directive rows in a TAB file (applications, etc.) 36func aop_rows(buf: *u8, blen: i64) -> i64 { 37 var n: i64 = 0 38 var ls: i64 = 0 39 while ls < blen { 40 let le: i64 = ad_eol(buf, ls, blen) 41 if le > ls { 42 let row: *u8 = ((buf as i64) + ls) as *u8 43 if row[0] != (35 as u8) { if row[0] != (64 as u8) { n = n + 1 } } 44 } 45 ls = le + 1 46 } 47 return n 48} 49 50// THE REPORT. Returns bytes written; 0 bytes = a healthy plane, which is the point. 51func aop_report(inv: *u8, ilen: i64, rates: *u8, rlen: i64, camps: *u8, clen: i64, apps: *u8, alen: i64, out: *u8, cap: i64) -> i64 { 52 var o: i64 = 0 53 let id: *u8 = sys_mmap(128) 54 55 // (1) SERVING BUT UNPRICED -- inventory with no rate row. This is live inventory that will invoice 56 // REFUSED, i.e. we are giving away impressions and the ledger correctly declines to bill them. 57 var ls: i64 = 0 58 while ls < ilen { 59 let le: i64 = ad_eol(inv, ls, ilen) 60 if le > ls { 61 let row: *u8 = ((inv as i64) + ls) as *u8 62 if row[0] != (64 as u8) { 63 aslot_field_b(row, le - ls, 0, id, 128) 64 if aslot_id_ok(id) == 1 { 65 if ainv_rate(rates, rlen, id, 1) < 0 { 66 if o + 200 < cap { 67 o = ad_cat(out, o, "ACTION unpriced-inventory id=" as *u8) 68 o = ad_cat(out, o, id) 69 o = ad_cat(out, o, " -- serving now but invoices REFUSED; add a row to adnet_rates.conf\n" as *u8) 70 } 71 } 72 } 73 } 74 } 75 ls = le + 1 76 } 77 78 // (2) CAMPAIGNS THAT CANNOT SERVE -- a real budget with no readable spend field refuses at serve 79 // time (correctly: assuming zero spend is how an exhausted campaign keeps running). Invisible 80 // unless surfaced, and the advertiser experiences it as "my ad stopped". 81 ls = 0 82 while ls < clen { 83 let le: i64 = ad_eol(camps, ls, clen) 84 if le > ls { 85 let row: *u8 = ((camps as i64) + ls) as *u8 86 if row[0] != (35 as u8) { 87 aslot_field_b(row, le - ls, 0, id, 128) 88 if aslot_id_ok(id) == 1 { 89 let bud: i64 = camp_row_budget(row, le - ls) 90 let sp: i64 = camp_row_spent(row, le - ls) 91 if bud >= 0 { if sp == CAMP_BAD_NUM { 92 if o + 200 < cap { 93 o = ad_cat(out, o, "ACTION campaign-cannot-serve id=" as *u8) 94 o = ad_cat(out, o, id) 95 o = ad_cat(out, o, " -- has a budget but no spend field; run the reconcile beat or set field 7\n" as *u8) 96 } 97 } } 98 if bud >= 0 { if sp >= 0 { if sp >= bud { 99 if o + 200 < cap { 100 o = ad_cat(out, o, "ACTION campaign-exhausted id=" as *u8) 101 o = ad_cat(out, o, id) 102 o = ad_cat(out, o, " -- budget spent, no longer serving; renew or close with the advertiser\n" as *u8) 103 } 104 } } } 105 } 106 } 107 } 108 ls = le + 1 109 } 110 111 // (3) PENDING APPLICATIONS -- staged-never-live means nothing happens without a human. An 112 // application nobody looks at is a lost client, so it is a work item, not a statistic. 113 let na: i64 = aop_rows(apps, alen) 114 if na > 0 { 115 if o + 160 < cap { 116 o = ad_cat(out, o, "ACTION pending-applications n=" as *u8) 117 o = aop_catd(out, o, na) 118 o = ad_cat(out, o, " -- staged, never live; review adnet_applications.log and reply\n" as *u8) 119 } 120 } 121 122 out[o] = 0 as u8 123 return o 124}