@trazum/core 2.6.0 → 2.7.1
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/billing.d.ts.map +1 -1
- package/dist/billing.js +2 -1
- package/dist/billing.js.map +1 -1
- package/dist/claude-code.d.ts +2 -0
- package/dist/claude-code.d.ts.map +1 -1
- package/dist/claude-code.js +6 -0
- package/dist/claude-code.js.map +1 -1
- package/dist/gateway.d.ts +8 -0
- package/dist/gateway.d.ts.map +1 -1
- package/dist/gateway.js +55 -12
- package/dist/gateway.js.map +1 -1
- package/dist/gemini-cli.d.ts +65 -0
- package/dist/gemini-cli.d.ts.map +1 -0
- package/dist/gemini-cli.js +162 -0
- package/dist/gemini-cli.js.map +1 -0
- package/dist/harnesses.d.ts +2 -2
- package/dist/harnesses.d.ts.map +1 -1
- package/dist/harnesses.js +1 -0
- package/dist/harnesses.js.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/pricing.d.ts +2 -0
- package/dist/pricing.d.ts.map +1 -1
- package/dist/pricing.js +504 -22
- package/dist/pricing.js.map +1 -1
- package/dist/slim-line.d.ts +37 -0
- package/dist/slim-line.d.ts.map +1 -0
- package/dist/slim-line.js +68 -0
- package/dist/slim-line.js.map +1 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/usage.d.ts +48 -0
- package/dist/usage.d.ts.map +1 -1
- package/dist/usage.js +129 -7
- package/dist/usage.js.map +1 -1
- package/package.json +2 -2
- package/src/billing.ts +2 -1
- package/src/claude-code.ts +8 -0
- package/src/gateway.ts +61 -11
- package/src/gemini-cli.ts +187 -0
- package/src/harnesses.ts +3 -2
- package/src/index.ts +10 -0
- package/src/pricing.ts +510 -22
- package/src/slim-line.ts +66 -0
- package/src/types.ts +14 -1
- package/src/usage.ts +174 -7
package/src/slim-line.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A usage-log line with its long strings set aside, for files too large to
|
|
3
|
+
* hold whole.
|
|
4
|
+
*
|
|
5
|
+
* JavaScript cannot hold a string much past 512 MiB, and an agent session can
|
|
6
|
+
* outgrow that: Claude Code writes every tool result into its transcript, so a
|
|
7
|
+
* long session that read large files or ran chatty commands reaches hundreds
|
|
8
|
+
* of megabytes. Reading such a file whole failed with "Invalid string length"
|
|
9
|
+
* and took the rest of the bill down with it.
|
|
10
|
+
*
|
|
11
|
+
* What a converter needs from a line is short: a type, a model id, a request
|
|
12
|
+
* or message id, a timestamp, a session id, a working directory and the token
|
|
13
|
+
* counts. What makes a line long is content: prompts, replies, thinking, tool
|
|
14
|
+
* arguments and tool results, none of which is ever billed by its length here
|
|
15
|
+
* (the counts are the provider's own). So a long line is parsed, every string
|
|
16
|
+
* longer than {@link SLIM_STRING_CHARS} is replaced by a marker that says how
|
|
17
|
+
* long it was, and the line is written back. Numbers, booleans, keys and short
|
|
18
|
+
* strings are untouched, so every converter reads the same fields it would
|
|
19
|
+
* have read from the whole line.
|
|
20
|
+
*
|
|
21
|
+
* **A line that is not JSON is returned unchanged.** A CSV row with a long
|
|
22
|
+
* quoted field is still a row; rewriting what could not be parsed would
|
|
23
|
+
* corrupt it rather than shorten it.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Strings longer than this are content. PATH_MAX on Linux is 4096, so even
|
|
28
|
+
* the longest working directory a label rule could match survives.
|
|
29
|
+
*/
|
|
30
|
+
export const SLIM_STRING_CHARS = 4096;
|
|
31
|
+
|
|
32
|
+
const marker = (length: number): string => `[trazum: ${length} characters not read]`;
|
|
33
|
+
|
|
34
|
+
const slim = (value: unknown, changed: { yes: boolean }): unknown => {
|
|
35
|
+
if (typeof value === 'string') {
|
|
36
|
+
if (value.length <= SLIM_STRING_CHARS) return value;
|
|
37
|
+
changed.yes = true;
|
|
38
|
+
return marker(value.length);
|
|
39
|
+
}
|
|
40
|
+
if (Array.isArray(value)) return value.map((item) => slim(item, changed));
|
|
41
|
+
if (typeof value === 'object' && value !== null) {
|
|
42
|
+
const out: Record<string, unknown> = {};
|
|
43
|
+
for (const [key, item] of Object.entries(value)) out[key] = slim(item, changed);
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
return value;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The line with its content strings shortened, or the line itself when it is
|
|
51
|
+
* short, is not JSON, or holds no string long enough to shorten. A line no
|
|
52
|
+
* longer than {@link SLIM_STRING_CHARS} cannot hold such a string, so it is
|
|
53
|
+
* returned without being parsed.
|
|
54
|
+
*/
|
|
55
|
+
export function slimUsageLine(line: string): string {
|
|
56
|
+
if (line.length <= SLIM_STRING_CHARS) return line;
|
|
57
|
+
let parsed: unknown;
|
|
58
|
+
try {
|
|
59
|
+
parsed = JSON.parse(line);
|
|
60
|
+
} catch {
|
|
61
|
+
return line;
|
|
62
|
+
}
|
|
63
|
+
const changed = { yes: false };
|
|
64
|
+
const slimmed = slim(parsed, changed);
|
|
65
|
+
return changed.yes ? JSON.stringify(slimmed) : line;
|
|
66
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -183,7 +183,20 @@ export type TierCondition =
|
|
|
183
183
|
* is `[1, 2, 3]` and not `[1, 4]`: an endpoint convention is the kind of
|
|
184
184
|
* thing two readers disagree about silently.
|
|
185
185
|
*/
|
|
186
|
-
| { kind: 'utc-hours'; hours: number[]; weekdaysOnly: boolean }
|
|
186
|
+
| { kind: 'utc-hours'; hours: number[]; weekdaysOnly: boolean }
|
|
187
|
+
/**
|
|
188
|
+
* The call ran at `speed`, as the response's own `usage.speed` records it.
|
|
189
|
+
*
|
|
190
|
+
* Anthropic's fast mode is an opt-in (`speed: "fast"` on the request) billed
|
|
191
|
+
* at a multiple of the standard rates, and the response says which speed ran,
|
|
192
|
+
* `"fast"` or `"standard"`. **Absent means standard and is decided**, unlike a
|
|
193
|
+
* size tier with no token count: fast mode is never on by default, a request
|
|
194
|
+
* for it that cannot run is refused rather than run slowly, and every response
|
|
195
|
+
* names its speed, so a log without the field is one that never asked. Pricing
|
|
196
|
+
* an unrecorded speed at the dearer rate would double every call of a model
|
|
197
|
+
* for the many logs that predate the field.
|
|
198
|
+
*/
|
|
199
|
+
| { kind: 'speed'; speed: 'fast' };
|
|
187
200
|
|
|
188
201
|
/**
|
|
189
202
|
* A rate that applies when its condition holds.
|
package/src/usage.ts
CHANGED
|
@@ -176,6 +176,21 @@ export interface UsageRecord {
|
|
|
176
176
|
* something of its own should not lose its records to it.
|
|
177
177
|
*/
|
|
178
178
|
requests?: number | null;
|
|
179
|
+
/**
|
|
180
|
+
* The speed the provider says the call ran at, when the log kept it:
|
|
181
|
+
* Anthropic's `usage.speed`, `"fast"` or `"standard"`. Fast mode bills the
|
|
182
|
+
* same tokens at a multiple of the standard rates (`kind: 'speed'` tiers in
|
|
183
|
+
* the catalogue). Absent on every log that does not record it, and then the
|
|
184
|
+
* call is priced at the standard rate, because fast mode is an explicit
|
|
185
|
+
* opt-in that every response names.
|
|
186
|
+
*/
|
|
187
|
+
speed?: string;
|
|
188
|
+
/**
|
|
189
|
+
* Set when an OpenAI-shaped line split its cache writes by lifetime and the
|
|
190
|
+
* parts did not add up to the total. The lifetime is then unknown even on a
|
|
191
|
+
* model whose unsplit writes are known to be the short kind.
|
|
192
|
+
*/
|
|
193
|
+
writeSplitContradicted?: true;
|
|
179
194
|
}
|
|
180
195
|
|
|
181
196
|
/** What a set of calls cost, split by where the money went. */
|
|
@@ -766,6 +781,19 @@ export function parseUsageLine(line: string): UsageRecord | null {
|
|
|
766
781
|
typeof detailsRaw === 'object' && detailsRaw !== null ? (detailsRaw as Record<string, unknown>) : null;
|
|
767
782
|
const openAiCached = details ? readCount(details.cached_tokens) : ({ kind: 'absent' } as Count);
|
|
768
783
|
const openAiWrite = details ? readCount(details.cache_write_tokens) : ({ kind: 'absent' } as Count);
|
|
784
|
+
/*
|
|
785
|
+
* The same writes split by lifetime, in the Responses API since the
|
|
786
|
+
* 12-hour entry: `ttl_30m_tokens` at 1.25x input and `ttl_12h_tokens` at
|
|
787
|
+
* 2.5x, which "sum to cache_write_tokens" and are "both returned, including
|
|
788
|
+
* when one is zero" (OpenAI's prompt-caching guide). So a write with no
|
|
789
|
+
* split was a 30-minute one: Chat Completions, or a log from before the
|
|
790
|
+
* 12-hour entry existed. Read as one, a 12-hour write cost half its price.
|
|
791
|
+
*/
|
|
792
|
+
const openAiSplitRaw = details?.cache_write_tokens_details;
|
|
793
|
+
const openAiSplit =
|
|
794
|
+
typeof openAiSplitRaw === 'object' && openAiSplitRaw !== null ? (openAiSplitRaw as Record<string, unknown>) : null;
|
|
795
|
+
const openAiWrite30m = openAiSplit ? readCount(openAiSplit.ttl_30m_tokens) : ({ kind: 'absent' } as Count);
|
|
796
|
+
const openAiWrite12h = openAiSplit ? readCount(openAiSplit.ttl_12h_tokens) : ({ kind: 'absent' } as Count);
|
|
769
797
|
|
|
770
798
|
/**
|
|
771
799
|
* Anthropic splits cache writes by time-to-live, and the two cost different
|
|
@@ -799,6 +827,20 @@ export function parseUsageLine(line: string): UsageRecord | null {
|
|
|
799
827
|
const geminiCached = geminiMeta
|
|
800
828
|
? readCount(geminiMeta.cachedContentTokenCount)
|
|
801
829
|
: ({ kind: 'absent' } as Count);
|
|
830
|
+
/**
|
|
831
|
+
* The two Gemini counts that sit outside `promptTokenCount` and
|
|
832
|
+
* `candidatesTokenCount` and are still billed. The SDK's own definitions
|
|
833
|
+
* (`@google/genai`, `UsageMetadata`): `thoughtsTokenCount` is "the model's
|
|
834
|
+
* generated 'thoughts' output", priced as output, and
|
|
835
|
+
* `toolUsePromptTokenCount` is "the results from tool executions, which are
|
|
836
|
+
* provided back to the model as input", priced as input; and
|
|
837
|
+
* `totalTokenCount` "is the sum of prompt, candidates, tool-use prompt and
|
|
838
|
+
* thoughts". Reading only the first two priced every thinking model's
|
|
839
|
+
* reasoning at zero: on Gemini 2.5 Pro and 3.1 Pro that is often most of the
|
|
840
|
+
* output, and it was the flattering direction.
|
|
841
|
+
*/
|
|
842
|
+
const geminiThoughts = geminiMeta ? readCount(geminiMeta.thoughtsTokenCount) : ({ kind: 'absent' } as Count);
|
|
843
|
+
const geminiToolPrompt = geminiMeta ? readCount(geminiMeta.toolUsePromptTokenCount) : ({ kind: 'absent' } as Count);
|
|
802
844
|
|
|
803
845
|
const counts: Record<string, Count> = {
|
|
804
846
|
input: readCount(
|
|
@@ -817,7 +859,11 @@ export function parseUsageLine(line: string): UsageRecord | null {
|
|
|
817
859
|
cacheWrite: readCount(usage.cache_creation_input_tokens, usage.cacheWriteTokens),
|
|
818
860
|
openAiCached,
|
|
819
861
|
openAiWrite,
|
|
862
|
+
openAiWrite30m,
|
|
863
|
+
openAiWrite12h,
|
|
820
864
|
geminiCached,
|
|
865
|
+
geminiThoughts,
|
|
866
|
+
geminiToolPrompt,
|
|
821
867
|
write5m,
|
|
822
868
|
write1h,
|
|
823
869
|
};
|
|
@@ -845,21 +891,37 @@ export function parseUsageLine(line: string): UsageRecord | null {
|
|
|
845
891
|
const split5m = valueOf(counts.write5m!);
|
|
846
892
|
const split1h = valueOf(counts.write1h!);
|
|
847
893
|
const hasSplit = counts.write5m!.kind === 'ok' || counts.write1h!.kind === 'ok';
|
|
894
|
+
// OpenAI's lifetimes, taken only when they add up to the writes they split.
|
|
895
|
+
const openAiSplitGiven = counts.openAiWrite30m!.kind === 'ok' || counts.openAiWrite12h!.kind === 'ok';
|
|
896
|
+
const openAiSplitAgrees =
|
|
897
|
+
counts.openAiWrite30m!.kind === 'ok' && counts.openAiWrite12h!.kind === 'ok'
|
|
898
|
+
&& valueOf(counts.openAiWrite30m!) + valueOf(counts.openAiWrite12h!) === openAiWritten;
|
|
899
|
+
const openAiLong = openAiSplitAgrees ? valueOf(counts.openAiWrite12h!) : 0;
|
|
848
900
|
|
|
849
901
|
return {
|
|
850
902
|
model,
|
|
851
|
-
inputTokens: Math.max(0, valueOf(counts.input!) - cached - openAiWritten),
|
|
903
|
+
inputTokens: Math.max(0, valueOf(counts.input!) - cached - openAiWritten) + valueOf(counts.geminiToolPrompt!),
|
|
852
904
|
cacheReadTokens: counts.cacheRead!.kind === 'ok' ? counts.cacheRead!.value : cached,
|
|
853
905
|
/**
|
|
854
906
|
* The split when the log carries it, the flat number otherwise — and
|
|
855
907
|
* `writeTtlKnown` says which, so the report can admit that a rate was assumed
|
|
856
908
|
* rather than quietly choosing the cheaper one.
|
|
857
909
|
*/
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
910
|
+
/*
|
|
911
|
+
* The short and the long write, whatever each provider calls them:
|
|
912
|
+
* Anthropic's 5-minute and 1-hour entries, OpenAI's 30-minute and
|
|
913
|
+
* 12-hour ones. Priced by the model's `cacheWrite5m` and `cacheWrite1h`.
|
|
914
|
+
*/
|
|
915
|
+
cacheWrite5mTokens: (hasSplit ? split5m : flatWrite) + openAiWritten - openAiLong,
|
|
916
|
+
cacheWrite1hTokens: (hasSplit ? split1h : 0) + openAiLong,
|
|
917
|
+
/*
|
|
918
|
+
* A split states the lifetime. A `cache_write_tokens` with none does not
|
|
919
|
+
* by itself: Kimi uses the same field and sells two entries at two prices.
|
|
920
|
+
* On OpenAI's own models it does (see above), which `profileUsage` decides
|
|
921
|
+
* by the model, since the line alone cannot say whose API wrote it.
|
|
922
|
+
*/
|
|
923
|
+
writeTtlKnown: hasSplit || (flatWrite === 0 && (openAiSplitGiven ? openAiSplitAgrees : openAiWritten === 0)),
|
|
924
|
+
outputTokens: valueOf(counts.output!) + valueOf(counts.geminiThoughts!),
|
|
863
925
|
/**
|
|
864
926
|
* Trimmed and internally normalised: any whitespace run becomes one space.
|
|
865
927
|
* A label is a workload name, and it is also used as half of structured keys
|
|
@@ -898,6 +960,13 @@ export function parseUsageLine(line: string): UsageRecord | null {
|
|
|
898
960
|
return reason === 'max_tokens' || reason === 'length' || reason === 'MAX_TOKENS';
|
|
899
961
|
})(),
|
|
900
962
|
...requestsOf(record.requests),
|
|
963
|
+
/*
|
|
964
|
+
* Anthropic's `usage.speed`, "fast" or "standard": fast mode bills the
|
|
965
|
+
* same tokens at a multiple of the standard rates, so a log that kept the
|
|
966
|
+
* field is priced by it. Absent stays absent, which prices as standard.
|
|
967
|
+
*/
|
|
968
|
+
...(typeof usage.speed === 'string' && usage.speed !== '' ? { speed: usage.speed } : {}),
|
|
969
|
+
...(openAiSplitGiven && !openAiSplitAgrees ? { writeSplitContradicted: true as const } : {}),
|
|
901
970
|
};
|
|
902
971
|
}
|
|
903
972
|
|
|
@@ -1207,7 +1276,14 @@ export function profileUsage(text: string, options: UsageProfileOptions): UsageP
|
|
|
1207
1276
|
if (
|
|
1208
1277
|
priced !== undefined
|
|
1209
1278
|
&& (cacheTtlOf(priced).longerMs === null
|
|
1210
|
-
|| multipliersFor(priced).cacheWrite1h === multipliersFor(priced).cacheWrite5m
|
|
1279
|
+
|| multipliersFor(priced).cacheWrite1h === multipliersFor(priced).cacheWrite5m
|
|
1280
|
+
/*
|
|
1281
|
+
* OpenAI returns the 30-minute and 12-hour split whenever a 12-hour
|
|
1282
|
+
* write is possible, "both fields ... including when one is zero",
|
|
1283
|
+
* so on its models an unsplit write is the 30-minute kind. The
|
|
1284
|
+
* guide's rule, not a guess; Kimi's same field gets no such rule.
|
|
1285
|
+
*/
|
|
1286
|
+
|| (priced.provider === 'openai' && !record.writeSplitContradicted))
|
|
1211
1287
|
) {
|
|
1212
1288
|
record = { ...record, writeTtlKnown: true };
|
|
1213
1289
|
}
|
|
@@ -1593,6 +1669,97 @@ export function spendByMonth(report: Pick<UsageProfileReport, 'spendByDay' | 'to
|
|
|
1593
1669
|
};
|
|
1594
1670
|
}
|
|
1595
1671
|
|
|
1672
|
+
/** The calendar unit a bill is split by. Weeks are ISO weeks: Monday to Sunday, UTC. */
|
|
1673
|
+
export type BillPeriodUnit = 'day' | 'week' | 'month';
|
|
1674
|
+
|
|
1675
|
+
/** One period of a bill, UTC. */
|
|
1676
|
+
export interface PeriodSpend {
|
|
1677
|
+
/** `YYYY-MM-DD` for a day, `YYYY-Www` (ISO) for a week, `YYYY-MM` for a month. */
|
|
1678
|
+
period: string;
|
|
1679
|
+
/** The first and last UTC day of the period, the `--since`/`--until` that bill it alone. */
|
|
1680
|
+
firstDay: string;
|
|
1681
|
+
lastDay: string;
|
|
1682
|
+
calls: number;
|
|
1683
|
+
usd: number;
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
/** A bill by day, week or month, and the part of it that sits in no period. */
|
|
1687
|
+
export interface SpendByPeriod {
|
|
1688
|
+
unit: BillPeriodUnit;
|
|
1689
|
+
/** Oldest first. Only periods with priced, dated calls appear. */
|
|
1690
|
+
periods: PeriodSpend[];
|
|
1691
|
+
/** Priced calls with no clock: in the total and in no period. */
|
|
1692
|
+
undated: { calls: number; usd: number };
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
const isoDay = (ms: number): string => new Date(ms).toISOString().slice(0, 10);
|
|
1696
|
+
|
|
1697
|
+
/** ISO 8601 week of a UTC day: the week belongs to the year of its Thursday. */
|
|
1698
|
+
function isoWeekOf(day: string): { key: string; monday: string; sunday: string } {
|
|
1699
|
+
const ms = Date.parse(`${day}T00:00:00Z`);
|
|
1700
|
+
const weekday = (new Date(ms).getUTCDay() + 6) % 7; // Monday = 0
|
|
1701
|
+
const monday = ms - weekday * 86_400_000;
|
|
1702
|
+
const thursday = new Date(monday + 3 * 86_400_000);
|
|
1703
|
+
const year = thursday.getUTCFullYear();
|
|
1704
|
+
const firstThursday = Date.UTC(year, 0, 4);
|
|
1705
|
+
const firstMonday = firstThursday - ((new Date(firstThursday).getUTCDay() + 6) % 7) * 86_400_000;
|
|
1706
|
+
const week = Math.round((monday - firstMonday) / (7 * 86_400_000)) + 1;
|
|
1707
|
+
return { key: `${year}-W${String(week).padStart(2, '0')}`, monday: isoDay(monday), sunday: isoDay(monday + 6 * 86_400_000) };
|
|
1708
|
+
}
|
|
1709
|
+
|
|
1710
|
+
/**
|
|
1711
|
+
* A bill by calendar day, ISO week or month, summed from the report's own days.
|
|
1712
|
+
*
|
|
1713
|
+
* The same rules as `spendByMonth`, which is this with `month`: UTC, priced
|
|
1714
|
+
* calls with a clock only, and the undated remainder returned beside the
|
|
1715
|
+
* periods so they reconcile to the total. A week is ISO 8601's, Monday to
|
|
1716
|
+
* Sunday, numbered by the year its Thursday falls in, so the last days of
|
|
1717
|
+
* December can belong to week 1 of the next year, as every ISO calendar says.
|
|
1718
|
+
*/
|
|
1719
|
+
export function spendByPeriod(
|
|
1720
|
+
report: Pick<UsageProfileReport, 'spendByDay' | 'total'>,
|
|
1721
|
+
unit: BillPeriodUnit,
|
|
1722
|
+
): SpendByPeriod {
|
|
1723
|
+
const byPeriod = new Map<string, { firstDay: string; lastDay: string; calls: number; usd: number }>();
|
|
1724
|
+
for (const day of report.spendByDay) {
|
|
1725
|
+
let key: string;
|
|
1726
|
+
let firstDay: string;
|
|
1727
|
+
let lastDay: string;
|
|
1728
|
+
if (unit === 'day') {
|
|
1729
|
+
key = day.day;
|
|
1730
|
+
firstDay = day.day;
|
|
1731
|
+
lastDay = day.day;
|
|
1732
|
+
} else if (unit === 'week') {
|
|
1733
|
+
const week = isoWeekOf(day.day);
|
|
1734
|
+
key = week.key;
|
|
1735
|
+
firstDay = week.monday;
|
|
1736
|
+
lastDay = week.sunday;
|
|
1737
|
+
} else {
|
|
1738
|
+
key = day.day.slice(0, 7);
|
|
1739
|
+
const [year, monthNumber] = key.split('-').map(Number);
|
|
1740
|
+
firstDay = `${key}-01`;
|
|
1741
|
+
lastDay = new Date(Date.UTC(year ?? 1970, monthNumber ?? 1, 0)).toISOString().slice(0, 10);
|
|
1742
|
+
}
|
|
1743
|
+
const entry = byPeriod.get(key) ?? { firstDay, lastDay, calls: 0, usd: 0 };
|
|
1744
|
+
entry.calls += day.calls;
|
|
1745
|
+
entry.usd += day.usd;
|
|
1746
|
+
byPeriod.set(key, entry);
|
|
1747
|
+
}
|
|
1748
|
+
const periods = [...byPeriod.entries()]
|
|
1749
|
+
.sort(([, a], [, b]) => a.firstDay.localeCompare(b.firstDay))
|
|
1750
|
+
.map(([period, { firstDay, lastDay, calls, usd }]) => ({ period, firstDay, lastDay, calls, usd }));
|
|
1751
|
+
const dated = periods.reduce((sum, p) => ({ calls: sum.calls + p.calls, usd: sum.usd + p.usd }), { calls: 0, usd: 0 });
|
|
1752
|
+
const undatedCalls = report.total.calls - dated.calls;
|
|
1753
|
+
return {
|
|
1754
|
+
unit,
|
|
1755
|
+
periods,
|
|
1756
|
+
undated:
|
|
1757
|
+
undatedCalls > 0
|
|
1758
|
+
? { calls: undatedCalls, usd: Math.max(0, report.total.totalUsd - dated.usd) }
|
|
1759
|
+
: { calls: 0, usd: 0 },
|
|
1760
|
+
};
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1596
1763
|
/**
|
|
1597
1764
|
* How much of the input that could have been cached was.
|
|
1598
1765
|
*
|