-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved helper functions to separate files, cleaned up other tests
Signed-off-by: Amit Galitzky <[email protected]>
- Loading branch information
1 parent
ea7055b
commit 6562355
Showing
10 changed files
with
178 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
...atedDetectors/components/__tests__/__snapshots__/ConfirmUnlinkDetectorModal.test.tsx.snap
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* 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, | ||
} 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 | ||
): Promise<AnomalyData[]> => { | ||
const anomalySummaryQuery = getAnomalySummaryQuery( | ||
startTime, | ||
endTime, | ||
detectorId, | ||
undefined, | ||
false | ||
); | ||
|
||
const anomalySummaryResponse = await getClient().post( | ||
`..${AD_NODE_API.DETECTOR}/results/_search`, | ||
{ | ||
body: JSON.stringify(anomalySummaryQuery), | ||
} | ||
); | ||
|
||
return parsePureAnomalies(anomalySummaryResponse); | ||
}; | ||
|
||
export const getDetectorResponse = async (detectorId: string) => { | ||
const resp = await getClient().get(`..${AD_NODE_API.DETECTOR}/${detectorId}`); | ||
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 + (anomaly.endTime - anomaly.startTime) / 2, | ||
metadata: {}, | ||
}; | ||
}); | ||
return { | ||
originPlugin: ORIGIN_PLUGIN_VIS_LAYER, | ||
type: VisLayerTypes.PointInTimeEvents, | ||
pluginResource: ADPluginResource, | ||
events: events, | ||
} 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; | ||
}; |
Oops, something went wrong.