Skip to content

Commit

Permalink
ensure anomaly search respects time filter
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarezmelissa87 committed Jan 18, 2022
1 parent af0cfdf commit 7af220b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
25 changes: 14 additions & 11 deletions x-pack/plugins/ml/public/maps/anomaly_source.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ import { ML_ANOMALY } from './anomaly_source_factory';
import { getResultsForJobId } from './util';
import { UpdateAnomalySourceEditor } from './update_anomaly_source_editor';

export type SearchFilters = any & {
applyGlobalQuery: boolean;
applyGlobalTime: boolean;
fieldNames: string[];
geogridPrecision?: number;
sourceQuery?: object;
sourceMeta: object;
};

export interface AnomalySourceDescriptor extends AbstractSourceDescriptor {
jobId: string;
typicalActual: 'typical' | 'actual';
Expand All @@ -45,24 +54,18 @@ export class AnomalySource implements IVectorSource {
constructor(sourceDescriptor: Partial<AnomalySourceDescriptor>, adapters?: Adapters) {
this._descriptor = AnomalySource.createDescriptor(sourceDescriptor);
}
// TODO: implement Time filter functionality and query awareness
// TODO: implement query awareness
async getGeoJsonWithMeta(
layerName: string,
searchFilters: any & {
applyGlobalQuery: boolean;
applyGlobalTime: boolean;
fieldNames: string[];
geogridPrecision?: number;
sourceQuery?: object;
sourceMeta: object;
},
searchFilters: SearchFilters,
registerCancelCallback: (callback: () => void) => void,
isRequestStillActive: () => boolean
): Promise<GeoJsonWithMeta> {
const results = await getResultsForJobId(
AnomalySource.services[2].mlResultsService,
this._descriptor.jobId,
this._descriptor.typicalActual
this._descriptor.typicalActual,
searchFilters
);

return {
Expand Down Expand Up @@ -108,7 +111,7 @@ export class AnomalySource implements IVectorSource {
}

getApplyGlobalTime(): boolean {
return false;
return true;
}

async getAttributions(): Promise<Attribution[]> {
Expand Down
20 changes: 18 additions & 2 deletions x-pack/plugins/ml/public/maps/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@

import { FeatureCollection, Feature } from 'geojson';
import { ESSearchResponse } from '../../../../../src/core/types/elasticsearch';
import { MlApiServices } from '../application/services/ml_api_service';
import { MLAnomalyDoc } from '../../common/types/anomalies';
import type { SearchFilters } from './anomaly_source';

export async function getResultsForJobId(
mlResultsService: any,
mlResultsService: MlApiServices['results'],
jobId: string,
locationType: 'typical' | 'actual'
locationType: 'typical' | 'actual',
searchFilters: SearchFilters
): Promise<FeatureCollection> {
const { timeFilters } = searchFilters;
// Query to look for the highest scoring anomaly.
const body: any = {
query: {
Expand All @@ -27,6 +31,18 @@ export async function getResultsForJobId(
},
};

if (timeFilters) {
const timerange = {
range: {
timestamp: {
gte: `${timeFilters.from}`,
lte: timeFilters.to,
},
},
};
body.query.bool.must.push(timerange);
}

let resp: ESSearchResponse<MLAnomalyDoc> | null = null;
let hits: Array<{ typical: number[]; actual: number[]; record_score: number }> = [];

Expand Down

0 comments on commit 7af220b

Please sign in to comment.