Skip to content

Commit

Permalink
[Data Views] Remove remaining "any" usage (#136343)
Browse files Browse the repository at this point in the history
* fix some any

* remove some any

* fix some any in saved_objects_client_wrapper

* fix some any in common/types

* remove some any from flattenHit and getFields*

* fix more any

* remove "any" in code comments

* fix types

* test commit

* fix ts

* fix types

* remove more any

* simplify / comment

* more simplification

* fix test

* export public APIs

* add fixme comment

* cleanup

* improve documentation and types for RollupIndexCapability

* simplify shortenDottedString

* fix jest test

* use AggregationRestrictions

* fix downstream types

* simplify

* add comments for public api items

* add ts-expect-error in lens integration toJSON

Co-authored-by: Joe Reuter <[email protected]>
  • Loading branch information
tsullivan and flash1293 authored Jul 19, 2022
1 parent f128db1 commit a2c3323
Show file tree
Hide file tree
Showing 40 changed files with 223 additions and 142 deletions.
2 changes: 1 addition & 1 deletion src/plugins/data_view_editor/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"name": "App Services",
"githubTeam": "kibana-app-services"
},
"description": "This plugin provides the ability to create data views via a modal flyout from any kibana app"
"description": "This plugin provides the ability to create data views via a modal flyout inside Kibana apps"
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const FieldPreviewProvider: FunctionComponent = ({ children }) => {
// If no documents could be fetched from the cluster (and we are not trying to load
// a custom doc ID) then we disable preview as the script field validation expect the result
// of the preview to before resolving. If there are no documents we can't have a preview
// (the _execute API expects one) and thus the validation should not expect any value.
// (the _execute API expects one) and thus the validation should not expect a value.
if (!isFetchingDocument && !isCustomDocId && documents.length === 0) {
isPreviewAvailable = false;
}
Expand Down Expand Up @@ -341,7 +341,7 @@ export const FieldPreviewProvider: FunctionComponent = ({ children }) => {

if (currentApiCall !== previewCount.current) {
// Discard this response as there is another one inflight
// or we have called reset() and don't need the response anymore.
// or we have called reset() and no longer need the response.
return;
}

Expand Down Expand Up @@ -410,7 +410,7 @@ export const FieldPreviewProvider: FunctionComponent = ({ children }) => {
}, [currentIdx, totalDocs]);

const reset = useCallback(() => {
// By resetting the previewCount we will discard any inflight
// By resetting the previewCount we will discard previous inflight
// API call response coming in after calling reset() was called
previewCount.current = 0;

Expand Down Expand Up @@ -603,7 +603,7 @@ export const FieldPreviewProvider: FunctionComponent = ({ children }) => {
}, [scriptEditorValidation, script?.source, setPreviewError, clearPreviewError]);

/**
* Whenever updatePreview() changes (meaning whenever any of the params changes)
* Whenever updatePreview() changes (meaning whenever a param changes)
* we call it to update the preview response with the field(s) value or possible error.
*/
useDebounce(updatePreview, 500, [updatePreview]);
Expand Down
14 changes: 7 additions & 7 deletions src/plugins/data_view_field_editor/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
* Side Public License, v 1.
*/

import { SerializableRecord } from '@kbn/utility-types';
import { FunctionComponent } from 'react';

import { DeleteFieldProviderProps } from './components';
import { OpenFieldDeleteModalOptions } from './open_delete_modal';
import { OpenFieldEditorOptions } from './open_editor';
import { FormatEditorServiceSetup, FormatEditorServiceStart } from './service';
import {
DataPublicPluginStart,
DataViewsPublicPluginStart,
FieldFormatsStart,
RuntimeField,
RuntimeType,
UsageCollectionStart,
FieldFormatsStart,
} from './shared_imports';
import { OpenFieldEditorOptions } from './open_editor';
import { OpenFieldDeleteModalOptions } from './open_delete_modal';
import { FormatEditorServiceSetup, FormatEditorServiceStart } from './service';
import { DeleteFieldProviderProps } from './components';

export interface PluginSetup {
fieldFormatEditors: FormatEditorServiceSetup['fieldFormatEditors'];
Expand Down Expand Up @@ -58,7 +58,7 @@ export interface Field {

export interface FieldFormatConfig {
id: string;
params?: { [key: string]: any };
params?: SerializableRecord;
}

export interface EsRuntimeField {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data_view_field_editor/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { PluginInitializerContext, CoreSetup, Plugin, Logger } from '@kbn/core/s

import { ApiRoutes } from './routes';

export class IndexPatternPlugin implements Plugin<void, void, any, any> {
export class IndexPatternPlugin implements Plugin {
private readonly logger: Logger;
private readonly apiRoutes: ApiRoutes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const EditIndexPattern = withRouter(
indexPattern.fields.getAll().filter((field) => field.type === 'conflict')
);
const [defaultIndex, setDefaultIndex] = useState<string>(uiSettings.get('defaultIndex'));
const [tags, setTags] = useState<any[]>([]);
const [tags, setTags] = useState<Array<{ key: string; name: string }>>([]);
const [showEditDialog, setShowEditDialog] = useState<boolean>(false);
const [relationships, setRelationships] = useState<SavedObjectRelationWithTitle[]>([]);
const [allowedTypes, setAllowedTypes] = useState<SavedObjectManagementTypeInfo[]>([]);
Expand Down Expand Up @@ -220,7 +220,7 @@ export const EditIndexPattern = withRouter(
<EuiBadge>{securityDataView}</EuiBadge>
</EuiFlexItem>
)}
{tags.map((tag: any) => (
{tags.map((tag) => (
<EuiFlexItem grow={false} key={tag.key}>
{tag.key === 'default' ? (
<EuiBadge iconType="starFilled" color="default">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class SourceFiltersTable extends Component<
SourceFiltersTableProps,
SourceFiltersTableState
> {
// Source filters do not have any unique ids, only the value is stored.
// Source filters do not have unique ids, only the value is stored.
// To ensure we can create a consistent and expected UX when managing
// source filters, we are assigning a unique id to each filter on the
// client side only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Format {
params() {}
}

// FIXME: which interface is this?
const field = {
scripted: true,
type: 'number',
Expand Down Expand Up @@ -190,7 +191,7 @@ describe('FieldEditor', () => {
add: jest.fn(),
},
};
indexPattern.fieldFormatMap = { test: field };
indexPattern.fieldFormatMap = { test: field } as {};
(indexPattern.deleteFieldFormat as any) = jest.fn();

const component = createComponentWithContext<FieldEdiorProps>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const tableTitle = i18n.translate('indexPatternManagement.dataViewTable.tableTit
});

export const deleteModalMsg = (views: RemoveDataViewProps[], hasSpaces: boolean) => {
const columns: Array<EuiTableFieldDataColumnType<any>> = [
const columns: Array<EuiTableFieldDataColumnType<RemoveDataViewProps>> = [
{
field: 'name',
name: dataViewColumnName,
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data_views/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ and field lists across the various Kibana apps. Its typically used in conjunctio

**hasData:** A standardized way to check the empty state for indices and data views.
- `hasESData: () => Promise<boolean>; // Check to see if ES data exists`
- `hasDataView: () => Promise<boolean>; // Check to see if any data view exists (managed or user created)`
- `hasDataView: () => Promise<boolean>; // Check to see if data view exists (managed or user created)`
- `hasUserDataView: () => Promise<boolean>; // Check to see if user created data views exists`
2 changes: 1 addition & 1 deletion src/plugins/data_views/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const RUNTIME_FIELD_TYPES = [
] as const;

/**
* Used to determine if the instance has any user created index patterns by filtering index patterns
* Used to determine if the instance has some user created index patterns by filtering index patterns
* that are created and backed only by Fleet server data
* Should be revised after https://github.com/elastic/kibana/issues/82851 is fixed
* For more background see: https://github.com/elastic/kibana/issues/107020
Expand Down
38 changes: 24 additions & 14 deletions src/plugins/data_views/common/data_views/data_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,31 @@
* Side Public License, v 1.
*/

import _, { cloneDeep, each, reject } from 'lodash';
import { castEsToKbnFieldTypeName, ES_FIELD_TYPES, KBN_FIELD_TYPES } from '@kbn/field-types';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { CharacterNotAllowedInField } from '@kbn/kibana-utils-plugin/common';
import {
FieldFormatsStartCommon,
import type { DataViewBase } from '@kbn/es-query';
import type {
FieldFormat,
FieldFormatsStartCommon,
SerializedFieldFormat,
} from '@kbn/field-formats-plugin/common';
import type { DataViewBase } from '@kbn/es-query';
import { FieldAttrs, FieldAttrSet, DataViewAttributes } from '..';
import type { RuntimeField, RuntimeFieldSpec, RuntimeType, FieldConfiguration } from '../types';
import { DataViewField, IIndexPatternFieldList, fieldList } from '../fields';
import { castEsToKbnFieldTypeName, ES_FIELD_TYPES, KBN_FIELD_TYPES } from '@kbn/field-types';
import { CharacterNotAllowedInField } from '@kbn/kibana-utils-plugin/common';
import _, { cloneDeep, each, reject } from 'lodash';
import type { DataViewAttributes, FieldAttrs, FieldAttrSet } from '..';
import type { DataViewField, IIndexPatternFieldList } from '../fields';
import { fieldList } from '../fields';
import type {
DataViewFieldMap,
DataViewSpec,
FieldConfiguration,
FieldFormatMap,
RuntimeField,
RuntimeFieldSpec,
RuntimeType,
SourceFilter,
TypeMeta,
} from '../types';
import { flattenHitWrapper } from './flatten_hit';
import { DataViewSpec, TypeMeta, SourceFilter, DataViewFieldMap } from '../types';
import { removeFieldAttrs } from './utils';

interface DataViewDeps {
Expand Down Expand Up @@ -70,7 +80,7 @@ export class DataView implements DataViewBase {
/**
* Map of field formats by field name
*/
public fieldFormatMap: Record<string, any>;
public fieldFormatMap: FieldFormatMap;
/**
* Only used by rollup indices, used by rollup specific endpoint to load field list.
*/
Expand All @@ -90,7 +100,7 @@ export class DataView implements DataViewBase {
/**
* @deprecated Use `flattenHit` utility method exported from data plugin instead.
*/
public flattenHit: (hit: Record<string, any>, deep?: boolean) => Record<string, any>;
public flattenHit: (hit: Record<string, unknown[]>, deep?: boolean) => Record<string, unknown>;
/**
* List of meta fields by name
*/
Expand Down Expand Up @@ -233,7 +243,7 @@ export class DataView implements DataViewBase {
};
}

// Date value returned in "_source" could be in any number of formats
// Date value returned in "_source" could be in a number of formats
// Use a docvalue for each date field to ensure standardized formats when working with date fields
// dataView.flattenHit will override "_source" values when the same field is also defined in "fields"
const docvalueFields = reject(this.fields.getByType('date'), 'scripted').map((dateField) => {
Expand Down Expand Up @@ -296,7 +306,7 @@ export class DataView implements DataViewBase {
name: this.name,
};

// Filter any undefined values from the spec
// Filter undefined values from the spec
return Object.fromEntries(Object.entries(spec).filter(([, v]) => typeof v !== 'undefined'));
}

Expand Down
19 changes: 13 additions & 6 deletions src/plugins/data_views/common/data_views/data_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ import { DuplicateDataViewError, DataViewInsufficientAccessError } from '../erro

const MAX_ATTEMPTS_TO_RESOLVE_CONFLICTS = 3;

/*
* Attributes of the data view saved object
* @public
*/
export type DataViewSavedObjectAttrs = Pick<
DataViewAttributes,
'title' | 'type' | 'typeMeta' | 'name'
Expand Down Expand Up @@ -445,7 +449,7 @@ export class DataViewsService {
* Get default index pattern id
*/
getDefaultId = async (): Promise<string | null> => {
const defaultIndexPatternId = await this.config.get('defaultIndex');
const defaultIndexPatternId = await this.config.get<string | null>('defaultIndex');
return defaultIndexPatternId ?? null;
};

Expand Down Expand Up @@ -473,7 +477,7 @@ export class DataViewsService {
* @returns FieldSpec[]
*/
getFieldsForWildcard = async (options: GetFieldsOptions): Promise<FieldSpec[]> => {
const metaFields = await this.config.get(META_FIELDS);
const metaFields = await this.config.get<string[]>(META_FIELDS);
return this.apiClient.getFieldsForWildcard({
pattern: options.pattern,
metaFields,
Expand Down Expand Up @@ -786,8 +790,8 @@ export class DataViewsService {
{ id, name, title, ...restOfSpec }: DataViewSpec,
skipFetchFields = false
): Promise<DataView> {
const shortDotsEnable = await this.config.get(FORMATS_UI_SETTINGS.SHORT_DOTS_ENABLE);
const metaFields = await this.config.get(META_FIELDS);
const shortDotsEnable = await this.config.get<boolean>(FORMATS_UI_SETTINGS.SHORT_DOTS_ENABLE);
const metaFields = await this.config.get<string[] | undefined>(META_FIELDS);

const spec = {
id: id ?? uuid.v4(),
Expand Down Expand Up @@ -886,7 +890,8 @@ export class DataViewsService {
// get changed keys
const originalChangedKeys: string[] = [];
Object.entries(body).forEach(([key, value]) => {
if (value !== (originalBody as any)[key]) {
const realKey = key as keyof typeof originalBody;
if (value !== originalBody[realKey]) {
originalChangedKeys.push(key);
}
});
Expand All @@ -913,7 +918,8 @@ export class DataViewsService {

const serverChangedKeys: string[] = [];
Object.entries(updatedBody).forEach(([key, value]) => {
if (value !== (body as any)[key] && value !== (originalBody as any)[key]) {
const realKey = key as keyof typeof originalBody;
if (value !== body[realKey] && value !== originalBody[realKey]) {
serverChangedKeys.push(key);
}
});
Expand Down Expand Up @@ -946,6 +952,7 @@ export class DataViewsService {

// Set the updated response on this object
serverChangedKeys.forEach((key) => {
// FIXME: this overwrites read-only properties
(indexPattern as any)[key] = (samePattern as any)[key];
});
indexPattern.version = samePattern.version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('flattenHit', () => {
zzz: ['z'],
_abc: ['a'],
},
});
} as {});
const expectedOrder = ['_abc', 'date', 'name', 'zzz', '_id', '_routing', '_score', '_type'];
expect(Object.keys(response)).toEqual(expectedOrder);
expect(Object.entries(response).map(([key]) => key)).toEqual(expectedOrder);
Expand Down
33 changes: 23 additions & 10 deletions src/plugins/data_views/common/data_views/flatten_hit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@
import _ from 'lodash';
import { DataView } from './data_view';

// Takes a hit, merges it with any stored/scripted fields, and with the metaFields
// returns a flattened version

function flattenHit(indexPattern: DataView, hit: Record<string, any>, deep: boolean) {
const flat = {} as Record<string, any>;
/**
* Takes a hit, merges it with whatever stored/scripted fields, and with the metaFields
* returns a flattened version
*
* @param {DataView} indexPattern
* @param {Record<string, unknown>} hit - userland data
* @param {boolean} deep - whether to look into objects within arrays
* @returns {Record<string, unknown[]>}
*/
function flattenHit(
indexPattern: DataView,
hit: Record<string, unknown>,
deep: boolean
): Record<string, unknown[]> {
const flat = {} as Record<string, unknown[]>;

// recursively merge _source
const fields = indexPattern.fields.getByName;
Expand Down Expand Up @@ -61,16 +71,19 @@ function flattenHit(indexPattern: DataView, hit: Record<string, any>, deep: bool
return flat;
}

function decorateFlattenedWrapper(hit: Record<string, any>, metaFields: Record<string, any>) {
return function (flattened: Record<string, any>) {
function decorateFlattenedWrapper(
hit: Record<string, unknown[]>,
metaFields: Record<string, string>
) {
return function (flattened: Record<string, unknown>) {
// assign the meta fields
_.each(metaFields, function (meta) {
if (meta === '_source') return;
flattened[meta] = hit[meta];
});

// unwrap computed fields
_.forOwn(hit.fields, function (val, key: any) {
_.forOwn(hit.fields, function (val, key: string) {
// Flatten an array with 0 or 1 elements to a single value.
if (Array.isArray(val) && val.length <= 1) {
flattened[key] = val[0];
Expand Down Expand Up @@ -108,8 +121,8 @@ function decorateFlattenedWrapper(hit: Record<string, any>, metaFields: Record<s
*
* @internal
*/
export function flattenHitWrapper(dataView: DataView, metaFields = {}, cache = new WeakMap()) {
return function cachedFlatten(hit: Record<string, any>, deep = false) {
export function flattenHitWrapper<T>(dataView: DataView, metaFields = {}, cache = new WeakMap()) {
return function cachedFlatten(hit: Record<string, unknown[]>, deep = false) {
const decorateFlattened = decorateFlattenedWrapper(hit, metaFields);
const cached = cache.get(hit);
const flattened = cached || flattenHit(dataView, hit, deep);
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/data_views/common/fields/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ describe('shortenDottedString', () => {
test('should ignore non-string values', () => {
const obj = { key: 'val' };

expect(shortenDottedString(true)).toBe(true);
expect(shortenDottedString(123)).toBe(123);
expect(shortenDottedString(obj)).toBe(obj);
expect(shortenDottedString(true as unknown as string)).toBe(true);
expect(shortenDottedString(123 as unknown as string)).toBe(123);
expect(shortenDottedString(obj as unknown as string)).toBe(obj);
});
});
4 changes: 1 addition & 3 deletions src/plugins/data_views/common/fields/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ const DOT_PREFIX_RE = /(.).+?\./g;
/**
* Convert a dot.notated.string into a short
* version (d.n.string)
*
* @return {any}
*/
export function shortenDottedString(input: any) {
export function shortenDottedString(input: string): string {
return typeof input !== 'string' ? input : input.replace(DOT_PREFIX_RE, '$1.');
}

Expand Down
Loading

0 comments on commit a2c3323

Please sign in to comment.