in server/routes/utils/adHelpers.ts [98:215]
startTime: get(
response,
'historicalTask.detection_date_range.start_time'
),
endTime: get(response, 'historicalTask.detection_date_range.end_time'),
},
};
}
return camelCaseResponse;
};
// Converts the static detector fields into camelcase. Ignores any job or task-related fields
export const convertStaticFieldsToCamelCase = (response: object) => {
return {
...mapKeysDeep(
omit(response, [
'filter_query',
'feature_query',
'feature_attributes',
'ui_metadata',
'anomaly_detector_job',
'anomaly_detection_task',
'realtime_detection_task',
'historical_analysis_task',
]),
toCamel
),
filterQuery: get(response, 'filter_query', {}),
featureAttributes: get(response, 'feature_attributes', []).map(
(feature: any) => ({
...mapKeysDeep({ ...omit(feature, ['aggregation_query']) }, toCamel),
aggregationQuery: feature.aggregation_query,
})
),
uiMetadata: get(response, 'ui_metadata', {}),
};
};
// Converts the task-related detector fields into camelcase
export const convertTaskAndJobFieldsToCamelCase = (
realtimeTask: any,
historicalTask: any,
detectorJob: object
) => {
let response = {};
// Populate AD job fields
response = {
...response,
enabled: get(detectorJob, 'enabled', false),
enabledTime: get(detectorJob, 'enabled_time'),
disabledTime: get(detectorJob, 'disabled_time'),
};
// Populate RT-task-related fields
response =
realtimeTask !== undefined
? {
...response,
curState: getTaskState(realtimeTask),
stateError: processTaskError(get(realtimeTask, 'error', '')),
initProgress: getTaskInitProgress(realtimeTask),
}
: {
...response,
curState: get(detectorJob, 'enabled', false)
? DETECTOR_STATE.RUNNING
: DETECTOR_STATE.DISABLED,
};
// Detection date range field is stored under the 'detector' field in legacy historical tasks.
// To handle this, need to add a check to fetch the date range from the correct place
const isLegacyHistorical =
get(historicalTask, 'detection_date_range') === undefined &&
get(historicalTask, 'detector.detection_date_range') !== undefined;
const legacyDateRangePrefix = isLegacyHistorical ? 'detector.' : '';
// Populate historical-task-related fields
response = {
...response,
taskId: get(historicalTask, 'id'),
taskState: getTaskState(historicalTask),
taskProgress: get(historicalTask, 'task_progress'),
taskError: processTaskError(get(historicalTask, 'error', '')),
detectionDateRange: {
startTime: get(
historicalTask,
`${legacyDateRangePrefix}detection_date_range.start_time`
),
endTime: get(
historicalTask,
`${legacyDateRangePrefix}detection_date_range.end_time`
),
},
};
if (isEmpty(historicalTask)) {
//@ts-ignore
delete response.detectionDateRange;
}
return response;
};
export const getResultAggregationQuery = (
detectors: string[],
queryParams: GetDetectorsQueryParams
) => {
const aggregationSort = {
totalAnomalies: 'total_anomalies_in_24hr',
latestAnomalyTime: 'latest_anomaly_time',
} as { [key: string]: string };
let aggsSortingOrder = {};
if (aggregationSort[queryParams.sortField]) {
aggsSortingOrder = {
order: {