[ML] Fix annotations feature startup behavior. (#27424)

* [ML] Fix annotations feature startup behavior.
* [ML] Move strings to constants.
This commit is contained in:
Walter Rafelsberger 2018-12-18 21:46:16 +01:00 committed by GitHub
parent d769722d30
commit 59d078e31c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 13 deletions

View file

@ -8,3 +8,6 @@ export enum ANNOTATION_TYPE {
ANNOTATION = 'annotation',
COMMENT = 'comment',
}
export const ANNOTATION_DOC_TYPE = 'doc';
export const ANNOTATION_USER_UNKNOWN = '<user unknown>';

View file

@ -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,
};
});

View file

@ -1353,7 +1353,7 @@ export class TimeseriesChart extends React.Component {
}
if (mlAnnotationsEnabled && _.has(marker, 'annotation')) {
contents = marker.annotation;
contents = mlEscape(marker.annotation);
contents += `<br />${moment(marker.timestamp).format('MMMM Do YYYY, HH:mm')}`;
if (typeof marker.end_timestamp !== 'undefined') {

View file

@ -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<any>
) {
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 = '<user unknown>';
annotation.create_username = username;
}
annotation.modified_time = new Date().getTime();
annotation.modified_username = '<user unknown>';
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',
};

View file

@ -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: {