Skip to content

Commit

Permalink
UI: Select time range with mouse drag
Browse files Browse the repository at this point in the history
  • Loading branch information
yulong-db committed Oct 22, 2024
1 parent 7d95913 commit ab04ef9
Show file tree
Hide file tree
Showing 7 changed files with 455 additions and 3 deletions.
11 changes: 11 additions & 0 deletions pkg/ui/react-app/src/pages/graph/Graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require('../../vendor/flot/jquery.flot');
require('../../vendor/flot/jquery.flot.stack');
require('../../vendor/flot/jquery.flot.time');
require('../../vendor/flot/jquery.flot.crosshair');
require('../../vendor/flot/jquery.flot.selection');
require('jquery.flot.tooltip');

export interface GraphProps {
Expand All @@ -20,6 +21,7 @@ export interface GraphProps {
};
stacked: boolean;
useLocalTime: boolean;
handleTimeRangeSelection: (startTime: number, endTime: number) => void;
queryParams: QueryParams | null;
}

Expand Down Expand Up @@ -66,6 +68,15 @@ class Graph extends PureComponent<GraphProps, GraphState> {

componentDidMount(): void {
this.plot();

$(`.graph-chart`).bind('plotselected', (_, ranges) => {
if (isPresent(this.$chart)) {
// eslint-disable-next-line
// @ts-ignore Typescript doesn't think this method exists although it actually does.
this.$chart.clearSelection();
this.props.handleTimeRangeSelection(ranges.xaxis.from, ranges.xaxis.to);
}
});
}

componentWillUnmount(): void {
Expand Down
3 changes: 3 additions & 0 deletions pkg/ui/react-app/src/pages/graph/GraphHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ export const getOptions = (stacked: boolean, useLocalTime: boolean): jquery.flot
},
shadowSize: 0,
},
selection: {
mode: 'x',
},
};
};

Expand Down
19 changes: 17 additions & 2 deletions pkg/ui/react-app/src/pages/graph/GraphTabContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ interface GraphTabContentProps {
data: any;
stacked: boolean;
useLocalTime: boolean;
handleTimeRangeSelection: (startTime: number, endTime: number) => void;
lastQueryParams: QueryParams | null;
}

export const GraphTabContent: FC<GraphTabContentProps> = ({ data, stacked, useLocalTime, lastQueryParams }) => {
export const GraphTabContent: FC<GraphTabContentProps> = ({
data,
stacked,
useLocalTime,
lastQueryParams,
handleTimeRangeSelection,
}) => {
if (!isPresent(data)) {
return <UncontrolledAlert color="light">No data queried yet</UncontrolledAlert>;
}
Expand All @@ -25,5 +32,13 @@ export const GraphTabContent: FC<GraphTabContentProps> = ({ data, stacked, useLo
</UncontrolledAlert>
);
}
return <Graph data={data} stacked={stacked} useLocalTime={useLocalTime} queryParams={lastQueryParams} />;
return (
<Graph
data={data}
stacked={stacked}
useLocalTime={useLocalTime}
handleTimeRangeSelection={handleTimeRangeSelection}
queryParams={lastQueryParams}
/>
);
};
5 changes: 5 additions & 0 deletions pkg/ui/react-app/src/pages/graph/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,10 @@ class Panel extends Component<PanelProps & PathPrefixProps, PanelState> {
}
};

handleTimeRangeSelection = (startTime: number, endTime: number) => {
this.setOptions({ range: endTime - startTime, endTime: endTime });
};

getExplainOutput = (): void => {
//We need to pass the same parameters as query endpoints, to the explain endpoints.
const endTime = this.getEndTime().valueOf() / 1000;
Expand Down Expand Up @@ -742,6 +746,7 @@ class Panel extends Component<PanelProps & PathPrefixProps, PanelState> {
data={this.state.data}
stacked={options.stacked}
useLocalTime={this.props.useLocalTime}
handleTimeRangeSelection={this.handleTimeRangeSelection}
lastQueryParams={this.state.lastQueryParams}
/>
</>
Expand Down
4 changes: 3 additions & 1 deletion pkg/ui/react-app/src/pages/graph/TimeInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ class TimeInput extends Component<TimeInputProps> {
});

this.$time.on('change.datetimepicker', (e: any) => {
if (e.date) {
// The end time can also be set by dragging a section on the graph,
// and that value will have decimal places.
if (e.date && e.date.valueOf() !== Math.trunc(this.props.time?.valueOf()!)) {
this.props.onChangeTime(e.date.valueOf());
}
});
Expand Down
3 changes: 3 additions & 0 deletions pkg/ui/react-app/src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ declare namespace jquery.flot {
series: { [K in keyof jquery.flot.seriesOptions]: jq.flot.seriesOptions[K] } & {
stack: boolean;
};
selection: {
mode: string;
};
}
}

Expand Down
Loading

0 comments on commit ab04ef9

Please sign in to comment.