Skip to content

Commit

Permalink
[Lens] Improve caching with more stable Elasticsearch requests (#100414
Browse files Browse the repository at this point in the history
…) (#100776)

Co-authored-by: Joe Reuter <[email protected]>
  • Loading branch information
kibanamachine and flash1293 authored May 27, 2021
1 parent 36e90c2 commit 7b2f4c3
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ describe('IndexPattern Data Source', () => {
true,
],
"id": Array [
"col1",
"0",
],
"schema": Array [
"metric",
Expand Down Expand Up @@ -388,7 +388,7 @@ describe('IndexPattern Data Source', () => {
"timestamp",
],
"id": Array [
"col2",
"1",
],
"interval": Array [
"1d",
Expand Down Expand Up @@ -442,7 +442,7 @@ describe('IndexPattern Data Source', () => {
Object {
"arguments": Object {
"idMap": Array [
"{\\"col-0-col1\\":{\\"label\\":\\"Count of records\\",\\"dataType\\":\\"number\\",\\"isBucketed\\":false,\\"sourceField\\":\\"Records\\",\\"operationType\\":\\"count\\",\\"id\\":\\"col1\\"},\\"col-1-col2\\":{\\"label\\":\\"Date\\",\\"dataType\\":\\"date\\",\\"isBucketed\\":true,\\"operationType\\":\\"date_histogram\\",\\"sourceField\\":\\"timestamp\\",\\"params\\":{\\"interval\\":\\"1d\\"},\\"id\\":\\"col2\\"}}",
"{\\"col-0-0\\":{\\"label\\":\\"Count of records\\",\\"dataType\\":\\"number\\",\\"isBucketed\\":false,\\"sourceField\\":\\"Records\\",\\"operationType\\":\\"count\\",\\"id\\":\\"col1\\"},\\"col-1-1\\":{\\"label\\":\\"Date\\",\\"dataType\\":\\"date\\",\\"isBucketed\\":true,\\"operationType\\":\\"date_histogram\\",\\"sourceField\\":\\"timestamp\\",\\"params\\":{\\"interval\\":\\"1d\\"},\\"id\\":\\"col2\\"}}",
],
},
"function": "lens_rename_columns",
Expand Down Expand Up @@ -563,7 +563,7 @@ describe('IndexPattern Data Source', () => {
"{\\"language\\":\\"kuery\\",\\"query\\":\\"bytes > 5\\"}",
],
"id": Array [
"col1-filter",
"0-filter",
],
"schema": Array [
"bucket",
Expand All @@ -585,7 +585,7 @@ describe('IndexPattern Data Source', () => {
true,
],
"id": Array [
"col1-metric",
"0-metric",
],
"schema": Array [
"metric",
Expand All @@ -602,7 +602,7 @@ describe('IndexPattern Data Source', () => {
true,
],
"id": Array [
"col1",
"0",
],
"schema": Array [
"metric",
Expand Down Expand Up @@ -795,9 +795,9 @@ describe('IndexPattern Data Source', () => {
const ast = indexPatternDatasource.toExpression(state, 'first') as Ast;
expect(ast.chain[0].arguments.metricsAtAllLevels).toEqual([false]);
expect(JSON.parse(ast.chain[1].arguments.idMap[0] as string)).toEqual({
'col-0-bucket1': expect.any(Object),
'col-1-bucket2': expect.any(Object),
'col-2-metric': expect.any(Object),
'col-0-0': expect.objectContaining({ id: 'bucket1' }),
'col-1-1': expect.objectContaining({ id: 'bucket2' }),
'col-2-2': expect.objectContaining({ id: 'metric' }),
});
});

Expand Down Expand Up @@ -919,7 +919,7 @@ describe('IndexPattern Data Source', () => {

const ast = indexPatternDatasource.toExpression(state, 'first') as Ast;
expect(JSON.parse(ast.chain[1].arguments.idMap[0] as string)).toEqual({
'col-0-col1': expect.objectContaining({
'col-0-0': expect.objectContaining({
id: 'col1',
}),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ describe('date_histogram', () => {
'col1',
indexPattern1,
layer,
uiSettingsMock
uiSettingsMock,
[]
);
expect(esAggsFn).toEqual(
expect.objectContaining({
Expand Down Expand Up @@ -258,7 +259,8 @@ describe('date_histogram', () => {
]),
},
layer,
uiSettingsMock
uiSettingsMock,
[]
);
expect(esAggsFn).toEqual(
expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ interface FieldBasedOperationDefinition<C extends BaseIndexPatternColumn> {
columnId: string,
indexPattern: IndexPattern,
layer: IndexPatternLayer,
uiSettings: IUiSettingsClient
uiSettings: IUiSettingsClient,
orderedColumnIds: string[]
) => ExpressionAstFunction;
/**
* Validate that the operation has the right preconditions in the state. For example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ describe('last_value', () => {
'col1',
{} as IndexPattern,
layer,
uiSettingsMock
uiSettingsMock,
[]
);
expect(esAggsFn).toEqual(
expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ describe('percentile', () => {
'col1',
{} as IndexPattern,
layer,
uiSettingsMock
uiSettingsMock,
[]
);
expect(esAggsFn).toEqual(
expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ describe('ranges', () => {
'col1',
{} as IndexPattern,
layer,
uiSettingsMock
uiSettingsMock,
[]
);
expect(esAggsFn).toMatchInlineSnapshot(`
Object {
Expand Down Expand Up @@ -219,7 +220,8 @@ describe('ranges', () => {
'col1',
{} as IndexPattern,
layer,
uiSettingsMock
uiSettingsMock,
[]
);

expect(esAggsFn).toEqual(
Expand All @@ -240,7 +242,8 @@ describe('ranges', () => {
'col1',
{} as IndexPattern,
layer,
uiSettingsMock
uiSettingsMock,
[]
);

expect(esAggsFn).toEqual(
Expand All @@ -261,7 +264,8 @@ describe('ranges', () => {
'col1',
{} as IndexPattern,
layer,
uiSettingsMock
uiSettingsMock,
[]
);

expect((esAggsFn as { arguments: unknown }).arguments).toEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,16 @@ export const termsOperation: OperationDefinition<TermsIndexPatternColumn, 'field
},
};
},
toEsAggsFn: (column, columnId, _indexPattern, layer) => {
toEsAggsFn: (column, columnId, _indexPattern, layer, uiSettings, orderedColumnIds) => {
return buildExpressionFunction<AggFunctionsMapping['aggTerms']>('aggTerms', {
id: columnId,
enabled: true,
schema: 'segment',
field: column.sourceField,
orderBy:
column.params.orderBy.type === 'alphabetical' ? '_key' : column.params.orderBy.columnId,
column.params.orderBy.type === 'alphabetical'
? '_key'
: String(orderedColumnIds.indexOf(column.params.orderBy.columnId)),
order: column.params.orderDirection,
size: column.params.size,
otherBucket: Boolean(column.params.otherBucket),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ describe('terms', () => {
'col1',
{} as IndexPattern,
layer,
uiSettingsMock
uiSettingsMock,
[]
);
expect(esAggsFn).toEqual(
expect.objectContaining({
Expand All @@ -95,7 +96,8 @@ describe('terms', () => {
'col1',
{} as IndexPattern,
layer,
uiSettingsMock
uiSettingsMock,
[]
);
expect(esAggsFn).toEqual(
expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,29 @@ function getExpressionForLayer(
}
});

esAggEntries.forEach(([colId, col]) => {
const orderedColumnIds = esAggEntries.map(([colId]) => colId);
esAggEntries.forEach(([colId, col], index) => {
const def = operationDefinitionMap[col.operationType];
if (def.input !== 'fullReference' && def.input !== 'managedReference') {
const wrapInFilter = Boolean(def.filterable && col.filter);
let aggAst = def.toEsAggsFn(
col,
wrapInFilter ? `${colId}-metric` : colId,
wrapInFilter ? `${index}-metric` : String(index),
indexPattern,
layer,
uiSettings
uiSettings,
orderedColumnIds
);
if (wrapInFilter) {
aggAst = buildExpressionFunction<AggFunctionsMapping['aggFilteredMetric']>(
'aggFilteredMetric',
{
id: colId,
id: String(index),
enabled: true,
schema: 'metric',
customBucket: buildExpression([
buildExpressionFunction<AggFunctionsMapping['aggFilter']>('aggFilter', {
id: `${colId}-filter`,
id: `${index}-filter`,
enabled: true,
schema: 'bucket',
filter: JSON.stringify(col.filter),
Expand All @@ -121,7 +123,7 @@ function getExpressionForLayer(
return null;
}
const idMap = esAggEntries.reduce((currentIdMap, [colId, column], index) => {
const esAggsId = `col-${index}-${colId}`;
const esAggsId = `col-${index}-${index}`;
return {
...currentIdMap,
[esAggsId]: {
Expand Down

0 comments on commit 7b2f4c3

Please sign in to comment.