Skip to content

Commit

Permalink
make partition field in tooltip consistent. simplify result fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarezmelissa87 committed Jan 24, 2022
1 parent 6a4dbf4 commit 690b39d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 75 deletions.
5 changes: 4 additions & 1 deletion x-pack/plugins/ml/public/maps/anomaly_source.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export class AnomalySource implements IVectorSource {
}

async getSupportedShapeTypes(): Promise<VECTOR_SHAPE_TYPE[]> {
return this._descriptor.typicalActual === 'connected'
return this._descriptor.typicalActual === 'typical to actual'
? [VECTOR_SHAPE_TYPE.LINE]
: [VECTOR_SHAPE_TYPE.POINT];
}
Expand All @@ -251,6 +251,9 @@ export class AnomalySource implements IVectorSource {
const label = ANOMALY_SOURCE_FIELDS[key]?.label;
if (label) {
tooltipProperties.push(new AnomalySourceTooltipProperty(label, properties[key]));
} else if (!ANOMALY_SOURCE_FIELDS[key]) {
// partition field keys will be different each time so won't be in ANOMALY_SOURCE_FIELDS
tooltipProperties.push(new AnomalySourceTooltipProperty(key, properties[key]));
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/ml/public/maps/anomaly_source_field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ export const ANOMALY_SOURCE_FIELDS: Record<string, Record<string, string>> = {
}),
type: 'string',
},
actual: {},
actualDisplay: {
label: i18n.translate('xpack.ml.maps.anomalyLayerActualLabel', {
defaultMessage: 'Actual',
}),
type: 'string',
},
typical: {},
typicalDisplay: {
label: i18n.translate('xpack.ml.maps.anomalyLayerTypicalLabel', {
defaultMessage: 'Typical',
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/ml/public/maps/layer_selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class LayerSelector extends Component<Props, State> {
const typicalActual: MlAnomalyLayers = selectedOptions[0].value! as
| 'typical'
| 'actual'
| 'connected';
| 'typical to actual';
if (this._isMounted) {
this.setState({ typicalActual });
this.props.onChange(typicalActual);
Expand All @@ -58,7 +58,7 @@ export class LayerSelector extends Component<Props, State> {
options={[
{ value: 'actual', label: 'actual' },
{ value: 'typical', label: 'typical' },
{ value: 'connected', label: 'connected' },
{ value: 'typical to actual', label: 'typical to actual' },
]}
selectedOptions={options}
/>
Expand Down
109 changes: 37 additions & 72 deletions x-pack/plugins/ml/public/maps/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,7 @@ import type { MlApiServices } from '../application/services/ml_api_service';
import { MLAnomalyDoc } from '../../common/types/anomalies';
import { VectorSourceRequestMeta } from '../../../maps/common';

export type MlAnomalyLayers = 'typical' | 'actual' | 'connected';
interface Hit {
[x: string]: any;
actual: number[];
actualDisplay: number[];
fieldName?: string;
functionDescription: string;
typical: number[];
typicalDisplay: number[];
record_score: number;
timestamp: string;
}
export type MlAnomalyLayers = 'typical' | 'actual' | 'typical to actual';

// 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 Down Expand Up @@ -75,7 +64,6 @@ export async function getResultsForJobId(
}

let resp: ESSearchResponse<MLAnomalyDoc> | null = null;
let hits: Hit[] = [];

try {
resp = await mlResultsService.anomalySearch(
Expand All @@ -88,8 +76,9 @@ export async function getResultsForJobId(
// search may fail if the job doesn't already exist
// ignore this error as the outer function call will raise a toast
}
if (resp !== null && resp.hits.total.value > 0) {
hits = resp.hits.hits.map(({ _source }) => {

const features: Feature[] =
resp?.hits.hits.map(({ _source }) => {
const geoResults = _source.geo_results;
const actualCoordStr = geoResults && geoResults.actual_point;
const typicalCoordStr = geoResults && geoResults.typical_point;
Expand All @@ -106,65 +95,41 @@ export async function getResultsForJobId(
typical = getCoordinates(typicalCoordStr);
typicalDisplay = getCoordinates(typicalCoordStr, true);
}
return {
fieldName: _source.field_name,
functionDescription: _source.function_description,
timestamp: formatHumanReadableDateTimeSeconds(_source.timestamp),
typical,
typicalDisplay,
actual,
actualDisplay,
record_score: Math.floor(_source.record_score),
...(_source.partition_field_name
? { partition_field_name: _source.partition_field_name }
: {}),
...(_source.partition_field_value
? { partition_field_value: _source.partition_field_value }
: {}),
...(_source.by_field_name ? { by_field_name: _source.by_field_name } : {}),
...(_source.by_field_value ? { by_field_value: _source.by_field_value } : {}),
...(_source.over_field_value ? { over_field_value: _source.over_field_value } : {}),
};
});
}

const features: Feature[] = hits.map((result) => {
let geometry: Geometry;
if (locationType === 'typical' || locationType === 'actual') {
geometry = {
type: 'Point',
coordinates: locationType === 'typical' ? result.typical : result.actual,
};
} else {
geometry = {
type: 'LineString',
coordinates: [result.typical, result.actual],
let geometry: Geometry;
if (locationType === 'typical' || locationType === 'actual') {
geometry = {
type: 'Point',
coordinates: locationType === 'typical' ? typical : actual,
};
} else {
geometry = {
type: 'LineString',
coordinates: [typical, actual],
};
}
return {
type: 'Feature',
geometry,
properties: {
actual,
actualDisplay,
typical,
typicalDisplay,
fieldName: _source.field_name,
functionDescription: _source.function_description,
timestamp: formatHumanReadableDateTimeSeconds(_source.timestamp),
record_score: Math.floor(_source.record_score),
...(_source.partition_field_name
? { [_source.partition_field_name]: _source.partition_field_value }
: {}),
...(_source.by_field_name ? { [_source.by_field_name]: _source.by_field_value } : {}),
...(_source.over_field_name
? { [_source.over_field_name]: _source.over_field_value }
: {}),
},
};
}
return {
type: 'Feature',
geometry,
properties: {
actual: result.actual,
actualDisplay: result.actualDisplay,
typical: result.typical,
typicalDisplay: result.typicalDisplay,
fieldName: result.fieldName,
functionDescription: result.functionDescription,
timestamp: result.timestamp,
record_score: result.record_score,
...(result.partition_field_name
? { partition_field_name: result.partition_field_name }
: {}),
...(result.partition_field_value
? { partition_field_value: result.partition_field_value }
: {}),
...(result.by_field_name ? { by_field_name: result.by_field_name } : {}),
...(result.by_field_value ? { by_field_value: result.by_field_value } : {}),
...(result.over_field_value ? { over_field_value: result.over_field_value } : {}),
},
};
});
}) || [];

return {
type: 'FeatureCollection',
Expand Down

0 comments on commit 690b39d

Please sign in to comment.