Skip to content

Commit

Permalink
[TSVB] Fields dropdowns are not populated if one of the indices is mi…
Browse files Browse the repository at this point in the history
…ssing (#77363) (#78123)

* Fields dropdowns are not populated if one of the indices is missing

* Fix tests and accept api changes

* Add indexPatternsService to get default index pattern

* Replace simple error message string with i18n translation

* Remove unnecessary displaying error code

Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
DianaDerevyankina and elasticmachine authored Sep 22, 2020
1 parent 2a6e6e0 commit b03c8bc
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ Get a list of field objects for an index pattern that may contain wildcards
getFieldsForWildcard(options: {
pattern: string | string[];
metaFields?: string[];
fieldCapsOptions?: {
allowNoIndices: boolean;
};
}): Promise<FieldDescriptor[]>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| options | <code>{</code><br/><code> pattern: string &#124; string[];</code><br/><code> metaFields?: string[];</code><br/><code> }</code> | |
| options | <code>{</code><br/><code> pattern: string &#124; string[];</code><br/><code> metaFields?: string[];</code><br/><code> fieldCapsOptions?: {</code><br/><code> allowNoIndices: boolean;</code><br/><code> };</code><br/><code> }</code> | |

<b>Returns:</b>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ export class IndexPatternsFetcher {
async getFieldsForWildcard(options: {
pattern: string | string[];
metaFields?: string[];
fieldCapsOptions?: { allowNoIndices: boolean };
}): Promise<FieldDescriptor[]> {
const { pattern, metaFields } = options;
return await getFieldCapabilities(this._callDataCluster, pattern, metaFields);
const { pattern, metaFields, fieldCapsOptions } = options;
return await getFieldCapabilities(this._callDataCluster, pattern, metaFields, fieldCapsOptions);
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/plugins/data/server/index_patterns/fetcher/lib/es_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,20 @@ export async function callIndexAliasApi(
*
* @param {Function} callCluster bound function for accessing an es client
* @param {Array<String>|String} indices
* @param {Object} fieldCapsOptions
* @return {Promise<FieldCapsResponse>}
*/
export async function callFieldCapsApi(callCluster: LegacyAPICaller, indices: string[] | string) {
export async function callFieldCapsApi(
callCluster: LegacyAPICaller,
indices: string[] | string,
fieldCapsOptions: { allowNoIndices: boolean } = { allowNoIndices: false }
) {
try {
return (await callCluster('fieldCaps', {
index: indices,
fields: '*',
ignoreUnavailable: true,
allowNoIndices: false,
...fieldCapsOptions,
})) as FieldCapsResponse;
} catch (error) {
throw convertEsError(indices, error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('index_patterns/field_capabilities/field_capabilities', () => {

await getFieldCapabilities(footballs[0], footballs[1]);
sinon.assert.calledOnce(callFieldCapsApi);
calledWithExactly(callFieldCapsApi, [footballs[0], footballs[1]]);
calledWithExactly(callFieldCapsApi, [footballs[0], footballs[1], undefined]);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,20 @@ import { FieldDescriptor } from '../../index_patterns_fetcher';
* @param {Function} callCluster bound function for accessing an es client
* @param {Array} [indices=[]] the list of indexes to check
* @param {Array} [metaFields=[]] the list of internal fields to include
* @param {Object} fieldCapsOptions
* @return {Promise<Array<FieldDescriptor>>}
*/
export async function getFieldCapabilities(
callCluster: LegacyAPICaller,
indices: string | string[] = [],
metaFields: string[] = []
metaFields: string[] = [],
fieldCapsOptions?: { allowNoIndices: boolean }
) {
const esFieldCaps: FieldCapsResponse = await callFieldCapsApi(callCluster, indices);
const esFieldCaps: FieldCapsResponse = await callFieldCapsApi(
callCluster,
indices,
fieldCapsOptions
);
const fieldsFromFieldCapsByName = keyBy(readFieldCapsResponse(esFieldCaps), 'name');

const allFieldsUnsorted = Object.keys(fieldsFromFieldCapsByName)
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/data/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,9 @@ export class IndexPatternsFetcher {
getFieldsForWildcard(options: {
pattern: string | string[];
metaFields?: string[];
fieldCapsOptions?: {
allowNoIndices: boolean;
};
}): Promise<IndexPatternFieldDescriptor[]>;
}

Expand Down
21 changes: 15 additions & 6 deletions src/plugins/vis_type_timeseries/server/lib/get_fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
* specific language governing permissions and limitations
* under the License.
*/
import { uniqBy } from 'lodash';
import { uniqBy, get } from 'lodash';
import { first, map } from 'rxjs/operators';
import { KibanaRequest, RequestHandlerContext } from 'kibana/server';

// @ts-ignore
import { getIndexPatternObject } from './vis_data/helpers/get_index_pattern';
import { indexPatterns } from '../../../data/server';
import { Framework } from '../plugin';
import { IndexPatternFieldDescriptor, IndexPatternsFetcher } from '../../../data/server';
import {
indexPatterns,
IndexPatternFieldDescriptor,
IndexPatternsFetcher,
} from '../../../data/server';
import { ReqFacade } from './search_strategies/strategies/abstract_search_strategy';

export async function getFields(
Expand Down Expand Up @@ -58,7 +59,15 @@ export async function getFields(
.toPromise();
},
};
const { indexPatternString } = await getIndexPatternObject(reqFacade, indexPattern);
let indexPatternString = indexPattern;

if (!indexPatternString) {
const [, { data }] = await framework.core.getStartServices();
const indexPatternsService = await data.indexPatterns.indexPatternsServiceFactory(request);
const defaultIndexPattern = await indexPatternsService.getDefault();
indexPatternString = get(defaultIndexPattern, 'title', '');
}

const {
searchStrategy,
capabilities,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('AbstractSearchStrategy', () => {
expect(fields).toBe(mockedFields);
expect(req.pre.indexPatternsService.getFieldsForWildcard).toHaveBeenCalledWith({
pattern: indexPattern,
fieldCapsOptions: { allowNoIndices: true },
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class AbstractSearchStrategy {

return await indexPatternsService!.getFieldsForWildcard({
pattern: indexPattern,
fieldCapsOptions: { allowNoIndices: true },
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
* under the License.
*/

import { get } from 'lodash';

const DEFAULT_TIME_FIELD = '@timestamp';

export function getIntervalAndTimefield(panel, series = {}, indexPatternObject) {
const getDefaultTimeField = () => get(indexPatternObject, 'timeFieldName', DEFAULT_TIME_FIELD);
const getDefaultTimeField = () => indexPatternObject?.timeFieldName ?? DEFAULT_TIME_FIELD;

const timeField =
(series.override_index_pattern && series.series_time_field) ||
Expand Down

0 comments on commit b03c8bc

Please sign in to comment.