-
Notifications
You must be signed in to change notification settings - Fork 58
/
helpers.ts
143 lines (136 loc) · 4.12 KB
/
helpers.ts
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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import {
getAnomalySummaryQuery,
parsePureAnomalies,
} from '../pages/utils/anomalyResultUtils';
import { AD_NODE_API } from '../../utils/constants';
import { AnomalyData } from '../models/interfaces';
import { getClient } from '../services';
import {
PluginResource,
PointInTimeEventsVisLayer,
VisLayerError,
VisLayerErrorTypes,
VisLayerTypes,
} from '../../../../src/plugins/vis_augmenter/public';
import {
DETECTOR_HAS_BEEN_DELETED,
ORIGIN_PLUGIN_VIS_LAYER,
PLUGIN_EVENT_TYPE,
} from './constants';
import {
DOES_NOT_HAVE_PERMISSIONS_KEY_WORD,
NO_PERMISSIONS_KEY_WORD,
} from '../../server/utils/helpers';
import { get } from 'lodash';
// This gets all the needed anomalies for the given detector ID and time range
export const getAnomalies = async (
detectorId: string,
startTime: number,
endTime: number,
resultIndex: string,
dataSourceId: string = '',
): Promise<AnomalyData[]> => {
const anomalySummaryQuery = getAnomalySummaryQuery(
startTime,
endTime,
detectorId,
undefined,
false
);
let anomalySummaryResponse;
if (resultIndex === '') {
anomalySummaryResponse = await getClient().post(
`..${AD_NODE_API.DETECTOR}/results/_search/${dataSourceId}`,
{
body: JSON.stringify(anomalySummaryQuery),
}
);
} else {
if (!resultIndex.endsWith('*')) {
resultIndex += '*';
}
anomalySummaryResponse = await getClient().post(
`..${AD_NODE_API.DETECTOR}/results/_search/${resultIndex}/true/${dataSourceId}`,
{
body: JSON.stringify(anomalySummaryQuery),
}
);
}
return parsePureAnomalies(anomalySummaryResponse);
};
export const getDetectorResponse = async (detectorId: string, dataSourceId: string = '') => {
const url = dataSourceId ? `..${AD_NODE_API.DETECTOR}/${detectorId}/${dataSourceId}` : `..${AD_NODE_API.DETECTOR}/${detectorId}`;
const resp = await getClient().get(url);
return resp;
};
// This takes anomalies and returns them as vis layer of type PointInTimeEvents
export const convertAnomaliesToPointInTimeEventsVisLayer = (
anomalies: AnomalyData[],
ADPluginResource: PluginResource
): PointInTimeEventsVisLayer => {
const events = anomalies.map((anomaly: AnomalyData) => {
return {
timestamp: anomaly.startTime,
metadata: {},
};
});
return {
originPlugin: ORIGIN_PLUGIN_VIS_LAYER,
type: VisLayerTypes.PointInTimeEvents,
pluginResource: ADPluginResource,
events: events,
pluginEventType: PLUGIN_EVENT_TYPE,
} as PointInTimeEventsVisLayer;
};
const checkIfPermissionErrors = (error): boolean => {
return typeof error === 'string'
? error.includes(NO_PERMISSIONS_KEY_WORD) ||
error.includes(DOES_NOT_HAVE_PERMISSIONS_KEY_WORD)
: get(error, 'message', '').includes(NO_PERMISSIONS_KEY_WORD) ||
get(error, 'message', '').includes(DOES_NOT_HAVE_PERMISSIONS_KEY_WORD);
};
const checkIfDeletionErrors = (error): boolean => {
return typeof error === 'string'
? error.includes(DETECTOR_HAS_BEEN_DELETED)
: get(error, 'message', '').includes(DETECTOR_HAS_BEEN_DELETED);
};
//Helps convert any possible errors into either permission, deletion or fetch related failures
export const getVisLayerError = (error): VisLayerError => {
let visLayerError: VisLayerError = {} as VisLayerError;
if (checkIfPermissionErrors(error)) {
visLayerError = {
type: VisLayerErrorTypes.PERMISSIONS_FAILURE,
message:
error === 'string'
? error
: error instanceof Error
? get(error, 'message', '')
: '',
};
} else if (checkIfDeletionErrors(error)) {
visLayerError = {
type: VisLayerErrorTypes.RESOURCE_DELETED,
message:
error === 'string'
? error
: error instanceof Error
? get(error, 'message', '')
: '',
};
} else {
visLayerError = {
type: VisLayerErrorTypes.FETCH_FAILURE,
message:
error === 'string'
? error
: error instanceof Error
? get(error, 'message', '')
: '',
};
}
return visLayerError;
};