Skip to content

Commit

Permalink
allow user to trigger query to avoid overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
on99 committed Sep 12, 2024
1 parent baf8384 commit 7407708
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions src/client/visualizations/data-provider/data-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ import { Essence } from "../../../common/models/essence/essence";
import { Stage } from "../../../common/models/stage/stage";
import { Timekeeper } from "../../../common/models/timekeeper/timekeeper";
import { debounceWithPromise, Unary } from "../../../common/utils/functional/functional";
import { Loader } from "../../components/loader/loader";
import { Button } from "../../components/button/button";
import { QueryError } from "../../components/query-error/query-error";
import { reportError } from "../../utils/error-reporter/error-reporter";
import { VisualizationQuery } from "../../views/cube-view/api-context";
import { DownloadableDataset, DownloadableDatasetContext } from "../../views/cube-view/downloadable-dataset-context";
import { Loader } from "../../components/loader/loader";

interface DataProviderProps {
refreshRequestTimestamp: number;
Expand All @@ -46,21 +47,17 @@ interface DataProviderProps {

interface DataProviderState {
dataset: DatasetRequest;
requesting: boolean;
}

export class DataProvider extends React.Component<DataProviderProps, DataProviderState> {
static contextType = DownloadableDatasetContext;
context: DownloadableDataset;

state: DataProviderState = { dataset: loading };
state: DataProviderState = { dataset: loading, requesting: false };

private lastQueryEssence: Essence = null;

componentDidMount() {
const { essence } = this.props;
this.loadData(essence);
}

componentWillUnmount() {
this.lastQueryEssence = null;
this.debouncedCallExecutor.cancel();
Expand All @@ -71,12 +68,14 @@ export class DataProvider extends React.Component<DataProviderProps, DataProvide
const { essence } = nextProps;
const hadDataLoaded = isLoaded(this.state.dataset);
const essenceChanged = !essence.equals(this.props.essence);
this.loadData(essence, hadDataLoaded && essenceChanged);
if (hadDataLoaded && essenceChanged) {
this.handleDatasetLoad(loading);
this.state.requesting = false;
}
}
}

private loadData(essence: Essence, showSpinner = true) {
if (showSpinner) this.handleDatasetLoad(loading);
private loadData(essence: Essence) {
this.fetchData(essence)
.then(loadedDataset => {
// TODO: encode it better
Expand All @@ -99,10 +98,10 @@ export class DataProvider extends React.Component<DataProviderProps, DataProvide
private callExecutor = (essence: Essence): Promise<DatasetRequest | null> =>
this.props.query(essence)
.then((dataset: Dataset) => {
// signal out of order requests with null
if (!this.wasUsedForLastQuery(essence)) return null;
return loaded(dataset);
},
// signal out of order requests with null
if (!this.wasUsedForLastQuery(essence)) return null;
return loaded(dataset);
},
err => {
// signal out of order requests with null
if (!this.wasUsedForLastQuery(essence)) return null;
Expand Down Expand Up @@ -153,14 +152,28 @@ export class DataProvider extends React.Component<DataProviderProps, DataProvide
return this.props.stage.equals(nextProps.stage);
}

private sendRequest = () => {
this.setState({ requesting: true });
this.loadData(this.props.essence);
};

render() {
const { children } = this.props;
const { dataset } = this.state;
const { dataset, requesting } = this.state;
switch (dataset.status) {
case DatasetRequestStatus.LOADING:
return <Loader/>;
return requesting ? <Loader /> : (
<div style={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
height: '70vh',
}}>
<Button type="primary" title="Query" onClick={this.sendRequest} />
</div>
);
case DatasetRequestStatus.ERROR:
return <QueryError error={dataset.error}/>;
return <QueryError error={dataset.error} />;
case DatasetRequestStatus.LOADED:
return children(dataset.dataset);
}
Expand Down

0 comments on commit 7407708

Please sign in to comment.