Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TSVB] Fields dropdowns are not populated if one of the indices is missing #77363

Merged
merged 10 commits into from
Sep 22, 2020
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 @@ -687,6 +687,9 @@ export class IndexPatternsFetcher {
getFieldsForWildcard(options: {
pattern: string | string[];
metaFields?: string[];
fieldCapsOptions?: {
allowNoIndices: boolean;
};
}): Promise<IndexPatternFieldDescriptor[]>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export function Visualization(props) {
const { visData, model } = props;
// Show the error panel
const error = _.get(visData, `${model.id}.error`);
if (_.get(error, 'error.type') === 'index_not_found_exception') {
const index = _.get(error, 'error.index');
error.message = `Index "${index}" is missing`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be great to use translations here:

Suggested change
error.message = `Index "${index}" is missing`;
error.message = i18n.translate('visTypeTimeseries.error.missingIndexErrorMessage', {
defaultMessage: 'Index {index} is missing',
values: { index }
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, updated the PR and its description

}
if (error) {
return (
<div className={props.className}>
Expand Down
12 changes: 8 additions & 4 deletions src/plugins/vis_type_timeseries/server/lib/get_fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
* 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';
Expand Down Expand Up @@ -73,7 +71,13 @@ export async function getFields(
.toPromise();
},
};
const { indexPatternString } = await getIndexPatternObject(reqFacade, indexPattern);
let indexPatternString = indexPattern;

if (!indexPatternString) {
const index = await reqFacade.getUiSettingsService().get('defaultIndex');
Copy link
Contributor

Choose a reason for hiding this comment

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

This will return a default index id, e.x. ff959d40-b880-11e8-a6d9-e546fe2bba5f.
To receive an index title, you should get an actual index via saveObjectsClient service as in getIndexPatternObject helper

You could literally go with

  if (!indexPatternString) {
    const object = await getIndexPatternObject(reqFacade, indexPattern);
    indexPatternString = object.indexPatternString;
  }

or you could use the data plugin indexPatternsService, where there are getDefaut function to retrieve the default index directly. I think this is a better variant.

indexPatternString = get(index, 'title', '');
}

const {
searchStrategy,
capabilities,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,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 @@ -75,6 +75,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