Skip to content

Commit

Permalink
feat(wrangler-d1): Teach wrangler d1 insights about more timePeriods (#…
Browse files Browse the repository at this point in the history
…5307)

* feat(wrangler-d1): Teach wrangler d1 insights about more timePeriods

* Change to patch

Co-authored-by: Max Rozen <3822106+rozenmd@users.noreply.github.com>

* Update changeset description

Co-authored-by: Max Rozen <3822106+rozenmd@users.noreply.github.com>

* Address review comments

* chore: add tests for d1 insights

---------

Co-authored-by: Max Rozen <3822106+rozenmd@users.noreply.github.com>
  • Loading branch information
achanda and rozenmd committed Jun 11, 2024
1 parent f1eb958 commit e6a3d24
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .changeset/large-seals-kiss.md
Original file line number Diff line number Diff line change
@@ -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.
42 changes: 42 additions & 0 deletions packages/wrangler/src/__tests__/d1/insights.test.ts
Original file line number Diff line number Diff line change
@@ -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));
});
});
50 changes: 42 additions & 8 deletions packages/wrangler/src/d1/insights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down Expand Up @@ -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<typeof Options>;
export const Handler = withConfig<HandlerOptions>(
async ({
Expand All @@ -80,11 +118,7 @@ export const Handler = withConfig<HandlerOptions>(
const output: Record<string, string | number>[] = [];

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"
Expand Down Expand Up @@ -122,8 +156,8 @@ export const Handler = withConfig<HandlerOptions>(
filter: {
AND: [
{
datetimeHour_geq: startDate.toISOString(),
datetimeHour_leq: endDate.toISOString(),
datetimeHour_geq: startDate,
datetimeHour_leq: endDate,
databaseId: db.uuid,
},
],
Expand Down

0 comments on commit e6a3d24

Please sign in to comment.