mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-10-25 15:56:12 +08:00
(ui): Initialize endTime and duration from URL params if set
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { useEffect } from "react";
|
import { useEffect, useMemo } from "react";
|
||||||
|
import { useLocation } from "react-router-dom";
|
||||||
|
|
||||||
// usePolling repeatedly calls doFn with a fix time delay specified
|
// usePolling repeatedly calls doFn with a fix time delay specified
|
||||||
// by interval (in millisecond).
|
// by interval (in millisecond).
|
||||||
@@ -9,3 +10,9 @@ export function usePolling(doFn: () => void, interval: number) {
|
|||||||
return () => clearInterval(id);
|
return () => clearInterval(id);
|
||||||
}, [interval, doFn]);
|
}, [interval, doFn]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// useQuery gets the URL search params from the current URL.
|
||||||
|
export function useQuery(): URLSearchParams {
|
||||||
|
const { search } = useLocation();
|
||||||
|
return useMemo(() => new URLSearchParams(search), [search]);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { connect, ConnectedProps } from "react-redux";
|
import { connect, ConnectedProps } from "react-redux";
|
||||||
|
import { useHistory } from "react-router-dom";
|
||||||
|
import queryString from "query-string";
|
||||||
import { makeStyles } from "@material-ui/core/styles";
|
import { makeStyles } from "@material-ui/core/styles";
|
||||||
import Container from "@material-ui/core/Container";
|
import Container from "@material-ui/core/Container";
|
||||||
import Grid from "@material-ui/core/Grid";
|
import Grid from "@material-ui/core/Grid";
|
||||||
@@ -9,6 +11,7 @@ import { AppState } from "../store";
|
|||||||
import QueueSizeMetricsChart from "../components/QueueSizeMetricsChart";
|
import QueueSizeMetricsChart from "../components/QueueSizeMetricsChart";
|
||||||
import { currentUnixtime } from "../utils";
|
import { currentUnixtime } from "../utils";
|
||||||
import MetricsFetchControls from "../components/MetricsFetchControls";
|
import MetricsFetchControls from "../components/MetricsFetchControls";
|
||||||
|
import { useQuery } from "../hooks";
|
||||||
|
|
||||||
const useStyles = makeStyles((theme) => ({
|
const useStyles = makeStyles((theme) => ({
|
||||||
container: {
|
container: {
|
||||||
@@ -33,12 +36,48 @@ function mapStateToProps(state: AppState) {
|
|||||||
const connector = connect(mapStateToProps, { getMetricsAsync });
|
const connector = connect(mapStateToProps, { getMetricsAsync });
|
||||||
type Props = ConnectedProps<typeof connector>;
|
type Props = ConnectedProps<typeof connector>;
|
||||||
|
|
||||||
|
const ENDTIME_URL_PARAM_KEY = "end";
|
||||||
|
const DURATION_URL_PARAM_KEY = "duration";
|
||||||
|
|
||||||
function MetricsView(props: Props) {
|
function MetricsView(props: Props) {
|
||||||
const classes = useStyles();
|
const classes = useStyles();
|
||||||
|
const history = useHistory();
|
||||||
|
const query = useQuery();
|
||||||
|
|
||||||
|
const endTimeStr = query.get(ENDTIME_URL_PARAM_KEY);
|
||||||
|
const endTime = endTimeStr ? parseFloat(endTimeStr) : currentUnixtime(); // default to now
|
||||||
|
|
||||||
|
const durationStr = query.get(DURATION_URL_PARAM_KEY);
|
||||||
|
const duration = durationStr ? parseFloat(durationStr) : 60 * 60; // default to 1h
|
||||||
|
|
||||||
const { pollInterval, getMetricsAsync, data } = props;
|
const { pollInterval, getMetricsAsync, data } = props;
|
||||||
|
|
||||||
const [endTimeSec, setEndTimeSec] = React.useState(currentUnixtime());
|
const [endTimeSec, setEndTimeSec] = React.useState(endTime);
|
||||||
const [durationSec, setDurationSec] = React.useState(60 * 60); // 1h
|
const [durationSec, setDurationSec] = React.useState(duration);
|
||||||
|
|
||||||
|
const handleEndTimeChange = (endTime: number) => {
|
||||||
|
const urlQuery = {
|
||||||
|
[ENDTIME_URL_PARAM_KEY]: endTime,
|
||||||
|
[DURATION_URL_PARAM_KEY]: durationSec,
|
||||||
|
};
|
||||||
|
history.push({
|
||||||
|
...history.location,
|
||||||
|
search: queryString.stringify(urlQuery),
|
||||||
|
});
|
||||||
|
setEndTimeSec(endTime);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDurationChange = (duration: number) => {
|
||||||
|
const urlQuery = {
|
||||||
|
[ENDTIME_URL_PARAM_KEY]: endTimeSec,
|
||||||
|
[DURATION_URL_PARAM_KEY]: duration,
|
||||||
|
};
|
||||||
|
history.push({
|
||||||
|
...history.location,
|
||||||
|
search: queryString.stringify(urlQuery),
|
||||||
|
});
|
||||||
|
setDurationSec(duration);
|
||||||
|
};
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
getMetricsAsync(endTimeSec, durationSec);
|
getMetricsAsync(endTimeSec, durationSec);
|
||||||
@@ -51,9 +90,9 @@ function MetricsView(props: Props) {
|
|||||||
<div className={classes.controlsContainer}>
|
<div className={classes.controlsContainer}>
|
||||||
<MetricsFetchControls
|
<MetricsFetchControls
|
||||||
endTimeSec={endTimeSec}
|
endTimeSec={endTimeSec}
|
||||||
onEndTimeChange={setEndTimeSec}
|
onEndTimeChange={handleEndTimeChange}
|
||||||
durationSec={durationSec}
|
durationSec={durationSec}
|
||||||
onDurationChange={setDurationSec}
|
onDurationChange={handleDurationChange}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|||||||
@@ -6,10 +6,11 @@ import Grid from "@material-ui/core/Grid";
|
|||||||
import TasksTable from "../components/TasksTable";
|
import TasksTable from "../components/TasksTable";
|
||||||
import QueueInfoBanner from "../components/QueueInfoBanner";
|
import QueueInfoBanner from "../components/QueueInfoBanner";
|
||||||
import QueueBreadCrumb from "../components/QueueBreadcrumb";
|
import QueueBreadCrumb from "../components/QueueBreadcrumb";
|
||||||
import { useParams, useLocation } from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
import { listQueuesAsync } from "../actions/queuesActions";
|
import { listQueuesAsync } from "../actions/queuesActions";
|
||||||
import { AppState } from "../store";
|
import { AppState } from "../store";
|
||||||
import { QueueDetailsRouteParams } from "../paths";
|
import { QueueDetailsRouteParams } from "../paths";
|
||||||
|
import { useQuery } from "../hooks";
|
||||||
|
|
||||||
function mapStateToProps(state: AppState) {
|
function mapStateToProps(state: AppState) {
|
||||||
return {
|
return {
|
||||||
@@ -34,10 +35,6 @@ const useStyles = makeStyles((theme) => ({
|
|||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
function useQuery(): URLSearchParams {
|
|
||||||
return new URLSearchParams(useLocation().search);
|
|
||||||
}
|
|
||||||
|
|
||||||
const validStatus = [
|
const validStatus = [
|
||||||
"active",
|
"active",
|
||||||
"pending",
|
"pending",
|
||||||
|
|||||||
Reference in New Issue
Block a user