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

fix dashboard index pattern race condition #72899

Merged
Merged
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 @@ -25,8 +25,8 @@ import React, { useState, ReactElement } from 'react';
import ReactDOM from 'react-dom';
import angular from 'angular';

import { Subscription } from 'rxjs';
import { map } from 'rxjs/operators';
import { Observable, pipe, Subscription } from 'rxjs';
import { filter, map, mapTo, startWith, switchMap } from 'rxjs/operators';
import { History } from 'history';
import { SavedObjectSaveOpts } from 'src/plugins/saved_objects/public';
import { NavigationPublicPluginStart as NavigationStart } from 'src/plugins/navigation/public';
Expand Down Expand Up @@ -253,11 +253,7 @@ export class DashboardAppController {
navActions[TopNavIds.VISUALIZE]();
};

const updateIndexPatterns = (container?: DashboardContainer) => {
if (!container || isErrorEmbeddable(container)) {
return;
}

function getDashboardIndexPatterns(container: DashboardContainer): IndexPattern[] {
let panelIndexPatterns: IndexPattern[] = [];
Object.values(container.getChildIds()).forEach((id) => {
const embeddableInstance = container.getChild(id);
Expand All @@ -267,19 +263,34 @@ export class DashboardAppController {
panelIndexPatterns.push(...embeddableIndexPatterns);
});
panelIndexPatterns = uniqBy(panelIndexPatterns, 'id');
return panelIndexPatterns;
}

if (panelIndexPatterns && panelIndexPatterns.length > 0) {
$scope.$evalAsync(() => {
$scope.indexPatterns = panelIndexPatterns;
});
} else {
indexPatterns.getDefault().then((defaultIndexPattern) => {
$scope.$evalAsync(() => {
$scope.indexPatterns = [defaultIndexPattern as IndexPattern];
});
const updateIndexPatternsOperator = pipe(
filter((container: DashboardContainer) => !!container && !isErrorEmbeddable(container)),
map(getDashboardIndexPatterns),
// using switchMap for previous task cancellation
switchMap((panelIndexPatterns: IndexPattern[]) => {
Dosant marked this conversation as resolved.
Show resolved Hide resolved
return new Observable((observer) => {
if (panelIndexPatterns && panelIndexPatterns.length > 0) {
$scope.$evalAsync(() => {
Dosant marked this conversation as resolved.
Show resolved Hide resolved
if (observer.closed) return;
$scope.indexPatterns = panelIndexPatterns;
observer.complete();
});
} else {
indexPatterns.getDefault().then((defaultIndexPattern) => {
if (observer.closed) return;
$scope.$evalAsync(() => {
if (observer.closed) return;
$scope.indexPatterns = [defaultIndexPattern as IndexPattern];
observer.complete();
});
});
}
});
}
};
})
);

const getEmptyScreenProps = (
shouldShowEditHelp: boolean,
Expand Down Expand Up @@ -384,11 +395,17 @@ export class DashboardAppController {
) : null;
};

updateIndexPatterns(dashboardContainer);

outputSubscription = dashboardContainer.getOutput$().subscribe(() => {
updateIndexPatterns(dashboardContainer);
});
outputSubscription = new Subscription();
outputSubscription.add(
dashboardContainer
.getOutput$()
.pipe(
mapTo(dashboardContainer),
startWith(dashboardContainer), // to trigger initial index pattern update
updateIndexPatternsOperator
)
.subscribe()
);

inputSubscription = dashboardContainer.getInput$().subscribe(() => {
let dirty = false;
Expand Down