Skip to content

Commit

Permalink
Simplify the code
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed Feb 15, 2023
1 parent df18770 commit 00e5b99
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 165 deletions.
88 changes: 0 additions & 88 deletions x-pack/plugins/alerting/server/lib/build_view_in_app_url.test.ts

This file was deleted.

50 changes: 0 additions & 50 deletions x-pack/plugins/alerting/server/lib/build_view_in_app_url.ts

This file was deleted.

6 changes: 0 additions & 6 deletions x-pack/plugins/alerting/server/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,3 @@ export { determineAlertsToReturn } from './determine_alerts_to_return';
export { updateFlappingHistory, isFlapping } from './flapping_utils';
export { getAlertsForNotification } from './get_alerts_for_notification';
export { trimRecoveredAlerts } from './trim_recovered_alerts';
export { buildViewInAppUrl } from './build_view_in_app_url';
export type {
GetViewInAppRelativeUrlFnOpts,
GetViewInAppRelativeUrlFn,
BuildViewInAppUrlOpts,
} from './build_view_in_app_url';
Original file line number Diff line number Diff line change
Expand Up @@ -1472,5 +1472,38 @@ describe('Execution Handler', () => {
]
`);
});

it('sets the rule.url to the value from getViewInAppRelativeUrl when the rule type has it defined', async () => {
const execParams = {
...defaultExecutionParams,
rule: ruleWithUrl,
taskRunnerContext: {
...defaultExecutionParams.taskRunnerContext,
kibanaBaseUrl: 'http://localhost:12345',
},
ruleType: {
...ruleType,
getViewInAppRelativeUrl() {
return '/app/management/some/other/place';
},
},
};

const executionHandler = new ExecutionHandler(generateExecutionParams(execParams));
await executionHandler.run(generateAlert({ id: 1 }));

expect(injectActionParamsMock.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"actionParams": Object {
"val": "rule url: http://localhost:12345/s/test1/app/management/some/other/place",
},
"actionTypeId": "test",
"ruleId": "1",
"spaceId": "test1",
},
]
`);
});
});
});
27 changes: 7 additions & 20 deletions x-pack/plugins/alerting/server/task_runner/execution_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { chunk } from 'lodash';
import { AlertingEventLogger } from '../lib/alerting_event_logger/alerting_event_logger';
import { parseDuration, RawRule, ThrottledActions } from '../types';
import { RuleRunMetricsStore } from '../lib/rule_run_metrics_store';
import { buildViewInAppUrl } from '../lib';
import { injectActionParams } from './inject_action_params';
import { ExecutionHandlerOptions, RuleTaskInstance } from './types';
import { TaskRunnerContext } from './task_runner_factory';
Expand Down Expand Up @@ -223,14 +222,7 @@ export class ExecutionHandler<
actionsPlugin,
actionTypeId,
kibanaBaseUrl: this.taskRunnerContext.kibanaBaseUrl,
ruleUrl:
buildViewInAppUrl({
kibanaBaseUrl: this.taskRunnerContext.kibanaBaseUrl,
spaceId,
getViewInAppRelativeUrl: this.ruleType.getViewInAppRelativeUrl,
opts: { rule: this.rule },
logger: this.logger,
}) || this.buildRuleUrl(spaceId),
ruleUrl: this.buildRuleUrl(spaceId),
}),
}),
};
Expand Down Expand Up @@ -279,14 +271,7 @@ export class ExecutionHandler<
alertParams: this.rule.params,
actionParams: action.params,
flapping: executableAlert.getFlapping(),
ruleUrl:
buildViewInAppUrl({
kibanaBaseUrl: this.taskRunnerContext.kibanaBaseUrl,
spaceId,
getViewInAppRelativeUrl: this.ruleType.getViewInAppRelativeUrl,
opts: { rule: this.rule },
logger: this.logger,
}) || this.buildRuleUrl(spaceId),
ruleUrl: this.buildRuleUrl(spaceId),
}),
}),
};
Expand Down Expand Up @@ -424,11 +409,13 @@ export class ExecutionHandler<
return;
}

const relativePath = this.ruleType.getViewInAppRelativeUrl
? this.ruleType.getViewInAppRelativeUrl({ rule: this.rule })
: `${triggersActionsRoute}${getRuleDetailsRoute(this.rule.id)}`;

try {
const ruleUrl = new URL(
`${
spaceId !== 'default' ? `/s/${spaceId}` : ''
}${triggersActionsRoute}${getRuleDetailsRoute(this.rule.id)}`,
`${spaceId !== 'default' ? `/s/${spaceId}` : ''}${relativePath}`,
this.taskRunnerContext.kibanaBaseUrl
);

Expand Down
8 changes: 7 additions & 1 deletion x-pack/plugins/alerting/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ import {
RuleSnooze,
IntervalSchedule,
RuleLastRun,
SanitizedRule,
} from '../common';
import { PublicAlertFactory } from './alert/create_alert_factory';
import { FieldMap } from '../common/alert_schema/field_maps/types';
import { RulesSettingsFlappingProperties } from '../common/rules_settings';
import { GetViewInAppRelativeUrlFn } from './lib';
export type WithoutQueryAndParams<T> = Pick<T, Exclude<keyof T, 'query' | 'params'>>;
export type SpaceIdToNamespaceFunction = (spaceId?: string) => string | undefined;
export type { RuleTypeParams };
Expand Down Expand Up @@ -162,6 +162,12 @@ export interface SummarizedAlerts {
};
}
export type GetSummarizedAlertsFn = (opts: GetSummarizedAlertsFnOpts) => Promise<SummarizedAlerts>;
export interface GetViewInAppRelativeUrlFnOpts<Params extends RuleTypeParams> {
rule: Omit<SanitizedRule<Params>, 'viewInAppRelativeUrl'>;
}
export type GetViewInAppRelativeUrlFn<Params extends RuleTypeParams> = (
opts: GetViewInAppRelativeUrlFnOpts<Params>
) => string;
export interface IRuleTypeAlerts {
context: string;
namespace?: string;
Expand Down

0 comments on commit 00e5b99

Please sign in to comment.