Name

sn_em_arm.EvtMgmtAlertMgmtUtils

Description

No description available

Script

var EvtMgmtAlertMgmtUtils = Class.create();
EvtMgmtAlertMgmtUtils.prototype = {

  type: 'EvtMgmtAlertMgmtUtils',

  initialize: function() {
      this.IS_GROUPED_ALERT = 'true';
      this.OPEN_STATES = ["Open", "Reopen", "Flapping"];
      this.START_SIMULTANEOUS_LOOP = '*START*';
      this.evtMgmtCommons = new global.EvtMgmtCommons();
  	this.NUM_OF_JOBS =  gs.getProperty('sn_em_arm.alert_management.num_of_jobs', 1);
  },

  getCreatedAlerts: function(startTime, endTime, chunkSize, jobNum) {
      //cover all the alerts between 2 group executions, in addition do not take the virtual
      //because it should handle in the update script.
      var alerts = this.getCreatedAlertsBaseGr(chunkSize);
      alerts.addQuery('sys_created_on', '>', startTime);
      alerts.addQuery('sys_created_on', '<=', endTime);
      alerts.orderBy('sys_created_on');
      if(jobNum != null){
          var encoded = this.constructFilter(this.getChars(jobNum));
          alerts.addQuery(encoded);
      }
      this.evtMgmtCommons.addDebugMessage('EvtMgmtAlertMgmtUtils.getCreatedAlerts - query: ' + alerts.getEncodedQuery()); 
  
      alerts.query();
      return alerts;
  },

  getCreatedAlertsThisSecond: function(exactTime, lastSimultaneousAlertNumber, chunkSize, jobNum) {
      var alerts = this.getCreatedAlertsBaseGr(chunkSize);
      if(jobNum != null){
          var encoded = this.constructFilter(this.getChars(jobNum));
          alerts.addQuery(encoded);
      }
      alerts.addQuery('sys_created_on', exactTime);
      return this.getNextAlertsThisSecond(alerts, lastSimultaneousAlertNumber);
  },

  getCreatedAlertsBaseGr: function(chunkSize) {
      var alerts = new GlideRecord('em_alert');
      alerts.addQuery('state', 'IN', this.OPEN_STATES);
      alerts.addQuery('is_group_alert', '!=', this.IS_GROUPED_ALERT);
      alerts.setLimit(chunkSize);
      return alerts;
  },

  getNextAlertsThisSecond: function(alerts, lastSimultaneousAlertNumber) {
      // This method is generic for create & update loop - order by number, and if we're not at the start of the loop, start after the last recorded number
      if (this.START_SIMULTANEOUS_LOOP != lastSimultaneousAlertNumber) {
          alerts.addQuery('number', '>', lastSimultaneousAlertNumber);
      }
      alerts.orderBy('number');
      alerts.query();
      return alerts;
  },

  getUpdatedAlerts: function(startTime, endTime, chunkSize, groupingG2Time, waitForGrouping, jobNum) {
      var alerts = this.getUpdatedAlertsBaseGr(chunkSize, groupingG2Time, waitForGrouping);
      alerts.addQuery('sys_updated_on', '>=', startTime);
      alerts.addQuery('sys_updated_on', '<', endTime);
      if(jobNum != null){
          var encoded = this.constructFilter(this.getChars(jobNum));
          alerts.addEncodedQuery(encoded);
      }
      alerts.orderBy('sys_updated_on');
      this.evtMgmtCommons.addDebugMessage('EvtMgmtAlertMgmtUtils.getUpdatedAlerts - query: ' + alerts.getEncodedQuery());
      
      alerts.query();
      return alerts;
  },

  getUpdatedAlertsThisSecond: function(exactTime, lastSimultaneousAlertNumber, chunkSize, groupingG2Time, waitForGrouping, jobNum) {
      var alerts = this.getUpdatedAlertsBaseGr(chunkSize, groupingG2Time, waitForGrouping);
      if(jobNum != null){
          var encoded = this.constructFilter(this.getChars(jobNum));
          alerts.addEncodedQuery(encoded);
      }

      alerts.addQuery('sys_updated_on', '=', exactTime);
      return this.getNextAlertsThisSecond(alerts, lastSimultaneousAlertNumber);
  },

  getUpdatedAlertsBaseGr: function(chunkSize, groupingG2Time, waitForGrouping) {
      var alerts = new GlideRecord('em_alert');
      //taking the the alerts after the grouping occur, and just the update. in addition taking the virtual
      //because it creates and update after the grouping time.
        if (waitForGrouping) {
          var preventRedundantUpdates = gs.getProperty('sn_em_arm.alert_management.prevent_redundant_updates_in_case_of_incoming_alerts_stream', 'true') === 'true';
          if (preventRedundantUpdates) {
  			// we do not want to get alerts that were not updated,
  			// alert that was not updated but was only created has  'sys_created_on' == 'sys_updated_on'
  			// alert that was updated has 'sys_created_on' < 'sys_updated_on'
             alerts.addQuery('sys_created_on', 'NSAMEAS', 'sys_updated_on');//get only updated alerts
  		}
  		
          alerts.addQuery('sys_created_on', '<=', groupingG2Time)
              .addOrCondition('is_group_alert', '=', this.IS_GROUPED_ALERT);
       }

      alerts.addQuery('state', 'IN', this.OPEN_STATES);
      alerts.setLimit(chunkSize);
      return alerts;
  },
  constructFilter: function(chars){
      var base = "numberENDSWITH";
      var filterStr =""; 
      for (var i = 0; i < chars.length; i++) {
      filterStr = filterStr.concat(base);
      filterStr = filterStr.concat(chars[i]);
      if(i < (chars.length - 1))
          filterStr = filterStr.concat("^OR");
    }
    return filterStr;
  },

  getChars: function(jobNum) {
      // we want to collect all the numbers between 0-9 that should be collected in for this jobNum
      // in case property is 2, the jobNum is 1 we will collect: 1,3,5,7,9
      // in case property is 3, the jobNum is 2 we will collect: 2,5,8 etc.
      var chars = [];
      for(var i=jobNum ; i<10 ; i+= parseInt(this.NUM_OF_JOBS,10)) // 10 is all the numeric chars between 0-9
          chars.push(i);
      return chars;
  }
};

Sys ID

6a63e5aab71d20107c038229ce11a9be

Offical Documentation

Official Docs: