-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathdecision.ts
More file actions
222 lines (200 loc) · 5.78 KB
/
Copy pathdecision.ts
File metadata and controls
222 lines (200 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import {
type DecisionDetailDto,
type DecisionDto,
// biome-ignore lint/suspicious/noShadowRestrictedNames: <TBD>
type Error,
type RuleExecutionDto,
type ScheduledExecutionDto,
} from 'marble-api';
import invariant from 'tiny-invariant';
import { assertNever } from 'typescript-utils';
import { adaptCase, type Case } from './cases';
import { adaptNodeEvaluation, type NodeEvaluation } from './node-evaluation';
import { type Outcome } from './outcome';
export const nonPendingReviewStatuses = ['approve', 'decline'] as const;
export const reviewStatuses = ['pending', ...nonPendingReviewStatuses] as const;
export type ReviewStatus = (typeof reviewStatuses)[number];
export interface Decision {
case?: Case;
createdAt: string;
error?: Error;
id: string;
outcome: Outcome;
reviewStatus?: ReviewStatus;
pivotValues: {
id?: string;
value?: string;
}[];
scenario: {
id: string;
name: string;
scenarioIterationId: string;
version: number;
};
score: number;
triggerObject: Record<string, unknown>;
triggerObjectType: string;
scheduledExecutionId?: string;
}
export interface ScheduledExecution {
id: string;
/** Whether the execution was manual or not */
manual: boolean;
status: 'pending' | 'processing' | 'success' | 'failure' | 'partial_failure';
numberOfCreatedDecisions: number;
numberOfEvaluatedDecisions: number;
numberOfPlannedDecisions: number | null;
scenarioId: string;
scenarioIterationId: string;
scenarioName: string;
scenarioTriggerObjectType: string;
startedAt: string;
finishedAt: string | null;
}
export function adaptScheduledExecution(dto: ScheduledExecutionDto): ScheduledExecution {
return {
id: dto.id,
manual: dto.manual,
status: dto.status,
numberOfCreatedDecisions: dto.number_of_created_decisions,
numberOfEvaluatedDecisions: dto.number_of_evaluated_decisions,
numberOfPlannedDecisions: dto.number_of_planned_decisions,
scenarioId: dto.scenario_id,
scenarioIterationId: dto.scenario_iteration_id,
scenarioName: dto.scenario_name,
scenarioTriggerObjectType: dto.scenario_trigger_object_type,
startedAt: dto.started_at,
finishedAt: dto.finished_at,
};
}
interface RuleExecutionCore {
name: string;
description?: string;
ruleId: string;
evaluation?: NodeEvaluation;
}
export interface RuleExecutionNoHit extends RuleExecutionCore {
outcome: 'no_hit';
}
export function isRuleExecutionNoHit(
ruleExecution: RuleExecution,
): ruleExecution is RuleExecutionNoHit {
return ruleExecution.outcome === 'no_hit';
}
export interface RuleExecutionHit extends RuleExecutionCore {
outcome: 'hit';
scoreModifier: number;
}
export function isRuleExecutionHit(
ruleExecution: RuleExecution,
): ruleExecution is RuleExecutionHit {
return ruleExecution.outcome === 'hit';
}
export type ExecutionError = {
code: 'division_by_zero' | 'null_value_found' | 'unknown_error';
message: string;
};
const errorCodeMappings: Record<number, ExecutionError['code']> = {
100: 'division_by_zero',
200: 'null_value_found',
};
function adaptExecutionError(dto: Error): ExecutionError {
const code = errorCodeMappings[dto.code] ?? 'unknown_error';
return {
code,
message: dto.message,
};
}
export interface RuleExecutionError extends RuleExecutionCore {
outcome: 'error';
error: ExecutionError;
}
export function isRuleExecutionError(
ruleExecution: RuleExecution,
): ruleExecution is RuleExecutionError {
return ruleExecution.outcome === 'error';
}
export interface RuleExecutionSnoozed extends RuleExecutionCore {
outcome: 'snoozed';
}
export function isRuleExecutionSnoozed(
ruleExecution: RuleExecution,
): ruleExecution is RuleExecutionSnoozed {
return ruleExecution.outcome === 'snoozed';
}
export type RuleExecution =
| RuleExecutionNoHit
| RuleExecutionHit
| RuleExecutionSnoozed
| RuleExecutionError;
export interface DecisionDetails extends Decision {
rules: RuleExecution[];
}
export function adaptDecision(dto: DecisionDto): Decision {
return {
id: dto.id,
createdAt: dto.created_at,
triggerObject: dto.trigger_object,
triggerObjectType: dto.trigger_object_type,
pivotValues: dto.pivot_values.map(({ pivot_id, pivot_value }) => ({
id: pivot_id ?? undefined,
value: pivot_value ?? undefined,
})),
outcome: dto.outcome,
reviewStatus: dto.review_status,
scenario: {
id: dto.scenario.id,
name: dto.scenario.name,
scenarioIterationId: dto.scenario.scenario_iteration_id,
version: dto.scenario.version,
},
score: dto.score,
case: dto.case ? adaptCase(dto.case) : undefined,
scheduledExecutionId: dto.scheduled_execution_id,
};
}
function adaptRuleExecutionDto(dto: RuleExecutionDto): RuleExecution {
const ruleExecution: RuleExecutionCore = {
name: dto.name,
ruleId: dto.rule_id,
description: dto.description || undefined,
evaluation: dto.rule_evaluation ? adaptNodeEvaluation(dto.rule_evaluation) : undefined,
};
switch (dto.outcome) {
case 'hit': {
return {
...ruleExecution,
outcome: 'hit',
scoreModifier: dto.score_modifier,
};
}
case 'error': {
invariant(dto.error, '[RuleExecutionDto] error is missing for error outcome');
return {
...ruleExecution,
outcome: 'error',
error: adaptExecutionError(dto.error),
};
}
case 'snoozed': {
return {
...ruleExecution,
outcome: 'snoozed',
};
}
case 'no_hit': {
return {
...ruleExecution,
outcome: 'no_hit',
};
}
default:
assertNever('[RuleExecutionDto] unknown outcome:', dto.outcome);
}
}
export function adaptDecisionDetail(dto: DecisionDetailDto): DecisionDetails {
return {
...adaptDecision(dto),
rules: dto.rules.map(adaptRuleExecutionDto),
};
}