Skip to content

Commit

Permalink
[ML] Use Discover locator in Data Visualizer instead of URL Generator (
Browse files Browse the repository at this point in the history
…#124283)

* use discover locator in data_visualizer to generate url

* pass in discover plugin into context

* format according to linter

* add console messages

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
vadimkibana and kibanamachine authored Feb 14, 2022
1 parent 57aefd0 commit f4713f5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 69 deletions.
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

0 comments on commit f4713f5

Please sign in to comment.