[data views] remove some any instances from service (#112632)

* remove some any instances from data view

* lint fix

* simplify scripted field type
This commit is contained in:
Matthew Kime 2021-09-22 07:04:15 -05:00 committed by GitHub
parent 11700a7d7a
commit 525d3a5920
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 31 deletions

View file

@ -16,12 +16,12 @@ export interface DataViewCache {
}
export function createDataViewCache(): DataViewCache {
const vals: Record<string, any> = {};
const vals: Record<string, Promise<DataView>> = {};
const cache: DataViewCache = {
get: (id: string) => {
return vals[id];
},
set: (id: string, prom: any) => {
set: (id: string, prom: Promise<DataView>) => {
vals[id] = prom;
return prom;
},

View file

@ -158,7 +158,7 @@ export class DataView implements IIndexPattern {
};
getComputedFields() {
const scriptFields: any = {};
const scriptFields: Record<string, { script: { source: string; lang: string } }> = {};
if (!this.fields) {
return {
storedFields: ['*'],
@ -171,23 +171,21 @@ export class DataView implements IIndexPattern {
// Date value returned in "_source" could be in any number of formats
// Use a docvalue for each date field to ensure standardized formats when working with date fields
// indexPattern.flattenHit will override "_source" values when the same field is also defined in "fields"
const docvalueFields = reject(this.fields.getByType('date'), 'scripted').map(
(dateField: any) => {
return {
field: dateField.name,
format:
dateField.esTypes && dateField.esTypes.indexOf('date_nanos') !== -1
? 'strict_date_time'
: 'date_time',
};
}
);
const docvalueFields = reject(this.fields.getByType('date'), 'scripted').map((dateField) => {
return {
field: dateField.name,
format:
dateField.esTypes && dateField.esTypes.indexOf('date_nanos') !== -1
? 'strict_date_time'
: 'date_time',
};
});
each(this.getScriptedFields(), function (field) {
scriptFields[field.name] = {
script: {
source: field.script,
lang: field.lang,
source: field.script as string,
lang: field.lang as string,
},
};
});
@ -227,7 +225,7 @@ export class DataView implements IIndexPattern {
*/
getSourceFiltering() {
return {
excludes: (this.sourceFilters && this.sourceFilters.map((filter: any) => filter.value)) || [],
excludes: (this.sourceFilters && this.sourceFilters.map((filter) => filter.value)) || [],
};
}
@ -299,8 +297,8 @@ export class DataView implements IIndexPattern {
}
isTimeNanosBased(): boolean {
const timeField: any = this.getTimeField();
return timeField && timeField.esTypes && timeField.esTypes.indexOf('date_nanos') !== -1;
const timeField = this.getTimeField();
return !!(timeField && timeField.esTypes && timeField.esTypes.indexOf('date_nanos') !== -1);
}
getTimeField() {

View file

@ -289,7 +289,7 @@ export class DataViewsService {
indexPattern.fields.replaceAll(fieldsWithSavedAttrs);
} catch (err) {
if (err instanceof DataViewMissingIndices) {
this.onNotification({ title: (err as any).message, color: 'danger', iconType: 'alert' });
this.onNotification({ title: err.message, color: 'danger', iconType: 'alert' });
}
this.onError(err, {
@ -334,7 +334,7 @@ export class DataViewsService {
return this.fieldArrayToMap(updatedFieldList, fieldAttrs);
} catch (err) {
if (err instanceof DataViewMissingIndices) {
this.onNotification({ title: (err as any).message, color: 'danger', iconType: 'alert' });
this.onNotification({ title: err.message, color: 'danger', iconType: 'alert' });
return {};
}
@ -475,7 +475,7 @@ export class DataViewsService {
} catch (err) {
if (err instanceof DataViewMissingIndices) {
this.onNotification({
title: (err as any).message,
title: err.message,
color: 'danger',
iconType: 'alert',
});

View file

@ -6,17 +6,18 @@
* Side Public License, v 1.
*/
import { SavedObjectsClientContract, SimpleSavedObject } from '../../../../../core/public';
import { SavedObjectsClientContract } from '../../../../../core/public';
import { DATA_VIEW_SAVED_OBJECT_TYPE } from '../../constants';
import { DataViewAttributes } from '../types';
export async function getTitle(
client: SavedObjectsClientContract,
indexPatternId: string
): Promise<SimpleSavedObject<any>> {
const savedObject = (await client.get(
): Promise<string> {
const savedObject = await client.get<DataViewAttributes>(
DATA_VIEW_SAVED_OBJECT_TYPE,
indexPatternId
)) as SimpleSavedObject<any>;
);
if (savedObject.error) {
throw new Error(`Unable to get index-pattern title: ${savedObject.error.message}`);

View file

@ -24,7 +24,7 @@ function findIllegalCharacters(indexPattern: string): string[] {
}
export function validateDataView(indexPattern: string) {
const errors: Record<string, any> = {};
const errors: { [ILLEGAL_CHARACTERS_KEY]?: string[]; [CONTAINS_SPACES_KEY]?: boolean } = {};
const illegalCharacters = findIllegalCharacters(indexPattern);

View file

@ -37,7 +37,7 @@ export function buildSearchBody(
},
},
stored_fields: computedFields.storedFields,
script_fields: computedFields.scriptFields,
script_fields: computedFields.scriptFields as Record<string, estypes.ScriptField>,
version: true,
},
};

View file

@ -37,7 +37,7 @@ export const indexPatternField =
// Validate illegal characters
const errors = indexPatterns.validate(value);
if (errors[indexPatterns.ILLEGAL_CHARACTERS_KEY]) {
if (errors.ILLEGAL_CHARACTERS) {
return {
code: 'ERR_FIELD_FORMAT',
formatType: 'INDEX_PATTERN',
@ -45,8 +45,8 @@ export const indexPatternField =
defaultMessage:
'The index pattern contains the invalid {characterListLength, plural, one {character} other {characters}} { characterList }.',
values: {
characterList: errors[indexPatterns.ILLEGAL_CHARACTERS_KEY].join(' '),
characterListLength: errors[indexPatterns.ILLEGAL_CHARACTERS_KEY].length,
characterList: errors.ILLEGAL_CHARACTERS.join(' '),
characterListLength: errors.ILLEGAL_CHARACTERS.length,
},
}),
};