@curless/hotel-storefront 0.89.0 → 0.91.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/dist/server.d.ts +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +1 -1
- package/dist/server.js.map +1 -1
- package/dist/usage/bookings-import.d.ts.map +1 -1
- package/dist/usage/bookings-import.js +1 -2
- package/dist/usage/bookings-import.js.map +1 -1
- package/dist/usage/boot.d.ts +1 -1
- package/dist/usage/boot.d.ts.map +1 -1
- package/dist/usage/boot.js +17 -68
- package/dist/usage/boot.js.map +1 -1
- package/dist/usage/platform.d.ts +1 -1
- package/dist/usage/platform.d.ts.map +1 -1
- package/dist/usage/platform.js.map +1 -1
- package/dist/usage/recorder.d.ts +1 -3
- package/dist/usage/recorder.d.ts.map +1 -1
- package/dist/usage/recorder.js.map +1 -1
- package/dist/usage/shadow-import.d.ts +1 -1
- package/dist/usage/shadow-import.d.ts.map +1 -1
- package/dist/usage/shadow-import.js +1 -1
- package/dist/usage/shadow-import.js.map +1 -1
- package/dist/usage-cli.js +7 -8
- package/dist/usage-cli.js.map +1 -1
- package/package.json +7 -5
- package/dist/usage/geo.d.ts +0 -88
- package/dist/usage/geo.d.ts.map +0 -1
- package/dist/usage/geo.js +0 -396
- package/dist/usage/geo.js.map +0 -1
- package/dist/usage/maintain.d.ts +0 -26
- package/dist/usage/maintain.d.ts.map +0 -1
- package/dist/usage/maintain.js +0 -177
- package/dist/usage/maintain.js.map +0 -1
- package/dist/usage/pseudonym.d.ts +0 -28
- package/dist/usage/pseudonym.d.ts.map +0 -1
- package/dist/usage/pseudonym.js +0 -73
- package/dist/usage/pseudonym.js.map +0 -1
- package/dist/usage/range.d.ts +0 -18
- package/dist/usage/range.d.ts.map +0 -1
- package/dist/usage/range.js +0 -97
- package/dist/usage/range.js.map +0 -1
- package/dist/usage/report.d.ts +0 -258
- package/dist/usage/report.d.ts.map +0 -1
- package/dist/usage/report.js +0 -862
- package/dist/usage/report.js.map +0 -1
- package/dist/usage/store.d.ts +0 -258
- package/dist/usage/store.d.ts.map +0 -1
- package/dist/usage/store.js +0 -475
- package/dist/usage/store.js.map +0 -1
package/dist/usage/maintain.js
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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,CAA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EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC3C,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,MAAM,GAAG,GAAkB,EAAE,CAAC;IAC9B,KAAK,MAAM,IAAI,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"}
|
|
@@ -1,28 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/dist/usage/pseudonym.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAC;IAC9C,MAAM,CAAC,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC;IACrB,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AACjD,CAAC,CAAC;AAQF,MAAM,OAAO,UAAU;IACJ,GAAG,CAAS;IAE7B,kIAAkI;IAClI,YAAY,MAAc;QACxB,IAAI,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,GAAG,aAAa,EAAE,CAAC;YAC7C,MAAM,IAAI,KAAK,CAAC,sCAAsC,aAAa,aAAa,CAAC,CAAC;QACpF,CAAC;Q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;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"}
|
package/dist/usage/range.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
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
|
-
export declare const reportFor: (store: UsageStore, deployment: string, window: {
|
|
11
|
-
from: string;
|
|
12
|
-
to: string;
|
|
13
|
-
}, opts?: {
|
|
14
|
-
now?: number;
|
|
15
|
-
tz?: Tz;
|
|
16
|
-
maxHeld?: number;
|
|
17
|
-
}) => Promise<Report>;
|
|
18
|
-
//# sourceMappingURL=range.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"range.d.ts","sourceRoot":"","sources":["../../src/usage/range.ts"],"names":[],"mappings":"AAMA,OAAO,EAIL,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;AAIF,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,OAAO,CAAC,EAAE,MAAM,CAAA;CAAO,KACrD,OAAO,CAAC,MAAM,CAgDhB,CAAC"}
|
package/dist/usage/range.js
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
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, MAX_HELD, 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
|
-
class TooMany extends Error {
|
|
40
|
-
}
|
|
41
|
-
export const reportFor = async (store, deployment, window, opts = {}) => {
|
|
42
|
-
const now = opts.now ?? Date.now();
|
|
43
|
-
const tz = opts.tz ?? BEIJING;
|
|
44
|
-
const max = opts.maxHeld ?? MAX_HELD;
|
|
45
|
-
const { from, to } = window;
|
|
46
|
-
const firstKept = firstKeptDay(dayOf(now));
|
|
47
|
-
// The first local day whose lines are all still here.
|
|
48
|
-
const whole = addDays(firstKept, 1);
|
|
49
|
-
// Who is new: the maintenance's index when there is one - then only the
|
|
50
|
-
// window's own files are read; without it, every kept line before the window.
|
|
51
|
-
const seen = (await Seen.load(store)) ?? undefined;
|
|
52
|
-
// ⚠️ BOUNDED BOTH WAYS (总线程 11-13, shopify 0.77.3 P3 carried over). The
|
|
53
|
-
// builder holds no more than `max` ids, every copy counted (report.ts Held).
|
|
54
|
-
// An exact window gives up the moment it is full - checked every line, not
|
|
55
|
-
// every ten thousand - and sums the days; a day summed alone stops at the
|
|
56
|
-
// ceiling too, and the report names it (`capped`) rather than running out of
|
|
57
|
-
// memory.
|
|
58
|
-
const sum = async (a, b, exact) => {
|
|
59
|
-
const r = new ReportBuilder(a, b, deployment, tz, seen, max);
|
|
60
|
-
for await (const e of store.read(addDays(a, -1), addDays(b, 1))) {
|
|
61
|
-
r.place(e);
|
|
62
|
-
if (exact && r.full)
|
|
63
|
-
throw new TooMany();
|
|
64
|
-
}
|
|
65
|
-
for await (const e of store.read(seen ? addDays(a, -1) : firstKept, addDays(b, 1))) {
|
|
66
|
-
r.add(e);
|
|
67
|
-
if (exact && r.full)
|
|
68
|
-
throw new TooMany();
|
|
69
|
-
}
|
|
70
|
-
return r.build();
|
|
71
|
-
};
|
|
72
|
-
const byDays = async (reason) => {
|
|
73
|
-
const kept = await readDaily(store, from, to);
|
|
74
|
-
const have = new Set(kept.map((d) => d.day));
|
|
75
|
-
const fill = [];
|
|
76
|
-
for (let d = whole > from ? whole : from; d <= to; d = addDays(d, 1)) {
|
|
77
|
-
if (have.has(d))
|
|
78
|
-
continue;
|
|
79
|
-
// One day at a time, held to the same ceiling.
|
|
80
|
-
const one = dailyTotals(await sum(d, d, false), d);
|
|
81
|
-
if (one)
|
|
82
|
-
fill.push(one);
|
|
83
|
-
}
|
|
84
|
-
return sumDays([...kept, ...fill], from, to, deployment, tz, reason);
|
|
85
|
-
};
|
|
86
|
-
if (from < whole)
|
|
87
|
-
return byDays('past-13-months');
|
|
88
|
-
try {
|
|
89
|
-
return await sum(from, to, true);
|
|
90
|
-
}
|
|
91
|
-
catch (err) {
|
|
92
|
-
if (err instanceof TooMany)
|
|
93
|
-
return byDays('too-many');
|
|
94
|
-
throw err;
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
//# sourceMappingURL=range.js.map
|
package/dist/usage/range.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"range.js","sourceRoot":"","sources":["../../src/usage/range.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,6EAA6E;AAC7E,gFAAgF;AAChF,6EAA6E;AAC7E,mDAAmD;AAC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