Explore: Removes Promise.All from runQueries thunk (#16957)

pull/16976/head
Hugo Häggmark 6 years ago committed by GitHub
parent 2abb009d68
commit a04b3a13e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 53
      packages/grafana-ui/src/components/SetInterval/SetInterval.tsx
  2. 2
      public/app/features/explore/ExploreToolbar.tsx
  3. 84
      public/app/features/explore/state/actions.ts

@ -1,47 +1,48 @@
import { PureComponent } from 'react'; import { PureComponent } from 'react';
import { interval, Subscription, empty, Subject } from 'rxjs';
import { tap, switchMap } from 'rxjs/operators';
import { stringToMs } from '../../utils/string'; import { stringToMs } from '../../utils/string';
interface Props { interface Props {
func: () => any; // TODO func: () => any; // TODO
loading: boolean;
interval: string; interval: string;
} }
export class SetInterval extends PureComponent<Props> { export class SetInterval extends PureComponent<Props> {
private intervalId = 0; private propsSubject: Subject<Props>;
private subscription: Subscription | null;
componentDidMount() { constructor(props: Props) {
this.addInterval(); super(props);
this.propsSubject = new Subject<Props>();
this.subscription = null;
} }
componentDidUpdate(prevProps: Props) { componentDidMount() {
const { interval } = this.props; this.subscription = this.propsSubject
if (interval !== prevProps.interval) { .pipe(
this.clearInterval(); switchMap(props => {
this.addInterval(); return props.loading ? empty() : interval(stringToMs(props.interval));
} }),
tap(() => this.props.func())
)
.subscribe();
this.propsSubject.next(this.props);
} }
componentWillUnmount() { componentDidUpdate() {
this.clearInterval(); this.propsSubject.next(this.props);
} }
addInterval = () => { componentWillUnmount() {
const { func, interval } = this.props; if (this.subscription) {
this.subscription.unsubscribe();
if (interval) {
func().then(() => {
if (interval) {
this.intervalId = window.setTimeout(() => {
this.addInterval();
}, stringToMs(interval));
}
});
} }
};
clearInterval = () => { this.propsSubject.unsubscribe();
window.clearTimeout(this.intervalId); }
};
render() { render() {
return null; return null;

@ -171,7 +171,7 @@ export class UnConnectedExploreToolbar extends PureComponent<Props, {}> {
value={refreshInterval} value={refreshInterval}
tooltip="Refresh" tooltip="Refresh"
/> />
{refreshInterval && <SetInterval func={this.onRunQuery} interval={refreshInterval} />} {refreshInterval && <SetInterval func={this.onRunQuery} interval={refreshInterval} loading={loading} />}
</div> </div>
<div className="explore-toolbar-content-item"> <div className="explore-toolbar-content-item">

@ -546,7 +546,7 @@ export function queryTransactionSuccess(
/** /**
* Main action to run queries and dispatches sub-actions based on which result viewers are active * Main action to run queries and dispatches sub-actions based on which result viewers are active
*/ */
export function runQueries(exploreId: ExploreId, ignoreUIState = false): ThunkResult<Promise<any>> { export function runQueries(exploreId: ExploreId, ignoreUIState = false): ThunkResult<void> {
return (dispatch, getState) => { return (dispatch, getState) => {
const { const {
datasourceInstance, datasourceInstance,
@ -563,13 +563,13 @@ export function runQueries(exploreId: ExploreId, ignoreUIState = false): ThunkRe
if (datasourceError) { if (datasourceError) {
// let's not run any queries if data source is in a faulty state // let's not run any queries if data source is in a faulty state
return Promise.resolve(); return;
} }
if (!hasNonEmptyQuery(queries)) { if (!hasNonEmptyQuery(queries)) {
dispatch(clearQueriesAction({ exploreId })); dispatch(clearQueriesAction({ exploreId }));
dispatch(stateSave()); // Remember to saves to state and update location dispatch(stateSave()); // Remember to saves to state and update location
return Promise.resolve(); return;
} }
// Some datasource's query builders allow per-query interval limits, // Some datasource's query builders allow per-query interval limits,
@ -578,46 +578,41 @@ export function runQueries(exploreId: ExploreId, ignoreUIState = false): ThunkRe
dispatch(runQueriesAction({ exploreId })); dispatch(runQueriesAction({ exploreId }));
// Keep table queries first since they need to return quickly // Keep table queries first since they need to return quickly
const tableQueriesPromise = if ((ignoreUIState || showingTable) && supportsTable) {
(ignoreUIState || showingTable) && supportsTable dispatch(
? dispatch( runQueriesForType(
runQueriesForType( exploreId,
exploreId, 'Table',
'Table', {
{ interval,
interval, format: 'table',
format: 'table', instant: true,
instant: true, valueWithRefId: true,
valueWithRefId: true, },
}, (data: any[]) => data[0]
(data: any[]) => data[0] )
) );
) }
: undefined; if ((ignoreUIState || showingGraph) && supportsGraph) {
const typeQueriesPromise = dispatch(
(ignoreUIState || showingGraph) && supportsGraph runQueriesForType(
? dispatch( exploreId,
runQueriesForType( 'Graph',
exploreId, {
'Graph', interval,
{ format: 'time_series',
interval, instant: false,
format: 'time_series', maxDataPoints: containerWidth,
instant: false, },
maxDataPoints: containerWidth, makeTimeSeriesList
}, )
makeTimeSeriesList );
) }
) if ((ignoreUIState || showingLogs) && supportsLogs) {
: undefined; dispatch(runQueriesForType(exploreId, 'Logs', { interval, format: 'logs' }));
const logsQueriesPromise = }
(ignoreUIState || showingLogs) && supportsLogs
? dispatch(runQueriesForType(exploreId, 'Logs', { interval, format: 'logs' }))
: undefined;
dispatch(stateSave()); dispatch(stateSave());
return Promise.all([tableQueriesPromise, typeQueriesPromise, logsQueriesPromise]);
}; };
} }
@ -638,7 +633,8 @@ function runQueriesForType(
const { datasourceInstance, eventBridge, queries, queryIntervals, range, scanning } = getState().explore[exploreId]; const { datasourceInstance, eventBridge, queries, queryIntervals, range, scanning } = getState().explore[exploreId];
const datasourceId = datasourceInstance.meta.id; const datasourceId = datasourceInstance.meta.id;
// Run all queries concurrently // Run all queries concurrently
const queryPromises = queries.map(async (query, rowIndex) => { for (let rowIndex = 0; rowIndex < queries.length; rowIndex++) {
const query = queries[rowIndex];
const transaction = buildQueryTransaction( const transaction = buildQueryTransaction(
query, query,
rowIndex, rowIndex,
@ -661,9 +657,7 @@ function runQueriesForType(
eventBridge.emit('data-error', response); eventBridge.emit('data-error', response);
dispatch(queryTransactionFailure(exploreId, transaction.id, response, datasourceId)); dispatch(queryTransactionFailure(exploreId, transaction.id, response, datasourceId));
} }
}); }
return Promise.all(queryPromises);
}; };
} }

Loading…
Cancel
Save