-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathworkflow-actions.service.ts
More file actions
104 lines (82 loc) · 3.29 KB
/
Copy pathworkflow-actions.service.ts
File metadata and controls
104 lines (82 loc) · 3.29 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
import {Injectable} from '@angular/core';
import {BehaviorSubject, Observable} from 'rxjs';
import {map} from 'rxjs/operators';
import {ActionConditionalEnum} from '../component/action-conditional/action-conditional.component';
import {IncidentResponseActionTemplate} from './incident-response-action-template.service';
@Injectable({
providedIn: 'root'
})
export class WorkflowActionsService {
private actionsBehaviorSubject: BehaviorSubject<IncidentResponseActionTemplate[]> = new BehaviorSubject([]);
actions$ = this.actionsBehaviorSubject.asObservable();
readonly command$: Observable<string> = this.actions$.pipe(
map(actions => this.buildCommand(actions))
);
buildCommand(actions: IncidentResponseActionTemplate[] = this.getActions()): string {
if (!actions || actions.length === 0) {
return '';
}
if (actions.length === 1) {
return actions[0].command;
}
return actions.map((action, index) => {
const operator = index === 0 ? ''
: action.conditional.key === ActionConditionalEnum.SUCCESS ? '&&'
: action.conditional.key === ActionConditionalEnum.FAILURE ? '||'
: ';';
return `${operator} ${action.command}`.trim();
}).join(' ').trim();
}
inferConditionals(command: string, actions: IncidentResponseActionTemplate[]): IncidentResponseActionTemplate[] {
if (!actions || actions.length === 0) {
return actions || [];
}
if (!command || actions.length === 1) {
return [{ ...actions[0], conditional: { key: ActionConditionalEnum.ALWAYS, value: ';' } }];
}
const result: IncidentResponseActionTemplate[] = [];
let cursor = 0;
actions.forEach((action, index) => {
const idx = command.indexOf(action.command, cursor);
if (index === 0 || idx === -1) {
result.push({ ...action, conditional: { key: ActionConditionalEnum.ALWAYS, value: ';' } });
} else {
const gap = command.slice(cursor, idx).trim();
const conditional = gap === '&&' ? { key: ActionConditionalEnum.SUCCESS, value: '&&' }
: gap === '||' ? { key: ActionConditionalEnum.FAILURE, value: '||' }
: { key: ActionConditionalEnum.ALWAYS, value: ';' };
result.push({ ...action, conditional });
}
if (idx !== -1) {
cursor = idx + action.command.length;
}
});
return result;
}
addActions(action: any) {
const actions = this.actionsBehaviorSubject.value ? this.actionsBehaviorSubject.value : [];
this.actionsBehaviorSubject.next([...actions, {
...action,
conditional: action.conditional ? action.conditional : { key: ActionConditionalEnum.ALWAYS, value: ';'},
}]);
}
updateAction(index: number, action: any) {
const actions = this.getActions();
if (index < 0 || index >= actions.length) {
return;
}
const newActions = [...actions];
newActions[index] = { ...action };
this.actionsBehaviorSubject.next(newActions);
}
deleteAction(action: any) {
const actions = this.actionsBehaviorSubject.value ? this.actionsBehaviorSubject.value : [];
this.actionsBehaviorSubject.next(actions.filter(act => act !== action));
}
clear() {
this.actionsBehaviorSubject.next([]);
}
getActions() {
return this.actionsBehaviorSubject.value ? this.actionsBehaviorSubject.value : [];
}
}