@curless/hotel-storefront 0.69.3 → 0.70.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/README.md +11 -0
  2. package/dist/account/index.d.ts +21 -0
  3. package/dist/account/index.d.ts.map +1 -1
  4. package/dist/account/index.js +63 -4
  5. package/dist/account/index.js.map +1 -1
  6. package/dist/config.d.ts +12 -0
  7. package/dist/config.d.ts.map +1 -1
  8. package/dist/config.js.map +1 -1
  9. package/dist/huazhu/client.d.ts +3 -1
  10. package/dist/huazhu/client.d.ts.map +1 -1
  11. package/dist/huazhu/client.js +26 -0
  12. package/dist/huazhu/client.js.map +1 -1
  13. package/dist/huazhu/english.d.ts.map +1 -1
  14. package/dist/huazhu/english.js +11 -0
  15. package/dist/huazhu/english.js.map +1 -1
  16. package/dist/otaless/backend.d.ts.map +1 -1
  17. package/dist/otaless/backend.js +17 -6
  18. package/dist/otaless/backend.js.map +1 -1
  19. package/dist/otaless/booking.d.ts +11 -4
  20. package/dist/otaless/booking.d.ts.map +1 -1
  21. package/dist/otaless/booking.js +95 -11
  22. package/dist/otaless/booking.js.map +1 -1
  23. package/dist/otaless/fake.d.ts +7 -2
  24. package/dist/otaless/fake.d.ts.map +1 -1
  25. package/dist/otaless/fake.js +4 -2
  26. package/dist/otaless/fake.js.map +1 -1
  27. package/dist/otaless/places.d.ts.map +1 -1
  28. package/dist/otaless/places.js +16 -2
  29. package/dist/otaless/places.js.map +1 -1
  30. package/dist/regions.d.ts.map +1 -1
  31. package/dist/regions.js +25 -2
  32. package/dist/regions.js.map +1 -1
  33. package/dist/server.d.ts +4 -1
  34. package/dist/server.d.ts.map +1 -1
  35. package/dist/server.js +280 -13
  36. package/dist/server.js.map +1 -1
  37. package/dist/source-allowlist.d.ts +9 -0
  38. package/dist/source-allowlist.d.ts.map +1 -1
  39. package/dist/source-allowlist.js +23 -1
  40. package/dist/source-allowlist.js.map +1 -1
  41. package/dist/state.d.ts +25 -2
  42. package/dist/state.d.ts.map +1 -1
  43. package/dist/state.js +88 -15
  44. package/dist/state.js.map +1 -1
  45. package/dist/tools.d.ts.map +1 -1
  46. package/dist/tools.js +61 -2
  47. package/dist/tools.js.map +1 -1
  48. package/dist/usage/boot.d.ts +32 -0
  49. package/dist/usage/boot.d.ts.map +1 -0
  50. package/dist/usage/boot.js +140 -0
  51. package/dist/usage/boot.js.map +1 -0
  52. package/dist/usage/geo.d.ts +48 -0
  53. package/dist/usage/geo.d.ts.map +1 -0
  54. package/dist/usage/geo.js +347 -0
  55. package/dist/usage/geo.js.map +1 -0
  56. package/dist/usage/maintain.d.ts +26 -0
  57. package/dist/usage/maintain.d.ts.map +1 -0
  58. package/dist/usage/maintain.js +177 -0
  59. package/dist/usage/maintain.js.map +1 -0
  60. package/dist/usage/platform.d.ts +69 -0
  61. package/dist/usage/platform.d.ts.map +1 -0
  62. package/dist/usage/platform.js +124 -0
  63. package/dist/usage/platform.js.map +1 -0
  64. package/dist/usage/pseudonym.d.ts +28 -0
  65. package/dist/usage/pseudonym.d.ts.map +1 -0
  66. package/dist/usage/pseudonym.js +73 -0
  67. package/dist/usage/pseudonym.js.map +1 -0
  68. package/dist/usage/range.d.ts +24 -0
  69. package/dist/usage/range.d.ts.map +1 -0
  70. package/dist/usage/range.js +102 -0
  71. package/dist/usage/range.js.map +1 -0
  72. package/dist/usage/recorder.d.ts +115 -0
  73. package/dist/usage/recorder.d.ts.map +1 -0
  74. package/dist/usage/recorder.js +227 -0
  75. package/dist/usage/recorder.js.map +1 -0
  76. package/dist/usage/report.d.ts +207 -0
  77. package/dist/usage/report.d.ts.map +1 -0
  78. package/dist/usage/report.js +730 -0
  79. package/dist/usage/report.js.map +1 -0
  80. package/dist/usage/shadow-import.d.ts +28 -0
  81. package/dist/usage/shadow-import.d.ts.map +1 -0
  82. package/dist/usage/shadow-import.js +107 -0
  83. package/dist/usage/shadow-import.js.map +1 -0
  84. package/dist/usage/store.d.ts +215 -0
  85. package/dist/usage/store.d.ts.map +1 -0
  86. package/dist/usage/store.js +430 -0
  87. package/dist/usage/store.js.map +1 -0
  88. package/dist/usage-cli.d.ts +3 -0
  89. package/dist/usage-cli.d.ts.map +1 -0
  90. package/dist/usage-cli.js +152 -0
  91. package/dist/usage-cli.js.map +1 -0
  92. package/package.json +3 -2
