@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.
- package/README.md +11 -0
- package/dist/account/index.d.ts +21 -0
- package/dist/account/index.d.ts.map +1 -1
- package/dist/account/index.js +63 -4
- package/dist/account/index.js.map +1 -1
- package/dist/config.d.ts +12 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js.map +1 -1
- package/dist/huazhu/client.d.ts +3 -1
- package/dist/huazhu/client.d.ts.map +1 -1
- package/dist/huazhu/client.js +26 -0
- package/dist/huazhu/client.js.map +1 -1
- package/dist/huazhu/english.d.ts.map +1 -1
- package/dist/huazhu/english.js +11 -0
- package/dist/huazhu/english.js.map +1 -1
- package/dist/otaless/backend.d.ts.map +1 -1
- package/dist/otaless/backend.js +17 -6
- package/dist/otaless/backend.js.map +1 -1
- package/dist/otaless/booking.d.ts +11 -4
- package/dist/otaless/booking.d.ts.map +1 -1
- package/dist/otaless/booking.js +95 -11
- package/dist/otaless/booking.js.map +1 -1
- package/dist/otaless/fake.d.ts +7 -2
- package/dist/otaless/fake.d.ts.map +1 -1
- package/dist/otaless/fake.js +4 -2
- package/dist/otaless/fake.js.map +1 -1
- package/dist/otaless/places.d.ts.map +1 -1
- package/dist/otaless/places.js +16 -2
- package/dist/otaless/places.js.map +1 -1
- package/dist/regions.d.ts.map +1 -1
- package/dist/regions.js +25 -2
- package/dist/regions.js.map +1 -1
- package/dist/server.d.ts +4 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +280 -13
- package/dist/server.js.map +1 -1
- package/dist/source-allowlist.d.ts +9 -0
- package/dist/source-allowlist.d.ts.map +1 -1
- package/dist/source-allowlist.js +23 -1
- package/dist/source-allowlist.js.map +1 -1
- package/dist/state.d.ts +25 -2
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +88 -15
- package/dist/state.js.map +1 -1
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +61 -2
- package/dist/tools.js.map +1 -1
- package/dist/usage/boot.d.ts +32 -0
- package/dist/usage/boot.d.ts.map +1 -0
- package/dist/usage/boot.js +140 -0
- package/dist/usage/boot.js.map +1 -0
- package/dist/usage/geo.d.ts +48 -0
- package/dist/usage/geo.d.ts.map +1 -0
- package/dist/usage/geo.js +347 -0
- package/dist/usage/geo.js.map +1 -0
- package/dist/usage/maintain.d.ts +26 -0
- package/dist/usage/maintain.d.ts.map +1 -0
- package/dist/usage/maintain.js +177 -0
- package/dist/usage/maintain.js.map +1 -0
- package/dist/usage/platform.d.ts +69 -0
- package/dist/usage/platform.d.ts.map +1 -0
- package/dist/usage/platform.js +124 -0
- package/dist/usage/platform.js.map +1 -0
- package/dist/usage/pseudonym.d.ts +28 -0
- package/dist/usage/pseudonym.d.ts.map +1 -0
- package/dist/usage/pseudonym.js +73 -0
- package/dist/usage/pseudonym.js.map +1 -0
- package/dist/usage/range.d.ts +24 -0
- package/dist/usage/range.d.ts.map +1 -0
- package/dist/usage/range.js +102 -0
- package/dist/usage/range.js.map +1 -0
- package/dist/usage/recorder.d.ts +115 -0
- package/dist/usage/recorder.d.ts.map +1 -0
- package/dist/usage/recorder.js +227 -0
- package/dist/usage/recorder.js.map +1 -0
- package/dist/usage/report.d.ts +207 -0
- package/dist/usage/report.d.ts.map +1 -0
- package/dist/usage/report.js +730 -0
- package/dist/usage/report.js.map +1 -0
- package/dist/usage/shadow-import.d.ts +28 -0
- package/dist/usage/shadow-import.d.ts.map +1 -0
- package/dist/usage/shadow-import.js +107 -0
- package/dist/usage/shadow-import.js.map +1 -0
- package/dist/usage/store.d.ts +215 -0
- package/dist/usage/store.d.ts.map +1 -0
- package/dist/usage/store.js +430 -0
- package/dist/usage/store.js.map +1 -0
- package/dist/usage-cli.d.ts +3 -0
- package/dist/usage-cli.d.ts.map +1 -0
- package/dist/usage-cli.js +152 -0
- package/dist/usage-cli.js.map +1 -0
- package/package.json +3 -2
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// The daily chores: each finished day's totals into `daily/`, kept for good,
|
|
2
|
+
// then event lines older than 13 months deleted - totals first, always.
|
|
3
|
+
// Totals hold counts and tool names only - no pseudonym - so keeping them
|
|
4
|
+
// forever keeps no one.
|
|
5
|
+
//
|
|
6
|
+
// ⚠️ ONLY THE DAYS THAT NEED IT, A LINE AT A TIME (security review of 0.77.0,
|
|
7
|
+
// P1). The first version read all 13 months into the server process every six
|
|
8
|
+
// hours: a million lines held requests for 2.5 s and took 850 MB, a few million
|
|
9
|
+
// would crash it, and the restart would do it again. Now a day's totals read
|
|
10
|
+
// that day's two files - streamed, twice (report.ts ReportBuilder) - and who is
|
|
11
|
+
// new comes from `seen.json`, the day each pseudonym was first and last seen,
|
|
12
|
+
// instead of from every earlier line. A day's file is capped too (store.ts).
|
|
13
|
+
//
|
|
14
|
+
// ⚠️ A TOTAL'S DAY IS A BEIJING DAY (report.ts), and the event files are UTC
|
|
15
|
+
// days: Beijing day D is UTC files D-1 (from 16:00) and D (to 16:00). A day's
|
|
16
|
+
// totals are rewritten when either file changed after them - an import that
|
|
17
|
+
// rewrote an old day, say - but only while both files are still kept; a day
|
|
18
|
+
// whose lines are partly gone keeps the totals made when they were whole.
|
|
19
|
+
import { promises as fs } from 'node:fs';
|
|
20
|
+
import { join } from 'node:path';
|
|
21
|
+
import { BEIJING, ReportBuilder, addDays, dailyTotals, dayIn, } from './report.js';
|
|
22
|
+
import { dayOf, firstKeptDay } from './store.js';
|
|
23
|
+
const mtime = async (file) => {
|
|
24
|
+
try {
|
|
25
|
+
return (await fs.stat(file)).mtimeMs;
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return 0;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
/** Pseudonyms remembered at most - beyond it the least recently seen go. */
|
|
32
|
+
const MAX_SEEN = 500_000;
|
|
33
|
+
/**
|
|
34
|
+
* When each user and signed-in shopper pseudonym was first and last seen
|
|
35
|
+
* (local days). Kept like the lines it stands for: an entry not seen for 13
|
|
36
|
+
* months is dropped.
|
|
37
|
+
*/
|
|
38
|
+
export class Seen {
|
|
39
|
+
users = new Map();
|
|
40
|
+
shoppers = new Map();
|
|
41
|
+
static file(store) {
|
|
42
|
+
return join(store.dir, 'seen.json');
|
|
43
|
+
}
|
|
44
|
+
static async load(store) {
|
|
45
|
+
try {
|
|
46
|
+
const raw = JSON.parse(await fs.readFile(Seen.file(store), 'utf8'));
|
|
47
|
+
if (raw.v !== 1)
|
|
48
|
+
return null;
|
|
49
|
+
const s = new Seen();
|
|
50
|
+
for (const [k, v] of Object.entries(raw.users ?? {}))
|
|
51
|
+
s.users.set(k, v);
|
|
52
|
+
for (const [k, v] of Object.entries(raw.shoppers ?? {}))
|
|
53
|
+
s.shoppers.set(k, v);
|
|
54
|
+
return s;
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
static touch(m, id, day) {
|
|
61
|
+
const hit = m.get(id);
|
|
62
|
+
if (!hit)
|
|
63
|
+
m.set(id, [day, day]);
|
|
64
|
+
else {
|
|
65
|
+
if (day < hit[0])
|
|
66
|
+
hit[0] = day;
|
|
67
|
+
if (day > hit[1])
|
|
68
|
+
hit[1] = day;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
touchUser(u, day) {
|
|
72
|
+
Seen.touch(this.users, u, day);
|
|
73
|
+
}
|
|
74
|
+
touchShopper(s, day) {
|
|
75
|
+
Seen.touch(this.shoppers, s, day);
|
|
76
|
+
}
|
|
77
|
+
userFirst(u) {
|
|
78
|
+
return this.users.get(u)?.[0];
|
|
79
|
+
}
|
|
80
|
+
shopperFirst(s) {
|
|
81
|
+
return this.shoppers.get(s)?.[0];
|
|
82
|
+
}
|
|
83
|
+
async save(store, firstKept) {
|
|
84
|
+
const keep = (m) => {
|
|
85
|
+
const live = [...m.entries()].filter(([, [, last]]) => last >= firstKept);
|
|
86
|
+
live.sort(([, a], [, b]) => (a[1] < b[1] ? 1 : a[1] > b[1] ? -1 : 0));
|
|
87
|
+
return Object.fromEntries(live.slice(0, MAX_SEEN));
|
|
88
|
+
};
|
|
89
|
+
const file = Seen.file(store);
|
|
90
|
+
await fs.mkdir(store.dir, { recursive: true, mode: 0o700 });
|
|
91
|
+
await fs.writeFile(`${file}.tmp`, JSON.stringify({ v: 1, users: keep(this.users), shoppers: keep(this.shoppers) }), { mode: 0o600 });
|
|
92
|
+
await fs.rename(`${file}.tmp`, file);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export const maintain = async (store, deployment, now = store.now(), tz = BEIJING) => {
|
|
96
|
+
const today = dayIn(now, tz);
|
|
97
|
+
const firstKept = firstKeptDay(dayOf(now));
|
|
98
|
+
const files = await store.days();
|
|
99
|
+
const changedAt = new Map();
|
|
100
|
+
for (const { day, files: list } of files) {
|
|
101
|
+
changedAt.set(day, Math.max(...(await Promise.all(list.map(mtime)))));
|
|
102
|
+
}
|
|
103
|
+
// No index yet (the first run, or it was lost): every day still whole is
|
|
104
|
+
// summed again, in order, and the index is built on the way.
|
|
105
|
+
const loaded = await Seen.load(store);
|
|
106
|
+
const seen = loaded ?? new Seen();
|
|
107
|
+
// Every finished local day any kept file touches.
|
|
108
|
+
const candidates = new Set();
|
|
109
|
+
for (const { day } of files) {
|
|
110
|
+
for (const d of [day, addDays(day, 1)])
|
|
111
|
+
if (d < today)
|
|
112
|
+
candidates.add(d);
|
|
113
|
+
}
|
|
114
|
+
const due = [];
|
|
115
|
+
for (const d of [...candidates].sort()) {
|
|
116
|
+
const written = await mtime(join(store.dailyDir, `${d}.json`));
|
|
117
|
+
const whole = addDays(d, -1) >= firstKept;
|
|
118
|
+
const changed = Math.max(changedAt.get(addDays(d, -1)) ?? 0, changedAt.get(d) ?? 0);
|
|
119
|
+
if (!written || (whole && (!loaded || changed > written)))
|
|
120
|
+
due.push(d);
|
|
121
|
+
}
|
|
122
|
+
const rolled = [];
|
|
123
|
+
for (const day of due) {
|
|
124
|
+
const b = new ReportBuilder(day, day, deployment, tz, seen);
|
|
125
|
+
for await (const e of store.read(addDays(day, -1), day))
|
|
126
|
+
b.place(e);
|
|
127
|
+
for await (const e of store.read(addDays(day, -1), day)) {
|
|
128
|
+
b.add(e);
|
|
129
|
+
if (e.d !== deployment)
|
|
130
|
+
continue;
|
|
131
|
+
const d = dayIn(e.t, tz);
|
|
132
|
+
if (d > day)
|
|
133
|
+
continue;
|
|
134
|
+
if (e.u)
|
|
135
|
+
seen.touchUser(e.u, d);
|
|
136
|
+
if (e.s && (e.step === 'ok' || e.step === 'orders'))
|
|
137
|
+
seen.touchShopper(e.s, d);
|
|
138
|
+
}
|
|
139
|
+
const totals = dailyTotals(b.build(), day);
|
|
140
|
+
if (!totals)
|
|
141
|
+
continue;
|
|
142
|
+
await fs.mkdir(store.dailyDir, { recursive: true, mode: 0o700 });
|
|
143
|
+
const file = join(store.dailyDir, `${day}.json`);
|
|
144
|
+
await fs.writeFile(`${file}.tmp`, `${JSON.stringify(totals)}\n`, { mode: 0o600 });
|
|
145
|
+
await fs.rename(`${file}.tmp`, file);
|
|
146
|
+
rolled.push(day);
|
|
147
|
+
}
|
|
148
|
+
if (due.length || !loaded)
|
|
149
|
+
await seen.save(store, firstKept);
|
|
150
|
+
const pruned = await store.prune(dayOf(now));
|
|
151
|
+
await store.measure();
|
|
152
|
+
return { rolled, pruned };
|
|
153
|
+
};
|
|
154
|
+
/** The kept totals of `from`..`to`. */
|
|
155
|
+
export const readDaily = async (store, from, to) => {
|
|
156
|
+
let names;
|
|
157
|
+
try {
|
|
158
|
+
names = await fs.readdir(store.dailyDir);
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
return [];
|
|
162
|
+
}
|
|
163
|
+
const out = [];
|
|
164
|
+
for (const name of names.sort()) {
|
|
165
|
+
const day = name.slice(0, 10);
|
|
166
|
+
if (!/^\d{4}-\d{2}-\d{2}\.json$/.test(name) || day < from || day > to)
|
|
167
|
+
continue;
|
|
168
|
+
try {
|
|
169
|
+
out.push(JSON.parse(await fs.readFile(join(store.dailyDir, name), 'utf8')));
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
// unreadable: skip
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return out;
|
|
176
|
+
};
|
|
177
|
+
//# sourceMappingURL=maintain.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"maintain.js","sourceRoot":"","sources":["../../src/usage/maintain.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,wEAAwE;AACxE,0EAA0E;AAC1E,wBAAwB;AACxB,EAAE;AACF,8EAA8E;AAC9E,8EAA8E;AAC9E,gFAAgF;AAChF,6EAA6E;AAC7E,gFAAgF;AAChF,8EAA8E;AAC9E,6EAA6E;AAC7E,EAAE;AACF,6EAA6E;AAC7E,8EAA8E;AAC9E,4EAA4E;AAC5E,4EAA4E;AAC5E,0EAA0E;AAC1E,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EACL,OAAO,EAEP,aAAa,EAGb,OAAO,EACP,WAAW,EACX,KAAK,GACN,MAAM,aAAa,CAAC;AACrB,OAAO,EAAmB,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAElE,MAAM,KAAK,GAAG,KAAK,EAAE,IAAY,EAAmB,EAAE;IACpD,IAAI,CAAC;QACH,OAAO,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC;IACvC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,CAAC,CAAC;IACX,CAAC;AACH,CAAC,CAAC;AAEF,4EAA4E;AAC5E,MAAM,QAAQ,GAAG,OAAO,CAAC;AAEzB;;;;GAIG;AACH,MAAM,OAAO,IAAI;IACN,KAAK,GAAG,IAAI,GAAG,EAA4B,CAAC;IAC5C,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AAI,IAAI,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC;QAChC,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QAC9B,IAAI,CAAC,2BAA2B,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,GAAG,IAAI,IAAI,GAAG,GAAG,EAAE;YAAE,SAAS;QAChF,IAAI,CAAC;YACH,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,CAAgB,CAAC,CAAC;QAC7F,CAAC;QAAC,MAAM,CAAC;YACP,mBAAmB;QACrB,CAAC;IACH,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { type PlatformId } from '../source-ranges.js';
|
|
2
|
+
import type { GeoDb, NetKind } from './geo.js';
|
|
3
|
+
export type Platform = 'chatgpt' | 'claude' | 'manus' | 'muse' | 'other';
|
|
4
|
+
export declare const PLATFORMS: readonly Platform[];
|
|
5
|
+
/** A set of ranges, asked one address at a time. */
|
|
6
|
+
export declare class Ranges {
|
|
7
|
+
private readonly cidrs;
|
|
8
|
+
constructor(list: readonly string[]);
|
|
9
|
+
has(ip: string | undefined): boolean;
|
|
10
|
+
}
|
|
11
|
+
/** A platform's published outbound ranges - the allowlist's own list. */
|
|
12
|
+
export declare const rangesOf: (id: PlatformId) => Ranges;
|
|
13
|
+
/** OpenAI's published ChatGPT connector ranges: the only source a guest's address is taken from. */
|
|
14
|
+
export declare const OPENAI: Ranges;
|
|
15
|
+
export type PlatformSignals = {
|
|
16
|
+
/** What the client called itself at `initialize` (sessions.ts keeps it per session). */
|
|
17
|
+
client?: string | undefined;
|
|
18
|
+
userAgent?: string | undefined;
|
|
19
|
+
/** Whether the request carried `X-OpenAI-Session`. */
|
|
20
|
+
openaiSession?: boolean;
|
|
21
|
+
/** The address our proxy saw (server.ts `sourceOf`). */
|
|
22
|
+
peer?: string | undefined;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* By the marks the connector already goes by (sessions.ts CARD_HOSTS): ChatGPT's
|
|
26
|
+
* `openai-mcp` agent and its session header; claude.ai by its client name or
|
|
27
|
+
* Anthropic's ranges; Manus and Muse by name only - neither publishes an address
|
|
28
|
+
* (docs/mcp-source-allowlist.md). Self-reported, which is fine for a count.
|
|
29
|
+
* Claude Code counts as Claude; Codex is `other`.
|
|
30
|
+
*/
|
|
31
|
+
export declare const platformOf: (s: PlatformSignals) => Platform;
|
|
32
|
+
/** A routable public address, as sent - or null. */
|
|
33
|
+
export declare const publicIp: (raw: unknown) => string | null;
|
|
34
|
+
export type Origin =
|
|
35
|
+
/** The guest's own address, for the country lookup only - never stored. */
|
|
36
|
+
{
|
|
37
|
+
kind: 'user';
|
|
38
|
+
ip: string;
|
|
39
|
+
}
|
|
40
|
+
/** A platform's data centre: its address says nothing of where the person is. */
|
|
41
|
+
| {
|
|
42
|
+
kind: 'platform';
|
|
43
|
+
}
|
|
44
|
+
/** ChatGPT, but no usable address came with it. */
|
|
45
|
+
| {
|
|
46
|
+
kind: 'unknown';
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Whose address this request carries.
|
|
50
|
+
*
|
|
51
|
+
* ⚠️ ONLY CHATGPT, AND ONLY FROM OPENAI'S OWN ADDRESSES. ChatGPT puts the person's
|
|
52
|
+
* address in the `X-Forwarded-For` it sends (first entry); every other host calls
|
|
53
|
+
* from its own machines, so its address is the platform's, not the person's -
|
|
54
|
+
* recorded as such, never as where they are (robin, 2026-11-12). A request that
|
|
55
|
+
* merely CLAIMS to be ChatGPT, from anywhere else, is `unknown`: the header is
|
|
56
|
+
* the caller's to write.
|
|
57
|
+
*
|
|
58
|
+
* `forwarded` is the X-Forwarded-For exactly as the caller sent it - the copy
|
|
59
|
+
* Caddy is asked to make (`X-Caller-Forwarded-For`, recorder.ts), because Caddy
|
|
60
|
+
* itself rewrites X-Forwarded-For to the peer it saw.
|
|
61
|
+
*/
|
|
62
|
+
export declare const originOf: (platform: Platform, peer: string | undefined, forwarded: string | undefined, openai: Pick<Ranges, "has">) => Origin;
|
|
63
|
+
export type GeoFields = {
|
|
64
|
+
g: string;
|
|
65
|
+
n?: NetKind;
|
|
66
|
+
};
|
|
67
|
+
/** The country (and network type) an event carries - looked up here, the address dropped here. */
|
|
68
|
+
export declare const geoFields: (platform: Platform, peer: string | undefined, forwarded: string | undefined, openai: Pick<Ranges, "has">, geo: Pick<GeoDb, "lookup">) => GeoFields;
|
|
69
|
+
//# sourceMappingURL=platform.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"platform.d.ts","sourceRoot":"","sources":["../../src/usage/platform.ts"],"names":[],"mappings":"AAUA,OAAO,EAAmB,KAAK,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACvE,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAE/C,MAAM,MAAM,QAAQ,GAAG,SAAS,GAAG,QAAQ,GAAG,OAAO,GAAG,MAAM,GAAG,OAAO,CAAC;AACzE,eAAO,MAAM,SAAS,EAAE,SAAS,QAAQ,EAAoD,CAAC;AAE9F,oDAAoD;AACpD,qBAAa,MAAM;IACjB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;gBAEnB,IAAI,EAAE,SAAS,MAAM,EAAE;IAQnC,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO;CAIrC;AAED,yEAAyE;AACzE,eAAO,MAAM,QAAQ,GAAI,IAAI,UAAU,KAAG,MACqD,CAAC;AAEhG,oGAAoG;AACpG,eAAO,MAAM,MAAM,QAAqB,CAAC;AAIzC,MAAM,MAAM,eAAe,GAAG;IAC5B,wFAAwF;IACxF,MAAM,CAAC,EAAE,MAAM,GA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. The same rules as
|
|
3
|
+
// @curless/shopify-storefront src/usage/platform.ts and where.ts (0.77.2), on
|
|
4
|
+
// this package's own ranges (source-ranges.ts, the allowlist's) and parser
|
|
5
|
+
// (cidr.ts) - so a hotel's events and a shop's count the same way.
|
|
6
|
+
//
|
|
7
|
+
// ⚠️ THE ADDRESS NEVER LEAVES `geoFields`. It is read, looked up in the local
|
|
8
|
+
// database (geo.ts), and the answer - two letters and a word - is all that comes
|
|
9
|
+
// back. Nothing here stores, logs or sends it.
|
|
10
|
+
import { inCidr, parseAddress, parseCidr } from '../cidr.js';
|
|
11
|
+
import { PLATFORM_RANGES } from '../source-ranges.js';
|
|
12
|
+
export const PLATFORMS = ['chatgpt', 'claude', 'manus', 'muse', 'other'];
|
|
13
|
+
/** A set of ranges, asked one address at a time. */
|
|
14
|
+
export class Ranges {
|
|
15
|
+
cidrs;
|
|
16
|
+
constructor(list) {
|
|
17
|
+
this.cidrs = list.map((c) => {
|
|
18
|
+
const cidr = parseCidr(c);
|
|
19
|
+
if (!cidr)
|
|
20
|
+
throw new Error(`usage: ${JSON.stringify(c)} is not a range`);
|
|
21
|
+
return cidr;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
has(ip) {
|
|
25
|
+
const a = parseAddress(ip);
|
|
26
|
+
return !!a && this.cidrs.some((c) => inCidr(a, c));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** A platform's published outbound ranges - the allowlist's own list. */
|
|
30
|
+
export const rangesOf = (id) => new Ranges((PLATFORM_RANGES.find((r) => r.id === id)?.sources ?? []).flatMap((s) => s.cidrs));
|
|
31
|
+
/** OpenAI's published ChatGPT connector ranges: the only source a guest's address is taken from. */
|
|
32
|
+
export const OPENAI = rangesOf('openai');
|
|
33
|
+
/** Anthropic's: claude.ai's calls come from them. */
|
|
34
|
+
const ANTHROPIC = rangesOf('anthropic');
|
|
35
|
+
/**
|
|
36
|
+
* By the marks the connector already goes by (sessions.ts CARD_HOSTS): ChatGPT's
|
|
37
|
+
* `openai-mcp` agent and its session header; claude.ai by its client name or
|
|
38
|
+
* Anthropic's ranges; Manus and Muse by name only - neither publishes an address
|
|
39
|
+
* (docs/mcp-source-allowlist.md). Self-reported, which is fine for a count.
|
|
40
|
+
* Claude Code counts as Claude; Codex is `other`.
|
|
41
|
+
*/
|
|
42
|
+
export const platformOf = (s) => {
|
|
43
|
+
const client = s.client ?? '';
|
|
44
|
+
const ua = s.userAgent ?? '';
|
|
45
|
+
if (s.openaiSession || /^openai-mcp\b/i.test(ua) || /^(openai-mcp|chatgpt)\b/i.test(client)) {
|
|
46
|
+
return 'chatgpt';
|
|
47
|
+
}
|
|
48
|
+
const named = `${client} ${ua}`;
|
|
49
|
+
if (/\bmanus\b/i.test(named))
|
|
50
|
+
return 'manus';
|
|
51
|
+
if (/\bmuse\b/i.test(named))
|
|
52
|
+
return 'muse';
|
|
53
|
+
if (/^(claude|anthropic)/i.test(client) || /\b(claude|anthropic)/i.test(ua))
|
|
54
|
+
return 'claude';
|
|
55
|
+
if (s.peer && ANTHROPIC.has(s.peer))
|
|
56
|
+
return 'claude';
|
|
57
|
+
return 'other';
|
|
58
|
+
};
|
|
59
|
+
/** Ranges that are nobody's location: private, shared, documentation, multicast. */
|
|
60
|
+
const NOT_PUBLIC = new Ranges([
|
|
61
|
+
'0.0.0.0/8',
|
|
62
|
+
'10.0.0.0/8',
|
|
63
|
+
'100.64.0.0/10',
|
|
64
|
+
'127.0.0.0/8',
|
|
65
|
+
'169.254.0.0/16',
|
|
66
|
+
'172.16.0.0/12',
|
|
67
|
+
'192.0.0.0/24',
|
|
68
|
+
'192.0.2.0/24',
|
|
69
|
+
'192.168.0.0/16',
|
|
70
|
+
'198.18.0.0/15',
|
|
71
|
+
'198.51.100.0/24',
|
|
72
|
+
'203.0.113.0/24',
|
|
73
|
+
'224.0.0.0/3',
|
|
74
|
+
'::/127',
|
|
75
|
+
'64:ff9b::/96',
|
|
76
|
+
'100::/64',
|
|
77
|
+
'2001:db8::/32',
|
|
78
|
+
'fc00::/7',
|
|
79
|
+
'fe80::/10',
|
|
80
|
+
'ff00::/8',
|
|
81
|
+
]);
|
|
82
|
+
/** A routable public address, as sent - or null. */
|
|
83
|
+
export const publicIp = (raw) => {
|
|
84
|
+
if (typeof raw !== 'string')
|
|
85
|
+
return null;
|
|
86
|
+
const ip = raw.trim().replace(/^\[|\]$/g, '');
|
|
87
|
+
if (!ip || ip.length > 64 || !parseAddress(ip) || NOT_PUBLIC.has(ip))
|
|
88
|
+
return null;
|
|
89
|
+
return ip;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Whose address this request carries.
|
|
93
|
+
*
|
|
94
|
+
* ⚠️ ONLY CHATGPT, AND ONLY FROM OPENAI'S OWN ADDRESSES. ChatGPT puts the person's
|
|
95
|
+
* address in the `X-Forwarded-For` it sends (first entry); every other host calls
|
|
96
|
+
* from its own machines, so its address is the platform's, not the person's -
|
|
97
|
+
* recorded as such, never as where they are (robin, 2026-11-12). A request that
|
|
98
|
+
* merely CLAIMS to be ChatGPT, from anywhere else, is `unknown`: the header is
|
|
99
|
+
* the caller's to write.
|
|
100
|
+
*
|
|
101
|
+
* `forwarded` is the X-Forwarded-For exactly as the caller sent it - the copy
|
|
102
|
+
* Caddy is asked to make (`X-Caller-Forwarded-For`, recorder.ts), because Caddy
|
|
103
|
+
* itself rewrites X-Forwarded-For to the peer it saw.
|
|
104
|
+
*/
|
|
105
|
+
export const originOf = (platform, peer, forwarded, openai) => {
|
|
106
|
+
if (platform !== 'chatgpt')
|
|
107
|
+
return { kind: 'platform' };
|
|
108
|
+
if (!peer || !openai.has(peer))
|
|
109
|
+
return { kind: 'unknown' };
|
|
110
|
+
const first = forwarded?.split(',')[0];
|
|
111
|
+
const ip = publicIp(first);
|
|
112
|
+
return ip ? { kind: 'user', ip } : { kind: 'unknown' };
|
|
113
|
+
};
|
|
114
|
+
/** The country (and network type) an event carries - looked up here, the address dropped here. */
|
|
115
|
+
export const geoFields = (platform, peer, forwarded, openai, geo) => {
|
|
116
|
+
const origin = originOf(platform, peer, forwarded, openai);
|
|
117
|
+
if (origin.kind === 'platform')
|
|
118
|
+
return { g: 'platform' };
|
|
119
|
+
if (origin.kind === 'unknown')
|
|
120
|
+
return { g: 'unknown' };
|
|
121
|
+
const { cc, net } = geo.lookup(origin.ip);
|
|
122
|
+
return cc ? { g: cc, n: net } : { g: 'unknown', ...(net !== 'unknown' ? { n: net } : {}) };
|
|
123
|
+
};
|
|
124
|
+
//# sourceMappingURL=platform.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"platform.js","sourceRoot":"","sources":["../../src/usage/platform.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,iEAAiE;AACjE,8EAA8E;AAC9E,2EAA2E;AAC3E,mEAAmE;AACnE,EAAE;AACF,8EAA8E;AAC9E,iFAAiF;AACjF,+CAA+C;AAC/C,OAAO,EAAa,MAAM,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACxE,OAAO,EAAE,eAAe,EAAmB,MAAM,qBAAqB,CAAC;AAIvE,MAAM,CAAC,MAAM,SAAS,GAAwB,CAAC,SAAS,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;AAE9F,oDAAoD;AACpD,MAAM,OAAO,MAAM;IACA,KAAK,CAAS;IAE/B,YAAY,IAAuB;QACjC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;YAC1B,MAAM,IAAI,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;YAC1B,IAAI,CAAC,IAAI;gBAAE,MAAM,IAAI,KAAK,CAAC,UAAU,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC;YACzE,OAAO,IAAI,CAAC;QACd,CAAC,CAAC,CAAC;IACL,CAAC;IAED,GAAG,CAAC,EAAsB;QACxB,MAAM,CAAC,GAAG,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|
1
|
+
/** Hex characters kept: 64 bits - no collision worth counting at our volume. */
|
|
2
|
+
export declare const PSEUDONYM_HEX = 16;
|
|
3
|
+
/**
|
|
4
|
+
* The shortest key taken: 32 characters (`openssl rand -hex 32` gives 64). A
|
|
5
|
+
* short key is one a guess can find, and with it every pseudonym can be tested
|
|
6
|
+
* against a candidate id (security review of 0.77.0, P3).
|
|
7
|
+
*/
|
|
8
|
+
export declare const MIN_KEY_CHARS = 32;
|
|
9
|
+
export type ConversationSource =
|
|
10
|
+
/** ChatGPT's `X-OpenAI-Session`, as sent. */
|
|
11
|
+
'oai'
|
|
12
|
+
/** An `Mcp-Session-Id` - ours, or a conversation's own key from the cart binding. */
|
|
13
|
+
| 'mcp';
|
|
14
|
+
export declare class Pseudonyms {
|
|
15
|
+
private readonly key;
|
|
16
|
+
/** `secret` is the deployment's own; the MAC key is derived from it, labelled - the label shopify's, so both write one format. */
|
|
17
|
+
constructor(secret: string);
|
|
18
|
+
private mac;
|
|
19
|
+
/** A platform's own user id - today only ChatGPT's `X-OpenAI-Subject`. */
|
|
20
|
+
user(platformUserId: unknown): string | undefined;
|
|
21
|
+
/** A conversation. Never an address-keyed one (`ip:…`): see the top. */
|
|
22
|
+
conversation(source: ConversationSource, id: unknown): string | undefined;
|
|
23
|
+
/** A guest signed in to the deployment's own account: their Otaless member id (`memberUserId`) - nothing else of theirs. */
|
|
24
|
+
shopper(customerId: unknown): string | undefined;
|
|
25
|
+
/** One of our booking ids - never the hotel's confirmation code. */
|
|
26
|
+
ref(curlessRef: unknown): string | undefined;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=pseudonym.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pseudonym.d.ts","sourceRoot":"","sources":["../../src/usage/pseudonym.ts"],"names":[],"mappings":"AAmBA,gFAAgF;AAChF,eAAO,MAAM,aAAa,KAAK,CAAC;AAEhC;;;;GAIG;AACH,eAAO,MAAM,aAAa,KAAK,CAAC;AAWhC,MAAM,MAAM,kBAAkB;AAC5B,6CAA6C;AAC3C,KAAK;AACP,qFAAqF;GACnF,KAAK,CAAC;AAEV,qBAAa,UAAU;IACrB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAS;IAE7B,kIAAkI;gBACtH,MAAM,EAAE,MAAM;IAO1B,OAAO,CAAC,GAAG;IAOX,0EAA0E;IAC1E,IAAI,CAAC,cAAc,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS;IAKjD,wEAAwE;IACxE,YAAY,CAAC,MAAM,EAAE,kBAAkB,EAAE,EAAE,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS;IAMzE,4HAA4H;IAC5H,OAAO,CAAC,UAAU,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS;IAKhD,oEAAoE;IACpE,GAAG,CAAC,UAAU,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS;CAI7C"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Copied from @curless/shopify-storefront src/usage/pseudonym.ts (0.77.2), the
|
|
2
|
+
// derivation unchanged (docs/usage-events.md §6) - only the key's variable is
|
|
3
|
+
// this package's: HOTEL_USAGE_KEY.
|
|
4
|
+
//
|
|
5
|
+
// Pseudonyms for the usage statistics: a keyed MAC of an identifier a platform
|
|
6
|
+
// or the account backend gave us, never the identifier itself.
|
|
7
|
+
//
|
|
8
|
+
// ⚠️ KEYED PER DEPLOYMENT (`HOTEL_USAGE_KEY`). Two deployments see the same
|
|
9
|
+
// ChatGPT user under different names, so one deployment's records cannot be joined to
|
|
10
|
+
// another's; and without the key nobody can test a guess against them - an
|
|
11
|
+
// unkeyed hash of an OpenAI subject or an Otaless member id could be.
|
|
12
|
+
//
|
|
13
|
+
// ⚠️ ONLY WHAT A PLATFORM OR THE SHOP HANDED US. A request with no platform user
|
|
14
|
+
// id gets no user pseudonym at all - never one made from its address or anything
|
|
15
|
+
// else to stand in (robin, 2026-11-12). An address-keyed conversation (`ip:…`)
|
|
16
|
+
// gets no conversation pseudonym for the same reason: an address MAC'd is still
|
|
17
|
+
// an address to whoever holds the key, and IPv4 is small enough to walk.
|
|
18
|
+
import { createHmac } from 'node:crypto';
|
|
19
|
+
/** Hex characters kept: 64 bits - no collision worth counting at our volume. */
|
|
20
|
+
export const PSEUDONYM_HEX = 16;
|
|
21
|
+
/**
|
|
22
|
+
* The shortest key taken: 32 characters (`openssl rand -hex 32` gives 64). A
|
|
23
|
+
* short key is one a guess can find, and with it every pseudonym can be tested
|
|
24
|
+
* against a candidate id (security review of 0.77.0, P3).
|
|
25
|
+
*/
|
|
26
|
+
export const MIN_KEY_CHARS = 32;
|
|
27
|
+
/** The longest identifier taken - anything longer is not a platform's id. */
|
|
28
|
+
const MAX_ID = 256;
|
|
29
|
+
const clean = (raw) => {
|
|
30
|
+
if (typeof raw !== 'string')
|
|
31
|
+
return undefined;
|
|
32
|
+
const v = raw.trim();
|
|
33
|
+
return v && v.length <= MAX_ID ? v : undefined;
|
|
34
|
+
};
|
|
35
|
+
export class Pseudonyms {
|
|
36
|
+
key;
|
|
37
|
+
/** `secret` is the deployment's own; the MAC key is derived from it, labelled - the label shopify's, so both write one format. */
|
|
38
|
+
constructor(secret) {
|
|
39
|
+
if (!secret || secret.length < MIN_KEY_CHARS) {
|
|
40
|
+
throw new Error(`the pseudonym key must be at least ${MIN_KEY_CHARS} characters`);
|
|
41
|
+
}
|
|
42
|
+
this.key = createHmac('sha256', secret).update('storefront-usage-pseudonyms-v1').digest();
|
|
43
|
+
}
|
|
44
|
+
mac(kind, value) {
|
|
45
|
+
return createHmac('sha256', this.key)
|
|
46
|
+
.update(`${kind}|${value}`)
|
|
47
|
+
.digest('hex')
|
|
48
|
+
.slice(0, PSEUDONYM_HEX);
|
|
49
|
+
}
|
|
50
|
+
/** A platform's own user id - today only ChatGPT's `X-OpenAI-Subject`. */
|
|
51
|
+
user(platformUserId) {
|
|
52
|
+
const v = clean(platformUserId);
|
|
53
|
+
return v ? this.mac('user', v) : undefined;
|
|
54
|
+
}
|
|
55
|
+
/** A conversation. Never an address-keyed one (`ip:…`): see the top. */
|
|
56
|
+
conversation(source, id) {
|
|
57
|
+
const v = clean(id);
|
|
58
|
+
if (!v || v.startsWith('ip:'))
|
|
59
|
+
return undefined;
|
|
60
|
+
return this.mac(`conversation:${source}`, v);
|
|
61
|
+
}
|
|
62
|
+
/** A guest signed in to the deployment's own account: their Otaless member id (`memberUserId`) - nothing else of theirs. */
|
|
63
|
+
shopper(customerId) {
|
|
64
|
+
const v = typeof customerId === 'number' ? String(customerId) : clean(customerId);
|
|
65
|
+
return v ? this.mac('shopper', v) : undefined;
|
|
66
|
+
}
|
|
67
|
+
/** One of our booking ids - never the hotel's confirmation code. */
|
|
68
|
+
ref(curlessRef) {
|
|
69
|
+
const v = clean(curlessRef);
|
|
70
|
+
return v ? this.mac('ref', v) : undefined;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=pseudonym.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pseudonym.js","sourceRoot":"","sources":["../../src/usage/pseudonym.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,8EAA8E;AAC9E,mCAAmC;AACnC,EAAE;AACF,+EAA+E;AAC/E,+DAA+D;AAC/D,EAAE;AACF,4EAA4E;AAC5E,sFAAsF;AACtF,2EAA2E;AAC3E,sEAAsE;AACtE,EAAE;AACF,iFAAiF;AACjF,iFAAiF;AACjF,+EAA+E;AAC/E,gFAAgF;AAChF,yEAAyE;AACzE,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,gFAAgF;AAChF,MAAM,CAAC,MAAM,aAAa,GAAG,EAAE,CAAC;AAEhC;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,EAAE,CAAC;AAEhC,6EAA6E;AAC7E,MAAM,MAAM,GAAG,GAAG,CAAC;AAEnB,MAAM,KAAK,GAAG,CAAC,GAAY,EAAsB,EAAE;IACjD,IAA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AE,OAAO,SAAS,CAAC;QAChD,OAAO,IAAI,CAAC,GAAG,CAAC,gBAAgB,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC;IAC/C,CAAC;IAED,4HAA4H;IAC5H,OAAO,CAAC,UAAmB;QACzB,MAAM,CAAC,GAAG,OAAO,UAAU,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;QAClF,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAChD,CAAC;IAED,oEAAoE;IACpE,GAAG,CAAC,UAAmB;QACrB,MAAM,CAAC,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC;QAC5B,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAC5C,CAAC;CACF"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type Report, type Tz } from './report.js';
|
|
2
|
+
import { type UsageStore } from './store.js';
|
|
3
|
+
export declare const RANGES: readonly ["today", "yesterday", "7d", "30d", "90d", "all"];
|
|
4
|
+
export type Range = (typeof RANGES)[number];
|
|
5
|
+
export declare const isRange: (s: unknown) => s is Range;
|
|
6
|
+
export declare const windowOf: (store: UsageStore, range: Range, now: number, tz: Tz) => Promise<{
|
|
7
|
+
from: string;
|
|
8
|
+
to: string;
|
|
9
|
+
}>;
|
|
10
|
+
/**
|
|
11
|
+
* The most distinct ids an exact report holds before it gives up and sums the
|
|
12
|
+
* days instead (总线程's review of 0.77.1, P3: `--range all` over a flooded
|
|
13
|
+
* store held every conversation and connection at once).
|
|
14
|
+
*/
|
|
15
|
+
export declare const MAX_DISTINCT = 1000000;
|
|
16
|
+
export declare const reportFor: (store: UsageStore, deployment: string, window: {
|
|
17
|
+
from: string;
|
|
18
|
+
to: string;
|
|
19
|
+
}, opts?: {
|
|
20
|
+
now?: number;
|
|
21
|
+
tz?: Tz;
|
|
22
|
+
maxDistinct?: number;
|
|
23
|
+
}) => Promise<Report>;
|
|
24
|
+
//# sourceMappingURL=range.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"range.d.ts","sourceRoot":"","sources":["../../src/usage/range.ts"],"names":[],"mappings":"AAMA,OAAO,EAGL,KAAK,MAAM,EAEX,KAAK,EAAE,EAKR,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,KAAK,UAAU,EAAuB,MAAM,YAAY,CAAC;AAElE,eAAO,MAAM,MAAM,4DAA6D,CAAC;AACjF,MAAM,MAAM,KAAK,GAAG,CAAC,OAAO,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;AAE5C,eAAO,MAAM,OAAO,GAAI,GAAG,OAAO,KAAG,CAAC,IAAI,KAAoC,CAAC;AAc/E,eAAO,MAAM,QAAQ,GACnB,OAAO,UAAU,EACjB,OAAO,KAAK,EACZ,KAAK,MAAM,EACX,IAAI,EAAE,KACL,OAAO,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,CAgBtC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,YAAY,UAAY,CAAC;AAItC,eAAO,MAAM,SAAS,GACpB,OAAO,UAAU,EACjB,YAAY,MAAM,EAClB,QAAQ;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,EACpC,OAAM;IAAE,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,EAAE,CAAC,EAAE,EAAE,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAAO,KACzD,OAAO,CAAC,MAAM,CA+ChB,CAAC"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// The six windows robin asked for (11-13): today, yesterday, 7, 30 and 90
|
|
2
|
+
// days, and everything - in Beijing days unless asked for UTC. Inside the 13
|
|
3
|
+
// months of event lines a window is counted exactly; one that reaches past them
|
|
4
|
+
// is the sum of the kept daily totals (report.ts sumDays), with the days not
|
|
5
|
+
// yet summed (today, say) summed from their lines.
|
|
6
|
+
import { Seen, readDaily } from './maintain.js';
|
|
7
|
+
import { BEIJING, ReportBuilder, addDays, dailyTotals, dayIn, sumDays, } from './report.js';
|
|
8
|
+
import { dayOf, firstKeptDay } from './store.js';
|
|
9
|
+
export const RANGES = ['today', 'yesterday', '7d', '30d', '90d', 'all'];
|
|
10
|
+
export const isRange = (s) => RANGES.includes(s);
|
|
11
|
+
/** The first local day anything is known for: the oldest totals or line. */
|
|
12
|
+
const earliestDay = async (store, tz) => {
|
|
13
|
+
const daily = await readDaily(store, '0000-00-00', '9999-99-99');
|
|
14
|
+
const firstTotals = daily[0]?.day ?? null;
|
|
15
|
+
let firstLine = null;
|
|
16
|
+
for await (const e of store.read()) {
|
|
17
|
+
firstLine = dayIn(e.t, tz);
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
return [firstTotals, firstLine].filter((d) => !!d).sort()[0] ?? null;
|
|
21
|
+
};
|
|
22
|
+
export const windowOf = async (store, range, now, tz) => {
|
|
23
|
+
const today = dayIn(now, tz);
|
|
24
|
+
switch (range) {
|
|
25
|
+
case 'today':
|
|
26
|
+
return { from: today, to: today };
|
|
27
|
+
case 'yesterday':
|
|
28
|
+
return { from: addDays(today, -1), to: addDays(today, -1) };
|
|
29
|
+
case '7d':
|
|
30
|
+
return { from: addDays(today, -6), to: today };
|
|
31
|
+
case '30d':
|
|
32
|
+
return { from: addDays(today, -29), to: today };
|
|
33
|
+
case '90d':
|
|
34
|
+
return { from: addDays(today, -89), to: today };
|
|
35
|
+
case 'all':
|
|
36
|
+
return { from: (await earliestDay(store, tz)) ?? today, to: today };
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* The most distinct ids an exact report holds before it gives up and sums the
|
|
41
|
+
* days instead (总线程's review of 0.77.1, P3: `--range all` over a flooded
|
|
42
|
+
* store held every conversation and connection at once).
|
|
43
|
+
*/
|
|
44
|
+
export const MAX_DISTINCT = 1_000_000;
|
|
45
|
+
class TooMany extends Error {
|
|
46
|
+
}
|
|
47
|
+
export const reportFor = async (store, deployment, window, opts = {}) => {
|
|
48
|
+
const now = opts.now ?? Date.now();
|
|
49
|
+
const tz = opts.tz ?? BEIJING;
|
|
50
|
+
const max = opts.maxDistinct ?? MAX_DISTINCT;
|
|
51
|
+
const { from, to } = window;
|
|
52
|
+
const firstKept = firstKeptDay(dayOf(now));
|
|
53
|
+
// The first local day whose lines are all still here.
|
|
54
|
+
const whole = addDays(firstKept, 1);
|
|
55
|
+
// Who is new: the maintenance's index when there is one - then only the
|
|
56
|
+
// window's own files are read; without it, every kept line before the window.
|
|
57
|
+
const seen = (await Seen.load(store)) ?? undefined;
|
|
58
|
+
const sum = async (a, b, bounded) => {
|
|
59
|
+
const r = new ReportBuilder(a, b, deployment, tz, seen);
|
|
60
|
+
let n = 0;
|
|
61
|
+
const check = () => {
|
|
62
|
+
if (bounded && ++n % 10_000 === 0 && r.distinct() > max)
|
|
63
|
+
throw new TooMany();
|
|
64
|
+
};
|
|
65
|
+
for await (const e of store.read(addDays(a, -1), addDays(b, 1))) {
|
|
66
|
+
r.place(e);
|
|
67
|
+
check();
|
|
68
|
+
}
|
|
69
|
+
for await (const e of store.read(seen ? addDays(a, -1) : firstKept, addDays(b, 1))) {
|
|
70
|
+
r.add(e);
|
|
71
|
+
check();
|
|
72
|
+
}
|
|
73
|
+
if (bounded && r.distinct() > max)
|
|
74
|
+
throw new TooMany();
|
|
75
|
+
return r.build();
|
|
76
|
+
};
|
|
77
|
+
const byDays = async (reason) => {
|
|
78
|
+
const kept = await readDaily(store, from, to);
|
|
79
|
+
const have = new Set(kept.map((d) => d.day));
|
|
80
|
+
const fill = [];
|
|
81
|
+
for (let d = whole > from ? whole : from; d <= to; d = addDays(d, 1)) {
|
|
82
|
+
if (have.has(d))
|
|
83
|
+
continue;
|
|
84
|
+
// One day at a time - a day's file is capped (store.ts), so this is bounded.
|
|
85
|
+
const one = dailyTotals(await sum(d, d, false), d);
|
|
86
|
+
if (one)
|
|
87
|
+
fill.push(one);
|
|
88
|
+
}
|
|
89
|
+
return sumDays([...kept, ...fill], from, to, deployment, tz, reason);
|
|
90
|
+
};
|
|
91
|
+
if (from < whole)
|
|
92
|
+
return byDays('past-13-months');
|
|
93
|
+
try {
|
|
94
|
+
return await sum(from, to, true);
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
if (err instanceof TooMany)
|
|
98
|
+
return byDays('too-many');
|
|
99
|
+
throw err;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
//# sourceMappingURL=range.js.map
|