Name

sn_itom_licensing.PopulateExclusionTablesStore

Description

No description available

Script

var PopulateExclusionTablesStore = Class.create();
PopulateExclusionTablesStore.prototype = {
  initialize: function() {
      jobStartTime = new GlideDateTime();
      batchUtil = new global.BatchCommandUtilScript();
      licensingUtil = new ITOMLicensingUtilsStore();
      batchSize = 5000;
      exclusionTable = 'license_exclusion_list';
  },

  updateExclusionTables: function() {
      //Step 1 - Delete Previous Day Exclusion Records
      this._removeStaleRecords();

      //Step 2 - Populate Exclusion Table for each type of exclusion
      var exGr = new GlideRecord('itom_license_exclusion_metadata');
      exGr.addActiveQuery();
      exGr.query();
      while (exGr.next()) {

          var excludedCIsList = [];
          var ci_identifier = exGr.getValue('ci_identifier');
          //if condition type is not script
          if (exGr.getValue('condition_type') != 'script') {
              var tableName = exGr.getValue('table_name');
              var encodedQuery = exGr.getValue('query');
              var conditionType = exGr.getValue('condition_type');
              var exclusionType = exGr.getValue('exclusion_type');

              if (conditionType == 'relationship') {
                  var excludedIdentifier = ci_identifier == 'parent' ? 'child' : 'parent';
                  var tableGr = new GlideRecord(tableName);
                  tableGr.addEncodedQuery(encodedQuery);
                  if (exclusionType == 1) {
                      //adding parent or child CI in exclusion table only when its child or parent is accounting in daily count.
                      licensingUtil.addCiStatusQuery(tableGr, excludedIdentifier + '.install_status');
                      tableGr.addQuery(excludedIdentifier + '.duplicate_of', 'NULL');
                      tableGr.addQuery(excludedIdentifier + '.discovery_source', 'IN', this._getAllowedSources());
                      tableGr.addNotNullQuery(excludedIdentifier + '.last_discovered').addCondition(excludedIdentifier + '.last_discovered', 'ON', 'Last 90 days@javascript:gs.beginningOfLast90Days()@javascript:gs.endOfLast90Days()');
                  }
                  tableGr.query();
                  while (tableGr.next()) {
                      var ciDetail = {};
                      ciDetail.ciSysId = tableGr.getValue(ci_identifier);
                      ciDetail.domain = tableGr[ci_identifier].sys_domain + ''; // domain of parent or child
                      excludedCIsList.push(ciDetail);
                  }
              } else {
                  var tableGr = new GlideRecord(tableName);
                  tableGr.addEncodedQuery(encodedQuery);
                  tableGr.query();
                  while (tableGr.next()) {
                      var ciDetail = {};
                      ciDetail.ciSysId = tableGr.getValue(ci_identifier);
                      ciDetail.domain = tableGr.getValue('sys_domain');
                      excludedCIsList.push(ciDetail);
                  }
              }

          } else {
              var evaluator = new GlideScopedEvaluator();
              excludedCIsList = evaluator.evaluateScript(exGr, 'script', null);
          }

          this._addCIsToExclusionTables(excludedCIsList, exGr);
      }


  },

  _addCIsToExclusionTables: function(excludedCIsList, exclusionMetaDataGr) {
      if (!(excludedCIsList && excludedCIsList.length != 0))
          return;

      var jsonArr = [];
      var counter = 0;
      excludedCIsList.forEach(function(excludedCI) {
          var ciSysId = excludedCI.ciSysId;
          if (!ciSysId)
              return;

          var domain = excludedCI.domain || 'global';
          jsonCountRecord = {};
          jsonCountRecord.ci = ciSysId;
          jsonCountRecord.exclusion_reason = exclusionMetaDataGr.getValue('sys_id');
          jsonCountRecord.sys_domain = domain;
          jsonArr.push(jsonCountRecord);
          counter++;
          if (counter == batchSize) {
              batchUtil.batchInsertMultiple(JSON.stringify(jsonArr), exclusionTable, '');
              counter = 0;
              jsonArr = [];
          }
      });

      batchUtil.batchInsertMultiple(JSON.stringify(jsonArr), exclusionTable, '');

  },

  _removeStaleRecords: function() {
      var tableGr = new GlideRecord(exclusionTable);
      tableGr.addQuery('sys_updated_on', '<', jobStartTime);
      tableGr.query();
      tableGr.deleteMultiple();
  },

  _getAllowedSources: function() {
      var allowedSources = [];
      var licenseDiscoverySourceGr = new GlideRecord('itom_lu_discovery_sources');
      licenseDiscoverySourceGr.query();
      while (licenseDiscoverySourceGr.next())
          allowedSources.push(licenseDiscoverySourceGr.getValue('source'));
      return allowedSources;
  },

  type: 'PopulateExclusionTablesStore'

};

Sys ID

3f68c000b7c6301046df8985de11a951

Offical Documentation

Official Docs: