@curless/hotel-storefront 0.90.0 → 0.92.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 (151) hide show
  1. package/dist/account/index.js +1 -1
  2. package/dist/account/index.js.map +1 -1
  3. package/dist/backend.d.ts +4 -4
  4. package/dist/backend.d.ts.map +1 -1
  5. package/dist/backend.js +2 -2
  6. package/dist/card.generated.d.ts +1 -1
  7. package/dist/card.generated.js +2 -2
  8. package/dist/card.generated.js.map +1 -1
  9. package/dist/choices.d.ts.map +1 -1
  10. package/dist/choices.js +1 -1
  11. package/dist/choices.js.map +1 -1
  12. package/dist/cidr.d.ts +2 -16
  13. package/dist/cidr.d.ts.map +1 -1
  14. package/dist/cidr.js +7 -63
  15. package/dist/cidr.js.map +1 -1
  16. package/dist/config.d.ts +14 -14
  17. package/dist/config.d.ts.map +1 -1
  18. package/dist/huazhu/backend.d.ts +3 -8
  19. package/dist/huazhu/backend.d.ts.map +1 -1
  20. package/dist/huazhu/backend.js +35 -68
  21. package/dist/huazhu/backend.js.map +1 -1
  22. package/dist/huazhu/booking-list.d.ts +56 -0
  23. package/dist/huazhu/booking-list.d.ts.map +1 -0
  24. package/dist/huazhu/booking-list.js +87 -0
  25. package/dist/huazhu/booking-list.js.map +1 -0
  26. package/dist/huazhu/make.d.ts.map +1 -1
  27. package/dist/huazhu/make.js +7 -35
  28. package/dist/huazhu/make.js.map +1 -1
  29. package/dist/huazhu/order-words.d.ts +9 -0
  30. package/dist/huazhu/order-words.d.ts.map +1 -0
  31. package/dist/huazhu/order-words.js +58 -0
  32. package/dist/huazhu/order-words.js.map +1 -0
  33. package/dist/huazhu/usage-row.d.ts +5 -0
  34. package/dist/huazhu/usage-row.d.ts.map +1 -0
  35. package/dist/huazhu/usage-row.js +23 -0
  36. package/dist/huazhu/usage-row.js.map +1 -0
  37. package/dist/legal.d.ts.map +1 -1
  38. package/dist/legal.js +9 -11
  39. package/dist/legal.js.map +1 -1
  40. package/dist/not-booked.d.ts +8 -0
  41. package/dist/not-booked.d.ts.map +1 -0
  42. package/dist/not-booked.js +8 -0
  43. package/dist/not-booked.js.map +1 -0
  44. package/dist/present.d.ts +2 -2
  45. package/dist/present.d.ts.map +1 -1
  46. package/dist/server.d.ts +1 -1
  47. package/dist/server.d.ts.map +1 -1
  48. package/dist/server.js +3 -14
  49. package/dist/server.js.map +1 -1
  50. package/dist/source-ranges.d.ts.map +1 -1
  51. package/dist/source-ranges.js +136 -26
  52. package/dist/source-ranges.js.map +1 -1
  53. package/dist/surface.d.ts.map +1 -1
  54. package/dist/surface.js +2 -2
  55. package/dist/surface.js.map +1 -1
  56. package/dist/tools/args.d.ts.map +1 -1
  57. package/dist/tools/booking-answer.d.ts.map +1 -1
  58. package/dist/tools/booking-answer.js +9 -1
  59. package/dist/tools/booking-answer.js.map +1 -1
  60. package/dist/tools/cancel-facts.d.ts.map +1 -1
  61. package/dist/tools/capability-adjust.d.ts +1 -1
  62. package/dist/tools/capability-adjust.d.ts.map +1 -1
  63. package/dist/tools/capability-adjust.js +1 -16
  64. package/dist/tools/capability-adjust.js.map +1 -1
  65. package/dist/tools/get-hotel.d.ts +5 -0
  66. package/dist/tools/get-hotel.d.ts.map +1 -1
  67. package/dist/tools/get-hotel.js +19 -1
  68. package/dist/tools/get-hotel.js.map +1 -1
  69. package/dist/tools/hotel-bookable.d.ts +22 -0
  70. package/dist/tools/hotel-bookable.d.ts.map +1 -0
  71. package/dist/tools/hotel-bookable.js +49 -0
  72. package/dist/tools/hotel-bookable.js.map +1 -0
  73. package/dist/tools/hotel-choices.js +4 -4
  74. package/dist/tools/hotel-choices.js.map +1 -1
  75. package/dist/tools/list-hotels-ask.d.ts.map +1 -1
  76. package/dist/tools/list-hotels-page.d.ts.map +1 -1
  77. package/dist/tools/list-hotels-page.js +13 -2
  78. package/dist/tools/list-hotels-page.js.map +1 -1
  79. package/dist/tools/output-schemas.d.ts +4 -3
  80. package/dist/tools/output-schemas.d.ts.map +1 -1
  81. package/dist/tools/output-schemas.js +4 -3
  82. package/dist/tools/output-schemas.js.map +1 -1
  83. package/dist/tools/paid-lines.d.ts.map +1 -1
  84. package/dist/tools/paid-lines.js +1 -1
  85. package/dist/tools/paid-lines.js.map +1 -1
  86. package/dist/tools/payment-words.d.ts.map +1 -1
  87. package/dist/tools/put-room-line.d.ts.map +1 -1
  88. package/dist/tools/put-room-line.js +27 -1
  89. package/dist/tools/put-room-line.js.map +1 -1
  90. package/dist/tools/quote-card.d.ts.map +1 -1
  91. package/dist/tools/request-words.d.ts +1 -1
  92. package/dist/tools/request-words.d.ts.map +1 -1
  93. package/dist/tools/request-words.js +2 -2
  94. package/dist/tools/request-words.js.map +1 -1
  95. package/dist/tools/room-choices.js +4 -4
  96. package/dist/tools/room-choices.js.map +1 -1
  97. package/dist/tools/tool-defs-browse.js +1 -1
  98. package/dist/tools/tool-defs-browse.js.map +1 -1
  99. package/dist/tools/tool-set.js +1 -1
  100. package/dist/tools/tool-set.js.map +1 -1
  101. package/dist/types.d.ts +9 -9
  102. package/dist/types.d.ts.map +1 -1
  103. package/dist/usage/bookings-import.d.ts.map +1 -1
  104. package/dist/usage/bookings-import.js +1 -2
  105. package/dist/usage/bookings-import.js.map +1 -1
  106. package/dist/usage/boot.d.ts +1 -1
  107. package/dist/usage/boot.d.ts.map +1 -1
  108. package/dist/usage/boot.js +17 -68
  109. package/dist/usage/boot.js.map +1 -1
  110. package/dist/usage/order-words.d.ts +10 -0
  111. package/dist/usage/order-words.d.ts.map +1 -0
  112. package/dist/usage/order-words.js +2 -0
  113. package/dist/usage/order-words.js.map +1 -0
  114. package/dist/usage/platform.d.ts +1 -1
  115. package/dist/usage/platform.d.ts.map +1 -1
  116. package/dist/usage/platform.js.map +1 -1
  117. package/dist/usage/recorder.d.ts +9 -3
  118. package/dist/usage/recorder.d.ts.map +1 -1
  119. package/dist/usage/recorder.js +38 -1
  120. package/dist/usage/recorder.js.map +1 -1
  121. package/dist/usage/shadow-import.d.ts +1 -1
  122. package/dist/usage/shadow-import.d.ts.map +1 -1
  123. package/dist/usage/shadow-import.js +1 -1
  124. package/dist/usage/shadow-import.js.map +1 -1
  125. package/dist/usage-cli.js +7 -8
  126. package/dist/usage-cli.js.map +1 -1
  127. package/package.json +3 -2
  128. package/dist/usage/geo.d.ts +0 -88
  129. package/dist/usage/geo.d.ts.map +0 -1
  130. package/dist/usage/geo.js +0 -396
  131. package/dist/usage/geo.js.map +0 -1
  132. package/dist/usage/maintain.d.ts +0 -26
  133. package/dist/usage/maintain.d.ts.map +0 -1
  134. package/dist/usage/maintain.js +0 -177
  135. package/dist/usage/maintain.js.map +0 -1
  136. package/dist/usage/pseudonym.d.ts +0 -28
  137. package/dist/usage/pseudonym.d.ts.map +0 -1
  138. package/dist/usage/pseudonym.js +0 -73
  139. package/dist/usage/pseudonym.js.map +0 -1
  140. package/dist/usage/range.d.ts +0 -18
  141. package/dist/usage/range.d.ts.map +0 -1
  142. package/dist/usage/range.js +0 -97
  143. package/dist/usage/range.js.map +0 -1
  144. package/dist/usage/report.d.ts +0 -258
  145. package/dist/usage/report.d.ts.map +0 -1
  146. package/dist/usage/report.js +0 -862
  147. package/dist/usage/report.js.map +0 -1
  148. package/dist/usage/store.d.ts +0 -258
  149. package/dist/usage/store.d.ts.map +0 -1
  150. package/dist/usage/store.js +0 -475
  151. package/dist/usage/store.js.map +0 -1
