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
  • Loading branch information
achanda committed Mar 20, 2024
1 parent 93150aa commit 156c381
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions packages/wrangler/src/d1/insights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,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 @@ -61,6 +60,35 @@ const cliOptionToGraphQLOption = {
count: "count",
};

function getDurationDates(durationString: string) {
const endDate = new Date();

Check warning on line 64 in packages/wrangler/src/d1/insights.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/d1/insights.ts#L63-L64

Added lines #L63 - L64 were not covered by tests

const durationValue = parseInt(durationString.slice(0, -1));
const durationUnit = durationString.slice(-1);

Check warning on line 67 in packages/wrangler/src/d1/insights.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/d1/insights.ts#L66-L67

Added lines #L66 - L67 were not covered by tests

let startDate;
switch (durationUnit) {
case "d":

Check warning on line 71 in packages/wrangler/src/d1/insights.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/d1/insights.ts#L71

Added line #L71 was not covered by tests
if (durationValue > 31) {
throw new Error("Duration cannot be greater than 31 days");

Check warning on line 73 in packages/wrangler/src/d1/insights.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/d1/insights.ts#L73

Added line #L73 was not covered by tests
}
startDate = new Date(

Check warning on line 75 in packages/wrangler/src/d1/insights.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/d1/insights.ts#L75

Added line #L75 was not covered by tests
endDate.getTime() - durationValue * 24 * 60 * 60 * 1000
);
break;
case "m":
startDate = new Date(endDate.getTime() - durationValue * 60 * 1000);
break;
case "h":
startDate = new Date(endDate.getTime() - durationValue * 60 * 60 * 1000);
break;
default:
throw new Error("Invalid duration unit");

Check warning on line 86 in packages/wrangler/src/d1/insights.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/d1/insights.ts#L78-L86

Added lines #L78 - L86 were not covered by tests
}

return [startDate.toISOString(), endDate.toISOString()];

Check warning on line 89 in packages/wrangler/src/d1/insights.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/d1/insights.ts#L89

Added line #L89 was not covered by tests
}

type HandlerOptions = StrictYargsOptionsToInterface<typeof Options>;
export const Handler = withConfig<HandlerOptions>(
async ({
Expand All @@ -85,11 +113,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);

Check warning on line 116 in packages/wrangler/src/d1/insights.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/d1/insights.ts#L116

Added line #L116 was not covered by tests
const parsedSortBy = cliOptionToGraphQLOption[sortBy];
const orderByClause =
parsedSortBy === "count"
Expand Down Expand Up @@ -127,8 +151,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 156c381

Please sign in to comment.