Skip to content

Commit

Permalink
[APM] Bug fixes from ML integration testing (#71564) (#71596)
Browse files Browse the repository at this point in the history
* fixes bug where the anomaly detection setup link was showing alert incorrectly, adds unit tests

* Fixes typo in getMlBucketSize query, uses terminate_after

* Improve readbility of helper function to show alerts and unit tests

Co-authored-by: Oliver Gupte <ogupte@users.noreply.github.com>
  • Loading branch information
sorenlouv and ogupte committed Jul 14, 2020
1 parent aa555d5 commit f8354d2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { showAlert } from './AnomalyDetectionSetupLink';

describe('#showAlert', () => {
describe('when an environment is selected', () => {
it('should return true when there are no jobs', () => {
const result = showAlert([], 'testing');
expect(result).toBe(true);
});
it('should return true when environment is not included in the jobs', () => {
const result = showAlert(
[{ environment: 'staging' }, { environment: 'production' }],
'testing'
);
expect(result).toBe(true);
});
it('should return false when environment is included in the jobs', () => {
const result = showAlert(
[{ environment: 'staging' }, { environment: 'production' }],
'staging'
);
expect(result).toBe(false);
});
});
describe('there is no environment selected (All)', () => {
it('should return true when there are no jobs', () => {
const result = showAlert([], undefined);
expect(result).toBe(true);
});
it('should return false when there are any number of jobs', () => {
const result = showAlert(
[{ environment: 'staging' }, { environment: 'production' }],
undefined
);
expect(result).toBe(false);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ export function AnomalyDetectionSetupLink() {
);
const isFetchSuccess = status === FETCH_STATUS.SUCCESS;

// Show alert if there are no jobs OR if no job matches the current environment
const showAlert =
isFetchSuccess && !data.jobs.some((job) => environment === job.environment);

return (
<APMLink path="/settings/anomaly-detection">
<EuiButtonEmpty size="s" color="primary" iconType="inspect">
{ANOMALY_DETECTION_LINK_LABEL}
</EuiButtonEmpty>
{showAlert && (
{isFetchSuccess && showAlert(data.jobs, environment) && (
<EuiToolTip position="bottom" content={getTooltipText(environment)}>
<EuiIcon type="alert" color="danger" />
</EuiToolTip>
Expand Down Expand Up @@ -61,3 +57,16 @@ const ANOMALY_DETECTION_LINK_LABEL = i18n.translate(
'xpack.apm.anomalyDetectionSetup.linkLabel',
{ defaultMessage: `Anomaly detection` }
);

export function showAlert(
jobs: Array<{ environment: string }> = [],
environment: string | undefined
) {
return (
// No job exists, or
jobs.length === 0 ||
// no job exists for the selected environment
(environment !== undefined &&
jobs.every((job) => environment !== job.environment))
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function getMlBucketSize({
body: {
_source: 'bucket_span',
size: 1,
terminateAfter: 1,
terminate_after: 1,
query: {
bool: {
filter: [
Expand Down

0 comments on commit f8354d2

Please sign in to comment.