diff --git a/.changeset/large-seals-kiss.md b/.changeset/large-seals-kiss.md new file mode 100644 index 000000000000..b87e00859592 --- /dev/null +++ b/.changeset/large-seals-kiss.md @@ -0,0 +1,7 @@ +--- +"wrangler": patch +--- + +fix: add more timePeriods to `wrangler d1 insights` + +This PR updates `wrangler d1 insights` to accept arbitrary timePeriod values up to 31 days. diff --git a/packages/wrangler/src/__tests__/d1/insights.test.ts b/packages/wrangler/src/__tests__/d1/insights.test.ts new file mode 100644 index 000000000000..095c7b85b603 --- /dev/null +++ b/packages/wrangler/src/__tests__/d1/insights.test.ts @@ -0,0 +1,42 @@ +import { vi } from "vitest"; +import { getDurationDates } from "../../d1/insights"; + +describe("getDurationDates()", () => { + beforeAll(() => { + vi.useFakeTimers(); + //lock time to 2023-08-01 UTC + vi.setSystemTime(new Date(2023, 7, 1)); + }); + + afterAll(() => { + vi.useRealTimers(); + }); + + it("should throw an error if duration is greater than 31 days (in days)", () => { + expect(() => getDurationDates("32d")).toThrowError( + "Duration cannot be greater than 31 days" + ); + }); + it("should throw an error if duration is greater than 31 days (in minutes)", () => { + expect(() => getDurationDates("44641m")).toThrowError( + "Duration cannot be greater than 44640 minutes (31 days)" + ); + }); + + it("should throw an error if duration is greater than 31 days (in hours)", () => { + expect(() => getDurationDates("745h")).toThrowError( + "Duration cannot be greater than 744 hours (31 days)" + ); + }); + + it("should throw an error if duration unit is invalid", () => { + expect(() => getDurationDates("1y")).toThrowError("Invalid duration unit"); + }); + + it("should return the correct start and end dates", () => { + const [startDate, endDate] = getDurationDates("5d"); + + expect(+new Date(startDate)).toBe(+new Date(2023, 6, 27)); + expect(+new Date(endDate)).toBe(+new Date(2023, 7, 1)); + }); +}); diff --git a/packages/wrangler/src/d1/insights.ts b/packages/wrangler/src/d1/insights.ts index 7f32254e0571..d9f4433ff023 100644 --- a/packages/wrangler/src/d1/insights.ts +++ b/packages/wrangler/src/d1/insights.ts @@ -18,7 +18,6 @@ export function Options(d1ListYargs: CommonYargsArgv) { demandOption: true, }) .option("timePeriod", { - choices: ["1d", "7d", "31d"] as const, describe: "Fetch data from now to the provided time period", default: "1d" as const, }) @@ -56,6 +55,45 @@ const cliOptionToGraphQLOption = { count: "count", }; +export function getDurationDates(durationString: string) { + const endDate = new Date(); + + const durationValue = parseInt(durationString.slice(0, -1)); + const durationUnit = durationString.slice(-1); + + let startDate; + switch (durationUnit) { + case "d": + if (durationValue > 31) { + throw new Error("Duration cannot be greater than 31 days"); + } + startDate = new Date( + endDate.getTime() - durationValue * 24 * 60 * 60 * 1000 + ); + break; + case "m": + if (durationValue > 31 * 24 * 60) { + throw new Error( + `Duration cannot be greater than ${31 * 24 * 60} minutes (31 days)` + ); + } + startDate = new Date(endDate.getTime() - durationValue * 60 * 1000); + break; + case "h": + if (durationValue > 31 * 24) { + throw new Error( + `Duration cannot be greater than ${31 * 24} hours (31 days)` + ); + } + startDate = new Date(endDate.getTime() - durationValue * 60 * 60 * 1000); + break; + default: + throw new Error("Invalid duration unit"); + } + + return [startDate.toISOString(), endDate.toISOString()]; +} + type HandlerOptions = StrictYargsOptionsToInterface; export const Handler = withConfig( async ({ @@ -80,11 +118,7 @@ export const Handler = withConfig( const output: Record[] = []; if (result.version !== "alpha") { - const convertedTimePeriod = Number(timePeriod.replace("d", "")); - const endDate = new Date(); - const startDate = new Date( - new Date(endDate).setDate(endDate.getDate() - convertedTimePeriod) - ); + const [startDate, endDate] = getDurationDates(timePeriod); const parsedSortBy = cliOptionToGraphQLOption[sortBy]; const orderByClause = parsedSortBy === "count" @@ -122,8 +156,8 @@ export const Handler = withConfig( filter: { AND: [ { - datetimeHour_geq: startDate.toISOString(), - datetimeHour_leq: endDate.toISOString(), + datetimeHour_geq: startDate, + datetimeHour_leq: endDate, databaseId: db.uuid, }, ],