-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathto-list-workflow-parameters.ts
More file actions
77 lines (61 loc) · 2.19 KB
/
to-list-workflow-parameters.ts
File metadata and controls
77 lines (61 loc) · 2.19 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
import { formatDuration } from 'date-fns';
import type { FilterParameters } from '$lib/types/workflows';
import { tokenize } from './tokenize';
import { isExecutionStatus } from '../is';
import { durationKeys, fromDate } from '../to-duration';
type Tokens = string[];
export type ParsedParameters = FilterParameters & { timeRange?: string };
const is =
(identifier: string) =>
(token: string): boolean => {
if (token.toLowerCase() === identifier.toLowerCase()) return true;
return false;
};
const getTwoAhead = (tokens: Tokens, index: number): string =>
tokens[index + 2];
export const getLargestDurationUnit = (duration: Duration): Duration => {
if (!duration) return;
for (const key of durationKeys) {
if (duration[key]) {
return { [key]: duration[key] };
}
}
};
const isWorkflowTypeStatement = is('WorkflowType');
const isWorkflowIdStatement = is('WorkflowId');
const isStartTimeStatement = is('StartTime');
const isExecutionStatusStatement = is('ExecutionStatus');
const isActivityIdStatement = is('ActivityId');
export const toListWorkflowParameters = (query: string): ParsedParameters => {
const tokens = tokenize(query);
const parameters: ParsedParameters = {
workflowId: '',
workflowType: '',
executionStatus: null,
timeRange: undefined,
activityId: '',
};
tokens.forEach((token, index) => {
if (isWorkflowIdStatement(token))
parameters.workflowId = getTwoAhead(tokens, index);
if (isWorkflowTypeStatement(token))
parameters.workflowType = getTwoAhead(tokens, index);
if (isExecutionStatusStatement(token)) {
const value = getTwoAhead(tokens, index);
if (isExecutionStatus(value)) parameters.executionStatus = value;
}
if (isStartTimeStatement(token)) {
const start = getTwoAhead(tokens, index);
try {
const duration = fromDate(start);
const largestUnit = getLargestDurationUnit(duration);
parameters.timeRange = formatDuration(largestUnit);
} catch (error) {
console.error('Error parsing StartTime from query', error);
}
}
if (isActivityIdStatement(token))
parameters.activityId = getTwoAhead(tokens, index);
});
return parameters;
};