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

[Backport 2.x] fix: fix create index pattern duplicate cases #338

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
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
80 changes: 43 additions & 37 deletions public/components/incontext_insight/generate_popover_body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const GeneratePopoverBody: React.FC<{
const [insight, setInsight] = useState('');
const [insightAvailable, setInsightAvailable] = useState(false);
const [showInsight, setShowInsight] = useState(false);
const [discoverLoading, setDiscoverLoading] = useState(false);
const metricAppName = 'alertSummary';

const toasts = getNotifications().toasts;
Expand Down Expand Up @@ -180,44 +181,49 @@ export const GeneratePopoverBody: React.FC<{
};

const handleNavigateToDiscover = async () => {
const context = await incontextInsight?.contextProvider?.();
const dsl = context?.additionalInfo?.dsl;
const indexName = context?.additionalInfo?.index;
if (!dsl || !indexName) return;
const dslObject = JSON.parse(dsl);
const filters = dslObject?.query?.bool?.filter;
if (!filters) return;
const timeDslIndex = filters?.findIndex((filter: Record<string, string>) => filter?.range);
const timeDsl = filters[timeDslIndex]?.range;
const timeFieldName = Object.keys(timeDsl)[0];
if (!timeFieldName) return;
filters?.splice(timeDslIndex, 1);
try {
setDiscoverLoading(true);
const context = await incontextInsight?.contextProvider?.();
const dsl = context?.additionalInfo?.dsl;
const indexName = context?.additionalInfo?.index;
if (!dsl || !indexName) return;
const dslObject = JSON.parse(dsl);
const filters = dslObject?.query?.bool?.filter;
if (!filters) return;
const timeDslIndex = filters?.findIndex((filter: Record<string, string>) => filter?.range);
const timeDsl = filters[timeDslIndex]?.range;
const timeFieldName = Object.keys(timeDsl)[0];
if (!timeFieldName) return;
filters?.splice(timeDslIndex, 1);

if (getStartServices) {
const [coreStart, startDeps] = await getStartServices();
const newDiscoverEnabled = coreStart.uiSettings.get(UI_SETTINGS.QUERY_ENHANCEMENTS_ENABLED);
if (!newDiscoverEnabled) {
// Only new discover supports DQL with filters.
coreStart.uiSettings.set(UI_SETTINGS.QUERY_ENHANCEMENTS_ENABLED, true);
}
if (getStartServices) {
const [coreStart, startDeps] = await getStartServices();
const newDiscoverEnabled = coreStart.uiSettings.get(UI_SETTINGS.QUERY_ENHANCEMENTS_ENABLED);
if (!newDiscoverEnabled) {
// Only new discover supports DQL with filters.
coreStart.uiSettings.set(UI_SETTINGS.QUERY_ENHANCEMENTS_ENABLED, true);
}

const indexPattern = await createIndexPatterns(
startDeps.data,
indexName,
timeFieldName,
context?.dataSourceId
);
if (!indexPattern) return;
const query = await buildUrlQuery(
startDeps.data,
coreStart.savedObjects,
indexPattern,
dslObject,
timeDsl[timeFieldName],
context?.dataSourceId
);
// Navigate to new discover with query built to populate
coreStart.application.navigateToUrl(`data-explorer/discover#?${query}`);
const indexPattern = await createIndexPatterns(
startDeps.data,
indexName,
timeFieldName,
context?.dataSourceId
);
if (!indexPattern) return;
const query = await buildUrlQuery(
startDeps.data,
coreStart.savedObjects,
indexPattern,
dslObject,
timeDsl[timeFieldName],
context?.dataSourceId
);
// Navigate to new discover with query built to populate
coreStart.application.navigateToUrl(`data-explorer/discover#?${query}`);
}
} finally {
setDiscoverLoading(false);
}
};

Expand Down Expand Up @@ -317,7 +323,7 @@ export const GeneratePopoverBody: React.FC<{
{renderInnerTitle()}
{renderContent()}
{displayDiscoverButton && (
<EuiButton onClick={handleNavigateToDiscover}>
<EuiButton onClick={handleNavigateToDiscover} isLoading={discoverLoading}>
{i18n.translate('assistantDashboards.incontextInsight.discover', {
defaultMessage: 'Discover details',
})}
Expand Down
12 changes: 6 additions & 6 deletions public/utils/alerting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { url } from '../../../../src/plugins/opensearch_dashboards_utils/public'
import {
DataPublicPluginStart,
opensearchFilters,
DuplicateIndexPatternError,
IndexPattern,
} from '../../../../src/plugins/data/public';
import { CoreStart } from '../../../../src/core/public';
Expand Down Expand Up @@ -48,14 +47,15 @@ export const createIndexPatterns = async (
dataSourceRef,
});
} catch (err) {
if (err instanceof DuplicateIndexPatternError) {
console.error('Create index pattern error', err.message);
// Err instanceof DuplicateIndexPatternError is not a trusted validation in some cases, so find index pattern directly.
try {
const result = await dataStart.indexPatterns.find(patternName);
if (result && result[0]) {
pattern = result[0];
}
console.error('Duplicate index pattern', err.message);
} else {
console.error('err', err.message);
} catch (e) {
console.error('Find index pattern error', err.message);
}
}
return pattern;
Expand Down Expand Up @@ -90,7 +90,7 @@ export const buildUrlQuery = async (
const dataSourceObject = await savedObjects.client.get('data-source', dataSourceId);
const dataSourceTitle = dataSourceObject?.get('title');
// If index pattern refers to a data source, discover list will display data source name as dataSourceTitle::indexPatternTitle
indexPatternTitle = `${dataSourceTitle}::indexPatternTitle`;
indexPatternTitle = `${dataSourceTitle}::${indexPatternTitle}`;
} catch (e) {
console.error('Get data source object error');
}
Expand Down
Loading