Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ const Details = () => {
if ("undefined" === typeof details) {
throw new Error("Details response is undefined");
}
setBeginDate(dayjs(details.begin_timestamp));
setEndDate(dayjs(details.end_timestamp));
setBeginDate(dayjs.utc(details.begin_timestamp));
setEndDate(dayjs.utc(details.end_timestamp));
setNumFiles(details.num_files);
setNumMessages(details.num_messages);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const TimeRangeInput = () => {
const handleSelectChange = (newTimeRangeOption: TIME_RANGE_OPTION) => {
updateTimeRangeOption(newTimeRangeOption);
if (newTimeRangeOption !== TIME_RANGE_OPTION.CUSTOM) {
const dayJsRange = TIME_RANGE_OPTION_DAYJS_MAP[newTimeRangeOption];
const dayJsRange = TIME_RANGE_OPTION_DAYJS_MAP[newTimeRangeOption]();
updateTimeRange(dayJsRange);
}
};
Expand All @@ -45,6 +45,7 @@ const TimeRangeInput = () => {
if (!isValidDateRange(dates)) {
return;
}

updateTimeRange(dates);
};

Expand All @@ -53,7 +54,7 @@ const TimeRangeInput = () => {
className={styles["timeRangeInputContainer"]}
>
<Select
listHeight={300}
listHeight={400}
options={TIME_RANGE_OPTION_NAMES.map((option) => ({label: option, value: option}))}
popupMatchSelectWidth={false}
size={"large"}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";


dayjs.extend(utc);

/**
* Time range options.
*/
Expand All @@ -21,32 +24,68 @@ enum TIME_RANGE_OPTION {
const DEFAULT_TIME_RANGE = TIME_RANGE_OPTION.ALL_TIME;

/* eslint-disable no-magic-numbers */
const TIME_RANGE_OPTION_DAYJS_MAP: Record<TIME_RANGE_OPTION, [dayjs.Dayjs, dayjs.Dayjs]> = {
[TIME_RANGE_OPTION.LAST_15_MINUTES]: [dayjs().subtract(15, "minute"),
dayjs()],
[TIME_RANGE_OPTION.LAST_HOUR]: [dayjs().subtract(1, "hour"),
dayjs()],
[TIME_RANGE_OPTION.TODAY]: [dayjs().startOf("day"),
dayjs().endOf("day")],
[TIME_RANGE_OPTION.YESTERDAY]: [dayjs().subtract(1, "d"),
dayjs().subtract(2, "d")],
[TIME_RANGE_OPTION.LAST_7_DAYS]: [dayjs().subtract(7, "d"),
dayjs()],
[TIME_RANGE_OPTION.LAST_30_DAYS]: [dayjs().subtract(30, "d"),
dayjs()],
[TIME_RANGE_OPTION.LAST_12_MONTHS]: [dayjs().subtract(12, "month"),
dayjs()],
[TIME_RANGE_OPTION.MONTH_TO_DATE]: [dayjs().startOf("month"),
dayjs()],
[TIME_RANGE_OPTION.YEAR_TO_DATE]: [dayjs().startOf("year"),
dayjs()],
[TIME_RANGE_OPTION.ALL_TIME]: [dayjs(0),
dayjs().add(1, "year")],
const TIME_RANGE_OPTION_DAYJS_MAP: Record<TIME_RANGE_OPTION, () => [dayjs.Dayjs, dayjs.Dayjs]> = {
[TIME_RANGE_OPTION.LAST_15_MINUTES]: () => [
dayjs().utc()
.subtract(15, "minute"),
dayjs().utc(),
],
[TIME_RANGE_OPTION.LAST_HOUR]: () => [
dayjs().utc()
.subtract(1, "hour"),
dayjs().utc(),
],
[TIME_RANGE_OPTION.TODAY]: () => [
dayjs().utc()
.startOf("day"),
dayjs().utc()
.endOf("day"),
],
[TIME_RANGE_OPTION.YESTERDAY]: () => [
dayjs().utc()
.subtract(1, "day")
.startOf("day"),
dayjs().utc()
.subtract(1, "day")
.endOf("day"),
],
[TIME_RANGE_OPTION.LAST_7_DAYS]: () => [
dayjs().utc()
.subtract(7, "day"),
dayjs().utc(),
],
[TIME_RANGE_OPTION.LAST_30_DAYS]: () => [
dayjs().utc()
.subtract(30, "day"),
dayjs().utc(),
],
[TIME_RANGE_OPTION.LAST_12_MONTHS]: () => [
dayjs().utc()
.subtract(12, "month"),
dayjs().utc(),
],
[TIME_RANGE_OPTION.MONTH_TO_DATE]: () => [
dayjs().utc()
.startOf("month"),
dayjs().utc(),
],
[TIME_RANGE_OPTION.YEAR_TO_DATE]: () => [
dayjs().utc()
.startOf("year"),
dayjs().utc(),
],
[TIME_RANGE_OPTION.ALL_TIME]: () => [
dayjs(0).utc(),
dayjs().utc()
.add(1, "year"),
],

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// Custom option is just a placeholder for typing purposes, its DayJs values should not
// be used.
[TIME_RANGE_OPTION.CUSTOM]: [dayjs(),
dayjs()],
[TIME_RANGE_OPTION.CUSTOM]: () => [
dayjs().utc(),
dayjs().utc(),
],
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const searchResultsTableColumns: NonNullable<TableProps<SearchResult>["columns"]
dataIndex: "timestamp",
defaultSortOrder: "descend",
key: "timestamp",
render: (timestamp: number) => dayjs(timestamp).format(DATETIME_FORMAT_TEMPLATE),
render: (timestamp: number) => dayjs.utc(timestamp).format(DATETIME_FORMAT_TEMPLATE),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

sorter: (a, b) => {
const timestampDiff = a.timestamp - b.timestamp;
if (0 !== timestampDiff) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ const SEARCH_STATE_DEFAULT = Object.freeze({
searchJobId: null,
searchResultsMetadata: null,
searchUiState: SEARCH_UI_STATE.DEFAULT,
timeRange: TIME_RANGE_OPTION_DAYJS_MAP[DEFAULT_TIME_RANGE],
timeRange: TIME_RANGE_OPTION_DAYJS_MAP[DEFAULT_TIME_RANGE](),
timeRangeOption: DEFAULT_TIME_RANGE,
timelineConfig: computeTimelineConfig(TIME_RANGE_OPTION_DAYJS_MAP[DEFAULT_TIME_RANGE]),
timelineConfig: computeTimelineConfig(TIME_RANGE_OPTION_DAYJS_MAP[DEFAULT_TIME_RANGE]()),
});
Comment on lines +26 to 29

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Avoid double evaluation of “now” when initialising default state

TIME_RANGE_OPTION_DAYJS_MAP[DEFAULT_TIME_RANGE]() is invoked twice, producing two slightly different ranges. This can cause the initial timelineConfig and the stored timeRange to drift by a few milliseconds.

-    timeRange: TIME_RANGE_OPTION_DAYJS_MAP[DEFAULT_TIME_RANGE](),
-    timeRangeOption: DEFAULT_TIME_RANGE,
-    timelineConfig: computeTimelineConfig(TIME_RANGE_OPTION_DAYJS_MAP[DEFAULT_TIME_RANGE]()),
+    timeRangeOption: DEFAULT_TIME_RANGE,
+    timeRange: (() => {
+        const range = TIME_RANGE_OPTION_DAYJS_MAP[DEFAULT_TIME_RANGE]();
+        return range;
+    })(),
+    timelineConfig: (() => {
+        const sameRange = TIME_RANGE_OPTION_DAYJS_MAP[DEFAULT_TIME_RANGE]();
+        return computeTimelineConfig(sameRange);
+    })(),

Capture the range once and reuse it for consistency.

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In components/log-viewer-webui/client/src/pages/SearchPage/SearchState/index.tsx
around lines 26 to 29, the function
TIME_RANGE_OPTION_DAYJS_MAP[DEFAULT_TIME_RANGE]() is called twice, causing two
slightly different time ranges due to separate evaluations of "now". To fix
this, call the function once, store its result in a variable, and then use that
variable for both timeRange and timelineConfig initialization to ensure
consistency.


interface SearchState {
Expand Down