Skip to content
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

ui: encode time window in url for metrics charts #101258

Merged
merged 1 commit into from
Apr 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,6 @@ export class LineGraph extends React.Component<LineGraphProps, {}> {
}
this.props.setMetricsFixedWindow(newTimeWindow);
this.props.setTimeScale(newTimeScale);
const { pathname, search } = this.props.history.location;
const urlParams = new URLSearchParams(search);

this.props.history.push({
pathname,
search: urlParams.toString(),
});
}

u: uPlot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,8 @@ import {
selectTimeScale,
} from "src/redux/timeScale";
import { InlineAlert } from "src/components";
import {
Anchor,
TimeScaleDropdown,
TimeScale,
} from "@cockroachlabs/cluster-ui";
import TimeScaleDropdown from "../timeScaleDropdownWithSearchParams";
import { Anchor, TimeScale } from "@cockroachlabs/cluster-ui";
import { reduceStorageOfTimeSeriesDataOperationalFlags } from "src/util/docs";
import moment from "moment-timezone";
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from "@cockroachlabs/cluster-ui";
import { createSelector } from "reselect";
import moment from "moment-timezone";
import { PayloadAction } from "src/interfaces/action";

// The time scale dropdown from cluster-ui that updates route params as
// options are selected.
Expand Down Expand Up @@ -79,39 +80,41 @@ const TimeScaleDropdownWithSearchParams = (
const urlSearchParams = new URLSearchParams(history.location.search);
const queryStart = urlSearchParams.get("start");
const queryEnd = urlSearchParams.get("end");
const start = queryStart && moment.unix(Number(queryStart)).utc();
const end = queryEnd && moment.unix(Number(queryEnd)).utc();

setDatesByQueryParams({ start, end });
// Only set the timescale if the url params exist.
if (queryStart !== null && queryEnd !== null) {
const start = queryStart && moment.unix(Number(queryStart)).utc();
const end = queryEnd && moment.unix(Number(queryEnd)).utc();
setDatesByQueryParams({ start, end });
}

// Passing an empty array of dependencies will cause this effect
// to only run on the initial render.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const setQueryParamsByDates = (
duration: moment.Duration,
dateEnd: moment.Moment,
) => {
const onTimeScaleChange = (timeScale: TimeScale) => {
props.setTimeScale(timeScale);
};

useEffect(() => {
// When history or props change, this effect will
// convert the start and end of the current time scale and
// write them to the URL as query params.
const duration = props.currentScale.windowSize;
const dateEnd = props.currentScale.fixedWindowEnd || moment.utc();
const { pathname, search } = history.location;
const urlParams = new URLSearchParams(search);
const seconds = duration.clone().asSeconds();
const end = dateEnd.clone();
const start = moment.utc(end).subtract(seconds, "seconds").format("X");

urlParams.set("start", start);
urlParams.set("end", moment.utc(dateEnd).format("X"));

history.push({
pathname,
search: urlParams.toString(),
});
};

const onTimeScaleChange = (timeScale: TimeScale) => {
props.setTimeScale(timeScale);
setQueryParamsByDates(
timeScale.windowSize,
timeScale.fixedWindowEnd || moment.utc(),
);
};
}, [history, props]);

return <TimeScaleDropdown {...props} setTimeScale={onTimeScaleChange} />;
};
Expand All @@ -121,7 +124,11 @@ const scaleSelector = createSelector(
tw => tw?.scale,
);

export default connect(
export default connect<
{ currentScale: TimeScale },
{ setTimeScale: (ts: TimeScale) => PayloadAction<TimeScale> },
Partial<TimeScaleDropdownProps>
>(
(state: AdminUIState) => ({
currentScale: scaleSelector(state),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ import { LineGraph } from "src/views/cluster/components/linegraph";
import { DropdownOption } from "src/views/shared/components/dropdown";
import { MetricsDataProvider } from "src/views/shared/containers/metricDataProvider";
import { Metric, Axis } from "src/views/shared/components/metricQuery";
import {
AxisUnits,
TimeScaleDropdown,
TimeScale,
} from "@cockroachlabs/cluster-ui";
import TimeScaleDropdown from "oss/src/views/cluster/containers/timeScaleDropdownWithSearchParams";
import { AxisUnits, TimeScale } from "@cockroachlabs/cluster-ui";
import {
PageConfig,
PageConfigItem,
Expand Down