Skip to content

Commit

Permalink
[ML] Change SMV annotations to only show series + non-partitioned
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed Jul 9, 2020
1 parent 02024e5 commit 0e24cb3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const annotations = {
latestMs: number;
maxAnnotations: number;
fields: FieldToBucket[];
detectorIndex: number;
entities: any[];
}) {
const body = JSON.stringify(obj);
return http$<GetAnnotationsResponse>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export function getFocusData(
missing: ANNOTATION_EVENT_USER,
},
],
detectorIndex,
entities: nonBlankEntities,
})
.pipe(
catchError(() => {
Expand Down
55 changes: 55 additions & 0 deletions x-pack/plugins/ml/server/models/annotation_service/annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import _ from 'lodash';
import { LegacyAPICaller } from 'kibana/server';

import { ANNOTATION_EVENT_USER, ANNOTATION_TYPE } from '../../../common/constants/annotations';
import { PARTITION_FIELDS } from '../../../common/constants/anomalies';
import {
ML_ANNOTATIONS_INDEX_ALIAS_READ,
ML_ANNOTATIONS_INDEX_ALIAS_WRITE,
Expand All @@ -19,6 +20,8 @@ import {
Annotations,
isAnnotation,
isAnnotations,
getAnnotationFieldName,
getAnnotationFieldValue,
EsAggregationResult,
} from '../../../common/types/annotations';

Expand All @@ -40,6 +43,8 @@ export interface IndexAnnotationArgs {
latestMs: number;
maxAnnotations: number;
fields?: FieldToBucket[];
detectorIndex?: number;
entities?: any[];
}

export interface AggTerm {
Expand Down Expand Up @@ -114,6 +119,8 @@ export function annotationProvider(callAsCurrentUser: LegacyAPICaller) {
latestMs,
maxAnnotations,
fields,
detectorIndex,
entities,
}: IndexAnnotationArgs) {
const obj: GetResponse = {
success: true,
Expand Down Expand Up @@ -201,6 +208,7 @@ export function annotationProvider(callAsCurrentUser: LegacyAPICaller) {
});
}

// Find unique buckets (e.g. events) from the queried annotations to show in dropdowns
const aggs: Record<string, AggTerm> = {};
if (fields) {
fields.forEach((fieldToBucket) => {
Expand All @@ -212,6 +220,52 @@ export function annotationProvider(callAsCurrentUser: LegacyAPICaller) {
});
}

// Build should clause to further query for annotations in SMV
// we want to show either the exact match with detector index and by/over/partition fields
// OR annotations without any partition fields defined
let shouldClauses;
if (detectorIndex !== undefined && Array.isArray(entities)) {
// build clause to get exact match of detector index and by/over/partition fields
const beExactMatch = [];
beExactMatch.push({
term: {
detector_index: detectorIndex,
},
});

entities.forEach(({ fieldName, fieldType, fieldValue }) => {
beExactMatch.push({
term: {
[getAnnotationFieldName(fieldType)]: fieldName,
},
});
beExactMatch.push({
term: {
[getAnnotationFieldValue(fieldType)]: fieldValue,
},
});
});

// clause to get annotations that have no partition fields
const haveAnyPartitionFields: object[] = [];
PARTITION_FIELDS.forEach((field) => {
haveAnyPartitionFields.push({
exists: {
field: getAnnotationFieldName(field),
},
});
haveAnyPartitionFields.push({
exists: {
field: getAnnotationFieldValue(field),
},
});
});
shouldClauses = [
{ bool: { must_not: haveAnyPartitionFields } },
{ bool: { must: beExactMatch } },
];
}

const params: GetParams = {
index: ML_ANNOTATIONS_INDEX_ALIAS_READ,
size: maxAnnotations,
Expand All @@ -231,6 +285,7 @@ export function annotationProvider(callAsCurrentUser: LegacyAPICaller) {
},
},
],
...(shouldClauses ? { should: shouldClauses, minimum_should_match: 1 } : {}),
},
},
...(fields ? { aggs } : {}),
Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/ml/server/routes/schemas/annotations_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const getAnnotationsSchema = schema.object({
earliestMs: schema.oneOf([schema.nullable(schema.number()), schema.maybe(schema.number())]),
latestMs: schema.oneOf([schema.nullable(schema.number()), schema.maybe(schema.number())]),
maxAnnotations: schema.number(),
/** Fields to find unique values for (e.g. events or created_by) */
fields: schema.maybe(
schema.arrayOf(
schema.object({
Expand All @@ -42,6 +43,16 @@ export const getAnnotationsSchema = schema.object({
})
)
),
detectorIndex: schema.maybe(schema.number()),
entities: schema.maybe(
schema.arrayOf(
schema.object({
fieldType: schema.maybe(schema.string()),
fieldName: schema.maybe(schema.string()),
fieldValue: schema.maybe(schema.string()),
})
)
),
});

export const deleteAnnotationSchema = schema.object({ annotationId: schema.string() });

0 comments on commit 0e24cb3

Please sign in to comment.