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

[ML] Use Discover locator in Data Visualizer instead of URL Generator #124283

Merged
merged 8 commits into from
Feb 14, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import React, { FC, useState, useEffect } from 'react';
import moment from 'moment';
import { FormattedMessage } from '@kbn/i18n-react';
import { EuiFlexGroup, EuiFlexItem, EuiCard, EuiIcon } from '@elastic/eui';
import {
DISCOVER_APP_URL_GENERATOR,
DiscoverUrlGeneratorState,
} from '../../../../../../../../src/plugins/discover/public';
import { TimeRange, RefreshInterval } from '../../../../../../../../src/plugins/data/public';
import { FindFileStructureResponse } from '../../../../../../file_upload/common';
import type { FileUploadPluginStart } from '../../../../../../file_upload/public';
Expand Down Expand Up @@ -61,9 +57,7 @@ export const ResultsLinks: FC<Props> = ({
services: {
fileUpload,
application: { getUrlForApp, capabilities },
share: {
urlGenerators: { getUrlGenerator },
},
discover,
},
} = useDataVisualizerKibana();

Expand All @@ -83,32 +77,18 @@ export const ResultsLinks: FC<Props> = ({

const getDiscoverUrl = async (): Promise<void> => {
const isDiscoverAvailable = capabilities.discover?.show ?? false;
if (!isDiscoverAvailable) {
if (!isDiscoverAvailable) return;
if (!discover.locator) {
// eslint-disable-next-line no-console
console.error('Discover locator not available');
return;
}

const state: DiscoverUrlGeneratorState = {
const discoverUrl = await discover.locator.getUrl({
indexPatternId,
};

if (globalState?.time) {
state.timeRange = globalState.time;
}

let discoverUrlGenerator;
try {
discoverUrlGenerator = getUrlGenerator(DISCOVER_APP_URL_GENERATOR);
} catch (error) {
// ignore error thrown when url generator is not available
}

if (!discoverUrlGenerator) {
return;
}
const discoverUrl = await discoverUrlGenerator.createUrl(state);
if (!unmounted) {
setDiscoverLink(discoverUrl);
}
timeRange: globalState?.time ? globalState.time : undefined,
});
if (unmounted) return;
setDiscoverLink(discoverUrl);
};

getDiscoverUrl();
Expand Down Expand Up @@ -148,7 +128,7 @@ export const ResultsLinks: FC<Props> = ({
unmounted = true;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [indexPatternId, getUrlGenerator, JSON.stringify(globalState)]);
}, [indexPatternId, discover, JSON.stringify(globalState)]);

useEffect(() => {
updateTimeValues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ interface Props {
export type FileDataVisualizerSpec = typeof FileDataVisualizer;
export const FileDataVisualizer: FC<Props> = ({ additionalLinks }) => {
const coreStart = getCoreStart();
const { data, maps, embeddable, share, security, fileUpload, cloud } = getPluginsStart();
const { data, maps, embeddable, discover, share, security, fileUpload, cloud } =
getPluginsStart();
const services = {
data,
maps,
embeddable,
discover,
share,
security,
fileUpload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import React, { FC, useState, useEffect } from 'react';
import { FormattedMessage } from '@kbn/i18n-react';
import { i18n } from '@kbn/i18n';
import { EuiSpacer, EuiTitle } from '@elastic/eui';
import {
DISCOVER_APP_URL_GENERATOR,
DiscoverUrlGeneratorState,
} from '../../../../../../../../src/plugins/discover/public';
import type { IndexPattern } from '../../../../../../../../src/plugins/data/common';
import { useDataVisualizerKibana } from '../../../kibana_context';
import { useUrlState } from '../../../common/util/url_state';
Expand Down Expand Up @@ -42,9 +38,7 @@ export const ActionsPanel: FC<Props> = ({
services: {
data,
application: { capabilities },
share: {
urlGenerators: { getUrlGenerator },
},
discover,
},
} = useDataVisualizerKibana();

Expand All @@ -54,38 +48,24 @@ export const ActionsPanel: FC<Props> = ({
const indexPatternId = indexPattern.id;
const getDiscoverUrl = async (): Promise<void> => {
const isDiscoverAvailable = capabilities.discover?.show ?? false;
if (!isDiscoverAvailable) {
if (!isDiscoverAvailable) return;
if (!discover.locator) {
// eslint-disable-next-line no-console
console.error('Discover locator not available');
return;
}

const state: DiscoverUrlGeneratorState = {
const discoverUrl = await discover.locator.getUrl({
indexPatternId,
};

state.filters = data.query.filterManager.getFilters() ?? [];

if (searchString && searchQueryLanguage !== undefined) {
state.query = { query: searchString, language: searchQueryLanguage };
}
if (globalState?.time) {
state.timeRange = globalState.time;
}
if (globalState?.refreshInterval) {
state.refreshInterval = globalState.refreshInterval;
}

let discoverUrlGenerator;
try {
discoverUrlGenerator = getUrlGenerator(DISCOVER_APP_URL_GENERATOR);
} catch (error) {
// ignore error thrown when url generator is not available
return;
}

const discoverUrl = await discoverUrlGenerator.createUrl(state);
if (!unmounted) {
setDiscoverLink(discoverUrl);
}
filters: data.query.filterManager.getFilters() ?? [],
query:
searchString && searchQueryLanguage !== undefined
? { query: searchString, language: searchQueryLanguage }
: undefined,
timeRange: globalState?.time ? globalState.time : undefined,
refreshInterval: globalState?.refreshInterval ? globalState.refreshInterval : undefined,
});
if (unmounted) return;
setDiscoverLink(discoverUrl);
};

Promise.all(
Expand Down Expand Up @@ -115,7 +95,7 @@ export const ActionsPanel: FC<Props> = ({
searchQueryLanguage,
globalState,
capabilities,
getUrlGenerator,
discover,
additionalLinks,
data.query,
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ export const IndexDataVisualizer: FC<{ additionalLinks: ResultLink[] }> = ({ add
data,
maps,
embeddable,
discover,
share,
security,
fileUpload,
Expand All @@ -279,6 +280,7 @@ export const IndexDataVisualizer: FC<{ additionalLinks: ResultLink[] }> = ({ add
data,
maps,
embeddable,
discover,
share,
security,
fileUpload,
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/data_visualizer/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ChartsPluginStart } from 'src/plugins/charts/public';
import type { CloudStart } from '../../cloud/public';
import type { EmbeddableSetup, EmbeddableStart } from '../../../../src/plugins/embeddable/public';
import type { SharePluginSetup, SharePluginStart } from '../../../../src/plugins/share/public';
import type { DiscoverSetup, DiscoverStart } from '../../../../src/plugins/discover/public';
import { Plugin } from '../../../../src/core/public';

import { setStartServices } from './kibana_services';
Expand All @@ -32,6 +33,7 @@ export interface DataVisualizerSetupDependencies {
home?: HomePublicPluginSetup;
embeddable: EmbeddableSetup;
share: SharePluginSetup;
discover: DiscoverSetup;
}
export interface DataVisualizerStartDependencies {
data: DataPublicPluginStart;
Expand All @@ -40,6 +42,7 @@ export interface DataVisualizerStartDependencies {
embeddable: EmbeddableStart;
security?: SecurityPluginSetup;
share: SharePluginStart;
discover: DiscoverStart;
lens?: LensPublicStart;
charts: ChartsPluginStart;
dataViewFieldEditor?: IndexPatternFieldEditorStart;
Expand Down