diff --git a/x-pack/plugins/ml/common/constants/annotations.ts b/x-pack/plugins/ml/common/constants/annotations.ts index 96367682ecf0..c27a4f620be6 100644 --- a/x-pack/plugins/ml/common/constants/annotations.ts +++ b/x-pack/plugins/ml/common/constants/annotations.ts @@ -8,3 +8,6 @@ export enum ANNOTATION_TYPE { ANNOTATION = 'annotation', COMMENT = 'comment', } + +export const ANNOTATION_DOC_TYPE = 'doc'; +export const ANNOTATION_USER_UNKNOWN = ''; diff --git a/x-pack/plugins/ml/index.js b/x-pack/plugins/ml/index.js index cbcb0ddb52f3..96e489fa307c 100644 --- a/x-pack/plugins/ml/index.js +++ b/x-pack/plugins/ml/index.js @@ -9,7 +9,8 @@ import { resolve } from 'path'; import Boom from 'boom'; import { checkLicense } from './server/lib/check_license'; -import { isAnnotationsFeatureAvailable } from './server/lib/check_annotations'; +import { FEATURE_ANNOTATIONS_ENABLED } from './common/constants/feature_flags'; + import { mirrorPluginStatus } from '../../server/lib/mirror_plugin_status'; import { annotationRoutes } from './server/routes/annotations'; import { jobRoutes } from './server/routes/anomaly_detectors'; @@ -78,13 +79,11 @@ export const ml = (kibana) => { ] }; - const mlAnnotationsEnabled = await isAnnotationsFeatureAvailable(server); - server.injectUiAppVars('ml', () => { const config = server.config(); return { kbnIndex: config.get('kibana.index'), - mlAnnotationsEnabled, + mlAnnotationsEnabled: FEATURE_ANNOTATIONS_ENABLED, }; }); diff --git a/x-pack/plugins/ml/public/timeseriesexplorer/components/timeseries_chart/timeseries_chart.js b/x-pack/plugins/ml/public/timeseriesexplorer/components/timeseries_chart/timeseries_chart.js index 3117a56beb0c..26a6b7435774 100644 --- a/x-pack/plugins/ml/public/timeseriesexplorer/components/timeseries_chart/timeseries_chart.js +++ b/x-pack/plugins/ml/public/timeseriesexplorer/components/timeseries_chart/timeseries_chart.js @@ -1353,7 +1353,7 @@ export class TimeseriesChart extends React.Component { } if (mlAnnotationsEnabled && _.has(marker, 'annotation')) { - contents = marker.annotation; + contents = mlEscape(marker.annotation); contents += `
${moment(marker.timestamp).format('MMMM Do YYYY, HH:mm')}`; if (typeof marker.end_timestamp !== 'undefined') { diff --git a/x-pack/plugins/ml/server/models/annotation_service/annotation.ts b/x-pack/plugins/ml/server/models/annotation_service/annotation.ts index d642b509f317..a05b57572116 100644 --- a/x-pack/plugins/ml/server/models/annotation_service/annotation.ts +++ b/x-pack/plugins/ml/server/models/annotation_service/annotation.ts @@ -7,7 +7,7 @@ import Boom from 'boom'; import _ from 'lodash'; -import { ANNOTATION_TYPE } from '../../../common/constants/annotations'; +import { ANNOTATION_DOC_TYPE, ANNOTATION_TYPE } from '../../../common/constants/annotations'; import { ML_ANNOTATIONS_INDEX_ALIAS_READ, ML_ANNOTATIONS_INDEX_ALIAS_WRITE, @@ -63,22 +63,22 @@ interface DeleteParams { export function annotationProvider( callWithRequest: (action: string, params: IndexParams | DeleteParams | GetParams) => Promise ) { - async function indexAnnotation(annotation: Annotation) { + async function indexAnnotation(annotation: Annotation, username: string) { if (isAnnotation(annotation) === false) { return Promise.reject(new Error('invalid annotation format')); } if (annotation.create_time === undefined) { annotation.create_time = new Date().getTime(); - annotation.create_username = ''; + annotation.create_username = username; } annotation.modified_time = new Date().getTime(); - annotation.modified_username = ''; + annotation.modified_username = username; const params: IndexParams = { index: ML_ANNOTATIONS_INDEX_ALIAS_WRITE, - type: 'annotation', + type: ANNOTATION_DOC_TYPE, body: annotation, refresh: 'wait_for', }; @@ -232,7 +232,7 @@ export function annotationProvider( async function deleteAnnotation(id: string) { const param: DeleteParams = { index: ML_ANNOTATIONS_INDEX_ALIAS_WRITE, - type: 'annotation', + type: ANNOTATION_DOC_TYPE, id, refresh: 'wait_for', }; diff --git a/x-pack/plugins/ml/server/routes/annotations.js b/x-pack/plugins/ml/server/routes/annotations.js index f9f319957681..bfb3a439a30d 100644 --- a/x-pack/plugins/ml/server/routes/annotations.js +++ b/x-pack/plugins/ml/server/routes/annotations.js @@ -4,12 +4,14 @@ * you may not use this file except in compliance with the Elastic License. */ - +import _ from 'lodash'; import { callWithRequestFactory } from '../client/call_with_request_factory'; import { wrapError } from '../client/errors'; import { annotationServiceProvider } from '../models/annotation_service'; +import { ANNOTATION_USER_UNKNOWN } from '../../common/constants/annotations'; + export function annotationRoutes(server, commonRouteConfig) { server.route({ method: 'POST', @@ -31,7 +33,8 @@ export function annotationRoutes(server, commonRouteConfig) { handler(request) { const callWithRequest = callWithRequestFactory(server, request); const { indexAnnotation } = annotationServiceProvider(callWithRequest); - return indexAnnotation(request.payload) + const username = _.get(request, 'auth.credentials.username', ANNOTATION_USER_UNKNOWN); + return indexAnnotation(request.payload, username) .catch(resp => wrapError(resp)); }, config: {