@@ -0,0 +1,730 @@
1
+ // Copied from @curless/shopify-storefront src/usage/report.ts (0.77.2): the same
2
+ // counts and the same daily totals (docs/usage-events.md §8), so the dashboard
3
+ // reads both alike. What the hotels add - booking outcomes, linked memberships,
4
+ // the groups (`byShop`) - rides beside them as keys of their own.
5
+ //
6
+ // What the usage store says, summed: by day × platform × country, new and
7
+ // returning users, the tools by use, the funnel, and the shop-account sign-ins.
8
+ //
9
+ // Counts only. A report shows how many pseudonyms, never which.
10
+ //
11
+ // ⚠️ DAYS ARE BEIJING DAYS unless asked otherwise (robin 11-13, the same as
12
+ // the dashboard): the event files are named by UTC day, a report re-cuts them.
13
+ //
14
+ // ⚠️ TWO MODES. Within the 13 months the event lines are kept, everything is
15
+ // counted from them - users and conversations de-duplicated across the whole
16
+ // window ("exact"). Past that only each day's totals are left (daily/), and a
17
+ // person seen on two days cannot be told from two people: users become 「每天
18
+ // 活跃之和」 and 「新用户累计」 ("daily-sum"), and the report says so.
19
+ import { PLATFORMS } from './platform.js';
20
+ export const PLATFORM_LABEL = {
21
+ chatgpt: 'ChatGPT',
22
+ claude: 'Claude',
23
+ manus: 'Manus',
24
+ muse: 'Muse',
25
+ other: '其他',
26
+ };
27
+ export const geoLabel = (g) => g === 'platform' ? '平台机房' : g === 'unknown' ? '未知' : g;
28
+ export const BEIJING = { name: 'Asia/Shanghai', offsetMin: 480 };
29
+ export const UTC_TZ = { name: 'UTC', offsetMin: 0 };
30
+ export const dayIn = (t, tz) => new Date((typeof t === 'number' ? t : Date.parse(t)) + tz.offsetMin * 60_000)
31
+ .toISOString()
32
+ .slice(0, 10);
33
+ export const addDays = (day, n) => {
34
+ const d = new Date(`${day}T00:00:00Z`);
35
+ d.setUTCDate(d.getUTCDate() + n);
36
+ return d.toISOString().slice(0, 10);
37
+ };
38
+ const bucket = () => ({
39
+ users: new Set(),
40
+ fresh: new Set(),
41
+ convs: new Set(),
42
+ conns: new Set(),
43
+ inits: 0,
44
+ calls: 0,
45
+ failed: 0,
46
+ reqs: 0,
47
+ cartAdds: 0,
48
+ checkouts: 0,
49
+ signIns: 0,
50
+ shoppers: new Set(),
51
+ });
52
+ const put = (b, e, isNew) => {
53
+ if (e.u)
54
+ b.users.add(e.u);
55
+ if (e.u && isNew)
56
+ b.fresh.add(e.u);
57
+ if (e.c)
58
+ b.convs.add(e.c);
59
+ if (e.k)
60
+ b.conns.add(e.k);
61
+ if (e.e === 'init')
62
+ b.inits++;
63
+ if (e.e === 'call') {
64
+ b.calls++;
65
+ if (e.ok === false)
66
+ b.failed++;
67
+ }
68
+ if (e.e === 'req')
69
+ b.reqs++;
70
+ if (e.e === 'cart_add')
71
+ b.cartAdds++;
72
+ if (e.e === 'checkout')
73
+ b.checkouts++;
74
+ if (e.e === 'account' && e.step === 'ok')
75
+ b.signIns++;
76
+ if (e.e === 'account' && e.s && (e.step === 'ok' || e.step === 'orders'))
77
+ b.shoppers.add(e.s);
78
+ };
79
+ const ROW_KEYS = [
80
+ 'users',
81
+ 'newUsers',
82
+ 'convs',
83
+ 'conns',
84
+ 'inits',
85
+ 'calls',
86
+ 'failed',
87
+ 'reqs',
88
+ 'cartAdds',
89
+ 'checkouts',
90
+ 'signIns',
91
+ 'shoppers',
92
+ ];
93
+ const row = (b) => ({
94
+ users: b.users.size,
95
+ newUsers: b.fresh.size,
96
+ convs: b.convs.size,
97
+ conns: b.conns.size,
98
+ inits: b.inits,
99
+ calls: b.calls,
100
+ failed: b.failed,
101
+ reqs: b.reqs,
102
+ cartAdds: b.cartAdds,
103
+ checkouts: b.checkouts,
104
+ signIns: b.signIns,
105
+ shoppers: b.shoppers.size,
106
+ });
107
+ const zero = () => Object.fromEntries(ROW_KEYS.map((k) => [k, 0]));
108
+ const addRow = (a, b) => {
109
+ for (const k of ROW_KEYS)
110
+ a[k] += Number(b[k] ?? 0);
111
+ return a;
112
+ };
113
+ class BookingTally {
114
+ ok = 0;
115
+ fail = 0;
116
+ checking = 0;
117
+ why = {};
118
+ put(e) {
119
+ if (e.e !== 'booking')
120
+ return;
121
+ if (e.res === 'ok')
122
+ this.ok++;
123
+ else if (e.res === 'checking')
124
+ this.checking++;
125
+ else if (e.res === 'fail') {
126
+ this.fail++;
127
+ const k = e.why ?? 'other';
128
+ this.why[k] = (this.why[k] ?? 0) + 1;
129
+ }
130
+ }
131
+ summary() {
132
+ return { ok: this.ok, fail: this.fail, checking: this.checking, why: this.why };
133
+ }
134
+ }
135
+ const memberCounts = () => ({ links: 0, ok: 0, failed: 0, out: 0 });
136
+ class MemberTally {
137
+ all = memberCounts();
138
+ signUps = 0;
139
+ why = {};
140
+ byGroup = new Map();
141
+ put(e) {
142
+ if (e.e !== 'member')
143
+ return;
144
+ const field = e.step === 'link'
145
+ ? 'links'
146
+ : e.step === 'ok'
147
+ ? 'ok'
148
+ : e.step === 'fail'
149
+ ? 'failed'
150
+ : e.step === 'out'
151
+ ? 'out'
152
+ : null;
153
+ if (!field)
154
+ return;
155
+ this.all[field]++;
156
+ if (e.shop) {
157
+ const g = this.byGroup.get(e.shop) ?? memberCounts();
158
+ g[field]++;
159
+ this.byGroup.set(e.shop, g);
160
+ }
161
+ if (e.step === 'ok' && e.signUp)
162
+ this.signUps++;
163
+ if (e.step === 'fail') {
164
+ const k = e.why ?? 'other';
165
+ this.why[k] = (this.why[k] ?? 0) + 1;
166
+ }
167
+ }
168
+ summary() {
169
+ return {
170
+ ...this.all,
171
+ signUps: this.signUps,
172
+ why: this.why,
173
+ byGroup: Object.fromEntries([...this.byGroup.entries()].sort(([a], [b]) => (a < b ? -1 : 1))),
174
+ };
175
+ }
176
+ }
177
+ const median = (xs) => {
178
+ if (!xs.length)
179
+ return null;
180
+ const s = [...xs].sort((a, b) => a - b);
181
+ return s[Math.floor(s.length / 2)] ?? null;
182
+ };
183
+ class AccountTally {
184
+ links = 0;
185
+ starts = 0;
186
+ ok = 0;
187
+ failed = 0;
188
+ why = {};
189
+ signOuts = 0;
190
+ orderReads = 0;
191
+ signUps = 0;
192
+ shoppers = new Set();
193
+ signedIn = new Set();
194
+ signedInCheckout = new Set();
195
+ convs = new Set();
196
+ put(e) {
197
+ if (e.c)
198
+ this.convs.add(e.c);
199
+ if (e.e === 'checkout' && e.c && (e.signedIn || this.signedIn.has(e.c))) {
200
+ this.signedInCheckout.add(e.c);
201
+ }
202
+ if (e.e !== 'account')
203
+ return;
204
+ if (e.step === 'link')
205
+ this.links++;
206
+ if (e.step === 'start')
207
+ this.starts++;
208
+ if (e.step === 'ok') {
209
+ this.ok++;
210
+ if (e.signUp)
211
+ this.signUps++;
212
+ if (e.c)
213
+ this.signedIn.add(e.c);
214
+ }
215
+ if (e.step === 'fail') {
216
+ this.failed++;
217
+ const k = e.why ?? 'other';
218
+ this.why[k] = (this.why[k] ?? 0) + 1;
219
+ }
220
+ if (e.step === 'out')
221
+ this.signOuts++;
222
+ if (e.step === 'orders') {
223
+ this.orderReads++;
224
+ if (e.c)
225
+ this.signedIn.add(e.c);
226
+ }
227
+ if (e.s && (e.step === 'ok' || e.step === 'orders'))
228
+ this.shoppers.add(e.s);
229
+ }
230
+ summary(seenBefore) {
231
+ const newShoppers = [...this.shoppers].filter((s) => !seenBefore.has(s)).length;
232
+ return {
233
+ begun: this.links + this.starts,
234
+ links: this.links,
235
+ starts: this.starts,
236
+ ok: this.ok,
237
+ failed: this.failed,
238
+ why: this.why,
239
+ signOuts: this.signOuts,
240
+ orderReads: this.orderReads,
241
+ signUps: this.signUps,
242
+ shoppers: this.shoppers.size,
243
+ newShoppers,
244
+ returningShoppers: this.shoppers.size - newShoppers,
245
+ signedInConvs: this.signedIn.size,
246
+ allConvs: this.convs.size,
247
+ signedInCheckoutConvs: this.signedInCheckout.size,
248
+ };
249
+ }
250
+ }
251
+ const geoRow = (v) => ({
252
+ ...row(v.b),
253
+ isp: v.isp.size,
254
+ hosting: v.hosting.size,
255
+ });
256
+ const putGeo = (m, e, isNew) => {
257
+ const gb = m.get(e.g) ?? { b: bucket(), isp: new Set(), hosting: new Set() };
258
+ put(gb.b, e, isNew);
259
+ const who = e.u ?? e.c;
260
+ if (who && e.n === 'isp')
261
+ gb.isp.add(who);
262
+ if (who && e.n === 'hosting')
263
+ gb.hosting.add(who);
264
+ m.set(e.g, gb);
265
+ };
266
+ const putIn = (m, key, e, isNew) => {
267
+ const b = m.get(key) ?? bucket();
268
+ put(b, e, isNew);
269
+ m.set(key, b);
270
+ };
271
+ const sortedRows = (m) => Object.fromEntries([...m.entries()].sort(([a], [b]) => (a < b ? -1 : 1)).map(([k, b]) => [k, row(b)]));
272
+ const platformRows = (m) => Object.fromEntries([
273
+ ...PLATFORMS.filter((p) => m.has(p)),
274
+ ...[...m.keys()].filter((p) => !PLATFORMS.includes(p)),
275
+ ].map((p) => [p, row(m.get(p))]));
276
+ const geoRows = (m) => Object.fromEntries([...m.entries()]
277
+ .sort(([, a], [, b]) => b.b.users.size + b.b.convs.size - a.b.users.size - a.b.convs.size)
278
+ .map(([g, v]) => [g, geoRow(v)]));
279
+ /**
280
+ * Sums events fed one at a time - so neither the maintenance nor the CLI ever
281
+ * holds a window's lines in memory, only its counts and distinct pseudonyms.
282
+ *
283
+ * Two passes over the same lines: `place` (who was where - see knownPlaces),
284
+ * then `add`. Lines before `from` only say who was seen before (new versus
285
+ * returning) - unless a `seen` index answers that instead.
286
+ */
287
+ export class ReportBuilder {
288
+ from;
289
+ to;
290
+ deployment;
291
+ tz;
292
+ seen;
293
+ tally = new Map();
294
+ places = null;
295
+ seenUsers = new Set();
296
+ shoppersBefore = new Set();
297
+ days = new Map();
298
+ total = bucket();
299
+ byPlatform = new Map();
300
+ byGeo = new Map();
301
+ byShop = new Map();
302
+ booking = new BookingTally();
303
+ member = new MemberTally();
304
+ tools = new Map();
305
+ account = new AccountTally();
306
+ imported = new Set();
307
+ funnel = {
308
+ connected: new Set(),
309
+ viewed: new Set(),
310
+ added: new Set(),
311
+ checkout: new Set(),
312
+ };
313
+ constructor(from, to, deployment = null, tz = BEIJING, seen) {
314
+ this.from = from;
315
+ this.to = to;
316
+ this.deployment = deployment;
317
+ this.tz = tz;
318
+ this.seen = seen;
319
+ }
320
+ /** How many distinct ids it holds - what a caller bounds its memory by. */
321
+ distinct() {
322
+ return this.total.users.size + this.total.convs.size + this.total.conns.size + this.tally.size;
323
+ }
324
+ mine(e) {
325
+ return !this.deployment || e.d === this.deployment;
326
+ }
327
+ /**
328
+ * First pass: where each user and conversation was seen, within the window.
329
+ * A request that said nothing of where is then counted where its user (or,
330
+ * with no user, its conversation) most often was - one request without an
331
+ * address does not put them in 「未知」 too (总线程 11-13: 5 ChatGPT users
332
+ * were in both).
333
+ */
334
+ place(e) {
335
+ if (!this.mine(e) || e.g === 'unknown' || e.g === 'platform')
336
+ return;
337
+ const day = dayIn(e.t, this.tz);
338
+ if (day < this.from || day > this.to)
339
+ return;
340
+ for (const who of [e.u && `u:${e.u}`, e.c && `c:${e.c}`]) {
341
+ if (!who)
342
+ continue;
343
+ const m = this.tally.get(who) ?? new Map();
344
+ const hit = m.get(e.g) ?? { count: 0, ...(e.n ? { n: e.n } : {}) };
345
+ hit.count++;
346
+ m.set(e.g, hit);
347
+ this.tally.set(who, m);
348
+ }
349
+ }
350
+ placeOf(e) {
351
+ if (e.g !== 'unknown')
352
+ return e;
353
+ if (!this.places) {
354
+ this.places = new Map();
355
+ for (const [who, m] of this.tally) {
356
+ const [g, best] = [...m.entries()].sort((a, b) => b[1].count - a[1].count)[0];
357
+ this.places.set(who, { g, ...(best.n ? { n: best.n } : {}) });
358
+ }
359
+ this.tally.clear();
360
+ }
361
+ const hit = (e.u && this.places.get(`u:${e.u}`)) || (e.c && this.places.get(`c:${e.c}`));
362
+ return hit ? { ...e, g: hit.g, ...(hit.n ? { n: hit.n } : {}) } : e;
363
+ }
364
+ /** Second pass: count it. */
365
+ add(raw) {
366
+ if (!this.mine(raw))
367
+ return;
368
+ const day = dayIn(raw.t, this.tz);
369
+ if (day > this.to)
370
+ return;
371
+ if (day < this.from) {
372
+ if (!this.seen) {
373
+ if (raw.u)
374
+ this.seenUsers.add(raw.u);
375
+ if (raw.s)
376
+ this.shoppersBefore.add(raw.s);
377
+ }
378
+ return;
379
+ }
380
+ const e = this.placeOf(raw);
381
+ let d = this.days.get(day);
382
+ if (!d) {
383
+ d = {
384
+ total: bucket(),
385
+ byPlatform: new Map(),
386
+ byGeo: new Map(),
387
+ rows: new Map(),
388
+ byShop: new Map(),
389
+ account: new AccountTally(),
390
+ booking: new BookingTally(),
391
+ member: new MemberTally(),
392
+ imported: false,
393
+ };
394
+ this.days.set(day, d);
395
+ }
396
+ if (e.src) {
397
+ this.imported.add(day);
398
+ d.imported = true;
399
+ }
400
+ let isNew = false;
401
+ if (e.u) {
402
+ if (this.seen) {
403
+ const first = this.seen.userFirst(e.u);
404
+ isNew = !first || first >= day;
405
+ }
406
+ else {
407
+ isNew = !this.seenUsers.has(e.u);
408
+ this.seenUsers.add(e.u);
409
+ }
410
+ }
411
+ if (e.s && this.seen) {
412
+ const first = this.seen.shopperFirst(e.s);
413
+ if (first && first < this.from)
414
+ this.shoppersBefore.add(e.s);
415
+ }
416
+ put(d.total, e, isNew);
417
+ putIn(d.byPlatform, e.p, e, isNew);
418
+ putGeo(d.byGeo, e, isNew);
419
+ putIn(d.rows, `${e.p}|${e.g}`, e, isNew);
420
+ d.account.put(e);
421
+ d.booking.put(e);
422
+ d.member.put(e);
423
+ if (e.shop)
424
+ putIn(d.byShop, e.shop, e, isNew);
425
+ put(this.total, e, isNew);
426
+ putIn(this.byPlatform, e.p, e, isNew);
427
+ putGeo(this.byGeo, e, isNew);
428
+ if (e.shop)
429
+ putIn(this.byShop, e.shop, e, isNew);
430
+ this.account.put(e);
431
+ this.booking.put(e);
432
+ this.member.put(e);
433
+ if (e.e === 'call' && e.tool) {
434
+ const t = this.tools.get(e.tool) ?? { calls: 0, failed: 0, ms: [] };
435
+ t.calls++;
436
+ if (e.ok === false)
437
+ t.failed++;
438
+ // A median from at most the first 10,000 timings: enough, and bounded.
439
+ if (typeof e.ms === 'number' && t.ms.length < 10_000)
440
+ t.ms.push(e.ms);
441
+ this.tools.set(e.tool, t);
442
+ }
443
+ // Recorded days only: an imported request has no tool, so its conversation
444
+ // could never reach the later steps and would only thin the rates.
445
+ if (e.c && !e.src) {
446
+ this.funnel.connected.add(e.c);
447
+ if (e.e === 'call' && (e.r === 'browse' || e.r === 'product'))
448
+ this.funnel.viewed.add(e.c);
449
+ if (e.e === 'cart_add')
450
+ this.funnel.added.add(e.c);
451
+ if (e.e === 'checkout')
452
+ this.funnel.checkout.add(e.c);
453
+ }
454
+ }
455
+ build() {
456
+ const t = row(this.total);
457
+ return {
458
+ v: 1,
459
+ deployment: this.deployment,
460
+ tz: this.tz.name,
461
+ from: this.from,
462
+ to: this.to,
463
+ mode: 'exact',
464
+ days: [...this.days.entries()]
465
+ .sort(([a], [b]) => (a < b ? -1 : 1))
466
+ .map(([day, d]) => ({
467
+ day,
468
+ total: row(d.total),
469
+ byPlatform: platformRows(d.byPlatform),
470
+ byGeo: geoRows(d.byGeo),
471
+ rows: sortedRows(d.rows),
472
+ byShop: sortedRows(d.byShop),
473
+ account: d.account.summary(new Set()),
474
+ booking: d.booking.summary(),
475
+ member: d.member.summary(),
476
+ imported: d.imported,
477
+ })),
478
+ total: { ...t, returningUsers: t.users - t.newUsers },
479
+ byPlatform: platformRows(this.byPlatform),
480
+ byGeo: geoRows(this.byGeo),
481
+ byShop: sortedRows(this.byShop),
482
+ tools: [...this.tools.entries()]
483
+ .sort(([, a], [, b]) => b.calls - a.calls)
484
+ .map(([tool, x]) => ({ tool, calls: x.calls, failed: x.failed, p50ms: median(x.ms) })),
485
+ funnel: {
486
+ connected: this.funnel.connected.size,
487
+ viewed: this.funnel.viewed.size,
488
+ added: this.funnel.added.size,
489
+ checkout: this.funnel.checkout.size,
490
+ },
491
+ account: this.account.summary(this.shoppersBefore),
492
+ booking: this.booking.summary(),
493
+ member: this.member.summary(),
494
+ importedDays: [...this.imported].sort(),
495
+ };
496
+ }
497
+ }
498
+ /** The whole report from events in hand - tests, and small windows. */
499
+ export const buildReport = (events, from, to, deployment = null, tz = BEIJING) => {
500
+ const list = Array.isArray(events) ? events : [...events];
501
+ const b = new ReportBuilder(from, to, deployment, tz);
502
+ for (const e of list)
503
+ b.place(e);
504
+ for (const e of list)
505
+ b.add(e);
506
+ return b.build();
507
+ };
508
+ export const dailyTotals = (r, day) => {
509
+ const d = r.days.find((x) => x.day === day);
510
+ return d
511
+ ? { v: 1, deployment: r.deployment, tz: r.tz, ...d, tools: r.tools, funnel: r.funnel }
512
+ : null;
513
+ };
514
+ const sumRecord = (into, add, make) => {
515
+ for (const [k, v] of Object.entries(add)) {
516
+ const cur = into[k] ?? make();
517
+ addRow(cur, v);
518
+ for (const extra of ['isp', 'hosting']) {
519
+ if (extra in v)
520
+ cur[extra] =
521
+ (cur[extra] ?? 0) + v[extra];
522
+ }
523
+ into[k] = cur;
524
+ }
525
+ };
526
+ /**
527
+ * The days' totals summed - for a window reaching past the 13 months of lines.
528
+ * Users are 「每天活跃之和」 and newUsers 「新用户累计」; conversations, the
529
+ * funnel and shoppers are per-day sums too.
530
+ */
531
+ export const sumDays = (daily, from, to, deployment, tz, reason = 'past-13-months') => {
532
+ const total = zero();
533
+ const byPlatform = {};
534
+ const byGeo = {};
535
+ const byShop = {};
536
+ const booking = { ok: 0, fail: 0, checking: 0, why: {} };
537
+ const member = { ...memberCounts(), signUps: 0, why: {}, byGroup: {} };
538
+ const tools = new Map();
539
+ const funnel = { connected: 0, viewed: 0, added: 0, checkout: 0 };
540
+ const account = {
541
+ begun: 0,
542
+ links: 0,
543
+ starts: 0,
544
+ ok: 0,
545
+ failed: 0,
546
+ why: {},
547
+ signOuts: 0,
548
+ orderReads: 0,
549
+ signUps: 0,
550
+ shoppers: 0,
551
+ newShoppers: 0,
552
+ returningShoppers: 0,
553
+ signedInConvs: 0,
554
+ allConvs: 0,
555
+ signedInCheckoutConvs: 0,
556
+ };
557
+ const days = daily
558
+ .filter((d) => d.day >= from && d.day <= to)
559
+ .sort((a, b) => (a.day < b.day ? -1 : 1));
560
+ for (const d of days) {
561
+ addRow(total, d.total);
562
+ sumRecord(byPlatform, d.byPlatform, zero);
563
+ sumRecord(byGeo, d.byGeo, () => ({ ...zero(), isp: 0, hosting: 0 }));
564
+ sumRecord(byShop, d.byShop ?? {}, zero);
565
+ const b = d.booking;
566
+ if (b) {
567
+ booking.ok += b.ok;
568
+ booking.fail += b.fail;
569
+ booking.checking += b.checking;
570
+ for (const [w, n] of Object.entries(b.why))
571
+ booking.why[w] = (booking.why[w] ?? 0) + n;
572
+ }
573
+ const m = d.member;
574
+ if (m) {
575
+ for (const k of ['links', 'ok', 'failed', 'out'])
576
+ member[k] += m[k];
577
+ member.signUps += m.signUps;
578
+ for (const [w, n] of Object.entries(m.why))
579
+ member.why[w] = (member.why[w] ?? 0) + n;
580
+ for (const [g, v] of Object.entries(m.byGroup)) {
581
+ const into = member.byGroup[g] ?? memberCounts();
582
+ for (const k of ['links', 'ok', 'failed', 'out'])
583
+ into[k] += v[k];
584
+ member.byGroup[g] = into;
585
+ }
586
+ }
587
+ for (const x of d.tools) {
588
+ const cur = tools.get(x.tool) ?? { tool: x.tool, calls: 0, failed: 0, p50ms: null };
589
+ cur.calls += x.calls;
590
+ cur.failed += x.failed;
591
+ tools.set(x.tool, cur);
592
+ }
593
+ for (const k of Object.keys(funnel))
594
+ funnel[k] += d.funnel[k];
595
+ for (const [k, v] of Object.entries(d.account)) {
596
+ if (k === 'why') {
597
+ for (const [w, n] of Object.entries(v))
598
+ account.why[w] = (account.why[w] ?? 0) + n;
599
+ }
600
+ else
601
+ account[k] =
602
+ (account[k] ?? 0) + Number(v);
603
+ }
604
+ }
605
+ return {
606
+ v: 1,
607
+ deployment,
608
+ tz: tz.name,
609
+ from,
610
+ to,
611
+ mode: 'daily-sum',
612
+ reason,
613
+ days,
614
+ total: { ...total, returningUsers: 0 },
615
+ byPlatform,
616
+ byGeo,
617
+ byShop,
618
+ tools: [...tools.values()].sort((a, b) => b.calls - a.calls),
619
+ funnel,
620
+ account,
621
+ booking,
622
+ member,
623
+ importedDays: days.filter((d) => d.imported).map((d) => d.day),
624
+ };
625
+ };
626
+ const pct = (a, b) => (b ? `${Math.round((a / b) * 100)}%` : '—');
627
+ const usersText = (r) => r.mode === 'exact'
628
+ ? `用户 ${r.total.users}(新 ${r.total.newUsers}、回头 ${r.total.returningUsers})`
629
+ : `用户:每天活跃之和 ${r.total.users}、新用户累计 ${r.total.newUsers}`;
630
+ /** The one line the daily check prints. */
631
+ export const summaryLine = (r, name) => {
632
+ const t = r.total;
633
+ const plats = Object.entries(r.byPlatform)
634
+ .map(([p, v]) => `${PLATFORM_LABEL[p] ?? p} ${v.convs}`)
635
+ .join(' / ');
636
+ const geos = Object.entries(r.byGeo)
637
+ .slice(0, 6)
638
+ .map(([g, v]) => `${geoLabel(g)} ${v.users || v.convs}`)
639
+ .join(' ');
640
+ const a = r.account;
641
+ const span = r.from === r.to ? r.from : `${r.from}..${r.to}`;
642
+ const parts = [
643
+ r.mode === 'exact' ? `用户 ${t.users}(新 ${t.newUsers})` : usersText(r),
644
+ `对话 ${t.convs}`,
645
+ `连接 ${t.conns}`,
646
+ `调用 ${t.calls}${t.failed ? `(失败 ${t.failed})` : ''}`,
647
+ ...(t.reqs ? [`导入请求 ${t.reqs}`] : []),
648
+ ...(plats ? [`对话按平台 ${plats}`] : []),
649
+ ...(geos ? [`国家 ${geos}`] : []),
650
+ `看酒店 ${r.funnel.viewed}`,
651
+ `提交预订 ${r.funnel.checkout}`,
652
+ `加入行程 ${r.funnel.added}`,
653
+ ...(r.booking.ok || r.booking.fail || r.booking.checking
654
+ ? [`下单 成功 ${r.booking.ok} / 核对中 ${r.booking.checking} / 失败 ${r.booking.fail}`]
655
+ : []),
656
+ ...(a.begun || a.ok ? [`登录 ${a.shoppers} 人(成功 ${a.ok} / 发起 ${a.begun})`] : []),
657
+ ...(r.member.links || r.member.ok ? [`关联集团会员 ${r.member.ok}`] : []),
658
+ ];
659
+ return `[usage] ${name} ${span}(${r.tz === 'UTC' ? 'UTC' : '北京时间'}): ${parts.join(' · ')}`;
660
+ };
661
+ const rowText = (v) => `用户 ${v.users}(新 ${v.newUsers}) · 对话 ${v.convs} · 连接 ${v.conns} · 调用 ${v.calls}${v.failed ? `(失败 ${v.failed})` : ''}${v.reqs ? ` · 导入请求 ${v.reqs}` : ''}${v.cartAdds ? ` · 加入行程 ${v.cartAdds}` : ''}${v.checkouts ? ` · 提交预订 ${v.checkouts}` : ''}${v.signIns ? ` · 登录 ${v.signIns}` : ''}`;
662
+ /** The report as text, for a person at a terminal. */
663
+ export const reportText = (r, name) => {
664
+ const out = [];
665
+ const t = r.total;
666
+ out.push(`${name} 使用统计 ${r.from} .. ${r.to}(${r.tz === 'UTC' ? 'UTC 日' : '北京时间分天'})`);
667
+ if (r.mode === 'daily-sum') {
668
+ out.push(`⚠️ ${r.reason === 'too-many' ? '这个范围里的对话和连接太多,逐条去重会占太多内存' : '这个范围超出逐条记录的 13 个月'},按每天的汇总相加:用户是「每天活跃之和」(同一个人来了几天算几次)和「新用户累计」;对话、漏斗、登录购物者也是每天相加。`);
669
+ }
670
+ out.push(`合计:${usersText(r)} · 对话 ${t.convs} · 连接 ${t.conns}(新开 ${t.inits}) · 调用 ${t.calls}(失败 ${t.failed})${t.reqs ? ` · 导入请求 ${t.reqs}` : ''}`);
671
+ out.push('口径:用户只数平台给了用户编号的(现在只有 ChatGPT,按 X-OpenAI-Subject);对话 = ChatGPT 按 X-OpenAI-Session,别的平台没有对话编号,按 MCP 会话(模型那条连接,卡片的调用算进它所在的对话);连接 = MCP 会话(Mcp-Session-Id),含 ChatGPT 每天扫工具开的会话。', '国家:同一个用户 / 对话只要有一次查到国家,就都算那个国家;只有 ChatGPT 能查(其他平台是平台机房)。');
672
+ if (r.importedDays.length) {
673
+ out.push(`导入,无工具名:${r.importedDays[0]} .. ${r.importedDays.at(-1)} 有导入的请求(来源白名单 shadow 记下的新连接:只有平台和请求数)。`);
674
+ }
675
+ out.push('', '按平台');
676
+ for (const [p, v] of Object.entries(r.byPlatform)) {
677
+ out.push(` ${(PLATFORM_LABEL[p] ?? p).padEnd(8)} ${rowText(v)}`);
678
+ }
679
+ out.push('', '按国家(家庭宽带或手机 / 机房或 VPN 按人数)');
680
+ for (const [g, v] of Object.entries(r.byGeo)) {
681
+ out.push(` ${geoLabel(g).padEnd(8)} ${rowText(v)}${v.isp || v.hosting ? ` · 宽带/手机 ${v.isp} · 机房/VPN ${v.hosting}` : ''}`);
682
+ }
683
+ if (Object.keys(r.byShop).length) {
684
+ out.push('', '按集团(只算说得出是哪一家的事件:单家酒店、预订、关联会员)');
685
+ for (const [g, v] of Object.entries(r.byShop))
686
+ out.push(` ${g.padEnd(10)} ${rowText(v)}`);
687
+ }
688
+ out.push('', '按天 × 平台 × 国家');
689
+ for (const d of r.days) {
690
+ out.push(` ${d.day} ${rowText(d.total)}${d.account.ok || d.account.begun ? ` · 登录 ${d.account.shoppers} 人(成功 ${d.account.ok} / 发起 ${d.account.begun})` : ''}`);
691
+ for (const [k, v] of Object.entries(d.rows)) {
692
+ const [p, g] = k.split('|');
693
+ out.push(` ${PLATFORM_LABEL[p] ?? p} · ${geoLabel(g)}: ${rowText(v)}`);
694
+ }
695
+ }
696
+ out.push('', '工具调用排行');
697
+ if (!r.tools.length)
698
+ out.push(' (没有 —— 导入的日子没有工具名)');
699
+ for (const x of r.tools) {
700
+ out.push(` ${x.tool.padEnd(36)} ${String(x.calls).padStart(6)} 次 · 失败 ${x.failed}${x.p50ms !== null ? ` · 中位 ${x.p50ms}ms` : ''}`);
701
+ }
702
+ const f = r.funnel;
703
+ out.push('', `漏斗(按对话,只算连接器自己记的,不含导入):连接 ${f.connected} → 看酒店 ${f.viewed} (${pct(f.viewed, f.connected)}) → 提交预订 ${f.checkout} (${pct(f.checkout, f.connected)});加入行程单列 ${f.added} (${pct(f.added, f.connected)})`);
704
+ const b = r.booking;
705
+ if (b.ok || b.fail || b.checking) {
706
+ out.push(`下单结果(每笔一行,不记金额):成功 ${b.ok} · 核对中 ${b.checking} · 失败 ${b.fail}${Object.keys(b.why).length
707
+ ? ` (${Object.entries(b.why)
708
+ .map(([k, v]) => `${k} ${v}`)
709
+ .join('、')})`
710
+ : ''}`);
711
+ }
712
+ const a = r.account;
713
+ if (a.begun || a.ok || a.orderReads) {
714
+ out.push('', `账户:发起登录 ${a.begun}(链接 ${a.links}、卡片 ${a.starts})· 成功 ${a.ok}(其中注册 ${a.signUps}) · 失败 / 过期 ${a.failed}${Object.keys(a.why).length
715
+ ? ` (${Object.entries(a.why)
716
+ .map(([k, v]) => `${k} ${v}`)
717
+ .join('、')})`
718
+ : ''} · 成功率 ${pct(a.ok, a.begun)} · 退出 ${a.signOuts} · 登录后查预订 ${a.orderReads}`, ` 登录过的账户 ${a.shoppers}(新 ${a.newShoppers}、回头 ${a.returningShoppers})· 登录过的对话 ${a.signedInConvs} / 全部对话 ${a.allConvs} (${pct(a.signedInConvs, a.allConvs)}) · 登录后提交预订的对话 ${a.signedInCheckoutConvs}`);
719
+ }
720
+ const m = r.member;
721
+ if (m.links || m.ok || m.failed || m.out) {
722
+ out.push('', `关联集团会员:给出关联页 ${m.links} · 关联成功 ${m.ok}(其中新入会 ${m.signUps}) · 失败 ${m.failed}${Object.keys(m.why).length
723
+ ? ` (${Object.entries(m.why)
724
+ .map(([k, v]) => `${k} ${v}`)
725
+ .join('、')})`
726
+ : ''} · 解除 ${m.out}`, ...Object.entries(m.byGroup).map(([g, v]) => ` ${g.padEnd(10)} 关联页 ${v.links} · 成功 ${v.ok} · 失败 ${v.failed} · 解除 ${v.out}`));
727
+ }
728
+ return out.join('\n');
729
+ };
730
+ //# sourceMappingURL=report.js.map