Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Fixes Anomaly Explorer URL for alerting context with non-default space #160899

Merged
merged 5 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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