Skip to content

Commit

Permalink
[ML] Fix date formatting in the alert context of the Anomaly detectio…
Browse files Browse the repository at this point in the history
…n job health rule type (elastic#109073)

* [ML] extend alert messages with job ids

* [ML] rename getJobsErrorMessages

* [ML] add rule name to a template

* [ML] utilize fieldFormats plugin for date formatting

* [ML] update types

* [ML] update unit tests

* [ML] guards for uiSettings and fieldRegistry, add custom errors

* [ML] add conditional call for uiSettings

* [ML] fix i18n

* [ML] tests for getCustomError

* [ML] fix i18n check

* [ML] create classes for custom errors
  • Loading branch information
darnautov authored and kibanamachine committed Aug 19, 2021
1 parent 2c9b854 commit 26effd3
Show file tree
Hide file tree
Showing 12 changed files with 305 additions and 74 deletions.
7 changes: 5 additions & 2 deletions x-pack/plugins/ml/common/types/kibana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

// custom edits or fixes for default kibana types which are incomplete

import { SimpleSavedObject } from 'kibana/public';
import { IndexPatternAttributes } from 'src/plugins/data/common';
import type { SimpleSavedObject } from 'kibana/public';
import type { IndexPatternAttributes } from 'src/plugins/data/common';
import type { FieldFormatsRegistry } from '../../../../../src/plugins/field_formats/common';

export type IndexPatternTitle = string;

Expand All @@ -26,3 +27,5 @@ export function isSavedSearchSavedObject(
): ss is SavedSearchSavedObject {
return ss !== null;
}

export type FieldFormatsRegistryProvider = () => Promise<FieldFormatsRegistry>;
3 changes: 2 additions & 1 deletion x-pack/plugins/ml/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"uiActions",
"kibanaLegacy",
"discover",
"triggersActionsUi"
"triggersActionsUi",
"fieldFormats"
],
"optionalPlugins": [
"alerting",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function registerJobsHealthAlertingRule(
defaultActionMessage: i18n.translate(
'xpack.ml.alertTypes.jobsHealthAlertingRule.defaultActionMessage',
{
defaultMessage: `Anomaly detection jobs health check result:
defaultMessage: `[\\{\\{rule.name\\}\\}] Anomaly detection jobs health check result:
\\{\\{context.message\\}\\}
\\{\\{#context.results\\}\\}
Job ID: \\{\\{job_id\\}\\}
Expand Down
34 changes: 24 additions & 10 deletions x-pack/plugins/ml/server/lib/alerts/jobs_health_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { AnnotationService } from '../../models/annotation_service/annotation';
import { JobsHealthExecutorOptions } from './register_jobs_monitoring_rule_type';
import { JobAuditMessagesService } from '../../models/job_audit_messages/job_audit_messages';
import { DeepPartial } from '../../../common/types/common';
import { FieldFormatsRegistryProvider } from '../../../common/types/kibana';

const MOCK_DATE_NOW = 1487076708000;

Expand Down Expand Up @@ -148,8 +149,8 @@ describe('JobsHealthService', () => {
} as unknown) as jest.Mocked<AnnotationService>;

const jobAuditMessagesService = ({
getJobsErrors: jest.fn().mockImplementation((jobIds: string) => {
return Promise.resolve({});
getJobsErrorMessages: jest.fn().mockImplementation((jobIds: string) => {
return Promise.resolve([]);
}),
} as unknown) as jest.Mocked<JobAuditMessagesService>;

Expand All @@ -159,11 +160,24 @@ describe('JobsHealthService', () => {
debug: jest.fn(),
} as unknown) as jest.Mocked<Logger>;

const getFieldsFormatRegistry = jest.fn().mockImplementation(() => {
return Promise.resolve({
deserialize: jest.fn().mockImplementation(() => {
return {
convert: jest.fn().mockImplementation((v) => {
return new Date(v).toUTCString();
}),
};
}),
});
}) as jest.Mocked<FieldFormatsRegistryProvider>;

const jobHealthService: JobsHealthService = jobsHealthServiceProvider(
mlClient,
datafeedsService,
annotationService,
jobAuditMessagesService,
getFieldsFormatRegistry,
logger
);

Expand Down Expand Up @@ -275,11 +289,11 @@ describe('JobsHealthService', () => {
job_id: 'test_job_01',
annotation:
'Datafeed has missed 11 documents due to ingest latency, latest bucket with missing data is [2021-07-30T13:50:00.000Z]. Consider increasing query_delay',
end_timestamp: 1627653300000,
end_timestamp: 'Fri, 30 Jul 2021 13:55:00 GMT',
missed_docs_count: 11,
},
],
message: '1 job is suffering from delayed data.',
message: 'Job test_job_01 is suffering from delayed data.',
},
},
]);
Expand Down Expand Up @@ -333,7 +347,7 @@ describe('JobsHealthService', () => {
datafeed_state: 'stopped',
},
],
message: 'Datafeed is not started for the following jobs:',
message: 'Datafeed is not started for job test_job_02',
},
},
{
Expand All @@ -342,12 +356,12 @@ describe('JobsHealthService', () => {
results: [
{
job_id: 'test_job_01',
log_time: 1626935914540,
log_time: 'Thu, 22 Jul 2021 06:38:34 GMT',
memory_status: 'hard_limit',
},
],
message:
'1 job reached the hard model memory limit. Assign the job more memory and restore from a snapshot from prior to reaching the hard limit.',
'Job test_job_01 reached the hard model memory limit. Assign the job more memory and restore from a snapshot from prior to reaching the hard limit.',
},
},
{
Expand All @@ -358,18 +372,18 @@ describe('JobsHealthService', () => {
job_id: 'test_job_01',
annotation:
'Datafeed has missed 11 documents due to ingest latency, latest bucket with missing data is [2021-07-30T13:50:00.000Z]. Consider increasing query_delay',
end_timestamp: 1627653300000,
end_timestamp: 'Fri, 30 Jul 2021 13:55:00 GMT',
missed_docs_count: 11,
},
{
job_id: 'test_job_02',
annotation:
'Datafeed has missed 8 documents due to ingest latency, latest bucket with missing data is [2021-07-30T13:50:00.000Z]. Consider increasing query_delay',
end_timestamp: 1627653300000,
end_timestamp: 'Fri, 30 Jul 2021 13:55:00 GMT',
missed_docs_count: 8,
},
],
message: '2 jobs are suffering from delayed data.',
message: 'Jobs test_job_01, test_job_02 are suffering from delayed data.',
},
},
]);
Expand Down
Loading

0 comments on commit 26effd3

Please sign in to comment.