Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
achanda marked this conversation as resolved.
Show resolved Hide resolved
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
Loading