-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathentities.js
More file actions
138 lines (123 loc) · 3.82 KB
/
Copy pathentities.js
File metadata and controls
138 lines (123 loc) · 3.82 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
import { StatusIcon } from '@newrelic/nr-labs-components';
import {
ALERT_STATUSES,
WORKLOAD,
WORKLOAD_STATUS_VALUE_CODES,
} from '../constants';
const {
STATUSES: { UNKNOWN, CRITICAL, WARNING, SUCCESS },
} = StatusIcon;
const alertSeverities = [
ALERT_STATUSES.NOT_ALERTING,
ALERT_STATUSES.WARNING,
ALERT_STATUSES.CRITICAL,
];
export const entityStatus = ({ alertSeverity } = {}) => {
switch (alertSeverity) {
case 'NOT_ALERTING': {
return SUCCESS;
}
case 'WARNING': {
return WARNING;
}
case 'CRITICAL': {
return CRITICAL;
}
default: {
return UNKNOWN;
}
}
};
const knownWorkloadStatusValues = Object.values(WORKLOAD_STATUS_VALUE_CODES);
export const workloadStatus = ({
statusValueCode = WORKLOAD_STATUS_VALUE_CODES.UNKNOWN,
}) => {
switch (statusValueCode) {
case WORKLOAD_STATUS_VALUE_CODES.OPERATIONAL: {
return SUCCESS;
}
case WORKLOAD_STATUS_VALUE_CODES.DEGRADED: {
return WARNING;
}
case WORKLOAD_STATUS_VALUE_CODES.DISRUPTED: {
return CRITICAL;
}
default: {
return UNKNOWN;
}
}
};
export const isWorkload = ({ domain, type } = {}) =>
domain === WORKLOAD.DOMAIN && type === WORKLOAD.TYPE;
export const guidsToArray = (guids = {}, maxArrayLen = 10) =>
Object.keys(guids).reduce((acc, type) => {
const typeGuids = guids[type];
if (!typeGuids || !typeGuids.length) return acc;
return [
...acc,
...Array.from(
{ length: Math.ceil(typeGuids.length / maxArrayLen) },
(_, i) => {
const startIdx = i * maxArrayLen;
return typeGuids.slice(startIdx, startIdx + maxArrayLen);
}
),
];
}, []);
// NerdGraph may return closedAt: null for zero-duration violations (openedAt
// === closedAt) due to a platform data consistency issue. These appear
// always open and leak into every band window. Filter them out by
// requiring that a violation with no closedAt opened within the band window.
const statusFromViolations = (violations = [], timeWindow) =>
alertSeverities[
violations
.filter(
({ closedAt, openedAt }) =>
closedAt !== null ||
!timeWindow?.start ||
openedAt >= timeWindow.start
)
.reduce((acc, { alertSeverity }) => {
const statusIndex =
alertSeverities.findIndex((severity) => severity === alertSeverity) ||
0;
return Math.max(acc, statusIndex);
}, 0)
];
export const entitiesDetailsFromQueryResults = (res = {}, timeWindow) =>
Object.keys(res).reduce((acc, cur) => {
const signalsArray = res[cur];
if (!Array.isArray(signalsArray)) return acc;
signalsArray.forEach((entity) => {
acc[entity.guid] = {
...entity,
alertSeverity:
entity.alertSeverity ||
statusFromViolations(entity.alertViolations, timeWindow),
};
});
return acc;
}, {});
export const getWorstWorkloadStatusValue = (events = [], { start, end }) => {
if (!events?.length) return WORKLOAD_STATUS_VALUE_CODES.UNKNOWN;
let worstInWindow = null;
let lastKnownCode = null;
let lastKnownTimestamp = -Infinity;
for (const { statusValueCode, timestamp } of events) {
const code = knownWorkloadStatusValues.includes(statusValueCode)
? statusValueCode
: WORKLOAD_STATUS_VALUE_CODES.UNKNOWN;
if (timestamp >= start && timestamp <= end) {
if (worstInWindow === null || code > worstInWindow) {
worstInWindow = code;
}
if (worstInWindow === WORKLOAD_STATUS_VALUE_CODES.DISRUPTED) break;
} else if (timestamp < start && timestamp > lastKnownTimestamp) {
lastKnownTimestamp = timestamp;
lastKnownCode = code;
}
}
if (worstInWindow !== null) return worstInWindow;
if (lastKnownCode !== null) return lastKnownCode;
return WORKLOAD_STATUS_VALUE_CODES.UNKNOWN;
};