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
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
Next Next commit
Fix time internal
  • Loading branch information
BrunoQuaresma committed Aug 8, 2023
commit f947530d0f82e800a944d8df7f7e7106619e4901
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ import {
UserLatencyInsightsResponse,
} from "api/typesGenerated"
import { ComponentProps, ReactNode, useState } from "react"
import { subDays, addHours, startOfHour } from "date-fns"
import {
subDays,
addHours,
startOfHour,
startOfDay,
format,
isToday,
addDays,
} 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 @@ -33,13 +41,17 @@ export default function TemplateInsightsPage() {
const now = new Date()
const [dateRangeValue, setDateRangeValue] = useState<DateRangeValue>({
startDate: subDays(now, 6),
endDate: addHours(now, 1),
endDate: now,
})
const { template } = useTemplateLayoutContext()
const insightsFilter = {
template_ids: template.id,
start_time: toStartTimeFilter(dateRangeValue.startDate),
end_time: startOfHour(dateRangeValue.endDate).toISOString(),
start_time: toISOLocal(startOfDay(dateRangeValue.startDate)),
end_time: toISOLocal(
isToday(dateRangeValue.endDate)
? startOfHour(addHours(now, 1))
: startOfDay(addDays(dateRangeValue.endDate, 1)),
),
}
const { data: templateInsights } = useQuery({
queryKey: ["templates", template.id, "usage", insightsFilter],
Expand Down Expand Up @@ -333,18 +345,14 @@ function mapToDAUsResponse(
entries: data.map((d) => {
return {
amount: d.active_users,
date: d.end_time,
date: d.start_time,
}
}),
}
}

function toStartTimeFilter(date: Date) {
date.setHours(0, 0, 0, 0)
const year = date.getUTCFullYear()
const month = String(date.getUTCMonth() + 1).padStart(2, "0")
const day = String(date.getUTCDate()).padStart(2, "0")
return `${year}-${month}-${day}T00:00:00Z`
function toISOLocal(d: Date) {
return format(d, "yyyy-MM-dd'T'HH:mm:ssxxx")
}

function formatTime(seconds: number): string {
Expand Down