Skip to content
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 @@ -8,14 +8,25 @@ import React from 'react';
import { ReactWrapper } from 'enzyme';
import { mountWithIntl as mount } from 'test_utils/enzyme_helpers';
import { EditorFrame } from './editor_frame';
import { Visualization, Datasource, DatasourcePublicAPI } from '../../types';
import { Visualization, Datasource, DatasourcePublicAPI, DatasourceSuggestion } from '../../types';
import { act } from 'react-dom/test-utils';
import { createMockVisualization, createMockDatasource } from '../mock_extensions';

// calling this function will wait for all pending Promises from mock
// datasources to be processed by its callers.
const waitForPromises = () => new Promise(resolve => setTimeout(resolve));

function generateSuggestion(datasourceSuggestionId = 1, state = {}): DatasourceSuggestion {
return {
state: {},
table: {
columns: [],
datasourceSuggestionId: 1,
isMultiRow: true,
},
};
}

describe('editor_frame', () => {
let mockVisualization: Visualization;
let mockDatasource: Datasource;
Expand Down Expand Up @@ -475,13 +486,13 @@ describe('editor_frame', () => {
...mockVisualization,
getSuggestions: () => [
{
tableIndex: 0,
datasourceSuggestionId: 0,
score: 0.5,
state: {},
title: 'Suggestion2',
},
{
tableIndex: 0,
datasourceSuggestionId: 0,
score: 0.8,
state: {},
title: 'Suggestion1',
Expand All @@ -492,13 +503,13 @@ describe('editor_frame', () => {
...mockVisualization,
getSuggestions: () => [
{
tableIndex: 0,
datasourceSuggestionId: 0,
score: 0.4,
state: {},
title: 'Suggestion4',
},
{
tableIndex: 0,
datasourceSuggestionId: 0,
score: 0.45,
state: {},
title: 'Suggestion3',
Expand All @@ -509,7 +520,7 @@ describe('editor_frame', () => {
datasourceMap={{
testDatasource: {
...mockDatasource,
getDatasourceSuggestionsFromCurrentState: () => [{ state: {}, tableColumns: [] }],
getDatasourceSuggestionsFromCurrentState: () => [generateSuggestion()],
},
}}
initialDatasourceId="testDatasource"
Expand Down Expand Up @@ -540,7 +551,7 @@ describe('editor_frame', () => {
...mockVisualization,
getSuggestions: () => [
{
tableIndex: 0,
datasourceSuggestionId: 0,
score: 0.8,
state: suggestionVisState,
title: 'Suggestion1',
Expand All @@ -552,9 +563,7 @@ describe('editor_frame', () => {
datasourceMap={{
testDatasource: {
...mockDatasource,
getDatasourceSuggestionsFromCurrentState: () => [
{ state: newDatasourceState, tableColumns: [] },
],
getDatasourceSuggestionsFromCurrentState: () => [generateSuggestion()],
},
}}
initialDatasourceId="testDatasource"
Expand Down Expand Up @@ -595,13 +604,13 @@ describe('editor_frame', () => {
...mockVisualization,
getSuggestions: () => [
{
tableIndex: 0,
datasourceSuggestionId: 0,
score: 0.2,
state: {},
title: 'Suggestion1',
},
{
tableIndex: 0,
datasourceSuggestionId: 0,
score: 0.8,
state: suggestionVisState,
title: 'Suggestion2',
Expand All @@ -613,8 +622,8 @@ describe('editor_frame', () => {
datasourceMap={{
testDatasource: {
...mockDatasource,
getDatasourceSuggestionsForField: () => [{ state: {}, tableColumns: [] }],
getDatasourceSuggestionsFromCurrentState: () => [{ state: {}, tableColumns: [] }],
getDatasourceSuggestionsForField: () => [generateSuggestion()],
getDatasourceSuggestionsFromCurrentState: () => [generateSuggestion()],
},
}}
initialDatasourceId="testDatasource"
Expand Down Expand Up @@ -648,13 +657,13 @@ describe('editor_frame', () => {
...mockVisualization,
getSuggestions: () => [
{
tableIndex: 0,
datasourceSuggestionId: 0,
score: 0.2,
state: {},
title: 'Suggestion1',
},
{
tableIndex: 0,
datasourceSuggestionId: 0,
score: 0.6,
state: {},
title: 'Suggestion2',
Expand All @@ -665,7 +674,7 @@ describe('editor_frame', () => {
...mockVisualization2,
getSuggestions: () => [
{
tableIndex: 0,
datasourceSuggestionId: 0,
score: 0.8,
state: suggestionVisState,
title: 'Suggestion3',
Expand All @@ -676,8 +685,8 @@ describe('editor_frame', () => {
datasourceMap={{
testDatasource: {
...mockDatasource,
getDatasourceSuggestionsForField: () => [{ state: {}, tableColumns: [] }],
getDatasourceSuggestionsFromCurrentState: () => [{ state: {}, tableColumns: [] }],
getDatasourceSuggestionsForField: () => [generateSuggestion()],
getDatasourceSuggestionsFromCurrentState: () => [generateSuggestion()],
},
}}
initialDatasourceId="testDatasource"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@

import { getSuggestions } from './suggestion_helpers';
import { createMockVisualization } from '../mock_extensions';
import { TableColumn } from '../../types';
import { TableSuggestion } from '../../types';

const generateSuggestion = (datasourceSuggestionId: number = 1, state = {}) => ({
state,
table: { datasourceSuggestionId, columns: [], isMultiRow: false },
});

describe('suggestion helpers', () => {
it('should return suggestions array', () => {
const mockVisualization = createMockVisualization();
const suggestedState = {};
const suggestions = getSuggestions(
[{ state: {}, tableColumns: [] }],
[generateSuggestion()],
{
vis1: {
...mockVisualization,
getSuggestions: () => [
{ tableIndex: 0, score: 0.5, title: 'Test', state: suggestedState },
{ datasourceSuggestionId: 0, score: 0.5, title: 'Test', state: suggestedState },
],
},
},
Expand All @@ -33,18 +38,20 @@ describe('suggestion helpers', () => {
const mockVisualization1 = createMockVisualization();
const mockVisualization2 = createMockVisualization();
const suggestions = getSuggestions(
[{ state: {}, tableColumns: [] }],
[generateSuggestion()],
{
vis1: {
...mockVisualization1,
getSuggestions: () => [
{ tableIndex: 0, score: 0.5, title: 'Test', state: {} },
{ tableIndex: 0, score: 0.5, title: 'Test2', state: {} },
{ datasourceSuggestionId: 0, score: 0.5, title: 'Test', state: {} },
{ datasourceSuggestionId: 0, score: 0.5, title: 'Test2', state: {} },
],
},
vis2: {
...mockVisualization2,
getSuggestions: () => [{ tableIndex: 0, score: 0.5, title: 'Test3', state: {} }],
getSuggestions: () => [
{ datasourceSuggestionId: 0, score: 0.5, title: 'Test3', state: {} },
],
},
},
'vis1',
Expand All @@ -57,18 +64,20 @@ describe('suggestion helpers', () => {
const mockVisualization1 = createMockVisualization();
const mockVisualization2 = createMockVisualization();
const suggestions = getSuggestions(
[{ state: {}, tableColumns: [] }],
[generateSuggestion()],
{
vis1: {
...mockVisualization1,
getSuggestions: () => [
{ tableIndex: 0, score: 0.2, title: 'Test', state: {} },
{ tableIndex: 0, score: 0.8, title: 'Test2', state: {} },
{ datasourceSuggestionId: 0, score: 0.2, title: 'Test', state: {} },
{ datasourceSuggestionId: 0, score: 0.8, title: 'Test2', state: {} },
],
},
vis2: {
...mockVisualization2,
getSuggestions: () => [{ tableIndex: 0, score: 0.6, title: 'Test3', state: {} }],
getSuggestions: () => [
{ datasourceSuggestionId: 0, score: 0.6, title: 'Test3', state: {} },
],
},
},
'vis1',
Expand All @@ -82,10 +91,10 @@ describe('suggestion helpers', () => {
it('should call all suggestion getters with all available data tables', () => {
const mockVisualization1 = createMockVisualization();
const mockVisualization2 = createMockVisualization();
const table1: TableColumn[] = [];
const table2: TableColumn[] = [];
const table1: TableSuggestion = { datasourceSuggestionId: 0, columns: [], isMultiRow: true };
const table2: TableSuggestion = { datasourceSuggestionId: 1, columns: [], isMultiRow: true };
getSuggestions(
[{ state: {}, tableColumns: table1 }, { state: {}, tableColumns: table2 }],
[{ state: {}, table: table1 }, { state: {}, table: table2 }],
{
vis1: mockVisualization1,
vis2: mockVisualization2,
Expand All @@ -105,18 +114,20 @@ describe('suggestion helpers', () => {
const tableState1 = {};
const tableState2 = {};
const suggestions = getSuggestions(
[{ state: tableState1, tableColumns: [] }, { state: tableState2, tableColumns: [] }],
[generateSuggestion(1, tableState1), generateSuggestion(1, tableState2)],
{
vis1: {
...mockVisualization1,
getSuggestions: () => [
{ tableIndex: 0, score: 0.3, title: 'Test', state: {} },
{ tableIndex: 1, score: 0.2, title: 'Test2', state: {} },
{ datasourceSuggestionId: 0, score: 0.3, title: 'Test', state: {} },
{ datasourceSuggestionId: 1, score: 0.2, title: 'Test2', state: {} },
],
},
vis2: {
...mockVisualization2,
getSuggestions: () => [{ tableIndex: 1, score: 0.1, title: 'Test3', state: {} }],
getSuggestions: () => [
{ datasourceSuggestionId: 1, score: 0.1, title: 'Test3', state: {} },
],
},
},
'vis1',
Expand All @@ -132,7 +143,7 @@ describe('suggestion helpers', () => {
const mockVisualization2 = createMockVisualization();
const currentState = {};
getSuggestions(
[{ state: {}, tableColumns: [] }, { state: {}, tableColumns: [] }],
[generateSuggestion(1), generateSuggestion(2)],
{
vis1: mockVisualization1,
vis2: mockVisualization2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function getSuggestions(
activeVisualizationId: string | null,
visualizationState: unknown
): Suggestion[] {
const datasourceTables = datasourceTableSuggestions.map(({ tableColumns }) => tableColumns);
const datasourceTables = datasourceTableSuggestions.map(({ table }) => table);

return (
Object.entries(visualizationMap)
Expand All @@ -39,7 +39,7 @@ export function getSuggestions(
tables: datasourceTables,
state: visualizationId === activeVisualizationId ? visualizationState : undefined,
})
.map(({ tableIndex: datasourceSuggestionId, ...suggestion }) => ({
.map(({ datasourceSuggestionId, ...suggestion }) => ({
...suggestion,
visualizationId,
datasourceState: datasourceTableSuggestions[datasourceSuggestionId].state,
Expand Down
13 changes: 10 additions & 3 deletions x-pack/plugins/lens/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface EditorFrameInstance {
mount: (element: Element) => void;
unmount: () => void;
}

export interface EditorFrameSetup {
createInstance: (options: EditorFrameOptions) => EditorFrameInstance;
// generic type on the API functions to pull the "unknown vs. specific type" error into the implementation
Expand All @@ -26,9 +27,15 @@ export interface TableColumn {
operation: Operation;
}

export interface TableSuggestion {
datasourceSuggestionId: number;
isMultiRow: boolean;
columns: TableColumn[];
}

export interface DatasourceSuggestion<T = unknown> {
state: T;
tableColumns: TableColumn[];
table: TableSuggestion;
}

/**
Expand Down Expand Up @@ -130,15 +137,15 @@ export interface VisualizationProps<T = unknown> {

export interface SuggestionRequest<T = unknown> {
// It is up to the Visualization to rank these tables
tables: TableColumn[][];
tables: TableSuggestion[];
state?: T; // State is only passed if the visualization is active
}

export interface VisualizationSuggestion<T = unknown> {
score: number;
title: string;
state: T;
tableIndex: number;
datasourceSuggestionId: number;
}

export interface Visualization<T = unknown, P = unknown> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,16 @@ function sampleArgs() {
seriesType: 'line',
title: 'My fanci line chart',
legend: {
type: 'lens_xy_legendConfig',
isVisible: false,
position: Position.Top,
},
y: {
type: 'lens_xy_yConfig',
accessors: ['a', 'b'],
position: Position.Left,
showGridlines: false,
title: 'A and B',
},
x: {
type: 'lens_xy_xConfig',
accessor: 'c',
position: Position.Bottom,
showGridlines: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ export const xConfig: ContextFunction<'lens_xy_xConfig', null, XConfig, XConfigR
export interface XYArgs {
seriesType: 'bar' | 'line' | 'area';
title: string;
legend: LegendConfigResult;
y: YConfigResult;
x: XConfigResult;
legend: LegendConfig;
y: YConfig;
x: XConfig;
splitSeriesAccessors: string[];
stackAccessors: string[];
}
Expand Down
Loading