Skip to content

Commit

Permalink
[ML] Fix Anomaly Explorer URL for alerting context with non-default s…
Browse files Browse the repository at this point in the history
…pace (elastic#160899)

## Summary

Fixes elastic#160762 


### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
darnautov authored Jul 5, 2023
1 parent e654c8e commit 7728519
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
16 changes: 13 additions & 3 deletions x-pack/plugins/ml/server/lib/alerts/alerting_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
type MlAnomalyResultType,
ML_ANOMALY_RESULT_TYPE,
} from '@kbn/ml-anomaly-utils';
import { DEFAULT_SPACE_ID } from '@kbn/spaces-plugin/common';
import { MlClient } from '../ml_client';
import {
MlAnomalyDetectionAlertParams,
Expand Down Expand Up @@ -78,6 +79,7 @@ export function buildExplorerUrl(
jobIds: string[],
timeRange: { from: string; to: string; mode?: string },
type: MlAnomalyResultType,
spaceId: string,
r?: AlertExecutionResult
): string {
const isInfluencerResult = type === ML_ANOMALY_RESULT_TYPE.INFLUENCER;
Expand Down Expand Up @@ -145,7 +147,11 @@ export function buildExplorerUrl(
},
},
};
return `/app/ml/explorer/?_g=${encodeURIComponent(

const spacePathComponent: string =
!spaceId || spaceId === DEFAULT_SPACE_ID ? '' : `/s/${spaceId}`;

return `${spacePathComponent}/app/ml/explorer/?_g=${encodeURIComponent(
rison.encode(globalState)
)}&_a=${encodeURIComponent(rison.encode(appState))}`;
}
Expand Down Expand Up @@ -765,9 +771,11 @@ export function alertingServiceProvider(
* Return the result of an alert condition execution.
*
* @param params - Alert params
* @param spaceId
*/
execute: async (
params: MlAnomalyDetectionAlertParams
params: MlAnomalyDetectionAlertParams,
spaceId: string
): Promise<
{ context: AnomalyDetectionAlertContext; name: string; isHealthy: boolean } | undefined
> => {
Expand All @@ -784,6 +792,7 @@ export function alertingServiceProvider(
result.jobIds,
{ from: result.bucketRange.start, to: result.bucketRange.end },
params.resultType,
spaceId,
result
);

Expand All @@ -806,7 +815,8 @@ export function alertingServiceProvider(
to: 'now',
mode: 'relative',
},
queryParams.resultType
queryParams.resultType,
spaceId
),
jobIds: queryParams.jobIds,
message: i18n.translate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ export function registerAnomalyDetectionAlertType({
minimumLicenseRequired: MINIMUM_FULL_LICENSE,
isExportable: true,
doesSetRecoveryContext: true,
async executor({ services, params }) {
async executor({ services, params, spaceId }) {
const fakeRequest = {} as KibanaRequest;
const { execute } = mlSharedServices.alertingServiceProvider(
services.savedObjectsClient,
fakeRequest
);
const executionResult = await execute(params);
const executionResult = await execute(params, spaceId);

if (executionResult && !executionResult.isHealthy) {
const alertInstanceName = executionResult.name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,16 @@ export default function alertTests({ getService }: FtrProviderContext) {

const docs = await waitForDocs(1);
for (const doc of docs) {
const { name, message } = doc._source.params;
const { name, message, anomalyExplorerUrl } = doc._source.params;

expect(name).to.be('Test AD job');
expect(message).to.be(
'Alerts are raised based on real-time scores. Remember that scores may be adjusted over time as data continues to be analyzed.'
);
// check only part of the URL as time bounds vary based on the anomaly
expect(anomalyExplorerUrl).to.contain(
'/s/space1/app/ml/explorer/?_g=(ml%3A(jobIds%3A!(rt-anomaly-mean-value))'
);
}
});

Expand Down Expand Up @@ -166,6 +170,7 @@ export default function alertTests({ getService }: FtrProviderContext) {
params: {
name: '{{{alertName}}}',
message: '{{{context.message}}}',
anomalyExplorerUrl: '{{{context.anomalyExplorerUrl}}}',
},
},
],
Expand Down

0 comments on commit 7728519

Please sign in to comment.