Skip to content

Commit

Permalink
feat: 🎸 enable "Explore underlying data" action for Lens vis
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 26, 2020
1 parent b567eb2 commit 8c2e4e3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ export abstract class AbstractExploreDataAction<Context extends { embeddable?: I
public async isCompatible({ embeddable }: Context): Promise<boolean> {
if (!embeddable) return false;
if (!this.params.start().plugins.discover.urlGenerator) return false;
if (!shared.isVisualizeEmbeddable(embeddable)) return false;
if (!shared.getIndexPattern(embeddable)) return false;
if (!shared.hasExactlyOneIndexPattern(embeddable)) return false;
if (embeddable.getInput().viewMode !== ViewMode.VIEW) return false;
return true;
}

public async execute(context: Context): Promise<void> {
if (!shared.isVisualizeEmbeddable(context.embeddable)) return;
if (!shared.hasExactlyOneIndexPattern(context.embeddable)) return;

const { core } = this.params.start();
const { appName, appPath } = await this.getUrl(context);
Expand All @@ -63,7 +62,7 @@ export abstract class AbstractExploreDataAction<Context extends { embeddable?: I
public async getHref(context: Context): Promise<string> {
const { embeddable } = context;

if (!shared.isVisualizeEmbeddable(embeddable)) {
if (!shared.hasExactlyOneIndexPattern(embeddable)) {
throw new Error(`Embeddable not supported for "${this.getDisplayName(context)}" action.`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class ExploreDataChartAction extends AbstractExploreDataAction<ExploreDat
};

if (embeddable) {
state.indexPatternId = shared.getIndexPattern(embeddable) || undefined;
state.indexPatternId = shared.getIndexPatterns(embeddable)[0] || undefined;

const input = embeddable.getInput();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class ExploreDataContextMenuAction extends AbstractExploreDataAction<Embe
const state: DiscoverUrlGeneratorState = {};

if (embeddable) {
state.indexPatternId = shared.getIndexPattern(embeddable) || undefined;
state.indexPatternId = shared.getIndexPatterns(embeddable)[0] || undefined;

const input = embeddable.getInput();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,20 @@
*/

import { IEmbeddable } from '../../../../../../src/plugins/embeddable/public';
import {
VISUALIZE_EMBEDDABLE_TYPE,
VisualizeEmbeddableContract,
} from '../../../../../../src/plugins/visualizations/public';

export const isOutputWithIndexPatterns = (
const isOutputWithIndexPatterns = (
output: unknown
): output is { indexPatterns: Array<{ id: string }> } => {
if (!output || typeof output !== 'object') return false;
return Array.isArray((output as any).indexPatterns);
};

export const isVisualizeEmbeddable = (
embeddable?: IEmbeddable
): embeddable is VisualizeEmbeddableContract =>
embeddable && embeddable?.type === VISUALIZE_EMBEDDABLE_TYPE ? true : false;

/**
* @returns Returns empty string if no index pattern ID found.
*/
export const getIndexPattern = (embeddable?: IEmbeddable): string => {
if (!embeddable) return '';
export const getIndexPatterns = (embeddable?: IEmbeddable): string[] => {
if (!embeddable) return [];
const output = embeddable.getOutput();

if (isOutputWithIndexPatterns(output) && output.indexPatterns.length > 0) {
return output.indexPatterns[0].id;
}

return '';
return isOutputWithIndexPatterns(output) ? output.indexPatterns.map(({ id }) => id) : [];
};

export const hasExactlyOneIndexPattern = (embeddable?: IEmbeddable): string[] =>
getIndexPatterns(embeddable).length === 1;

0 comments on commit 8c2e4e3

Please sign in to comment.