From 64a14557ee033a945ebbc44a51703ee00412832c Mon Sep 17 00:00:00 2001
From: Nathan Reese <reese.nathan@gmail.com>
Date: Wed, 9 Sep 2020 20:10:32 -0600
Subject: [PATCH] [Maps] convert ESAggSource to TS (#76999)

* [Maps] convert ESAggSource to TS

* one more rename

* tslint fixes

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
---
 .../maps/public/classes/joins/inner_join.js   |  4 +-
 .../layers/vector_layer/vector_layer.js       |  6 +-
 .../ems_file_source/ems_file_source.test.tsx  |  6 +-
 .../ems_file_source/ems_file_source.tsx       |  3 +-
 .../sources/es_agg_source/es_agg_source.d.ts  | 29 ----------
 .../{es_agg_source.js => es_agg_source.ts}    | 58 +++++++++++++------
 .../es_geo_grid_source.d.ts                   |  2 +
 .../es_geo_grid_source/es_geo_grid_source.js  |  4 --
 .../es_pew_pew_source/es_pew_pew_source.js    |  4 --
 .../es_search_source/es_search_source.js      |  2 +-
 .../sources/es_term_source/es_term_source.js  |  4 --
 .../mvt_single_layer_vector_source.test.tsx   |  4 +-
 .../mvt_single_layer_vector_source.tsx        |  2 +-
 .../sources/vector_source/vector_source.d.ts  |  4 +-
 .../sources/vector_source/vector_source.js    |  2 +-
 .../metrics_editor/metrics_editor.tsx         |  1 -
 16 files changed, 56 insertions(+), 79 deletions(-)
 delete mode 100644 x-pack/plugins/maps/public/classes/sources/es_agg_source/es_agg_source.d.ts
 rename x-pack/plugins/maps/public/classes/sources/es_agg_source/{es_agg_source.js => es_agg_source.ts} (56%)

diff --git a/x-pack/plugins/maps/public/classes/joins/inner_join.js b/x-pack/plugins/maps/public/classes/joins/inner_join.js
index 76afe2430b818..75bf59d9d6404 100644
--- a/x-pack/plugins/maps/public/classes/joins/inner_join.js
+++ b/x-pack/plugins/maps/public/classes/joins/inner_join.js
@@ -94,8 +94,8 @@ export class InnerJoin {
     return this._descriptor;
   }
 
-  async filterAndFormatPropertiesForTooltip(properties) {
-    return await this._rightSource.filterAndFormatPropertiesToHtml(properties);
+  async getTooltipProperties(properties) {
+    return await this._rightSource.getTooltipProperties(properties);
   }
 
   getIndexPatternIds() {
diff --git a/x-pack/plugins/maps/public/classes/layers/vector_layer/vector_layer.js b/x-pack/plugins/maps/public/classes/layers/vector_layer/vector_layer.js
index c49d0044e6ad6..27c344b713a60 100644
--- a/x-pack/plugins/maps/public/classes/layers/vector_layer/vector_layer.js
+++ b/x-pack/plugins/maps/public/classes/layers/vector_layer/vector_layer.js
@@ -949,13 +949,11 @@ export class VectorLayer extends AbstractLayer {
 
   async getPropertiesForTooltip(properties) {
     const vectorSource = this.getSource();
-    let allProperties = await vectorSource.filterAndFormatPropertiesToHtml(properties);
+    let allProperties = await vectorSource.getTooltipProperties(properties);
     this._addJoinsToSourceTooltips(allProperties);
 
     for (let i = 0; i < this.getJoins().length; i++) {
-      const propsFromJoin = await this.getJoins()[i].filterAndFormatPropertiesForTooltip(
-        properties
-      );
+      const propsFromJoin = await this.getJoins()[i].getTooltipProperties(properties);
       allProperties = [...allProperties, ...propsFromJoin];
     }
     return allProperties;
diff --git a/x-pack/plugins/maps/public/classes/sources/ems_file_source/ems_file_source.test.tsx b/x-pack/plugins/maps/public/classes/sources/ems_file_source/ems_file_source.test.tsx
index c5d6ced76b5c0..674ee832daab9 100644
--- a/x-pack/plugins/maps/public/classes/sources/ems_file_source/ems_file_source.test.tsx
+++ b/x-pack/plugins/maps/public/classes/sources/ems_file_source/ems_file_source.test.tsx
@@ -17,10 +17,10 @@ function makeEMSFileSource(tooltipProperties: string[]) {
 }
 
 describe('EMS file source', () => {
-  describe('filterAndFormatPropertiesToHtml', () => {
+  describe('getTooltipProperties', () => {
     it('should create tooltip-properties with human readable label', async () => {
       const mockEMSFileSource = makeEMSFileSource(['iso2']);
-      const out = await mockEMSFileSource.filterAndFormatPropertiesToHtml({
+      const out = await mockEMSFileSource.getTooltipProperties({
         iso2: 'US',
       });
 
@@ -33,7 +33,7 @@ describe('EMS file source', () => {
     it('should order tooltip-properties', async () => {
       const tooltipProperties = ['iso3', 'iso2', 'name'];
       const mockEMSFileSource = makeEMSFileSource(tooltipProperties);
-      const out = await mockEMSFileSource.filterAndFormatPropertiesToHtml({
+      const out = await mockEMSFileSource.getTooltipProperties({
         name: 'United States',
         iso3: 'USA',
         iso2: 'US',
diff --git a/x-pack/plugins/maps/public/classes/sources/ems_file_source/ems_file_source.tsx b/x-pack/plugins/maps/public/classes/sources/ems_file_source/ems_file_source.tsx
index f55a7434d1217..5f73a9e23431b 100644
--- a/x-pack/plugins/maps/public/classes/sources/ems_file_source/ems_file_source.tsx
+++ b/x-pack/plugins/maps/public/classes/sources/ems_file_source/ems_file_source.tsx
@@ -23,7 +23,6 @@ import { ITooltipProperty } from '../../tooltips/tooltip_property';
 
 export interface IEmsFileSource extends IVectorSource {
   getEmsFieldLabel(emsFieldName: string): Promise<string>;
-  createField({ fieldName }: { fieldName: string }): IField;
 }
 
 export const sourceTitle = i18n.translate('xpack.maps.source.emsFileTitle', {
@@ -168,7 +167,7 @@ export class EMSFileSource extends AbstractVectorSource implements IEmsFileSourc
     return this._tooltipFields.length > 0;
   }
 
-  async filterAndFormatPropertiesToHtml(properties: unknown): Promise<ITooltipProperty[]> {
+  async getTooltipProperties(properties: unknown): Promise<ITooltipProperty[]> {
     const promises = this._tooltipFields.map((field) => {
       // @ts-ignore
       const value = properties[field.getName()];
diff --git a/x-pack/plugins/maps/public/classes/sources/es_agg_source/es_agg_source.d.ts b/x-pack/plugins/maps/public/classes/sources/es_agg_source/es_agg_source.d.ts
deleted file mode 100644
index eb50cd7528c8b..0000000000000
--- a/x-pack/plugins/maps/public/classes/sources/es_agg_source/es_agg_source.d.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
- * or more contributor license agreements. Licensed under the Elastic License;
- * you may not use this file except in compliance with the Elastic License.
- */
-
-import { IESSource } from '../es_source';
-import { AbstractESSource } from '../es_source';
-import { AGG_TYPE } from '../../../../common/constants';
-import { IESAggField } from '../../fields/es_agg_field';
-import { AbstractESAggSourceDescriptor } from '../../../../common/descriptor_types';
-
-export interface IESAggSource extends IESSource {
-  getAggKey(aggType: AGG_TYPE, fieldName: string): string;
-  getAggLabel(aggType: AGG_TYPE, fieldName: string): string;
-  getMetricFields(): IESAggField[];
-  hasMatchingMetricField(fieldName: string): boolean;
-  getMetricFieldForName(fieldName: string): IESAggField | null;
-}
-
-export class AbstractESAggSource extends AbstractESSource implements IESAggSource {
-  constructor(sourceDescriptor: AbstractESAggSourceDescriptor, inspectorAdapters: object);
-
-  getAggKey(aggType: AGG_TYPE, fieldName: string): string;
-  getAggLabel(aggType: AGG_TYPE, fieldName: string): string;
-  getMetricFields(): IESAggField[];
-  hasMatchingMetricField(fieldName: string): boolean;
-  getMetricFieldForName(fieldName: string): IESAggField | null;
-}
diff --git a/x-pack/plugins/maps/public/classes/sources/es_agg_source/es_agg_source.js b/x-pack/plugins/maps/public/classes/sources/es_agg_source/es_agg_source.ts
similarity index 56%
rename from x-pack/plugins/maps/public/classes/sources/es_agg_source/es_agg_source.js
rename to x-pack/plugins/maps/public/classes/sources/es_agg_source/es_agg_source.ts
index e20c509ccd4a2..a9c886617d3af 100644
--- a/x-pack/plugins/maps/public/classes/sources/es_agg_source/es_agg_source.js
+++ b/x-pack/plugins/maps/public/classes/sources/es_agg_source/es_agg_source.ts
@@ -5,19 +5,38 @@
  */
 
 import { i18n } from '@kbn/i18n';
+import { Adapters } from 'src/plugins/inspector/public';
+import { GeoJsonProperties } from 'geojson';
+import { IESSource } from '../es_source';
 import { AbstractESSource } from '../es_source';
 import { esAggFieldsFactory } from '../../fields/es_agg_field';
 import { AGG_TYPE, COUNT_PROP_LABEL, FIELD_ORIGIN } from '../../../../common/constants';
+import { IESAggField } from '../../fields/es_agg_field';
 import { getSourceAggKey } from '../../../../common/get_agg_key';
+import { AbstractESAggSourceDescriptor, AggDescriptor } from '../../../../common/descriptor_types';
+import { IndexPattern } from '../../../../../../../src/plugins/data/public';
+import { IField } from '../../fields/field';
+import { ITooltipProperty } from '../../tooltips/tooltip_property';
 
 export const DEFAULT_METRIC = { type: AGG_TYPE.COUNT };
 
+export interface IESAggSource extends IESSource {
+  getAggKey(aggType: AGG_TYPE, fieldName: string): string;
+  getAggLabel(aggType: AGG_TYPE, fieldName: string): string;
+  getMetricFields(): IESAggField[];
+  hasMatchingMetricField(fieldName: string): boolean;
+  getMetricFieldForName(fieldName: string): IESAggField | null;
+  getValueAggsDsl(indexPattern: IndexPattern): { [key: string]: unknown };
+}
+
 export class AbstractESAggSource extends AbstractESSource {
-  constructor(descriptor, inspectorAdapters) {
+  private readonly _metricFields: IESAggField[];
+
+  constructor(descriptor: AbstractESAggSourceDescriptor, inspectorAdapters: Adapters) {
     super(descriptor, inspectorAdapters);
     this._metricFields = [];
-    if (this._descriptor.metrics) {
-      this._descriptor.metrics.forEach((aggDescriptor) => {
+    if (descriptor.metrics) {
+      descriptor.metrics.forEach((aggDescriptor: AggDescriptor) => {
         this._metricFields.push(
           ...esAggFieldsFactory(aggDescriptor, this, this.getOriginForField())
         );
@@ -25,30 +44,31 @@ export class AbstractESAggSource extends AbstractESSource {
     }
   }
 
-  getFieldByName(name) {
-    return this.getMetricFieldForName(name);
+  getFieldByName(fieldName: string) {
+    return this.getMetricFieldForName(fieldName);
   }
 
-  createField() {
+  createField({ fieldName }: { fieldName: string }): IField {
     throw new Error('Cannot create a new field from just a fieldname for an es_agg_source.');
   }
 
-  hasMatchingMetricField(fieldName) {
+  hasMatchingMetricField(fieldName: string): boolean {
     const matchingField = this.getMetricFieldForName(fieldName);
     return !!matchingField;
   }
 
-  getMetricFieldForName(fieldName) {
-    return this.getMetricFields().find((metricField) => {
+  getMetricFieldForName(fieldName: string): IESAggField | null {
+    const targetMetricField = this.getMetricFields().find((metricField: IESAggField) => {
       return metricField.getName() === fieldName;
     });
+    return targetMetricField ? targetMetricField : null;
   }
 
   getOriginForField() {
     return FIELD_ORIGIN.SOURCE;
   }
 
-  getMetricFields() {
+  getMetricFields(): IESAggField[] {
     const metrics = this._metricFields.filter((esAggField) => esAggField.isValid());
     // Handle case where metrics is empty because older saved object state is empty array or there are no valid aggs.
     return metrics.length === 0
@@ -56,14 +76,14 @@ export class AbstractESAggSource extends AbstractESSource {
       : metrics;
   }
 
-  getAggKey(aggType, fieldName) {
+  getAggKey(aggType: AGG_TYPE, fieldName: string): string {
     return getSourceAggKey({
       aggType,
       aggFieldName: fieldName,
     });
   }
 
-  getAggLabel(aggType, fieldName) {
+  getAggLabel(aggType: AGG_TYPE, fieldName: string): string {
     switch (aggType) {
       case AGG_TYPE.COUNT:
         return COUNT_PROP_LABEL;
@@ -81,8 +101,8 @@ export class AbstractESAggSource extends AbstractESSource {
     return this.getMetricFields();
   }
 
-  getValueAggsDsl(indexPattern) {
-    const valueAggsDsl = {};
+  getValueAggsDsl(indexPattern: IndexPattern) {
+    const valueAggsDsl: { [key: string]: unknown } = {};
     this.getMetricFields().forEach((esAggMetric) => {
       const aggDsl = esAggMetric.getValueAggDsl(indexPattern);
       if (aggDsl) {
@@ -92,9 +112,9 @@ export class AbstractESAggSource extends AbstractESSource {
     return valueAggsDsl;
   }
 
-  async filterAndFormatPropertiesToHtmlForMetricFields(properties) {
-    const metricFields = this.getMetricFields();
-    const tooltipPropertiesPromises = [];
+  async getTooltipProperties(properties: GeoJsonProperties) {
+    const metricFields = await this.getFields();
+    const promises: Array<Promise<ITooltipProperty>> = [];
     metricFields.forEach((metricField) => {
       let value;
       for (const key in properties) {
@@ -105,9 +125,9 @@ export class AbstractESAggSource extends AbstractESSource {
       }
 
       const tooltipPromise = metricField.createTooltipProperty(value);
-      tooltipPropertiesPromises.push(tooltipPromise);
+      promises.push(tooltipPromise);
     });
 
-    return await Promise.all(tooltipPropertiesPromises);
+    return await Promise.all(promises);
   }
 }
diff --git a/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.d.ts b/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.d.ts
index 51ee15e7ea5af..2ce4353fca13c 100644
--- a/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.d.ts
+++ b/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.d.ts
@@ -7,6 +7,7 @@
 import { AbstractESAggSource } from '../es_agg_source';
 import { ESGeoGridSourceDescriptor } from '../../../../common/descriptor_types';
 import { GRID_RESOLUTION } from '../../../../common/constants';
+import { IField } from '../../fields/field';
 
 export class ESGeoGridSource extends AbstractESAggSource {
   static createDescriptor({
@@ -21,4 +22,5 @@ export class ESGeoGridSource extends AbstractESAggSource {
   getFieldNames(): string[];
   getGridResolution(): GRID_RESOLUTION;
   getGeoGridPrecision(zoom: number): number;
+  createField({ fieldName }: { fieldName: string }): IField;
 }
diff --git a/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.js b/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.js
index a6322ff3ba784..aa167cb577672 100644
--- a/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.js
+++ b/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.js
@@ -321,10 +321,6 @@ export class ESGeoGridSource extends AbstractESAggSource {
     return true;
   }
 
-  async filterAndFormatPropertiesToHtml(properties) {
-    return await this.filterAndFormatPropertiesToHtmlForMetricFields(properties);
-  }
-
   async getSupportedShapeTypes() {
     if (this._descriptor.requestType === RENDER_AS.GRID) {
       return [VECTOR_SHAPE_TYPE.POLYGON];
diff --git a/x-pack/plugins/maps/public/classes/sources/es_pew_pew_source/es_pew_pew_source.js b/x-pack/plugins/maps/public/classes/sources/es_pew_pew_source/es_pew_pew_source.js
index 92b0c717f6724..9ec54335d4e78 100644
--- a/x-pack/plugins/maps/public/classes/sources/es_pew_pew_source/es_pew_pew_source.js
+++ b/x-pack/plugins/maps/public/classes/sources/es_pew_pew_source/es_pew_pew_source.js
@@ -223,10 +223,6 @@ export class ESPewPewSource extends AbstractESAggSource {
   canFormatFeatureProperties() {
     return true;
   }
-
-  async filterAndFormatPropertiesToHtml(properties) {
-    return await this.filterAndFormatPropertiesToHtmlForMetricFields(properties);
-  }
 }
 
 registerSource({
diff --git a/x-pack/plugins/maps/public/classes/sources/es_search_source/es_search_source.js b/x-pack/plugins/maps/public/classes/sources/es_search_source/es_search_source.js
index 7ac2738eaeb51..df83bd1cf5e60 100644
--- a/x-pack/plugins/maps/public/classes/sources/es_search_source/es_search_source.js
+++ b/x-pack/plugins/maps/public/classes/sources/es_search_source/es_search_source.js
@@ -438,7 +438,7 @@ export class ESSearchSource extends AbstractESSource {
     return properties;
   }
 
-  async filterAndFormatPropertiesToHtml(properties) {
+  async getTooltipProperties(properties) {
     const indexPattern = await this.getIndexPattern();
     const propertyValues = await this._loadTooltipProperties(
       properties._id,
diff --git a/x-pack/plugins/maps/public/classes/sources/es_term_source/es_term_source.js b/x-pack/plugins/maps/public/classes/sources/es_term_source/es_term_source.js
index 8cc8dd5c4a080..b4ad256c1598a 100644
--- a/x-pack/plugins/maps/public/classes/sources/es_term_source/es_term_source.js
+++ b/x-pack/plugins/maps/public/classes/sources/es_term_source/es_term_source.js
@@ -129,10 +129,6 @@ export class ESTermSource extends AbstractESAggSource {
     return `es_table ${this.getIndexPatternId()}`;
   }
 
-  async filterAndFormatPropertiesToHtml(properties) {
-    return await this.filterAndFormatPropertiesToHtmlForMetricFields(properties);
-  }
-
   getFieldNames() {
     return this.getMetricFields().map((esAggMetricField) => esAggMetricField.getName());
   }
diff --git a/x-pack/plugins/maps/public/classes/sources/mvt_single_layer_vector_source/mvt_single_layer_vector_source.test.tsx b/x-pack/plugins/maps/public/classes/sources/mvt_single_layer_vector_source/mvt_single_layer_vector_source.test.tsx
index 4e9e1e9cd7680..48f7b30261f38 100644
--- a/x-pack/plugins/maps/public/classes/sources/mvt_single_layer_vector_source/mvt_single_layer_vector_source.test.tsx
+++ b/x-pack/plugins/maps/public/classes/sources/mvt_single_layer_vector_source/mvt_single_layer_vector_source.test.tsx
@@ -45,7 +45,7 @@ describe('canFormatFeatureProperties', () => {
   });
 });
 
-describe('filterAndFormatPropertiesToHtml', () => {
+describe('getTooltipProperties', () => {
   const descriptorWithFields = {
     ...descriptor,
     fields: [
@@ -67,7 +67,7 @@ describe('filterAndFormatPropertiesToHtml', () => {
 
   it('should get tooltipproperties', async () => {
     const source = new MVTSingleLayerVectorSource(descriptorWithFields);
-    const tooltipProperties = await source.filterAndFormatPropertiesToHtml({
+    const tooltipProperties = await source.getTooltipProperties({
       foo: 'bar',
       fooz: 123,
     });
diff --git a/x-pack/plugins/maps/public/classes/sources/mvt_single_layer_vector_source/mvt_single_layer_vector_source.tsx b/x-pack/plugins/maps/public/classes/sources/mvt_single_layer_vector_source/mvt_single_layer_vector_source.tsx
index 52dc89a6bba58..3e515613b3fd0 100644
--- a/x-pack/plugins/maps/public/classes/sources/mvt_single_layer_vector_source/mvt_single_layer_vector_source.tsx
+++ b/x-pack/plugins/maps/public/classes/sources/mvt_single_layer_vector_source/mvt_single_layer_vector_source.tsx
@@ -192,7 +192,7 @@ export class MVTSingleLayerVectorSource
     return false;
   }
 
-  async filterAndFormatPropertiesToHtml(
+  async getTooltipProperties(
     properties: GeoJsonProperties,
     featureId?: string | number
   ): Promise<ITooltipProperty[]> {
diff --git a/x-pack/plugins/maps/public/classes/sources/vector_source/vector_source.d.ts b/x-pack/plugins/maps/public/classes/sources/vector_source/vector_source.d.ts
index fd9c179275444..a481e273bc33e 100644
--- a/x-pack/plugins/maps/public/classes/sources/vector_source/vector_source.d.ts
+++ b/x-pack/plugins/maps/public/classes/sources/vector_source/vector_source.d.ts
@@ -36,7 +36,7 @@ export type BoundsFilters = {
 };
 
 export interface IVectorSource extends ISource {
-  filterAndFormatPropertiesToHtml(properties: GeoJsonProperties): Promise<ITooltipProperty[]>;
+  getTooltipProperties(properties: GeoJsonProperties): Promise<ITooltipProperty[]>;
   getBoundsForFilters(
     boundsFilters: BoundsFilters,
     registerCancelCallback: (requestToken: symbol, callback: () => void) => void
@@ -58,7 +58,7 @@ export interface IVectorSource extends ISource {
 }
 
 export class AbstractVectorSource extends AbstractSource implements IVectorSource {
-  filterAndFormatPropertiesToHtml(properties: GeoJsonProperties): Promise<ITooltipProperty[]>;
+  getTooltipProperties(properties: GeoJsonProperties): Promise<ITooltipProperty[]>;
   getBoundsForFilters(
     boundsFilters: BoundsFilters,
     registerCancelCallback: (requestToken: symbol, callback: () => void) => void
diff --git a/x-pack/plugins/maps/public/classes/sources/vector_source/vector_source.js b/x-pack/plugins/maps/public/classes/sources/vector_source/vector_source.js
index 98ed89a6ff0ad..9569b8626aabf 100644
--- a/x-pack/plugins/maps/public/classes/sources/vector_source/vector_source.js
+++ b/x-pack/plugins/maps/public/classes/sources/vector_source/vector_source.js
@@ -106,7 +106,7 @@ export class AbstractVectorSource extends AbstractSource {
   }
 
   // Allow source to filter and format feature properties before displaying to user
-  async filterAndFormatPropertiesToHtml(properties) {
+  async getTooltipProperties(properties) {
     const tooltipProperties = [];
     for (const key in properties) {
       if (key.startsWith('__kbn')) {
diff --git a/x-pack/plugins/maps/public/components/metrics_editor/metrics_editor.tsx b/x-pack/plugins/maps/public/components/metrics_editor/metrics_editor.tsx
index 17cfc5f62fee5..dae1f51469281 100644
--- a/x-pack/plugins/maps/public/components/metrics_editor/metrics_editor.tsx
+++ b/x-pack/plugins/maps/public/components/metrics_editor/metrics_editor.tsx
@@ -8,7 +8,6 @@ import React, { Fragment } from 'react';
 import { FormattedMessage } from '@kbn/i18n/react';
 import { EuiButtonEmpty, EuiComboBoxOptionOption, EuiSpacer, EuiTextAlign } from '@elastic/eui';
 import { MetricEditor } from './metric_editor';
-// @ts-expect-error
 import { DEFAULT_METRIC } from '../../classes/sources/es_agg_source';
 import { IFieldType } from '../../../../../../src/plugins/data/public';
 import { AggDescriptor } from '../../../common/descriptor_types';