Skip to content

Commit

Permalink
[Maps] convert tooltip classes to typescript (#59589)
Browse files Browse the repository at this point in the history
* getting started

* fix ts lint errors

* TS es_tooltip_property

* convert ESAggTooltipProperty to TS

* final clean up

* ts lint cleanup

* review feedback

* remove unused import

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
nreese and elasticmachine authored Mar 10, 2020
1 parent 848188e commit eb533c8
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 151 deletions.
15 changes: 5 additions & 10 deletions x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { IVectorSource } from '../sources/vector_source';
import { ESDocField } from './es_doc_field';
import { AGG_TYPE, FIELD_ORIGIN } from '../../../common/constants';
import { isMetricCountable } from '../util/is_metric_countable';
// @ts-ignore
import { ESAggMetricTooltipProperty } from '../tooltips/es_aggmetric_tooltip_property';
import { getField, addFieldToDSL } from '../util/es_agg_utils';
import { TopTermPercentageField } from './top_term_percentage_field';
import { ITooltipProperty, TooltipProperty } from '../tooltips/tooltip_property';
import { ESAggTooltipProperty } from '../tooltips/es_agg_tooltip_property';

export interface IESAggField extends IField {
getValueAggDsl(indexPattern: IndexPattern): unknown | null;
Expand Down Expand Up @@ -92,15 +92,10 @@ export class ESAggField implements IESAggField {
return this._esDocField ? this._esDocField.getName() : '';
}

async createTooltipProperty(value: number | string): Promise<unknown> {
async createTooltipProperty(value: string | undefined): Promise<ITooltipProperty> {
const indexPattern = await this._source.getIndexPattern();
return new ESAggMetricTooltipProperty(
this.getName(),
await this.getLabel(),
value,
indexPattern,
this
);
const tooltipProperty = new TooltipProperty(this.getName(), await this.getLabel(), value);
return new ESAggTooltipProperty(tooltipProperty, indexPattern, this);
}

getValueAggDsl(indexPattern: IndexPattern): unknown | null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { AbstractField } from './field';
import { ESTooltipProperty } from '../tooltips/es_tooltip_property';
import { TooltipProperty } from '../tooltips/tooltip_property';
import { COLOR_PALETTE_MAX_SIZE } from '../../../common/constants';
import { indexPatterns } from '../../../../../../../src/plugins/data/public';

Expand All @@ -20,7 +21,8 @@ export class ESDocField extends AbstractField {

async createTooltipProperty(value) {
const indexPattern = await this._source.getIndexPattern();
return new ESTooltipProperty(this.getName(), this.getName(), value, indexPattern);
const tooltipProperty = new TooltipProperty(this.getName(), this.getName(), value);
return new ESTooltipProperty(tooltipProperty, indexPattern, this);
}

async getDataType() {
Expand Down
4 changes: 3 additions & 1 deletion x-pack/legacy/plugins/maps/public/layers/fields/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

import { FIELD_ORIGIN } from '../../../common/constants';
import { IVectorSource } from '../sources/vector_source';
import { ITooltipProperty } from '../tooltips/tooltip_property';

export interface IField {
getName(): string;
getRootName(): string;
canValueBeFormatted(): boolean;
getLabel(): Promise<string>;
getDataType(): Promise<string>;
createTooltipProperty(value: string | undefined): Promise<ITooltipProperty>;
getSource(): IVectorSource;
getOrigin(): FIELD_ORIGIN;
isValid(): boolean;
Expand Down Expand Up @@ -65,7 +67,7 @@ export class AbstractField implements IField {
return this._fieldName;
}

async createTooltipProperty(): Promise<unknown> {
async createTooltipProperty(value: string | undefined): Promise<ITooltipProperty> {
throw new Error('must implement Field#createTooltipProperty');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { IESAggField } from './es_agg_field';
import { IVectorSource } from '../sources/vector_source';
// @ts-ignore
import { TooltipProperty } from '../tooltips/tooltip_property';
import { ITooltipProperty, TooltipProperty } from '../tooltips/tooltip_property';
import { TOP_TERM_PERCENTAGE_SUFFIX } from '../../../common/constants';
import { FIELD_ORIGIN } from '../../../common/constants';

Expand Down Expand Up @@ -48,7 +48,7 @@ export class TopTermPercentageField implements IESAggField {
return 'number';
}

async createTooltipProperty(value: unknown): Promise<unknown> {
async createTooltipProperty(value: string | undefined): Promise<ITooltipProperty> {
return new TooltipProperty(this.getName(), await this.getLabel(), value);
}

Expand Down
11 changes: 11 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/joins/join.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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 { IESTermSource } from '../sources/es_term_source';

export interface IJoin {
getRightJoinSource(): IESTermSource;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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 { IField } from '../fields/field';
import { IESAggSource } from './es_agg_source';

export interface IESTermSource extends IESAggSource {
getTermField(): IField;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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 { ESTooltipProperty } from './es_tooltip_property';

export class ESAggTooltipProperty extends ESTooltipProperty {
isFilterable(): boolean {
return false;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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 _ from 'lodash';
import { ITooltipProperty } from './tooltip_property';
import { IField } from '../fields/field';
import { esFilters, IFieldType, IndexPattern } from '../../../../../../../src/plugins/data/public';
import { PhraseFilter } from '../../../../../../../src/plugins/data/public';

export class ESTooltipProperty implements ITooltipProperty {
private readonly _tooltipProperty: ITooltipProperty;
private readonly _indexPattern: IndexPattern;
private readonly _field: IField;

constructor(tooltipProperty: ITooltipProperty, indexPattern: IndexPattern, field: IField) {
this._tooltipProperty = tooltipProperty;
this._indexPattern = indexPattern;
this._field = field;
}

getPropertyKey(): string {
return this._tooltipProperty.getPropertyKey();
}

getPropertyName(): string {
return this._tooltipProperty.getPropertyName();
}

getRawValue(): string | undefined {
return this._tooltipProperty.getRawValue();
}

_getIndexPatternField(): IFieldType | undefined {
return this._indexPattern.fields.getByName(this._field.getRootName());
}

getHtmlDisplayValue(): string {
if (typeof this.getRawValue() === 'undefined') {
return '-';
}

const indexPatternField = this._getIndexPatternField();
if (!indexPatternField || !this._field.canValueBeFormatted()) {
return _.escape(this.getRawValue());
}

const htmlConverter = indexPatternField.format.getConverterFor('html');
return htmlConverter
? htmlConverter(this.getRawValue())
: indexPatternField.format.convert(this.getRawValue());
}

isFilterable(): boolean {
const indexPatternField = this._getIndexPatternField();
return (
!!indexPatternField &&
(indexPatternField.type === 'string' ||
indexPatternField.type === 'date' ||
indexPatternField.type === 'ip' ||
indexPatternField.type === 'number')
);
}

async getESFilters(): Promise<PhraseFilter[]> {
const indexPatternField = this._getIndexPatternField();
if (!indexPatternField) {
return [];
}

return [esFilters.buildPhraseFilter(indexPatternField, this.getRawValue(), this._indexPattern)];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,40 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { TooltipProperty } from './tooltip_property';
import { ITooltipProperty } from './tooltip_property';
import { IJoin } from '../joins/join';
import { PhraseFilter } from '../../../../../../../src/plugins/data/public';

export class JoinTooltipProperty extends TooltipProperty {
constructor(tooltipProperty, leftInnerJoins) {
super();
export class JoinTooltipProperty implements ITooltipProperty {
private readonly _tooltipProperty: ITooltipProperty;
private readonly _leftInnerJoins: IJoin[];

constructor(tooltipProperty: ITooltipProperty, leftInnerJoins: IJoin[]) {
this._tooltipProperty = tooltipProperty;
this._leftInnerJoins = leftInnerJoins;
}

isFilterable() {
isFilterable(): boolean {
return true;
}

getPropertyKey() {
getPropertyKey(): string {
return this._tooltipProperty.getPropertyKey();
}

getPropertyName() {
getPropertyName(): string {
return this._tooltipProperty.getPropertyName();
}

getHtmlDisplayValue() {
getRawValue(): string | undefined {
return this._tooltipProperty.getRawValue();
}

getHtmlDisplayValue(): string {
return this._tooltipProperty.getHtmlDisplayValue();
}

async getESFilters() {
async getESFilters(): Promise<PhraseFilter[]> {
const esFilters = [];
if (this._tooltipProperty.isFilterable()) {
esFilters.push(...(await this._tooltipProperty.getESFilters()));
Expand All @@ -46,6 +54,7 @@ export class JoinTooltipProperty extends TooltipProperty {
esFilters.push(...(await esTooltipProperty.getESFilters()));
}
} catch (e) {
// eslint-disable-next-line no-console
console.error('Cannot create joined filter', e);
}
}
Expand Down

This file was deleted.

Loading

0 comments on commit eb533c8

Please sign in to comment.