|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import { createCoreSetupMock } from '@kbn/core-lifecycle-browser-mocks/src/core_setup.mock'; |
| 9 | +import type { FormBasedLayer, GenericIndexPatternColumn, IndexPattern } from '@kbn/lens-common'; |
| 10 | + |
| 11 | +import { getESQLForLayer } from './to_esql'; |
| 12 | + |
| 13 | +const defaultUiSettingsGet = (key: string) => { |
| 14 | + switch (key) { |
| 15 | + case 'dateFormat': |
| 16 | + return 'MMM D, YYYY @ HH:mm:ss.SSS'; |
| 17 | + case 'dateFormat:scaled': |
| 18 | + return [[]]; |
| 19 | + case 'dateFormat:tz': |
| 20 | + return 'UTC'; |
| 21 | + case 'histogram:barTarget': |
| 22 | + return 50; |
| 23 | + case 'histogram:maxBars': |
| 24 | + return 100; |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +const aggEntries: Array<readonly [string, GenericIndexPatternColumn]> = [ |
| 29 | + [ |
| 30 | + '1', |
| 31 | + { |
| 32 | + label: 'Top 5 values of host.keyword', |
| 33 | + dataType: 'string', |
| 34 | + operationType: 'terms', |
| 35 | + sourceField: 'host.keyword', |
| 36 | + isBucketed: true, |
| 37 | + params: { |
| 38 | + size: 5, |
| 39 | + orderBy: { |
| 40 | + type: 'column', |
| 41 | + columnId: '1', |
| 42 | + }, |
| 43 | + orderDirection: 'desc', |
| 44 | + otherBucket: true, |
| 45 | + missingBucket: false, |
| 46 | + parentFormat: { |
| 47 | + id: 'terms', |
| 48 | + }, |
| 49 | + include: [], |
| 50 | + exclude: [], |
| 51 | + includeIsRegex: false, |
| 52 | + excludeIsRegex: false, |
| 53 | + secondaryFields: [], |
| 54 | + }, |
| 55 | + }, |
| 56 | + ], |
| 57 | + [ |
| 58 | + '2', |
| 59 | + { |
| 60 | + label: 'Average of bytes', |
| 61 | + dataType: 'number', |
| 62 | + operationType: 'average', |
| 63 | + sourceField: 'bytes', |
| 64 | + isBucketed: false, |
| 65 | + params: { |
| 66 | + // emptyAsNull: true, |
| 67 | + }, |
| 68 | + }, |
| 69 | + ], |
| 70 | +]; |
| 71 | + |
| 72 | +const indexPattern = { |
| 73 | + title: 'kibana_sample_data_logs', |
| 74 | + timeFieldName: 'timestamp', |
| 75 | + getFieldByName: (field: string) => { |
| 76 | + if (field === 'records') return undefined; |
| 77 | + return { name: field }; |
| 78 | + }, |
| 79 | + getFormatterForField: () => ({ convert: (v: unknown) => v }), |
| 80 | +} as unknown as IndexPattern; |
| 81 | + |
| 82 | +const layer: FormBasedLayer = { |
| 83 | + columns: { |
| 84 | + '1': { |
| 85 | + label: 'Top 5 values of host.keyword', |
| 86 | + dataType: 'string', |
| 87 | + operationType: 'terms', |
| 88 | + sourceField: 'host.keyword', |
| 89 | + isBucketed: true, |
| 90 | + params: { |
| 91 | + size: 5, |
| 92 | + orderBy: { |
| 93 | + type: 'column', |
| 94 | + columnId: 'd9abe9de-cd59-47d4-b9b3-b0772e489ee5', |
| 95 | + }, |
| 96 | + orderDirection: 'desc', |
| 97 | + otherBucket: true, |
| 98 | + missingBucket: false, |
| 99 | + parentFormat: { |
| 100 | + id: 'terms', |
| 101 | + }, |
| 102 | + include: [], |
| 103 | + exclude: [], |
| 104 | + includeIsRegex: false, |
| 105 | + excludeIsRegex: false, |
| 106 | + secondaryFields: [], |
| 107 | + }, |
| 108 | + }, |
| 109 | + '2': { |
| 110 | + label: 'Average of bytes', |
| 111 | + dataType: 'number', |
| 112 | + operationType: 'average', |
| 113 | + sourceField: 'bytes', |
| 114 | + isBucketed: false, |
| 115 | + params: { |
| 116 | + emptyAsNull: true, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + columnOrder: ['1', '2'], |
| 121 | + incompleteColumns: {}, |
| 122 | + sampling: 1, |
| 123 | + indexPatternId: indexPattern.id, |
| 124 | +}; |
| 125 | + |
| 126 | +const dateRange = { |
| 127 | + fromDate: '2021-01-01T00:00:00.000Z', |
| 128 | + toDate: '2021-01-01T23:59:59.999Z', |
| 129 | +}; |
| 130 | + |
| 131 | +const nowInstant = new Date(); |
| 132 | + |
| 133 | +describe('to_esql top N', () => { |
| 134 | + const { uiSettings } = createCoreSetupMock(); |
| 135 | + uiSettings.get.mockImplementation((key: string) => { |
| 136 | + return defaultUiSettingsGet(key); |
| 137 | + }); |
| 138 | + |
| 139 | + // Note: operationDefinitionMap for "terms" does not support toESQL |
| 140 | + // should generate a valid ESQL query for top N terms and average aggregation |
| 141 | + it('should return undefined for top N terms and average aggregation', () => { |
| 142 | + const result = getESQLForLayer( |
| 143 | + aggEntries, |
| 144 | + layer, |
| 145 | + indexPattern, |
| 146 | + uiSettings, |
| 147 | + dateRange, |
| 148 | + nowInstant |
| 149 | + ); |
| 150 | + |
| 151 | + expect(result?.esql).toEqual(undefined); |
| 152 | + }); |
| 153 | +}); |
0 commit comments