Skip to content

fix(site): fix week range for insights #10173

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 1 commit into from
Oct 10, 2023
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
17 changes: 14 additions & 3 deletions site/src/pages/TemplatePage/TemplateInsightsPage/DateRange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import Button from "@mui/material/Button";
import ArrowRightAltOutlined from "@mui/icons-material/ArrowRightAltOutlined";
import Popover from "@mui/material/Popover";
import { DateRangePicker, createStaticRanges } from "react-date-range";
import { format, subDays } from "date-fns";
import {
addDays,
addHours,
format,
isToday,
startOfDay,
startOfHour,
subDays,
} from "date-fns";

// The type definition from @types is wrong
declare module "react-date-range" {
Expand Down Expand Up @@ -46,9 +54,12 @@ export const DateRange = ({
endDate: ranges[0].endDate as Date,
};
const handleClose = () => {
const now = new Date();
onChange({
startDate: currentRange.startDate,
endDate: currentRange.endDate,
startDate: startOfDay(currentRange.startDate),
endDate: isToday(currentRange.endDate)
? startOfHour(addHours(now, 1))
: startOfDay(addDays(currentRange.endDate, 1)),
});
setIsOpen(false);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ import {
UserLatencyInsightsResponse,
} from "api/typesGenerated";
import { ComponentProps, ReactNode } from "react";
import { subDays, addWeeks } from "date-fns";
import { subDays, addWeeks, format } from "date-fns";
import "react-date-range/dist/styles.css";
import "react-date-range/dist/theme/default.css";
import { DateRange as DailyPicker, DateRangeValue } from "./DateRange";
import Link from "@mui/material/Link";
import CheckCircleOutlined from "@mui/icons-material/CheckCircleOutlined";
import CancelOutlined from "@mui/icons-material/CancelOutlined";
import { getDateRangeFilter, lastWeeks } from "./utils";
import { lastWeeks } from "./utils";
import Tooltip from "@mui/material/Tooltip";
import LinkOutlined from "@mui/icons-material/LinkOutlined";
import { InsightsInterval, IntervalMenu } from "./IntervalMenu";
Expand Down Expand Up @@ -70,7 +70,8 @@ export default function TemplateInsightsPage() {

const commonFilters = {
template_ids: template.id,
...getDateRangeFilter(dateRange),
start_time: toISOLocal(dateRange.startDate),
end_time: toISOLocal(dateRange.endDate),
};

const insightsFilter = { ...commonFilters, interval };
Expand Down Expand Up @@ -780,3 +781,7 @@ function formatTime(seconds: number): string {
return hours.toFixed(1) + " hours";
}
}

function toISOLocal(d: Date) {
return format(d, "yyyy-MM-dd'T'HH:mm:ssxxx");
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ export const WeekPicker = ({
}) => {
const anchorRef = useRef<HTMLButtonElement>(null);
const [open, setOpen] = useState(false);
// Why +1? If you get the week 1 and week 2 the diff is 1, but there are 2 weeks
const numberOfWeeks = differenceInWeeks(value.endDate, value.startDate) + 1;
const numberOfWeeks = differenceInWeeks(value.endDate, value.startDate);

const handleClose = () => {
setOpen(false);
Expand Down
36 changes: 0 additions & 36 deletions site/src/pages/TemplatePage/TemplateInsightsPage/utils.test.ts

This file was deleted.

48 changes: 3 additions & 45 deletions site/src/pages/TemplatePage/TemplateInsightsPage/utils.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,8 @@
import {
addDays,
addHours,
format,
startOfDay,
startOfHour,
isToday as isTodayDefault,
startOfWeek,
endOfDay,
endOfWeek,
isSunday,
subWeeks,
} from "date-fns";

type GetDateRangeFilterOptions = {
startDate: Date;
endDate: Date;
// Testing purposes
now?: Date;
isToday?: (date: Date) => boolean;
};

export function getDateRangeFilter(props: GetDateRangeFilterOptions) {
const {
startDate,
endDate,
now = new Date(),
isToday = isTodayDefault,
} = props;

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");
}
import { startOfDay, subDays } from "date-fns";

export const lastWeeks = (numberOfWeeks: number) => {
const now = new Date();
const startDate = startOfWeek(subWeeks(now, numberOfWeeks));
const endDate = isSunday(now) ? endOfDay(now) : endOfWeek(subWeeks(now, 1));
const endDate = startOfDay(subDays(now, 1));
Copy link
Member

@mafredri mafredri Oct 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this perhaps be just startOfDay(now)? Let's say we have now = 2023-10-10T15:00:00 (Tuesday), subDays would put us at 2023-10-09T15:00:00 and startOfDay would give us 2023-10-09T00:00:00 meaning we're only looking at data up to and including Sunday (whereas including Monday would make sense IMO).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startOfDay does something like 2023-10-09T23:59:59 but the API only accepts dates as "00:00:00" at the end.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't understand how startOfDay gives 23:59, I would assume that's endOfDay?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh sorry, I missread your comment. I will give it a try

const startDate = startOfDay(subDays(endDate, 7 * numberOfWeeks));
return { startDate, endDate };
};