|
1 | 1 | import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
2 | 3 | import path from "node:path"; |
3 | 4 | import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
4 | 5 | import type { OpenClawConfig } from "../config/config.js"; |
@@ -274,6 +275,17 @@ describe("session cost usage", () => { |
274 | 275 | expect(summary?.dailyLatency?.[0]?.count).toBe(1); |
275 | 276 | expect(summary?.dailyModelUsage?.[0]?.date).toBe("2026-02-01"); |
276 | 277 | expect(summary?.dailyModelUsage?.[0]?.model).toBe("gpt-5.4"); |
| 278 | + |
| 279 | + // utcQuarterHourMessageCounts should use UTC quarter-hour buckets |
| 280 | + // start = 2026-02-01T10:00Z → quarterIndex = floor((10*60+0)/15) = 40 |
| 281 | + // end = 2026-02-01T10:05Z → quarterIndex = floor((10*60+5)/15) = 40 |
| 282 | + expect(summary?.utcQuarterHourMessageCounts).toBeDefined(); |
| 283 | + expect(summary?.utcQuarterHourMessageCounts?.length).toBe(1); |
| 284 | + expect(summary?.utcQuarterHourMessageCounts?.[0]?.quarterIndex).toBe(40); |
| 285 | + expect(summary?.utcQuarterHourMessageCounts?.[0]?.date).toBe("2026-02-01"); |
| 286 | + expect(summary?.utcQuarterHourMessageCounts?.[0]?.total).toBe(2); |
| 287 | + expect(summary?.utcQuarterHourMessageCounts?.[0]?.user).toBe(1); |
| 288 | + expect(summary?.utcQuarterHourMessageCounts?.[0]?.assistant).toBe(1); |
277 | 289 | }); |
278 | 290 |
|
279 | 291 | it("does not exclude sessions with mtime after endMs during discovery", async () => { |
@@ -753,6 +765,83 @@ example |
753 | 765 | expect(logs?.[0]?.content).toBe("hello there"); |
754 | 766 | }); |
755 | 767 |
|
| 768 | + it("buckets hourly message counts into UTC quarter-hour slots", async () => { |
| 769 | + const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-cost-quarter-")); |
| 770 | + const sessionFile = path.join(root, "session.jsonl"); |
| 771 | + |
| 772 | + // Messages at different UTC quarter-hour boundaries: |
| 773 | + // 00:14 UTC → quarterIndex = floor((0*60+14)/15) = 0 |
| 774 | + // 00:15 UTC → quarterIndex = floor((0*60+15)/15) = 1 |
| 775 | + // 06:30 UTC → quarterIndex = floor((6*60+30)/15) = 26 |
| 776 | + // 23:59 UTC → quarterIndex = floor((23*60+59)/15) = 95 |
| 777 | + const entries = [ |
| 778 | + { |
| 779 | + type: "message", |
| 780 | + timestamp: "2026-03-15T00:14:00.000Z", |
| 781 | + message: { role: "user", content: "a" }, |
| 782 | + }, |
| 783 | + { |
| 784 | + type: "message", |
| 785 | + timestamp: "2026-03-15T00:15:00.000Z", |
| 786 | + message: { role: "user", content: "b" }, |
| 787 | + }, |
| 788 | + { |
| 789 | + type: "message", |
| 790 | + timestamp: "2026-03-15T06:30:00.000Z", |
| 791 | + message: { |
| 792 | + role: "assistant", |
| 793 | + provider: "openai", |
| 794 | + model: "gpt-5.2", |
| 795 | + usage: { input: 5, output: 5, totalTokens: 10, cost: { total: 0.001 } }, |
| 796 | + }, |
| 797 | + }, |
| 798 | + { |
| 799 | + type: "message", |
| 800 | + timestamp: "2026-03-15T23:59:00.000Z", |
| 801 | + message: { |
| 802 | + role: "assistant", |
| 803 | + provider: "openai", |
| 804 | + model: "gpt-5.2", |
| 805 | + stopReason: "error", |
| 806 | + usage: { input: 3, output: 3, totalTokens: 6, cost: { total: 0.001 } }, |
| 807 | + }, |
| 808 | + }, |
| 809 | + ]; |
| 810 | + |
| 811 | + await fs.writeFile( |
| 812 | + sessionFile, |
| 813 | + entries.map((entry) => JSON.stringify(entry)).join("\n"), |
| 814 | + "utf-8", |
| 815 | + ); |
| 816 | + |
| 817 | + const summary = await loadSessionCostSummary({ sessionFile }); |
| 818 | + const quarterHourly = summary?.utcQuarterHourMessageCounts; |
| 819 | + expect(quarterHourly).toBeDefined(); |
| 820 | + expect(quarterHourly?.length).toBe(4); |
| 821 | + |
| 822 | + // Sort by quarterIndex for deterministic checks |
| 823 | + const sorted = [...(quarterHourly ?? [])].toSorted((a, b) => a.quarterIndex - b.quarterIndex); |
| 824 | + expect(sorted[0]?.quarterIndex).toBe(0); // 00:14 |
| 825 | + expect(sorted[0]?.user).toBe(1); |
| 826 | + expect(sorted[1]?.quarterIndex).toBe(1); // 00:15 |
| 827 | + expect(sorted[1]?.user).toBe(1); |
| 828 | + expect(sorted[2]?.quarterIndex).toBe(26); // 06:30 |
| 829 | + expect(sorted[2]?.assistant).toBe(1); |
| 830 | + expect(sorted[3]?.quarterIndex).toBe(95); // 23:59 |
| 831 | + expect(sorted[3]?.assistant).toBe(1); |
| 832 | + expect(sorted[3]?.errors).toBe(1); // stopReason "error" |
| 833 | + }); |
| 834 | + |
| 835 | + it("returns undefined utcQuarterHourMessageCounts when session has no messages", async () => { |
| 836 | + const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-cost-empty-hourly-")); |
| 837 | + const sessionFile = path.join(root, "session.jsonl"); |
| 838 | + // Empty file — no entries at all |
| 839 | + await fs.writeFile(sessionFile, "", "utf-8"); |
| 840 | + |
| 841 | + const summary = await loadSessionCostSummary({ sessionFile }); |
| 842 | + expect(summary?.utcQuarterHourMessageCounts).toBeUndefined(); |
| 843 | + }); |
| 844 | + |
756 | 845 | it("preserves totals and cumulative values when downsampling timeseries", async () => { |
757 | 846 | const root = await makeSessionCostRoot("timeseries-downsample"); |
758 | 847 | const sessionsDir = path.join(root, "agents", "main", "sessions"); |
|
0 commit comments