Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaweiWu committed Apr 17, 2024
1 parent 7440a12 commit 976b04a
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export interface FindRulesResponse<Params extends RuleParamsV1 = never> {
page: number;
per_page: number;
total: number;
data: Array<RuleResponseV1<Params>>;
data: Array<Partial<RuleResponseV1<Params>>>;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const buildFindRulesRoute = ({
});

const responseBody: FindRulesResponseV1<RuleParamsV1>['body'] =
transformFindRulesResponseV1<RuleParamsV1>(findResult);
transformFindRulesResponseV1<RuleParamsV1>(findResult, options.fields);

return res.ok({
body: responseBody,
Expand Down Expand Up @@ -127,7 +127,7 @@ const buildFindRulesRoute = ({
});

const responseBody: FindRulesResponseV1<RuleParamsV1>['body'] =
transformFindRulesResponseV1<RuleParamsV1>(findResult);
transformFindRulesResponseV1<RuleParamsV1>(findResult, options.fields);

return res.ok({
body: responseBody,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,106 @@
*/

import type { FindRulesResponseV1 } from '../../../../../../../common/routes/rule/apis/find';
import type { RuleParamsV1 } from '../../../../../../../common/routes/rule/response';
import type {
RuleResponseV1,
RuleParamsV1,
} from '../../../../../../../common/routes/rule/response';
import type { FindResult } from '../../../../../../application/rule/methods/find';
import type { Rule, RuleParams } from '../../../../../../application/rule/types';
import { transformRuleToRuleResponseV1 } from '../../../../transforms';
import { Rule, RuleParams } from '../../../../../../application/rule/types';
import {
transformRuleActionsV1,
transformMonitoringV1,
transformRuleLastRunV1,
} from '../../../../transforms';

export const transformPartialRule = <Params extends RuleParams = never>(
rule: Partial<Rule<Params>>,
fields?: string[]
): Partial<RuleResponseV1<RuleParamsV1>> => {
const ruleResponse = {
...(rule.id !== undefined ? { id: rule.id } : {}),
...(rule.enabled !== undefined ? { enabled: rule.enabled } : {}),
...(rule.name !== undefined ? { name: rule.name } : {}),
...(rule.tags ? { tags: rule.tags } : {}),
...(rule.alertTypeId !== undefined ? { rule_type_id: rule.alertTypeId } : {}),
...(rule.consumer !== undefined ? { consumer: rule.consumer } : {}),
...(rule.schedule ? { schedule: rule.schedule } : {}),
...(rule.actions || rule.systemActions
? { actions: transformRuleActionsV1(rule.actions || [], rule.systemActions || []) }
: {}),
...(rule.params ? { params: rule.params } : {}),
...(rule.mapped_params ? { mapped_params: rule.mapped_params } : {}),
...(rule.scheduledTaskId !== undefined ? { scheduled_task_id: rule.scheduledTaskId } : {}),
...(rule.createdBy !== undefined ? { created_by: rule.createdBy } : {}),
...(rule.updatedBy !== undefined ? { updated_by: rule.updatedBy } : {}),
...(rule.createdAt ? { created_at: rule.createdAt.toISOString() } : {}),
...(rule.updatedAt ? { updated_at: rule.updatedAt.toISOString() } : {}),
...(rule.apiKeyOwner !== undefined ? { api_key_owner: rule.apiKeyOwner } : {}),
...(rule.apiKeyCreatedByUser !== undefined
? { api_key_created_by_user: rule.apiKeyCreatedByUser }
: {}),
...(rule.throttle !== undefined ? { throttle: rule.throttle } : {}),
...(rule.muteAll !== undefined ? { mute_all: rule.muteAll } : {}),
...(rule.notifyWhen !== undefined ? { notify_when: rule.notifyWhen } : {}),
...(rule.mutedInstanceIds ? { muted_alert_ids: rule.mutedInstanceIds } : {}),
...(rule.scheduledTaskId !== undefined ? { scheduled_task_id: rule.scheduledTaskId } : {}),
...(rule.executionStatus
? {
execution_status: {
status: rule.executionStatus.status,
...(rule.executionStatus.error ? { error: rule.executionStatus.error } : {}),
...(rule.executionStatus.warning ? { warning: rule.executionStatus.warning } : {}),
last_execution_date: rule.executionStatus.lastExecutionDate?.toISOString(),
...(rule.executionStatus.lastDuration !== undefined
? { last_duration: rule.executionStatus.lastDuration }
: {}),
},
}
: {}),
...(rule.monitoring ? { monitoring: transformMonitoringV1(rule.monitoring) } : {}),
...(rule.snoozeSchedule ? { snooze_schedule: rule.snoozeSchedule } : {}),
...(rule.activeSnoozes ? { active_snoozes: rule.activeSnoozes } : {}),
...(rule.isSnoozedUntil !== undefined
? { is_snoozed_until: rule.isSnoozedUntil?.toISOString() || null }
: {}),
...(rule.lastRun !== undefined
? { last_run: rule.lastRun ? transformRuleLastRunV1(rule.lastRun) : null }
: {}),
...(rule.nextRun !== undefined ? { next_run: rule.nextRun?.toISOString() || null } : {}),
...(rule.revision !== undefined ? { revision: rule.revision } : {}),
...(rule.running !== undefined ? { running: rule.running } : {}),
...(rule.viewInAppRelativeUrl !== undefined
? { view_in_app_relative_url: rule.viewInAppRelativeUrl }
: {}),
...(rule.alertDelay !== undefined ? { alert_delay: rule.alertDelay } : {}),
};

type RuleKeys = keyof RuleResponseV1<RuleParamsV1>;
for (const key in ruleResponse) {
if (ruleResponse[key as RuleKeys] !== undefined) {
continue;
}
if (!fields) {
continue;
}
if (fields.includes(key)) {
continue;
}
delete ruleResponse[key as RuleKeys];
}
return ruleResponse;
};

export const transformFindRulesResponse = <Params extends RuleParams = never>(
result: FindResult<Params>
result: FindResult<Params>,
fields?: string[]
): FindRulesResponseV1<RuleParamsV1>['body'] => {
return {
page: result.page,
per_page: result.perPage,
total: result.total,
data: result.data.map((rule) => transformRuleToRuleResponseV1<RuleParamsV1>(rule as Rule)),
data: result.data.map((rule) =>
transformPartialRule<RuleParamsV1>(rule as Partial<Rule<Params>>, fields)
),
};
};
16 changes: 12 additions & 4 deletions x-pack/plugins/alerting/server/routes/rule/transforms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
* 2.0.
*/

export { transformRuleToRuleResponse } from './transform_rule_to_rule_response/latest';
export { transformRuleToRuleResponse as transformRuleToRuleResponseV1 } from './transform_rule_to_rule_response/v1';
export { transformRuleActions } from './transform_rule_to_rule_response/latest';
export { transformRuleActions as transformRuleActionsV1 } from './transform_rule_to_rule_response/v1';
export {
transformRuleToRuleResponse,
transformRuleActions,
transformRuleLastRun,
transformMonitoring,
} from './transform_rule_to_rule_response/latest';
export {
transformRuleToRuleResponse as transformRuleToRuleResponseV1,
transformRuleActions as transformRuleActionsV1,
transformRuleLastRun as transformRuleLastRunV1,
transformMonitoring as transformMonitoringV1,
} from './transform_rule_to_rule_response/v1';
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* 2.0.
*/

export { transformRuleToRuleResponse, transformRuleActions } from './v1';
export * from './v1';
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '../../../../../common/routes/rule/response';
import { Rule, RuleLastRun, RuleParams, Monitoring } from '../../../../application/rule/types';

const transformRuleLastRun = (lastRun: RuleLastRun): RuleLastRunV1 => {
export const transformRuleLastRun = (lastRun: RuleLastRun): RuleLastRunV1 => {
return {
outcome: lastRun.outcome,
...(lastRun.outcomeOrder !== undefined ? { outcome_order: lastRun.outcomeOrder } : {}),
Expand All @@ -23,7 +23,7 @@ const transformRuleLastRun = (lastRun: RuleLastRun): RuleLastRunV1 => {
};
};

const transformMonitoring = (monitoring: Monitoring): MonitoringV1 => {
export const transformMonitoring = (monitoring: Monitoring): MonitoringV1 => {
return {
run: {
history: monitoring.run.history.map((history) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ const findTestUtils = (
monitoring: match.monitoring,
snooze_schedule: match.snooze_schedule,
...(hasActiveSnoozes && { active_snoozes: activeSnoozes }),
is_snoozed_until: null,
}
: {}),
});
Expand Down Expand Up @@ -348,6 +349,7 @@ const findTestUtils = (
monitoring: match.monitoring,
snooze_schedule: match.snooze_schedule,
...(hasActiveSnoozes && { active_snoozes: activeSnoozes }),
is_snoozed_until: null,
}
: {}),
});
Expand Down Expand Up @@ -426,6 +428,7 @@ const findTestUtils = (
tags: [myTag],
...(describeType === 'internal' && {
snooze_schedule: [],
is_snoozed_until: null,
}),
});
expect(omit(matchSecond, 'updatedAt')).to.eql({
Expand All @@ -434,6 +437,7 @@ const findTestUtils = (
tags: [myTag],
...(describeType === 'internal' && {
snooze_schedule: [],
is_snoozed_until: null,
}),
});
break;
Expand Down Expand Up @@ -510,6 +514,7 @@ const findTestUtils = (
execution_status: matchFirst.execution_status,
...(describeType === 'internal' && {
snooze_schedule: [],
is_snoozed_until: null,
}),
});
expect(omit(matchSecond, 'updatedAt')).to.eql({
Expand All @@ -519,6 +524,7 @@ const findTestUtils = (
execution_status: matchSecond.execution_status,
...(describeType === 'internal' && {
snooze_schedule: [],
is_snoozed_until: null,
}),
});
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const findTestUtils = (
monitoring: match.monitoring,
snooze_schedule: match.snooze_schedule,
...(hasActiveSnoozes && { active_snoozes: activeSnoozes }),
is_snoozed_until: null,
}
: {}),
});
Expand Down Expand Up @@ -321,6 +322,7 @@ const findTestUtils = (
monitoring: match.monitoring,
snooze_schedule: match.snooze_schedule,
...(hasActiveSnoozes && { active_snoozes: activeSnoozes }),
is_snoozed_until: null,
}
: {}),
});
Expand Down Expand Up @@ -405,6 +407,7 @@ const findTestUtils = (
tags: [myTag],
...(describeType === 'internal' && {
snooze_schedule: [],
is_snoozed_until: null,
}),
});
expect(omit(matchSecond, 'updatedAt')).to.eql({
Expand All @@ -413,6 +416,7 @@ const findTestUtils = (
tags: [myTag],
...(describeType === 'internal' && {
snooze_schedule: [],
is_snoozed_until: null,
}),
});
break;
Expand Down Expand Up @@ -495,6 +499,7 @@ const findTestUtils = (
execution_status: matchFirst.execution_status,
...(describeType === 'internal' && {
snooze_schedule: [],
is_snoozed_until: null,
}),
});
expect(omit(matchSecond, 'updatedAt')).to.eql({
Expand All @@ -504,6 +509,7 @@ const findTestUtils = (
execution_status: matchSecond.execution_status,
...(describeType === 'internal' && {
snooze_schedule: [],
is_snoozed_until: null,
}),
});
break;
Expand Down

0 comments on commit 976b04a

Please sign in to comment.