Skip to content

Commit

Permalink
[Maps] convert ESAggSource to TS (elastic#76999)
Browse files Browse the repository at this point in the history
* [Maps] convert ESAggSource to TS

* one more rename

* tslint fixes

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
nreese and elasticmachine committed Sep 10, 2020
1 parent 9d69007 commit 64a1455
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 79 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/maps/public/classes/joins/inner_join.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});

Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down Expand Up @@ -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()];
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,85 @@
*/

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())
);
});
}
}

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
? esAggFieldsFactory({ type: AGG_TYPE.COUNT }, this, this.getOriginForField())
: 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;
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -21,4 +22,5 @@ export class ESGeoGridSource extends AbstractESAggSource {
getFieldNames(): string[];
getGridResolution(): GRID_RESOLUTION;
getGeoGridPrecision(zoom: number): number;
createField({ fieldName }: { fieldName: string }): IField;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,6 @@ export class ESPewPewSource extends AbstractESAggSource {
canFormatFeatureProperties() {
return true;
}

async filterAndFormatPropertiesToHtml(properties) {
return await this.filterAndFormatPropertiesToHtmlForMetricFields(properties);
}
}

registerSource({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('canFormatFeatureProperties', () => {
});
});

describe('filterAndFormatPropertiesToHtml', () => {
describe('getTooltipProperties', () => {
const descriptorWithFields = {
...descriptor,
fields: [
Expand All @@ -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,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export class MVTSingleLayerVectorSource
return false;
}

async filterAndFormatPropertiesToHtml(
async getTooltipProperties(
properties: GeoJsonProperties,
featureId?: string | number
): Promise<ITooltipProperty[]> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 64a1455

Please sign in to comment.