@@ -1,862 +0,0 @@
1
- // Copied from @curless/shopify-storefront src/usage/report.ts (0.77.2, memory ceiling 0.77.3): 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
- import { isOrderLine } from './store.js';
21
- export const PLATFORM_LABEL = {
22
- chatgpt: 'ChatGPT',
23
- claude: 'Claude',
24
- manus: 'Manus',
25
- muse: 'Muse',
26
- other: '其他',
27
- };
28
- export const geoLabel = (g) => g === 'platform' ? '平台机房' : g === 'unknown' ? '未知' : g;
29
- export const BEIJING = { name: 'Asia/Shanghai', offsetMin: 480 };
30
- export const UTC_TZ = { name: 'UTC', offsetMin: 0 };
31
- export const dayIn = (t, tz) => new Date((typeof t === 'number' ? t : Date.parse(t)) + tz.offsetMin * 60_000)
32
- .toISOString()
33
- .slice(0, 10);
34
- export const addDays = (day, n) => {
35
- const d = new Date(`${day}T00:00:00Z`);
36
- d.setUTCDate(d.getUTCDate() + n);
37
- return d.toISOString().slice(0, 10);
38
- };
39
- const bucket = () => ({
40
- users: new Set(),
41
- fresh: new Set(),
42
- convs: new Set(),
43
- conns: new Set(),
44
- inits: 0,
45
- calls: 0,
46
- failed: 0,
47
- reqs: 0,
48
- cartAdds: 0,
49
- checkouts: 0,
50
- signIns: 0,
51
- shoppers: new Set(),
52
- });
53
- /**
54
- * The most ids one report holds - every copy of every id, in every set and map
55
- * it keeps (Held). Taken from @curless/shopify-storefront 0.77.3, which
56
- * measured it on a 64 MB day shaped like a ChatGPT flood (311,000 lines, each
57
- * a new user, conversation and connection, with a country): 45 units a line,
58
- * 40 bytes a unit kept and 57 at the peak. At this ceiling the same day peaks
59
- * at 150 MB, and at 380 MB beside a full `seen.json` - under the CLI's 512 MB
60
- * heap. The hotels add the groups' buckets (`byShop`), a few more units a line.
61
- */
62
- export const MAX_HELD = 3_000_000;
63
- /** What holding something costs, in units of one id in one set. */
64
- const COST = {
65
- /** A day, platform, country, platform × country or group: five sets and an object. */
66
- bucket: 8,
67
- /** A user or conversation's places (ReportBuilder.place): a map of its own. */
68
- who: 4,
69
- /** A tool's row. */
70
- tool: 4,
71
- };
72
- /**
73
- * ⚠️ EVERY COPY COUNTS (总线程 11-13, review of the shopify 0.77.2 copy: the
74
- * hotel usage code still had the same P3). A conversation sits in the window's
75
- * sets and its day's, its platform's, its country's, its platform × country's
76
- * and its group's, the funnel's and the account's; a user in those and in who
77
- * was seen before. Counting the window's ids once each missed the rest.
78
- *
79
- * At the ceiling nothing more goes in: counts that need no id (calls, bookings
80
- * by outcome, sign-ins, linked memberships, cart adds, checkouts) stay exact,
81
- * the distinct ones stop where they were, and the report names the days
82
- * (`capped`).
83
- */
84
- export class Held {
85
- max;
86
- n = 0;
87
- full = false;
88
- constructor(max = MAX_HELD) {
89
- this.max = max;
90
- }
91
- /** An id into a set - refused at the ceiling, so the set stops growing there. */
92
- add(s, v) {
93
- if (s.has(v))
94
- return;
95
- if (this.n >= this.max) {
96
- this.full = true;
97
- return;
98
- }
99
- s.add(v);
100
- this.n++;
101
- }
102
- /** Room for something bigger than one id - false at the ceiling (or at `upTo`, a part of it). */
103
- room(cost, upTo = this.max) {
104
- if (this.n + cost > upTo) {
105
- this.full = true;
106
- return false;
107
- }
108
- this.n += cost;
109
- return true;
110
- }
111
- /**
112
- * Something held whatever the ceiling: a bucket. There are only so many
113
- * (days × platforms × countries × groups) - but a long window's are counted,
114
- * so an exact report over too many of them gives up like one over too many ids.
115
- */
116
- must(cost) {
117
- this.n += cost;
118
- if (this.n > this.max)
119
- this.full = true;
120
- }
121
- }
122
- const put = (b, e, isNew, h) => {
123
- if (e.u)
124
- h.add(b.users, e.u);
125
- if (e.u && isNew)
126
- h.add(b.fresh, e.u);
127
- if (e.c)
128
- h.add(b.convs, e.c);
129
- if (e.k)
130
- h.add(b.conns, e.k);
131
- if (e.e === 'init')
132
- b.inits++;
133
- if (e.e === 'call') {
134
- b.calls++;
135
- if (e.ok === false)
136
- b.failed++;
137
- }
138
- if (e.e === 'req')
139
- b.reqs++;
140
- if (e.e === 'cart_add')
141
- b.cartAdds++;
142
- if (e.e === 'checkout')
143
- b.checkouts++;
144
- if (e.e === 'account' && e.step === 'ok')
145
- b.signIns++;
146
- if (e.e === 'account' && e.s && (e.step === 'ok' || e.step === 'orders'))
147
- h.add(b.shoppers, e.s);
148
- };
149
- const ROW_KEYS = [
150
- 'users',
151
- 'newUsers',
152
- 'convs',
153
- 'conns',
154
- 'inits',
155
- 'calls',
156
- 'failed',
157
- 'reqs',
158
- 'cartAdds',
159
- 'checkouts',
160
- 'signIns',
161
- 'shoppers',
162
- ];
163
- const row = (b) => ({
164
- users: b.users.size,
165
- newUsers: b.fresh.size,
166
- convs: b.convs.size,
167
- conns: b.conns.size,
168
- inits: b.inits,
169
- calls: b.calls,
170
- failed: b.failed,
171
- reqs: b.reqs,
172
- cartAdds: b.cartAdds,
173
- checkouts: b.checkouts,
174
- signIns: b.signIns,
175
- shoppers: b.shoppers.size,
176
- });
177
- const zero = () => Object.fromEntries(ROW_KEYS.map((k) => [k, 0]));
178
- const addRow = (a, b) => {
179
- for (const k of ROW_KEYS)
180
- a[k] += Number(b[k] ?? 0);
181
- return a;
182
- };
183
- class BookingTally {
184
- ok = 0;
185
- fail = 0;
186
- checking = 0;
187
- why = {};
188
- put(e) {
189
- if (e.e !== 'booking')
190
- return;
191
- if (e.res === 'ok')
192
- this.ok++;
193
- else if (e.res === 'checking')
194
- this.checking++;
195
- else if (e.res === 'fail') {
196
- this.fail++;
197
- const k = e.why ?? 'other';
198
- this.why[k] = (this.why[k] ?? 0) + 1;
199
- }
200
- }
201
- summary() {
202
- return { ok: this.ok, fail: this.fail, checking: this.checking, why: this.why };
203
- }
204
- }
205
- const memberCounts = () => ({ links: 0, ok: 0, failed: 0, out: 0 });
206
- class MemberTally {
207
- all = memberCounts();
208
- signUps = 0;
209
- why = {};
210
- byGroup = new Map();
211
- put(e) {
212
- if (e.e !== 'member')
213
- return;
214
- const field = e.step === 'link'
215
- ? 'links'
216
- : e.step === 'ok'
217
- ? 'ok'
218
- : e.step === 'fail'
219
- ? 'failed'
220
- : e.step === 'out'
221
- ? 'out'
222
- : null;
223
- if (!field)
224
- return;
225
- this.all[field]++;
226
- if (e.shop) {
227
- const g = this.byGroup.get(e.shop) ?? memberCounts();
228
- g[field]++;
229
- this.byGroup.set(e.shop, g);
230
- }
231
- if (e.step === 'ok' && e.signUp)
232
- this.signUps++;
233
- if (e.step === 'fail') {
234
- const k = e.why ?? 'other';
235
- this.why[k] = (this.why[k] ?? 0) + 1;
236
- }
237
- }
238
- summary() {
239
- return {
240
- ...this.all,
241
- signUps: this.signUps,
242
- why: this.why,
243
- byGroup: Object.fromEntries([...this.byGroup.entries()].sort(([a], [b]) => (a < b ? -1 : 1))),
244
- };
245
- }
246
- }
247
- const median = (xs) => {
248
- if (!xs.length)
249
- return null;
250
- const s = [...xs].sort((a, b) => a - b);
251
- return s[Math.floor(s.length / 2)] ?? null;
252
- };
253
- class AccountTally {
254
- h;
255
- constructor(h) {
256
- this.h = h;
257
- }
258
- links = 0;
259
- starts = 0;
260
- ok = 0;
261
- failed = 0;
262
- why = {};
263
- signOuts = 0;
264
- orderReads = 0;
265
- signUps = 0;
266
- shoppers = new Set();
267
- signedIn = new Set();
268
- signedInCheckout = new Set();
269
- convs = new Set();
270
- put(e) {
271
- const h = this.h;
272
- if (e.c)
273
- h.add(this.convs, e.c);
274
- if (e.e === 'checkout' && e.c && (e.signedIn || this.signedIn.has(e.c))) {
275
- h.add(this.signedInCheckout, e.c);
276
- }
277
- if (e.e !== 'account')
278
- return;
279
- if (e.step === 'link')
280
- this.links++;
281
- if (e.step === 'start')
282
- this.starts++;
283
- if (e.step === 'ok') {
284
- this.ok++;
285
- if (e.signUp)
286
- this.signUps++;
287
- if (e.c)
288
- h.add(this.signedIn, e.c);
289
- }
290
- if (e.step === 'fail') {
291
- this.failed++;
292
- const k = e.why ?? 'other';
293
- this.why[k] = (this.why[k] ?? 0) + 1;
294
- }
295
- if (e.step === 'out')
296
- this.signOuts++;
297
- if (e.step === 'orders') {
298
- this.orderReads++;
299
- if (e.c)
300
- h.add(this.signedIn, e.c);
301
- }
302
- if (e.s && (e.step === 'ok' || e.step === 'orders'))
303
- h.add(this.shoppers, e.s);
304
- }
305
- summary(seenBefore) {
306
- const newShoppers = [...this.shoppers].filter((s) => !seenBefore.has(s)).length;
307
- return {
308
- begun: this.links + this.starts,
309
- links: this.links,
310
- starts: this.starts,
311
- ok: this.ok,
312
- failed: this.failed,
313
- why: this.why,
314
- signOuts: this.signOuts,
315
- orderReads: this.orderReads,
316
- signUps: this.signUps,
317
- shoppers: this.shoppers.size,
318
- newShoppers,
319
- returningShoppers: this.shoppers.size - newShoppers,
320
- signedInConvs: this.signedIn.size,
321
- allConvs: this.convs.size,
322
- signedInCheckoutConvs: this.signedInCheckout.size,
323
- };
324
- }
325
- }
326
- const geoRow = (v) => ({
327
- ...row(v.b),
328
- isp: v.isp.size,
329
- hosting: v.hosting.size,
330
- });
331
- const putGeo = (m, e, isNew, h) => {
332
- let gb = m.get(e.g);
333
- if (!gb) {
334
- h.must(COST.bucket);
335
- gb = { b: bucket(), isp: new Set(), hosting: new Set() };
336
- m.set(e.g, gb);
337
- }
338
- put(gb.b, e, isNew, h);
339
- const who = e.u ?? e.c;
340
- if (who && e.n === 'isp')
341
- h.add(gb.isp, who);
342
- if (who && e.n === 'hosting')
343
- h.add(gb.hosting, who);
344
- };
345
- const putIn = (m, key, e, isNew, h) => {
346
- let b = m.get(key);
347
- if (!b) {
348
- h.must(COST.bucket);
349
- b = bucket();
350
- m.set(key, b);
351
- }
352
- put(b, e, isNew, h);
353
- };
354
- const sortedRows = (m) => Object.fromEntries([...m.entries()].sort(([a], [b]) => (a < b ? -1 : 1)).map(([k, b]) => [k, row(b)]));
355
- const platformRows = (m) => Object.fromEntries([
356
- ...PLATFORMS.filter((p) => m.has(p)),
357
- ...[...m.keys()].filter((p) => !PLATFORMS.includes(p)),
358
- ].map((p) => [p, row(m.get(p))]));
359
- const geoRows = (m) => Object.fromEntries([...m.entries()]
360
- .sort(([, a], [, b]) => b.b.users.size + b.b.convs.size - a.b.users.size - a.b.convs.size)
361
- .map(([g, v]) => [g, geoRow(v)]));
362
- /**
363
- * Sums events fed one at a time - so neither the maintenance nor the CLI ever
364
- * holds a window's lines in memory, only its counts and distinct pseudonyms.
365
- *
366
- * Two passes over the same lines: `place` (who was where - see knownPlaces),
367
- * then `add`. Lines before `from` only say who was seen before (new versus
368
- * returning) - unless a `seen` index answers that instead.
369
- */
370
- export class ReportBuilder {
371
- from;
372
- to;
373
- deployment;
374
- tz;
375
- seen;
376
- tally = new Map();
377
- places = null;
378
- seenUsers = new Set();
379
- shoppersBefore = new Set();
380
- days = new Map();
381
- total = bucket();
382
- byPlatform = new Map();
383
- byGeo = new Map();
384
- byShop = new Map();
385
- booking = new BookingTally();
386
- member = new MemberTally();
387
- tools = new Map();
388
- held;
389
- account;
390
- imported = new Set();
391
- funnel = {
392
- connected: new Set(),
393
- viewed: new Set(),
394
- added: new Set(),
395
- checkout: new Set(),
396
- };
397
- constructor(from, to, deployment = null, tz = BEIJING, seen, maxHeld = MAX_HELD) {
398
- this.from = from;
399
- this.to = to;
400
- this.deployment = deployment;
401
- this.tz = tz;
402
- this.seen = seen;
403
- this.held = new Held(maxHeld);
404
- this.account = new AccountTally(this.held);
405
- }
406
- /** What it holds, in ids (Held) - every copy counted. */
407
- get size() {
408
- return this.held.n;
409
- }
410
- /** At the ceiling: its distinct counts stopped growing - an exact report is no longer one. */
411
- get full() {
412
- return this.held.full;
413
- }
414
- mine(e) {
415
- return (!this.deployment || e.d === this.deployment) && !isOrderLine(e);
416
- }
417
- /**
418
- * First pass: where each user and conversation was seen, within the window.
419
- * A request that said nothing of where is then counted where its user (or,
420
- * with no user, its conversation) most often was - one request without an
421
- * address does not put them in 「未知」 too (总线程 11-13: 5 ChatGPT users
422
- * were in both).
423
- */
424
- place(e) {
425
- if (!this.mine(e) || e.g === 'unknown' || e.g === 'platform')
426
- return;
427
- const day = dayIn(e.t, this.tz);
428
- if (day < this.from || day > this.to)
429
- return;
430
- for (const who of [e.u && `u:${e.u}`, e.c && `c:${e.c}`]) {
431
- if (!who)
432
- continue;
433
- let m = this.tally.get(who);
434
- if (!m) {
435
- // A third of the ceiling at most: the counting pass needs the rest,
436
- // or a flooded day would come out as no one at all.
437
- if (!this.held.room(COST.who, this.held.max / 3))
438
- continue;
439
- m = new Map();
440
- this.tally.set(who, m);
441
- }
442
- let hit = m.get(e.g);
443
- if (!hit) {
444
- if (!this.held.room(1, this.held.max / 3))
445
- continue;
446
- hit = { count: 0, ...(e.n ? { n: e.n } : {}) };
447
- m.set(e.g, hit);
448
- }
449
- hit.count++;
450
- }
451
- }
452
- placeOf(e) {
453
- if (e.g !== 'unknown')
454
- return e;
455
- if (!this.places) {
456
- // Takes the tally's place (and its count in Held): each entry leaves
457
- // the tally as it goes into the places.
458
- this.places = new Map();
459
- for (const [who, m] of this.tally) {
460
- const [g, best] = [...m.entries()].sort((a, b) => b[1].count - a[1].count)[0];
461
- this.places.set(who, { g, ...(best.n ? { n: best.n } : {}) });
462
- this.tally.delete(who);
463
- }
464
- }
465
- const hit = (e.u && this.places.get(`u:${e.u}`)) || (e.c && this.places.get(`c:${e.c}`));
466
- return hit ? { ...e, g: hit.g, ...(hit.n ? { n: hit.n } : {}) } : e;
467
- }
468
- /**
469
- * A user seen - true if seen before. Held like every other id: with no
470
- * `seen.json` that is every user of the 13 months before the window.
471
- */
472
- userSeen(u) {
473
- const before = this.seenUsers.has(u);
474
- this.held.add(this.seenUsers, u);
475
- return before;
476
- }
477
- shopperSeen(s) {
478
- this.held.add(this.shoppersBefore, s);
479
- }
480
- /** Second pass: count it. */
481
- add(raw) {
482
- if (!this.mine(raw))
483
- return;
484
- const day = dayIn(raw.t, this.tz);
485
- if (day > this.to)
486
- return;
487
- if (day < this.from) {
488
- if (!this.seen) {
489
- if (raw.u)
490
- this.userSeen(raw.u);
491
- if (raw.s)
492
- this.shopperSeen(raw.s);
493
- }
494
- return;
495
- }
496
- const e = this.placeOf(raw);
497
- const h = this.held;
498
- let d = this.days.get(day);
499
- if (!d) {
500
- h.must(COST.bucket);
501
- d = {
502
- total: bucket(),
503
- byPlatform: new Map(),
504
- byGeo: new Map(),
505
- rows: new Map(),
506
- byShop: new Map(),
507
- account: new AccountTally(h),
508
- booking: new BookingTally(),
509
- member: new MemberTally(),
510
- imported: false,
511
- };
512
- this.days.set(day, d);
513
- }
514
- if (e.src) {
515
- this.imported.add(day);
516
- d.imported = true;
517
- }
518
- let isNew = false;
519
- if (e.u) {
520
- if (this.seen) {
521
- const first = this.seen.userFirst(e.u);
522
- isNew = !first || first >= day;
523
- }
524
- else {
525
- isNew = !this.userSeen(e.u);
526
- }
527
- }
528
- if (e.s && this.seen) {
529
- const first = this.seen.shopperFirst(e.s);
530
- if (first && first < this.from)
531
- this.shopperSeen(e.s);
532
- }
533
- put(d.total, e, isNew, h);
534
- putIn(d.byPlatform, e.p, e, isNew, h);
535
- putGeo(d.byGeo, e, isNew, h);
536
- putIn(d.rows, `${e.p}|${e.g}`, e, isNew, h);
537
- d.account.put(e);
538
- d.booking.put(e);
539
- d.member.put(e);
540
- if (e.shop)
541
- putIn(d.byShop, e.shop, e, isNew, h);
542
- put(this.total, e, isNew, h);
543
- putIn(this.byPlatform, e.p, e, isNew, h);
544
- putGeo(this.byGeo, e, isNew, h);
545
- if (e.shop)
546
- putIn(this.byShop, e.shop, e, isNew, h);
547
- this.account.put(e);
548
- this.booking.put(e);
549
- this.member.put(e);
550
- if (e.e === 'call' && e.tool) {
551
- let t = this.tools.get(e.tool);
552
- if (!t && h.room(COST.tool)) {
553
- t = { calls: 0, failed: 0, ms: [] };
554
- this.tools.set(e.tool, t);
555
- }
556
- if (t) {
557
- t.calls++;
558
- if (e.ok === false)
559
- t.failed++;
560
- // A median from at most the first 10,000 timings: enough, and bounded.
561
- if (typeof e.ms === 'number' && t.ms.length < 10_000 && h.room(1))
562
- t.ms.push(e.ms);
563
- }
564
- }
565
- // Recorded days only: an imported request has no tool, so its conversation
566
- // could never reach the later steps and would only thin the rates.
567
- if (e.c && !e.src) {
568
- const f = this.funnel;
569
- h.add(f.connected, e.c);
570
- if (e.e === 'call' && (e.r === 'browse' || e.r === 'product'))
571
- h.add(f.viewed, e.c);
572
- if (e.e === 'cart_add')
573
- h.add(f.added, e.c);
574
- if (e.e === 'checkout')
575
- h.add(f.checkout, e.c);
576
- }
577
- }
578
- build() {
579
- const t = row(this.total);
580
- return {
581
- v: 1,
582
- deployment: this.deployment,
583
- tz: this.tz.name,
584
- from: this.from,
585
- to: this.to,
586
- mode: 'exact',
587
- days: [...this.days.entries()]
588
- .sort(([a], [b]) => (a < b ? -1 : 1))
589
- .map(([day, d]) => ({
590
- day,
591
- total: row(d.total),
592
- byPlatform: platformRows(d.byPlatform),
593
- byGeo: geoRows(d.byGeo),
594
- rows: sortedRows(d.rows),
595
- byShop: sortedRows(d.byShop),
596
- account: d.account.summary(new Set()),
597
- booking: d.booking.summary(),
598
- member: d.member.summary(),
599
- imported: d.imported,
600
- ...(this.held.full ? { capped: true } : {}),
601
- })),
602
- total: { ...t, returningUsers: t.users - t.newUsers },
603
- byPlatform: platformRows(this.byPlatform),
604
- byGeo: geoRows(this.byGeo),
605
- byShop: sortedRows(this.byShop),
606
- tools: [...this.tools.entries()]
607
- .sort(([, a], [, b]) => b.calls - a.calls)
608
- .map(([tool, x]) => ({ tool, calls: x.calls, failed: x.failed, p50ms: median(x.ms) })),
609
- funnel: {
610
- connected: this.funnel.connected.size,
611
- viewed: this.funnel.viewed.size,
612
- added: this.funnel.added.size,
613
- checkout: this.funnel.checkout.size,
614
- },
615
- account: this.account.summary(this.shoppersBefore),
616
- booking: this.booking.summary(),
617
- member: this.member.summary(),
618
- importedDays: [...this.imported].sort(),
619
- ...(this.held.full ? { capped: [...this.days.keys()].sort() } : {}),
620
- };
621
- }
622
- }
623
- /** The whole report from events in hand - tests, and small windows. */
624
- export const buildReport = (events, from, to, deployment = null, tz = BEIJING) => {
625
- const list = Array.isArray(events) ? events : [...events];
626
- const b = new ReportBuilder(from, to, deployment, tz);
627
- for (const e of list)
628
- b.place(e);
629
- for (const e of list)
630
- b.add(e);
631
- return b.build();
632
- };
633
- export const dailyTotals = (r, day) => {
634
- const d = r.days.find((x) => x.day === day);
635
- return d
636
- ? { v: 1, deployment: r.deployment, tz: r.tz, ...d, tools: r.tools, funnel: r.funnel }
637
- : null;
638
- };
639
- const sumRecord = (into, add, make) => {
640
- for (const [k, v] of Object.entries(add)) {
641
- const cur = into[k] ?? make();
642
- addRow(cur, v);
643
- for (const extra of ['isp', 'hosting']) {
644
- if (extra in v)
645
- cur[extra] =
646
- (cur[extra] ?? 0) + v[extra];
647
- }
648
- into[k] = cur;
649
- }
650
- };
651
- /**
652
- * The days' totals summed - for a window reaching past the 13 months of lines.
653
- * Users are 「每天活跃之和」 and newUsers 「新用户累计」; conversations, the
654
- * funnel and shoppers are per-day sums too.
655
- */
656
- export const sumDays = (daily, from, to, deployment, tz, reason = 'past-13-months') => {
657
- const total = zero();
658
- const byPlatform = {};
659
- const byGeo = {};
660
- const byShop = {};
661
- const booking = { ok: 0, fail: 0, checking: 0, why: {} };
662
- const member = { ...memberCounts(), signUps: 0, why: {}, byGroup: {} };
663
- const tools = new Map();
664
- const funnel = { connected: 0, viewed: 0, added: 0, checkout: 0 };
665
- const account = {
666
- begun: 0,
667
- links: 0,
668
- starts: 0,
669
- ok: 0,
670
- failed: 0,
671
- why: {},
672
- signOuts: 0,
673
- orderReads: 0,
674
- signUps: 0,
675
- shoppers: 0,
676
- newShoppers: 0,
677
- returningShoppers: 0,
678
- signedInConvs: 0,
679
- allConvs: 0,
680
- signedInCheckoutConvs: 0,
681
- };
682
- const days = daily
683
- .filter((d) => d.day >= from && d.day <= to)
684
- .sort((a, b) => (a.day < b.day ? -1 : 1));
685
- for (const d of days) {
686
- addRow(total, d.total);
687
- sumRecord(byPlatform, d.byPlatform, zero);
688
- sumRecord(byGeo, d.byGeo, () => ({ ...zero(), isp: 0, hosting: 0 }));
689
- sumRecord(byShop, d.byShop ?? {}, zero);
690
- const b = d.booking;
691
- if (b) {
692
- booking.ok += b.ok;
693
- booking.fail += b.fail;
694
- booking.checking += b.checking;
695
- for (const [w, n] of Object.entries(b.why))
696
- booking.why[w] = (booking.why[w] ?? 0) + n;
697
- }
698
- const m = d.member;
699
- if (m) {
700
- for (const k of ['links', 'ok', 'failed', 'out'])
701
- member[k] += m[k];
702
- member.signUps += m.signUps;
703
- for (const [w, n] of Object.entries(m.why))
704
- member.why[w] = (member.why[w] ?? 0) + n;
705
- for (const [g, v] of Object.entries(m.byGroup)) {
706
- const into = member.byGroup[g] ?? memberCounts();
707
- for (const k of ['links', 'ok', 'failed', 'out'])
708
- into[k] += v[k];
709
- member.byGroup[g] = into;
710
- }
711
- }
712
- for (const x of d.tools) {
713
- const cur = tools.get(x.tool) ?? { tool: x.tool, calls: 0, failed: 0, p50ms: null };
714
- cur.calls += x.calls;
715
- cur.failed += x.failed;
716
- tools.set(x.tool, cur);
717
- }
718
- for (const k of Object.keys(funnel))
719
- funnel[k] += d.funnel[k];
720
- for (const [k, v] of Object.entries(d.account)) {
721
- if (k === 'why') {
722
- for (const [w, n] of Object.entries(v))
723
- account.why[w] = (account.why[w] ?? 0) + n;
724
- }
725
- else
726
- account[k] =
727
- (account[k] ?? 0) + Number(v);
728
- }
729
- }
730
- return {
731
- v: 1,
732
- deployment,
733
- tz: tz.name,
734
- from,
735
- to,
736
- mode: 'daily-sum',
737
- reason,
738
- days,
739
- total: { ...total, returningUsers: 0 },
740
- byPlatform,
741
- byGeo,
742
- byShop,
743
- tools: [...tools.values()].sort((a, b) => b.calls - a.calls),
744
- funnel,
745
- account,
746
- booking,
747
- member,
748
- importedDays: days.filter((d) => d.imported).map((d) => d.day),
749
- ...(days.some((d) => d.capped)
750
- ? { capped: days.filter((d) => d.capped).map((d) => d.day) }
751
- : {}),
752
- };
753
- };
754
- const pct = (a, b) => (b ? `${Math.round((a / b) * 100)}%` : '—');
755
- const usersText = (r) => r.mode === 'exact'
756
- ? `用户 ${r.total.users}(新 ${r.total.newUsers}、回头 ${r.total.returningUsers})`
757
- : `用户:每天活跃之和 ${r.total.users}、新用户累计 ${r.total.newUsers}`;
758
- /** The one line the daily check prints. */
759
- export const summaryLine = (r, name) => {
760
- const t = r.total;
761
- const plats = Object.entries(r.byPlatform)
762
- .map(([p, v]) => `${PLATFORM_LABEL[p] ?? p} ${v.convs}`)
763
- .join(' / ');
764
- const geos = Object.entries(r.byGeo)
765
- .slice(0, 6)
766
- .map(([g, v]) => `${geoLabel(g)} ${v.users || v.convs}`)
767
- .join(' ');
768
- const a = r.account;
769
- const span = r.from === r.to ? r.from : `${r.from}..${r.to}`;
770
- const parts = [
771
- r.mode === 'exact' ? `用户 ${t.users}(新 ${t.newUsers})` : usersText(r),
772
- `对话 ${t.convs}`,
773
- `连接 ${t.conns}`,
774
- `调用 ${t.calls}${t.failed ? `(失败 ${t.failed})` : ''}`,
775
- ...(t.reqs ? [`导入请求 ${t.reqs}`] : []),
776
- ...(plats ? [`对话按平台 ${plats}`] : []),
777
- ...(geos ? [`国家 ${geos}`] : []),
778
- `看酒店 ${r.funnel.viewed}`,
779
- `提交预订 ${r.funnel.checkout}`,
780
- `加入行程 ${r.funnel.added}`,
781
- ...(r.booking.ok || r.booking.fail || r.booking.checking
782
- ? [`下单 成功 ${r.booking.ok} / 核对中 ${r.booking.checking} / 失败 ${r.booking.fail}`]
783
- : []),
784
- ...(a.begun || a.ok ? [`登录 ${a.shoppers} 人(成功 ${a.ok} / 发起 ${a.begun})`] : []),
785
- ...(r.member.links || r.member.ok ? [`关联集团会员 ${r.member.ok}`] : []),
786
- ...(r.capped?.length ? [`⚠️ ${r.capped.join('、')} 人数只数到上限`] : []),
787
- ];
788
- return `[usage] ${name} ${span}(${r.tz === 'UTC' ? 'UTC' : '北京时间'}): ${parts.join(' · ')}`;
789
- };
790
- 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}` : ''}`;
791
- /** The report as text, for a person at a terminal. */
792
- export const reportText = (r, name) => {
793
- const out = [];
794
- const t = r.total;
795
- out.push(`${name} 使用统计 ${r.from} .. ${r.to}(${r.tz === 'UTC' ? 'UTC 日' : '北京时间分天'})`);
796
- if (r.mode === 'daily-sum') {
797
- out.push(`⚠️ ${r.reason === 'too-many' ? '这个范围里的对话和连接太多,逐条去重会占太多内存' : '这个范围超出逐条记录的 13 个月'},按每天的汇总相加:用户是「每天活跃之和」(同一个人来了几天算几次)和「新用户累计」;对话、漏斗、登录购物者也是每天相加。`);
798
- }
799
- if (r.capped?.length) {
800
- out.push(`⚠️ ${r.capped.join('、')} 的对话和连接太多:用户、对话、连接只数到上限为止,实际更多;工具调用、提交预订、加入行程、下单结果、登录、关联会员这些次数照常是全数。`);
801
- }
802
- out.push(`合计:${usersText(r)} · 对话 ${t.convs} · 连接 ${t.conns}(新开 ${t.inits}) · 调用 ${t.calls}(失败 ${t.failed})${t.reqs ? ` · 导入请求 ${t.reqs}` : ''}`);
803
- out.push('口径:用户只数平台给了用户编号的(现在只有 ChatGPT,按 X-OpenAI-Subject);对话 = ChatGPT 按 X-OpenAI-Session,别的平台没有对话编号,按 MCP 会话(模型那条连接,卡片的调用算进它所在的对话);连接 = MCP 会话(Mcp-Session-Id),含 ChatGPT 每天扫工具开的会话。', '国家:同一个用户 / 对话只要有一次查到国家,就都算那个国家;只有 ChatGPT 能查(其他平台是平台机房)。');
804
- if (r.importedDays.length) {
805
- out.push(`导入,无工具名:${r.importedDays[0]} .. ${r.importedDays.at(-1)} 有导入的请求(来源白名单 shadow 记下的新连接:只有平台和请求数)。`);
806
- }
807
- out.push('', '按平台');
808
- for (const [p, v] of Object.entries(r.byPlatform)) {
809
- out.push(` ${(PLATFORM_LABEL[p] ?? p).padEnd(8)} ${rowText(v)}`);
810
- }
811
- out.push('', '按国家(家庭宽带或手机 / 机房或 VPN 按人数)');
812
- for (const [g, v] of Object.entries(r.byGeo)) {
813
- out.push(` ${geoLabel(g).padEnd(8)} ${rowText(v)}${v.isp || v.hosting ? ` · 宽带/手机 ${v.isp} · 机房/VPN ${v.hosting}` : ''}`);
814
- }
815
- if (Object.keys(r.byShop).length) {
816
- out.push('', '按集团(只算说得出是哪一家的事件:单家酒店、预订、关联会员)');
817
- for (const [g, v] of Object.entries(r.byShop))
818
- out.push(` ${g.padEnd(10)} ${rowText(v)}`);
819
- }
820
- out.push('', '按天 × 平台 × 国家');
821
- for (const d of r.days) {
822
- out.push(` ${d.day} ${rowText(d.total)}${d.account.ok || d.account.begun ? ` · 登录 ${d.account.shoppers} 人(成功 ${d.account.ok} / 发起 ${d.account.begun})` : ''}`);
823
- for (const [k, v] of Object.entries(d.rows)) {
824
- const [p, g] = k.split('|');
825
- out.push(` ${PLATFORM_LABEL[p] ?? p} · ${geoLabel(g)}: ${rowText(v)}`);
826
- }
827
- }
828
- out.push('', '工具调用排行');
829
- if (!r.tools.length)
830
- out.push(' (没有 —— 导入的日子没有工具名)');
831
- for (const x of r.tools) {
832
- out.push(` ${x.tool.padEnd(36)} ${String(x.calls).padStart(6)} 次 · 失败 ${x.failed}${x.p50ms !== null ? ` · 中位 ${x.p50ms}ms` : ''}`);
833
- }
834
- const f = r.funnel;
835
- 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)})`);
836
- const b = r.booking;
837
- if (b.ok || b.fail || b.checking) {
838
- out.push(`下单结果(每笔一行,不记金额):成功 ${b.ok} · 核对中 ${b.checking} · 失败 ${b.fail}${Object.keys(b.why).length
839
- ? ` (${Object.entries(b.why)
840
- .map(([k, v]) => `${k} ${v}`)
841
- .join('、')})`
842
- : ''}`);
843
- }
844
- const a = r.account;
845
- if (a.begun || a.ok || a.orderReads) {
846
- out.push('', `账户:发起登录 ${a.begun}(链接 ${a.links}、卡片 ${a.starts})· 成功 ${a.ok}(其中注册 ${a.signUps}) · 失败 / 过期 ${a.failed}${Object.keys(a.why).length
847
- ? ` (${Object.entries(a.why)
848
- .map(([k, v]) => `${k} ${v}`)
849
- .join('、')})`
850
- : ''} · 成功率 ${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}`);
851
- }
852
- const m = r.member;
853
- if (m.links || m.ok || m.failed || m.out) {
854
- out.push('', `关联集团会员:给出关联页 ${m.links} · 关联成功 ${m.ok}(其中新入会 ${m.signUps}) · 失败 ${m.failed}${Object.keys(m.why).length
855
- ? ` (${Object.entries(m.why)
856
- .map(([k, v]) => `${k} ${v}`)
857
- .join('、')})`
858
- : ''} · 解除 ${m.out}`, ...Object.entries(m.byGroup).map(([g, v]) => ` ${g.padEnd(10)} 关联页 ${v.links} · 成功 ${v.ok} · 失败 ${v.failed} · 解除 ${v.out}`));
859
- }
860
- return out.join('\n');
861
- };
862
- //# sourceMappingURL=report.js.map