Name

sn_sow_em.EmAiInsightForRF

Description

This script is a duplicate of global.EmAiInsight code for Recommendation Framework. Small changes were made in the object returned from the methods& 13; **All changes in this file need to be changed in EmAiInsight as well **

Script

var EmAiInsightForRF = Class.create();
EmAiInsightForRF.prototype = {
  
  initialize: function() {
      this.emAiInsightCommon = new global.EmAiInsightCommon();
      this.emAiInsight = new global.EmAiInsight();
  	this.emRfCommons = new sn_sow_em.EmRfCommons();
  	this.debugEnabled = new global.EvtMgmtCommons().isDebugEnabled();
  },
  
  //Duplicate of global.EmAiInsight().getRelatedAlertQuery
  getRelatedAlerts: function(alertGr, currentAlertGr) {
      var evaluationOutput = [];
      var useMl = String(gs.getProperty("evt_mgmt.similarity_use_ml", true));
      var isDomain = String(this.emAiInsight.isDomainSepActive());
      var isMlPluginActive = String(GlidePluginManager.isActive('com.glide.platform_ml'));
  	var isEntitledToML = this.checkMLSimilarityEntitlement();
      if (useMl === "true") {
         if (isMlPluginActive === "true") {
             if (isEntitledToML === "true") {
                 if (isDomain === "false") {
                     try {
                         evaluationOutput = this.getSimilarAlertQueryForRF(alertGr, currentAlertGr);
                         if (evaluationOutput.length > 0) {
                             gs.warn(" Alerts Similarities: Alerts Similarities were calculate by ML alogritm ");
                             return evaluationOutput;
                         }
                     } catch (exception) {
                         gs.error(" Alerts Similarities: Similarities calculations using ML failed, possibly do to empty input fields  " + exception);
                     }
                 }
             } else {
                 this.evtMgmtCommons.addDebugLogNoPrefix("Alerts Similarities: Similarities calculations using ML failed, no ML entitelment ");
             }
         }
     }
  	
      gs.warn("Alerts Similarities:  Alerts Similarities were calculated by field comparison ");
      var cmdbCiClass = currentAlertGr.cmdb_ci.sys_class_name;
      if (gs.nil(cmdbCiClass)) {
          //return empty results
          return evaluationOutput;
      }
      this.addDebugLogNoPrefix("Alert Insight: calculate Similar Alerts");
      alertGr.addQuery("cmdb_ci.sys_class_name", "=", cmdbCiClass);
      if (!gs.nil(currentAlertGr.message_key)) {
          alertGr.addQuery("message_key", "!=", currentAlertGr.message_key); // Dont show repeated alerts.
      }
      this.emAiInsightCommon.addNotTheSameSysIdQuery(alertGr, currentAlertGr);
      this.emAiInsightCommon.addTimeBeforeQuery(alertGr, "evt_mgmt.alert_insight_alert_history_min", 43200 /* 30 days */ );
      this.emAiInsight.setSameAsFilter(alertGr, currentAlertGr, "evt_mgmt.alert_insight_alert_same_as_filter", "source,type,resource,metric_name");
  	
  	//added code for RF
      alertGr.orderByDesc('initial_remote_time');
      alertGr.setLimit(this.emRfCommons.TOP_N_RESULTS); // default value of 10 results
      this.addDebugLogNoPrefix("Alert Insight: calculate Similar Alerts. Query string: " + alertGr.getEncodedQuery());
      alertGr.query();
  	
  	evaluationOutput= this.emRfCommons.insertAlertsToResultArray(alertGr, "sys_id");
      return evaluationOutput;
  },
  
  checkMLSimilarityEntitlement: function() {
      if (this.emAiInsight.checkMLSimilarityEntitlement && (typeof this.emAiInsight.checkMLSimilarityEntitlement == 'function')) {
          return this.emAiInsight.checkMLSimilarityEntitlement();
      }
  	// Do not check for entitlement
      return 'true';
  },
  
  //Duplicate of global.EmAiInsight().getSimilarAlertQuery
  getSimilarAlertQueryForRF: function(alertGr, currentAlertGr) {
      this.emAiInsight.slowStepsManager.setTopic("ML:Get Similarity Alert Query");
      this.emAiInsight.slowStepsManager.startStep("EmAiInsightForRF: getSimilarAlertQueryForRF");
      var soultionTitle = gs.getProperty("evt_mgmt_similarity_solution_title", "ml_x_global_alert_similarity");
      var use_threshold = gs.getProperty("evt_mgmt.similarity_use_threshold", true);
      var options = {
          "top_n": this.emRfCommons.TOP_N_RESULTS, // default value of 10 results
          "apply_threshold": use_threshold
      };
      var solutions = [];
      solutions[0] = soultionTitle;
      var predictor = new global.MLPredictor();
      var isPredictedSuccessful = false;
      var evaluationOutputs = [];
      try {
          var solutionName = solutions[0];
          var solution = predictor.findActiveSolution(solutionName);
          if (!solution)
              return evaluationOutputs;
          var outcome_array = predictor.getPredictions(currentAlertGr, solution, options);
          if (outcome_array == null) {
              return evaluationOutputs;
          }
          if (outcome_array.length == 0) {
              return evaluationOutputs;
          }
  		
          isPredictedSuccessful = true;
  		
          for (var predictionIdx = 0; predictionIdx < outcome_array.length; predictionIdx++) {
              var currAlertSysId = outcome_array[predictionIdx].predictedValueSysId();
              if (currentAlertGr.sys_id == currAlertSysId) { //don't add the alert itself
                  continue;
              }
  			//added code for RF
              var singlePrediction = {};
              singlePrediction[this.emRfCommons.ALERT_RECORD_NAME] = currAlertSysId;
              singlePrediction[this.emRfCommons.CONFIDNECE_SCORE] = outcome_array[predictionIdx].confidence();
              evaluationOutputs.push(singlePrediction);
          }
      } catch (exception) {
          var err = "Alerts Similarities\n Alert similarities model fail to locat similarities, possible some input fields are empty /n" + exception;
          gs.error(err);
      } finally {
          this.emAiInsight.slowStepsManager.report();
          if (!isPredictedSuccessful) {
              return evaluationOutputs;
          }
          if (this.emAiInsight.isDebugEnabled())
              gs.error("Alerts Similarities\n" + this.printReturnedArray(returnArray));
          return evaluationOutputs;
      }
  },
  
  
  printReturnedArray: function(returnArray) {
      var infoString = "";
      for (var i = 0; i < returnArray.length; i++) {
          var arr = returnArray[i];
          infoString = infoString +
              "   Sys ID     " + arr[this.emRfCommons.ALERT_RECORD_NAME] +
              "   Confidence " + arr[this.emRfCommons.CONFIDNECE_SCORE] + "\n";
      }
      return infoString;
  },
  
  // write log using gs.info() without adding a prefix to message
  addDebugLogNoPrefix: function(logStr)  {
  	if (this.debugEnabled) {
          gs.info(logStr);
  	}
  },

  type: 'EmAiInsightForRF'
};

Sys ID

8105f24f4f010110b34cb38b43ce0b77

Offical Documentation

Official Docs: