# Copyright (C) 2005-2024 Splunk Inc. All Rights Reserved. from ITOA.itoa_exceptions import ItoaValidationError from ITOA.itoa_object import ItoaObject, CRUDMethodTypes from ITOA.setup_logging import logger class ItsiDriftDetection(ItoaObject): """ Implements ITSI drift detection templates (for use with the drift detection feature) """ collection_name = "itsi_drift_detection_template" REQUIRED_FIELDS = ["aggregation_function", "aggregation_span", "lookback_period", "threshold_direction", "tolerance_in_percent"] def __init__(self, session_key, current_user_name): super(ItsiDriftDetection, self).__init__(session_key, current_user_name, "drift_detection_template", collection_name=self.collection_name) def do_object_validation(self, owner, objects, validate_name=True, dupname_tag=None, transaction_id=None, skip_local_failure=False, ignore_same_key=False): """ See ItoaObject.do_object_validation() """ aggregation_function_values = set(["avg", "max", "min", "sum"]) threshold_values = set(["up", "down", "both"]) for object in objects: for field in self.REQUIRED_FIELDS: if object.get(field) is None: raise ItoaValidationError(message="Required field %s missing" % field, logger=logger) if object["aggregation_function"] not in aggregation_function_values: raise ItoaValidationError( message="Invalid aggregation_function value: %s" % object["aggregation_function"], logger=logger, ) if object["threshold_direction"] not in threshold_values: raise ItoaValidationError( message="Invalid threshold value: %s" % object["threshold_direction"], logger=logger, ) return super(ItsiDriftDetection, self).do_object_validation( owner, objects, validate_name=validate_name, dupname_tag=dupname_tag, transaction_id=transaction_id, skip_local_failure=skip_local_failure, ignore_same_key=ignore_same_key, ) def do_additional_setup(self, owner, objects, req_source="unknown", method=CRUDMethodTypes.METHOD_UPSERT, transaction_id=None, skip_local_failure=False, **kwargs): """ See ItoaObject.identify_dependencies() """ for object in objects: object["tolerance_in_percent"] = int(object["tolerance_in_percent"]) def identify_dependencies(self, owner, objects, method, req_source="unknown", transaction_id=None): """ See ItoaObject.identify_dependencies() """ refresh_jobs = [] if method == CRUDMethodTypes.METHOD_DELETE: change_type = "delete_drift_detection_template_from_service" for object in objects: change = self.get_refresh_job_meta_data( change_type, object["_key"], self.object_type, transaction_id=transaction_id, ) refresh_jobs.append(change) return len(refresh_jobs) > 0, refresh_jobs