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

[Maps] convert tooltip classes to typescript #59589

Merged
merged 12 commits into from
Mar 10, 2020
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 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

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

imho use declaration file like everywhere else. so either rename to inner_join.d.ts or rename the others to join.js and join.test.js

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the interface description should be in a file called join.ts. The interface is for join, not the implementation that we have. I would like to leave as is.

* 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