Skip to content

feat(site): add date range picker for the template insights #8976

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

Merged
merged 5 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add tests for the filter
  • Loading branch information
BrunoQuaresma committed Aug 8, 2023
commit 19a74b1914eecdf0ac1ffb439c6d311623821bb9
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ export const DateRange = ({
}),
},
{
label: "Last 15 days",
label: "Last 14 days",
range: () => ({
startDate: subDays(new Date(), 14),
startDate: subDays(new Date(), 13),
endDate: new Date(),
}),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,7 @@ import {
UserLatencyInsightsResponse,
} from "api/typesGenerated"
import { ComponentProps, ReactNode, useState } from "react"
import {
subDays,
addHours,
startOfHour,
startOfDay,
format,
isToday,
addDays,
} from "date-fns"
import { subDays, isToday } from "date-fns"
import "react-date-range/dist/styles.css"
import "react-date-range/dist/theme/default.css"
import { DateRange, DateRangeValue } from "./DateRange"
Expand All @@ -43,6 +35,7 @@ import OpenInNewOutlined from "@mui/icons-material/OpenInNewOutlined"
import Link from "@mui/material/Link"
import CheckCircleOutlined from "@mui/icons-material/CheckCircleOutlined"
import CancelOutlined from "@mui/icons-material/CancelOutlined"
import { getDateRangeFilter } from "./utils"

export default function TemplateInsightsPage() {
const now = new Date()
Expand All @@ -53,12 +46,12 @@ export default function TemplateInsightsPage() {
const { template } = useTemplateLayoutContext()
const insightsFilter = {
template_ids: template.id,
start_time: toISOLocal(startOfDay(dateRangeValue.startDate)),
end_time: toISOLocal(
isToday(dateRangeValue.endDate)
? startOfHour(addHours(now, 1))
: startOfDay(addDays(dateRangeValue.endDate, 1)),
),
...getDateRangeFilter({
startDate: dateRangeValue.startDate,
endDate: dateRangeValue.endDate,
now,
isToday,
}),
}
const { data: templateInsights } = useQuery({
queryKey: ["templates", template.id, "usage", insightsFilter],
Expand Down Expand Up @@ -584,10 +577,6 @@ function mapToDAUsResponse(
}
}

function toISOLocal(d: Date) {
return format(d, "yyyy-MM-dd'T'HH:mm:ssxxx")
}

function formatTime(seconds: number): string {
if (seconds < 60) {
return seconds + " seconds"
Expand Down
36 changes: 36 additions & 0 deletions site/src/pages/TemplatePage/TemplateInsightsPage/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { getDateRangeFilter } from "./utils"

describe("getDateRangeFilter", () => {
it("returns the start time at the start of the day", () => {
const date = new Date("2020-01-01T12:00:00.000Z")
const { start_time } = getDateRangeFilter({
startDate: date,
endDate: date,
now: date,
isToday: () => false,
})
expect(start_time).toEqual("2020-01-01T00:00:00+00:00")
})

it("returns the end time at the start of the next day", () => {
const date = new Date("2020-01-01T12:00:00.000Z")
const { end_time } = getDateRangeFilter({
startDate: date,
endDate: date,
now: date,
isToday: () => false,
})
expect(end_time).toEqual("2020-01-02T00:00:00+00:00")
})

it("returns the end time at the start of the next hour if the end date is today", () => {
const date = new Date("2020-01-01T12:00:00.000Z")
const { end_time } = getDateRangeFilter({
startDate: date,
endDate: date,
now: date,
isToday: () => true,
})
expect(end_time).toEqual("2020-01-01T13:00:00+00:00")
})
})
26 changes: 26 additions & 0 deletions site/src/pages/TemplatePage/TemplateInsightsPage/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { addDays, addHours, format, startOfDay, startOfHour } from "date-fns"

export function getDateRangeFilter({
startDate,
endDate,
now,
isToday,
}: {
startDate: Date
endDate: Date
now: Date
isToday: (date: Date) => boolean
}) {
return {
start_time: toISOLocal(startOfDay(startDate)),
end_time: toISOLocal(
isToday(endDate)
? startOfHour(addHours(now, 1))
: startOfDay(addDays(endDate, 1)),
),
}
}

function toISOLocal(d: Date) {
return format(d, "yyyy-MM-dd'T'HH:mm:ssxxx")
}