Skip to content

Commit

Permalink
fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarezmelissa87 committed Jan 24, 2022
1 parent 1ae5ddc commit 5e55ba0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 33 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/ml/public/maps/anomaly_source.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ import type { SourceEditorArgs } from '../../../maps/public';
import type { DataRequest } from '../../../maps/public';
import type { IVectorSource, SourceStatus } from '../../../maps/public';
import { ML_ANOMALY } from './anomaly_source_factory';
import { getResultsForJobId, ML_ANOMALY_LAYERS } from './util';
import { getResultsForJobId, ML_ANOMALY_LAYERS, MlAnomalyLayersType } from './util';
import { UpdateAnomalySourceEditor } from './update_anomaly_source_editor';
import type { MlApiServices } from '../application/services/ml_api_service';

export interface AnomalySourceDescriptor extends AbstractSourceDescriptor {
jobId: string;
typicalActual: ML_ANOMALY_LAYERS;
typicalActual: MlAnomalyLayersType;
}

export class AnomalySource implements IVectorSource {
Expand Down
18 changes: 12 additions & 6 deletions x-pack/plugins/ml/public/maps/anomaly_source_field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ import { AnomalySource } from './anomaly_source';
import { ITooltipProperty } from '../../../maps/public';
import { Filter } from '../../../../../src/plugins/data/public';

export const ACTUAL_LABEL = i18n.translate('xpack.ml.maps.anomalyLayerActualLabel', {
defaultMessage: 'Actual',
});
export const TYPICAL_LABEL = i18n.translate('xpack.ml.maps.anomalyLayerTypicalLabel', {
defaultMessage: 'Typical',
});
export const TYPICAL_TO_ACTUAL = i18n.translate('xpack.ml.maps.anomalyLayerTypicalToActualLabel', {
defaultMessage: 'Typical to actual',
});

export const ANOMALY_SOURCE_FIELDS: Record<string, Record<string, string>> = {
record_score: {
label: i18n.translate('xpack.ml.maps.anomalyLayerRecordScoreLabel', {
Expand Down Expand Up @@ -43,17 +53,13 @@ export const ANOMALY_SOURCE_FIELDS: Record<string, Record<string, string>> = {
// this value is only used to place the point on the map
actual: {},
actualDisplay: {
label: i18n.translate('xpack.ml.maps.anomalyLayerActualLabel', {
defaultMessage: 'Actual',
}),
label: ACTUAL_LABEL,
type: 'string',
},
// this value is only used to place the point on the map
typical: {},
typicalDisplay: {
label: i18n.translate('xpack.ml.maps.anomalyLayerTypicalLabel', {
defaultMessage: 'Typical',
}),
label: TYPICAL_LABEL,
type: 'string',
},
partition_field_name: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { EuiPanel } from '@elastic/eui';
import { AnomalySourceDescriptor } from './anomaly_source';
import { AnomalyJobSelector } from './anomaly_job_selector';
import { LayerSelector } from './layer_selector';
import { ML_ANOMALY_LAYERS } from './util';
import { ML_ANOMALY_LAYERS, MlAnomalyLayersType } from './util';
import type { MlApiServices } from '../application/services/ml_api_service';

interface Props {
Expand All @@ -21,7 +21,7 @@ interface Props {

interface State {
jobId?: string;
typicalActual?: ML_ANOMALY_LAYERS;
typicalActual?: MlAnomalyLayersType;
}

export class CreateAnomalySourceEditor extends Component<Props, State> {
Expand All @@ -41,7 +41,7 @@ export class CreateAnomalySourceEditor extends Component<Props, State> {
this._isMounted = true;
}

private onTypicalActualChange = (typicalActual: ML_ANOMALY_LAYERS) => {
private onTypicalActualChange = (typicalActual: MlAnomalyLayersType) => {
if (!this._isMounted) {
return;
}
Expand Down
21 changes: 8 additions & 13 deletions x-pack/plugins/ml/public/maps/layer_selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import React, { Component } from 'react';

import { EuiComboBox, EuiFormRow, EuiComboBoxOptionOption } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { ML_ANOMALY_LAYERS } from './util';
import { ML_ANOMALY_LAYERS, MlAnomalyLayersType } from './util';
import { ACTUAL_LABEL, TYPICAL_LABEL, TYPICAL_TO_ACTUAL } from './anomaly_source_field';

interface Props {
onChange: (typicalActual: ML_ANOMALY_LAYERS) => void;
typicalActual: ML_ANOMALY_LAYERS;
onChange: (typicalActual: MlAnomalyLayersType) => void;
typicalActual: MlAnomalyLayersType;
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
Expand All @@ -33,7 +34,7 @@ export class LayerSelector extends Component<Props, State> {
}

onSelect = (selectedOptions: Array<EuiComboBoxOptionOption<string>>) => {
const typicalActual: ML_ANOMALY_LAYERS = selectedOptions[0].value! as ML_ANOMALY_LAYERS;
const typicalActual: MlAnomalyLayersType = selectedOptions[0].value! as MlAnomalyLayersType;
if (this._isMounted) {
this.setState({ typicalActual });
this.props.onChange(typicalActual);
Expand All @@ -55,21 +56,15 @@ export class LayerSelector extends Component<Props, State> {
options={[
{
value: ML_ANOMALY_LAYERS.ACTUAL,
label: i18n.translate('xpack.ml.maps.actualLabel', {
defaultMessage: ML_ANOMALY_LAYERS.ACTUAL,
}),
label: ACTUAL_LABEL,
},
{
value: ML_ANOMALY_LAYERS.TYPICAL,
label: i18n.translate('xpack.ml.maps.typicalLabel', {
defaultMessage: ML_ANOMALY_LAYERS.TYPICAL,
}),
label: TYPICAL_LABEL,
},
{
value: ML_ANOMALY_LAYERS.TYPICAL_TO_ACTUAL,
label: i18n.translate('xpack.ml.maps.typicalToActualLabel', {
defaultMessage: ML_ANOMALY_LAYERS.TYPICAL_TO_ACTUAL,
}),
label: TYPICAL_TO_ACTUAL,
},
]}
selectedOptions={options}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import React, { Fragment, Component } from 'react';
import { FormattedMessage } from '@kbn/i18n-react';
import { EuiPanel, EuiSpacer, EuiTitle } from '@elastic/eui';
import { LayerSelector } from './layer_selector';
import { ML_ANOMALY_LAYERS } from './util';
import { MlAnomalyLayersType } from './util';

interface Props {
onChange: (...args: Array<{ propName: string; value: unknown }>) => void;
typicalActual: ML_ANOMALY_LAYERS;
typicalActual: MlAnomalyLayersType;
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
Expand All @@ -34,7 +34,7 @@ export class UpdateAnomalySourceEditor extends Component<Props, State> {
</EuiTitle>
<EuiSpacer size="s" />
<LayerSelector
onChange={(typicalActual: ML_ANOMALY_LAYERS) => {
onChange={(typicalActual: MlAnomalyLayersType) => {
this.props.onChange({
propName: 'typicalActual',
value: typicalActual,
Expand Down
14 changes: 8 additions & 6 deletions x-pack/plugins/ml/public/maps/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import type { MlApiServices } from '../application/services/ml_api_service';
import { MLAnomalyDoc } from '../../common/types/anomalies';
import { VectorSourceRequestMeta } from '../../../maps/common';

export enum ML_ANOMALY_LAYERS {
TYPICAL = 'typical',
ACTUAL = 'actual',
TYPICAL_TO_ACTUAL = 'typical to actual',
}
export const ML_ANOMALY_LAYERS = {
TYPICAL: 'typical',
ACTUAL: 'actual',
TYPICAL_TO_ACTUAL: 'typical to actual',
} as const;

export type MlAnomalyLayersType = typeof ML_ANOMALY_LAYERS[keyof typeof ML_ANOMALY_LAYERS];

// Must reverse coordinates here. Map expects [lon, lat] - anomalies are stored as [lat, lon] for lat_lon jobs
function getCoordinates(actualCoordinateStr: string, round: boolean = false): number[] {
Expand All @@ -32,7 +34,7 @@ function getCoordinates(actualCoordinateStr: string, round: boolean = false): nu
export async function getResultsForJobId(
mlResultsService: MlApiServices['results'],
jobId: string,
locationType: ML_ANOMALY_LAYERS,
locationType: MlAnomalyLayersType,
searchFilters: VectorSourceRequestMeta
): Promise<FeatureCollection> {
const { timeFilters } = searchFilters;
Expand Down

0 comments on commit 5e55ba0

Please sign in to comment.