Skip to content

Commit

Permalink
[Maps] replace custom types with types from elasticsearch-js client (#…
Browse files Browse the repository at this point in the history
…132718)

* [Maps] replace custom types with types from elasticsearch-js client

* replace unknowns with types

* fix typings in AnomalySourceField

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
nreese and kibanamachine committed May 24, 2022
1 parent 07bc517 commit 8952207
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 31 deletions.
18 changes: 15 additions & 3 deletions x-pack/plugins/maps/public/classes/fields/agg/agg_field.ts
Expand Up @@ -6,6 +6,11 @@
*/

import { DataView } from '@kbn/data-plugin/common';
import type {
AggregationsExtendedStatsAggregation,
AggregationsPercentilesAggregation,
AggregationsTermsAggregation,
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { AGG_TYPE } from '../../../../common/constants';
import { TileMetaFeature } from '../../../../common/descriptor_types';
import { CountAggField } from './count_agg_field';
Expand Down Expand Up @@ -97,17 +102,24 @@ export class AggField extends CountAggField {
return this._getAggType() === AGG_TYPE.TERMS ? TERMS_AGG_SHARD_SIZE : 0;
}

async getExtendedStatsFieldMetaRequest(): Promise<unknown | null> {
async getExtendedStatsFieldMetaRequest(): Promise<Record<
string,
{ extended_stats: AggregationsExtendedStatsAggregation }
> | null> {
return this._esDocField ? await this._esDocField.getExtendedStatsFieldMetaRequest() : null;
}

async getPercentilesFieldMetaRequest(percentiles: number[]): Promise<unknown | null> {
async getPercentilesFieldMetaRequest(
percentiles: number[]
): Promise<Record<string, { percentiles: AggregationsPercentilesAggregation }> | null> {
return this._esDocField
? await this._esDocField.getPercentilesFieldMetaRequest(percentiles)
: null;
}

async getCategoricalFieldMetaRequest(size: number): Promise<unknown> {
async getCategoricalFieldMetaRequest(
size: number
): Promise<Record<string, { terms: AggregationsTermsAggregation }> | null> {
return this._esDocField ? await this._esDocField.getCategoricalFieldMetaRequest(size) : null;
}

Expand Down
18 changes: 15 additions & 3 deletions x-pack/plugins/maps/public/classes/fields/agg/count_agg_field.ts
Expand Up @@ -5,6 +5,11 @@
* 2.0.
*/

import type {
AggregationsExtendedStatsAggregation,
AggregationsPercentilesAggregation,
AggregationsTermsAggregation,
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { DataView } from '@kbn/data-plugin/common';
import { IESAggSource } from '../../sources/es_agg_source';
import { IVectorSource } from '../../sources/vector_source';
Expand Down Expand Up @@ -100,15 +105,22 @@ export class CountAggField implements IESAggField {
return false;
}

async getExtendedStatsFieldMetaRequest(): Promise<unknown | null> {
async getExtendedStatsFieldMetaRequest(): Promise<Record<
string,
{ extended_stats: AggregationsExtendedStatsAggregation }
> | null> {
return null;
}

async getPercentilesFieldMetaRequest(percentiles: number[]): Promise<unknown | null> {
async getPercentilesFieldMetaRequest(
percentiles: number[]
): Promise<Record<string, { percentiles: AggregationsPercentilesAggregation }> | null> {
return null;
}

async getCategoricalFieldMetaRequest(size: number): Promise<unknown> {
async getCategoricalFieldMetaRequest(
size: number
): Promise<Record<string, { terms: AggregationsTermsAggregation }> | null> {
return null;
}

Expand Down
Expand Up @@ -82,15 +82,15 @@ export class TopTermPercentageField implements IESAggField {
return false;
}

async getExtendedStatsFieldMetaRequest(): Promise<unknown | null> {
async getExtendedStatsFieldMetaRequest(): Promise<null> {
return null;
}

async getPercentilesFieldMetaRequest(percentiles: number[]): Promise<unknown | null> {
async getPercentilesFieldMetaRequest(percentiles: number[]): Promise<null> {
return null;
}

async getCategoricalFieldMetaRequest(): Promise<unknown> {
async getCategoricalFieldMetaRequest(): Promise<null> {
return null;
}

Expand Down
34 changes: 21 additions & 13 deletions x-pack/plugins/maps/public/classes/fields/es_doc_field.ts
Expand Up @@ -7,6 +7,11 @@

import type { IndexPatternField } from '@kbn/data-plugin/public';
import { indexPatterns } from '@kbn/data-plugin/public';
import type {
AggregationsExtendedStatsAggregation,
AggregationsPercentilesAggregation,
AggregationsTermsAggregation,
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { FIELD_ORIGIN } from '../../../common/constants';
import { ESTooltipProperty } from '../tooltips/es_tooltip_property';
import { ITooltipProperty, TooltipProperty } from '../tooltips/tooltip_property';
Expand Down Expand Up @@ -78,7 +83,10 @@ export class ESDocField extends AbstractField implements IField {
: super.getLabel();
}

async getExtendedStatsFieldMetaRequest(): Promise<unknown | null> {
async getExtendedStatsFieldMetaRequest(): Promise<Record<
string,
{ extended_stats: AggregationsExtendedStatsAggregation }
> | null> {
const indexPatternField = await this._getIndexPatternField();

if (
Expand All @@ -88,10 +96,8 @@ export class ESDocField extends AbstractField implements IField {
return null;
}

// TODO remove local typing once Kibana has figured out a core place for Elasticsearch aggregation request types
// https://github.com/elastic/kibana/issues/60102
const metricAggConfig: { script?: unknown; field?: string } = {};
if (indexPatternField.scripted) {
const metricAggConfig: AggregationsExtendedStatsAggregation = {};
if (indexPatternField.scripted && indexPatternField.script) {
metricAggConfig.script = {
source: indexPatternField.script,
lang: indexPatternField.lang,
Expand All @@ -106,17 +112,19 @@ export class ESDocField extends AbstractField implements IField {
};
}

async getPercentilesFieldMetaRequest(percentiles: number[]): Promise<unknown | null> {
async getPercentilesFieldMetaRequest(
percentiles: number[]
): Promise<Record<string, { percentiles: AggregationsPercentilesAggregation }> | null> {
const indexPatternField = await this._getIndexPatternField();

if (!indexPatternField || indexPatternField.type !== 'number') {
return null;
}

const metricAggConfig: { script?: unknown; field?: string; percents: number[] } = {
const metricAggConfig: AggregationsPercentilesAggregation = {
percents: [0, ...percentiles],
};
if (indexPatternField.scripted) {
if (indexPatternField.scripted && indexPatternField.script) {
metricAggConfig.script = {
source: indexPatternField.script,
lang: indexPatternField.lang,
Expand All @@ -131,18 +139,18 @@ export class ESDocField extends AbstractField implements IField {
};
}

async getCategoricalFieldMetaRequest(size: number): Promise<unknown> {
async getCategoricalFieldMetaRequest(
size: number
): Promise<Record<string, { terms: AggregationsTermsAggregation }> | null> {
const indexPatternField = await this._getIndexPatternField();
if (!indexPatternField || size <= 0) {
return null;
}

// TODO remove local typing once Kibana has figured out a core place for Elasticsearch aggregation request types
// https://github.com/elastic/kibana/issues/60102
const topTerms: { size: number; script?: unknown; field?: string } = {
const topTerms: AggregationsTermsAggregation = {
size: size - 1, // need additional color for the "other"-value
};
if (indexPatternField.scripted) {
if (indexPatternField.scripted && indexPatternField.script) {
topTerms.script = {
source: indexPatternField.script,
lang: indexPatternField.lang,
Expand Down
31 changes: 25 additions & 6 deletions x-pack/plugins/maps/public/classes/fields/field.ts
Expand Up @@ -5,6 +5,11 @@
* 2.0.
*/

import type {
AggregationsExtendedStatsAggregation,
AggregationsPercentilesAggregation,
AggregationsTermsAggregation,
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { TileMetaFeature } from '../../../common/descriptor_types';
import { FIELD_ORIGIN } from '../../../common/constants';
import { IVectorSource } from '../sources/vector_source';
Expand All @@ -21,9 +26,16 @@ export interface IField {
getSource(): IVectorSource;
getOrigin(): FIELD_ORIGIN;
isValid(): boolean;
getExtendedStatsFieldMetaRequest(): Promise<unknown | null>;
getPercentilesFieldMetaRequest(percentiles: number[]): Promise<unknown | null>;
getCategoricalFieldMetaRequest(size: number): Promise<unknown | null>;
getExtendedStatsFieldMetaRequest(): Promise<Record<
string,
{ extended_stats: AggregationsExtendedStatsAggregation }
> | null>;
getPercentilesFieldMetaRequest(
percentiles: number[]
): Promise<Record<string, { percentiles: AggregationsPercentilesAggregation }> | null>;
getCategoricalFieldMetaRequest(
size: number
): Promise<Record<string, { terms: AggregationsTermsAggregation }> | null>;

/*
* IField.supportsFieldMetaFromLocalData returns boolean indicating whether field value domain
Expand Down Expand Up @@ -107,15 +119,22 @@ export class AbstractField implements IField {
return this._origin;
}

async getExtendedStatsFieldMetaRequest(): Promise<unknown> {
async getExtendedStatsFieldMetaRequest(): Promise<Record<
string,
{ extended_stats: AggregationsExtendedStatsAggregation }
> | null> {
return null;
}

async getPercentilesFieldMetaRequest(percentiles: number[]): Promise<unknown | null> {
async getPercentilesFieldMetaRequest(
percentiles: number[]
): Promise<Record<string, { percentiles: AggregationsPercentilesAggregation }> | null> {
return null;
}

async getCategoricalFieldMetaRequest(size: number): Promise<unknown> {
async getCategoricalFieldMetaRequest(
size: number
): Promise<Record<string, { terms: AggregationsTermsAggregation }> | null> {
return null;
}

Expand Down
18 changes: 15 additions & 3 deletions x-pack/plugins/ml/public/maps/anomaly_source_field.tsx
Expand Up @@ -7,6 +7,11 @@

// eslint-disable-next-line max-classes-per-file
import React, { ReactNode } from 'react';
import type {
AggregationsExtendedStatsAggregation,
AggregationsPercentilesAggregation,
AggregationsTermsAggregation,
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { escape } from 'lodash';
import { i18n } from '@kbn/i18n';
import { Filter } from '@kbn/es-query';
Expand Down Expand Up @@ -230,15 +235,22 @@ export class AnomalySourceField implements IField {
return false;
}

async getExtendedStatsFieldMetaRequest(): Promise<unknown> {
async getExtendedStatsFieldMetaRequest(): Promise<Record<
string,
{ extended_stats: AggregationsExtendedStatsAggregation }
> | null> {
return null;
}

async getPercentilesFieldMetaRequest(percentiles: number[]): Promise<unknown> {
async getPercentilesFieldMetaRequest(
percentiles: number[]
): Promise<Record<string, { percentiles: AggregationsPercentilesAggregation }> | null> {
return null;
}

async getCategoricalFieldMetaRequest(size: number): Promise<unknown> {
async getCategoricalFieldMetaRequest(
size: number
): Promise<Record<string, { terms: AggregationsTermsAggregation }> | null> {
return null;
}

Expand Down

0 comments on commit 8952207

Please sign in to comment.