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 @@ -91,6 +91,7 @@ describe('IndexPattern Data Source', () => {

// Private
operationType: 'value',
sourceField: 'op',
},
},
};
Expand Down Expand Up @@ -158,6 +159,46 @@ describe('IndexPattern Data Source', () => {
});
});

describe('#toExpression', () => {
it('should generate an empty expression when no columns are selected', async () => {
const state = await indexPatternDatasource.initialize();
expect(indexPatternDatasource.toExpression(state)).toEqual('');
});

it('should generate an expression for a values query', async () => {
const queryPersistedState: IndexPatternPersistedState = {
currentIndexPatternId: '1',
columnOrder: ['col1', 'col2'],
columns: {
col1: {
operationId: 'op1',
label: 'My Op',
dataType: 'string',
isBucketed: false,

// Private
operationType: 'value',
sourceField: 'op',
},
col2: {
operationId: 'op2',
label: 'My Op 2',
dataType: 'number',
isBucketed: false,

// Private
operationType: 'value',
sourceField: 'op2',
},
},
};
const state = await indexPatternDatasource.initialize(queryPersistedState);
expect(indexPatternDatasource.toExpression(state)).toMatchInlineSnapshot(
`"esdocs index=\\"1\\" fields=\\"op, op2\\" sort=\\"op, DESC\\""`
);
});
});

describe('#getPublicAPI', () => {
let publicAPI: DatasourcePublicAPI;

Expand Down Expand Up @@ -262,6 +303,7 @@ describe('IndexPattern Data Source', () => {
dataType: 'date',
isBucketed: false,
operationType: 'value',
sourceField: 'timestamp',
},
},
columnOrder: ['col1', 'col2'],
Expand Down
13 changes: 12 additions & 1 deletion x-pack/plugins/lens/public/indexpattern_plugin/indexpattern.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface IndexPatternColumn {

// Private
operationType: OperationType;
sourceField: string;
}

export interface IndexPattern {
Expand Down Expand Up @@ -107,6 +108,7 @@ export function IndexPatternDimensionPanel(props: IndexPatternDimensionPanelProp
isBucketed: false,

operationType: 'value' as OperationType,
sourceField: field.name,
}));

const filteredColumns = columns.filter(col => {
Expand Down Expand Up @@ -196,7 +198,16 @@ export function getIndexPatternDatasource(chrome: Chrome, toastNotifications: To
},

toExpression(state: IndexPatternPrivateState) {
return `${JSON.stringify(state.columns)}`;
if (state.columnOrder.length === 0) {
return '';
}

const fieldNames = state.columnOrder.map(col => state.columns[col].sourceField);
const expression = `esdocs index="${state.currentIndexPatternId}" fields="${fieldNames.join(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we need to escape the field names here. Is there a possibility that field names have weird characters, such as " which will cause the expression string to be invalid?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Characters should already have been escaped during JSON deserialization, so I don't see the possible errors here.

', '
)}" sort="${fieldNames[0]}, DESC"`;

return expression;
},

renderDataPanel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ export const xyVisualization: Visualization<XyVisualizationState, XyVisualizatio
}}
render={props.datasource.renderDimensionPanel}
/>
<NativeRenderer
nativeProps={{
columnId: 'col2',
filterOperations: (op: Operation) => true,
suggestedOrder: 2,
}}
render={props.datasource.renderDimensionPanel}
/>
</div>,
domElement
);
Expand Down