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

[Data Views] Remove remaining "any" usage #136343

Merged
merged 32 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
deead9c
fix some any
tsullivan Jul 13, 2022
8ff1b10
remove some any
tsullivan Jul 13, 2022
f1eed72
fix some any in saved_objects_client_wrapper
tsullivan Jul 13, 2022
ad9b69d
fix some any in common/types
tsullivan Jul 13, 2022
700bdea
remove some any from flattenHit and getFields*
tsullivan Jul 13, 2022
b3a071a
fix more any
tsullivan Jul 13, 2022
7604fe2
remove "any" in code comments
tsullivan Jul 13, 2022
266f750
fix types
tsullivan Jul 13, 2022
bf3d887
test commit
tsullivan Jul 13, 2022
b0b7817
Merge remote-tracking branch 'elastic/main' into dataviews/remove-any…
tsullivan Jul 13, 2022
366c2f8
fix ts
tsullivan Jul 13, 2022
40a62ed
fix types
tsullivan Jul 13, 2022
5ee6bd6
remove more any
tsullivan Jul 13, 2022
7bcd468
Merge remote-tracking branch 'elastic/main' into dataviews/remove-any…
tsullivan Jul 13, 2022
daf68f9
simplify / comment
tsullivan Jul 13, 2022
7712b06
more simplification
tsullivan Jul 13, 2022
b7d44a6
fix test
tsullivan Jul 13, 2022
d3dbc00
export public APIs
tsullivan Jul 14, 2022
5db73e7
add fixme comment
tsullivan Jul 14, 2022
ef9e85d
cleanup
tsullivan Jul 14, 2022
043a4e5
Merge remote-tracking branch 'elastic' into dataviews/remove-any-usag…
tsullivan Jul 14, 2022
fbad73a
improve documentation and types for RollupIndexCapability
tsullivan Jul 14, 2022
524027e
simplify shortenDottedString
tsullivan Jul 14, 2022
b7a43ec
fix jest test
tsullivan Jul 14, 2022
90b995c
use AggregationRestrictions
tsullivan Jul 14, 2022
275e7a3
fix downstream types
tsullivan Jul 14, 2022
35c4dcf
simplify
tsullivan Jul 15, 2022
250d034
add comments for public api items
tsullivan Jul 15, 2022
9db3690
add ts-expect-error in lens integration toJSON
tsullivan Jul 15, 2022
2a2d14a
Merge remote-tracking branch 'elastic/main' into dataviews/remove-any…
tsullivan Jul 15, 2022
d3c66b3
Merge branch 'main' into dataviews/remove-any-usage-ii
flash1293 Jul 19, 2022
d525db7
Merge remote-tracking branch 'elastic/main' into dataviews/remove-any…
tsullivan Jul 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor

Choose a reason for hiding this comment

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

😆

// 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
15 changes: 9 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 @@ -435,7 +435,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 @@ -463,7 +463,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 @@ -773,8 +773,8 @@ export class DataViewsService {
* @returns DataView
*/
async create({ id, ...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[]>(META_FIELDS);

const spec = {
id: id ?? uuid.v4(),
Expand Down Expand Up @@ -870,7 +870,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 @@ -897,7 +898,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 @@ -930,6 +932,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: 2 additions & 4 deletions src/plugins/data_views/common/fields/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ const DOT_PREFIX_RE = /(.).+?\./g;
/**
* Convert a dot.notated.string into a short
* version (d.n.string)
*
* @return {any}
*/
export function shortenDottedString(input: any) {
return typeof input !== 'string' ? input : input.replace(DOT_PREFIX_RE, '$1.');
export function shortenDottedString(input: unknown): string {
Copy link
Contributor

Choose a reason for hiding this comment

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

When is this called on something aside from a string?

Copy link
Member Author

Choose a reason for hiding this comment

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

it was only in unit tests

return typeof input !== 'string' ? (input as string) : input.replace(DOT_PREFIX_RE, '$1.');
}

// Note - this code is duplicated from @kbn/es-query
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/data_views/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ export type {
} from './types';
export { DataViewType } from './types';

export type { DataViewsContract, DataViewsServiceDeps } from './data_views';
export type {
DataViewsContract,
DataViewsServiceDeps,
DataViewSavedObjectAttrs,
} from './data_views';
export { DataViewsService, DataViewPersistableStateService } from './data_views';
export type {
DataViewListItem,
Expand Down
Loading