import { i18n } from '@kbn/i18n' ; import { CONDITIONS_NOT_SUPPORTED_FUNCTIONS } from '../constants/detector_rule' ; import { MULTI_BUCKET_IMPACT } from '../constants/multi_bucket_impact' ; import { ANOMALY_SEVERITY , ANOMALY_THRESHOLD , SEVERITY_COLORS } from '../constants/anomalies' ; import type { AnomaliesTableRecord , AnomalyRecordDoc } from '../types/anomalies' ; export interface SeverityType { id : ANOMALY_SEVERITY ; label : string ; } export enum ENTITY_FIELD_TYPE { BY = 'by' , OVER = 'over' , PARTITON = 'partition' } export const ENTITY_FIELD_OPERATIONS = { ADD : '+' , REMOVE : '-' , } as const ; export type EntityFieldOperation = typeof ENTITY_FIELD_OPERATIONS [ keyof typeof ENTITY_FIELD_OPERATIONS ] ; export interface EntityField { fieldName : string ; fieldValue : string | number | undefined ; fieldType ? : ENTITY_FIELD_TYPE ; operation ? : EntityFieldOperation ; } const DISPLAY_ACTUAL_FUNCTIONS = [ 'count' , 'distinct_count' , 'lat_long' , 'mean' , 'max' , 'min' , 'sum' , 'median' , 'varp' , 'info_content' , 'time' , ] ; const DISPLAY_TYPICAL_FUNCTIONS = [ 'count' , 'distinct_count' , 'lat_long' , 'mean' , 'max' , 'min' , 'sum' , 'median' , 'varp' , 'info_content' , 'time' , ] ; let severityTypes ; function getSeverityTypes ( ) { if ( severityTypes ) { return severityTypes ; } return ( severityTypes = { critical : { id : ANOMALY_SEVERITY . CRITICAL , label : i18n . translate ( 'xpack.ml.anomalyUtils.severity.criticalLabel' , { defaultMessage : 'critical' , } ) , } , major : { id : ANOMALY_SEVERITY . MAJOR , label : i18n . translate ( 'xpack.ml.anomalyUtils.severity.majorLabel' , { defaultMessage : 'major' , } ) , } , minor : { id : ANOMALY_SEVERITY . MINOR , label : i18n . translate ( 'xpack.ml.anomalyUtils.severity.minorLabel' , { defaultMessage : 'minor' , } ) , } , warning : { id : ANOMALY_SEVERITY . WARNING , label : i18n . translate ( 'xpack.ml.anomalyUtils.severity.warningLabel' , { defaultMessage : 'warning' , } ) , } , unknown : { id : ANOMALY_SEVERITY . UNKNOWN , label : i18n . translate ( 'xpack.ml.anomalyUtils.severity.unknownLabel' , { defaultMessage : 'unknown' , } ) , } , low : { id : ANOMALY_SEVERITY . LOW , label : i18n . translate ( 'xpack.ml.anomalyUtils.severityWithLow.lowLabel' , { defaultMessage : 'low' , } ) , } , } ) ; } export function isCategorizationAnomaly ( anomaly ) { return anomaly . entityName === 'mlcategory' ; } /**
 * Return formatted severity score.
 */ export function getFormattedSeverityScore ( score ) { return score < 1 ? '< 1' : String ( parseInt ( String ( score ) , 10 ) ) ; } export function getSeverity ( normalizedScore ) { const severityTypesList = getSeverityTypes ( ) ; if ( normalizedScore >= ANOMALY_THRESHOLD . CRITICAL ) { return severityTypesList . critical ; } else if ( normalizedScore >= ANOMALY_THRESHOLD . MAJOR ) { return severityTypesList . major ; } else if ( normalizedScore >= ANOMALY_THRESHOLD . MINOR ) { return severityTypesList . minor ; } else if ( normalizedScore >= ANOMALY_THRESHOLD . LOW ) { return severityTypesList . warning ; } else { return severityTypesList . unknown ; } } export function getSeverityType ( normalizedScore ) { if ( normalizedScore >= 75 ) { return ANOMALY_SEVERITY . CRITICAL ; } else if ( normalizedScore >= 50 ) { return ANOMALY_SEVERITY . MAJOR ; } else if ( normalizedScore >= 25 ) { return ANOMALY_SEVERITY . MINOR ; } else if ( normalizedScore >= 3 ) { return ANOMALY_SEVERITY . WARNING ; } else if ( normalizedScore >= 0 ) { return ANOMALY_SEVERITY . LOW ; } else { return ANOMALY_SEVERITY . UNKNOWN ; } } export function getSeverityWithLow ( normalizedScore ) { const severityTypesList = getSeverityTypes ( ) ; if ( normalizedScore >= ANOMALY_THRESHOLD . CRITICAL ) { return severityTypesList . critical ; } else if ( normalizedScore >= ANOMALY_THRESHOLD . MAJOR ) { return severityTypesList . major ; } else if ( normalizedScore >= ANOMALY_THRESHOLD . MINOR ) { return severityTypesList . minor ; } else if ( normalizedScore >= ANOMALY_THRESHOLD . WARNING ) { return severityTypesList . warning ; } else if ( normalizedScore >= ANOMALY_THRESHOLD . LOW ) { return severityTypesList . low ; } else { return severityTypesList . unknown ; } } export function getSeverityColor ( normalizedScore ) { if ( normalizedScore >= ANOMALY_THRESHOLD . CRITICAL ) { return SEVERITY_COLORS . CRITICAL ; } else if ( normalizedScore >= ANOMALY_THRESHOLD . MAJOR ) { return SEVERITY_COLORS . MAJOR ; } else if ( normalizedScore >= ANOMALY_THRESHOLD . MINOR ) { return SEVERITY_COLORS . MINOR ; } else if ( normalizedScore >= ANOMALY_THRESHOLD . WARNING ) { return SEVERITY_COLORS . WARNING ; } else if ( normalizedScore >= ANOMALY_THRESHOLD . LOW ) { return SEVERITY_COLORS . LOW ; } else { return SEVERITY_COLORS . BLANK ; } } export function getMultiBucketImpactLabel ( multiBucketImpact ) { if ( multiBucketImpact >= MULTI_BUCKET_IMPACT . HIGH ) { return i18n . translate ( 'xpack.ml.anomalyUtils.multiBucketImpact.highLabel' , { defaultMessage : 'high' , } ) ; } else if ( multiBucketImpact >= MULTI_BUCKET_IMPACT . MEDIUM ) { return i18n . translate ( 'xpack.ml.anomalyUtils.multiBucketImpact.mediumLabel' , { defaultMessage : 'medium' , } ) ; } else if ( multiBucketImpact >= MULTI_BUCKET_IMPACT . LOW ) { return i18n . translate ( 'xpack.ml.anomalyUtils.multiBucketImpact.lowLabel' , { defaultMessage : 'low' , } ) ; } else { return i18n . translate ( 'xpack.ml.anomalyUtils.multiBucketImpact.noneLabel' , { defaultMessage : 'none' , } ) ; } } export function getEntityFieldName ( record ) { if ( record . by_field_name !== undefined && record . by_field_value !== undefined ) { return record . by_field_name ; } if ( record . over_field_name !== undefined ) { return record . over_field_name ; } if ( record . partition_field_name !== undefined ) { return record . partition_field_name ; } } export function getEntityFieldValue ( record ) { if ( record . by_field_value !== undefined ) { return record . by_field_value ; } if ( record . over_field_value !== undefined ) { return record . over_field_value ; } if ( record . partition_field_value !== undefined ) { return record . partition_field_value ; } } export function getEntityFieldList ( record ) { const entityFields = [ ] ; if ( record . partition_field_name !== undefined ) { entityFields . push ( { fieldName : record . partition_field_name , fieldValue : record . partition_field_value , fieldType : ENTITY_FIELD_TYPE . PARTITON , } ) ; } if ( record . over_field_name !== undefined ) { entityFields . push ( { fieldName : record . over_field_name , fieldValue : record . over_field_value , fieldType : ENTITY_FIELD_TYPE . OVER , } ) ; } if ( record . by_field_name !== undefined && record . over_field_name === undefined ) { entityFields . push ( { fieldName : record . by_field_name , fieldValue : record . by_field_value , fieldType : ENTITY_FIELD_TYPE . BY , } ) ; } return entityFields ; } export function showActualForFunction ( functionDescription ) { return DISPLAY_ACTUAL_FUNCTIONS . indexOf ( functionDescription ) > - 1 ; } export function showTypicalForFunction ( functionDescription ) { return DISPLAY_TYPICAL_FUNCTIONS . indexOf ( functionDescription ) > - 1 ; } export function isRuleSupported ( record ) { return ( CONDITIONS_NOT_SUPPORTED_FUNCTIONS . indexOf ( record . function ) === - 1 || getEntityFieldName ( record ) !== undefined ) ; } export const aggregationTypeTransform = { toES ( oldAggType ) { let newAggType = oldAggType ; if ( newAggType === 'mean' ) { newAggType = 'avg' ; } else if ( newAggType === 'distinct_count' ) { newAggType = 'cardinality' ; } else if ( newAggType === 'median' ) { newAggType = 'percentiles' ; } return newAggType ; } , toML ( oldAggType ) { let newAggType = oldAggType ; if ( newAggType === 'avg' ) { newAggType = 'mean' ; } else if ( newAggType === 'cardinality' ) { newAggType = 'distinct_count' ; } else if ( newAggType === 'percentiles' ) { newAggType = 'median' ; } return newAggType ; } , } ;