Name

sn_rf.RFTrendDefinitionService

Description

No description available

Script

var RFTrendDefinitionService = Class.create();
RFTrendDefinitionService.prototype = {
  tableName: RFConstants.tables.SN_RF_TREND_DEFINITION,
  ids: "ids",

  initialize: function(grOrSysId) {
      this.rfLogger = new RFLogger("RFTrendDefinitionService");
      if (grOrSysId && grOrSysId.sys_class_name == this.tableName && grOrSysId.isValidRecord()) {
          this.trendDefinition = grOrSysId;
      } else {
          var trendDefinitionGr = new GlideRecord(this.tableName);
          if (trendDefinitionGr.get(grOrSysId)) {
              this.trendDefinition = trendDefinitionGr;
          }
      }
  },

  hasValidRecord: function() {
      return this.trendDefinition && this.trendDefinition.sys_class_name == this.tableName && this.trendDefinition.isValidRecord();
  },

  getTrendDefinitionId: function() {
      return this.trendDefinition.getValue("sys_id");
  },

  getTable: function() {
      return this.trendDefinition.getValue(RFConstants.fields.SN_RF_TREND_DEFINITION_TABLE);
  },

  getTrendType: function() {
      return this.trendDefinition.getValue(RFConstants.fields.SN_RF_TREND_DEFINITION_TREND_TYPE);
  },

  getCommonField: function() {
      return this.trendDefinition.getValue(RFConstants.fields.SN_RF_TREND_DEFINITION_COMMON_FIELD);
  },

  getReferenceType: function() {
      return this.trendDefinition.getValue(RFConstants.fields.SN_RF_TREND_DEFINITION_REFERENCE_TYPE);
  },

  getTrendField: function() {
      return this.trendDefinition.getValue(RFConstants.fields.SN_RF_TREND_DEFINITION_TREND_FIELD);
  },

  getM2MTable: function() {
      return this.trendDefinition.getValue(RFConstants.fields.SN_RF_TREND_DEFINITION_M2M_TABLE);
  },

  getM2MFromField: function() {
      return this.trendDefinition.getValue(RFConstants.fields.SN_RF_TREND_DEFINITION_M2M_FROM_FIELD);
  },

  getM2MToField: function() {
      return this.trendDefinition.getValue(RFConstants.fields.SN_RF_TREND_DEFINITION_M2M_TO_FIELD);
  },

  getCondition: function() {
      return this.trendDefinition.getValue(RFConstants.fields.SN_RF_TREND_DEFINITION_CONDITION);
  },

  getThreshold: function() {
      return this.trendDefinition.getValue(RFConstants.fields.SN_RF_TREND_DEFINITION_THRESHOLD);
  },

  getSourceTable: function() {
      var referenceType = this.getReferenceType();
      if (referenceType == RFConstants.trendReferenceTypes.SIMPLE) {
          return RFUtils.getFieldReferenceTable(this.getTable(), this.getTrendField());
      } else if (referenceType == RFConstants.trendReferenceTypes.M2M) {
          return RFUtils.getFieldReferenceTable(this.getM2MTable(), this.getM2MToField());
      }
  },

  getTrendResult: function(recordSysIds) {
      this.recordSysIds = recordSysIds;
      this.isTrendConfigValid = true;
      this.trendData = [];
      this.sourceTable = null;
      this._buildRecordsAgeMap();

      switch (this.getTrendType()) {
          case RFConstants.trendTypes.COMMON_VALUE:
              this._processCommonValue();
              break;
          case RFConstants.trendTypes.COMMON_REFERENCE:
              this._processCommonReference();
              break;
          case RFConstants.trendTypes.COMMON_CONDITION:
              this._processCommonCondition();
              break;
      }
      if (!this.isTrendConfigValid) {
          errorDetails = new RFExceptionManager("INVALID_TREND_DEFINITION_CONFIG");
          this.rfLogger.logError(errorDetails.getMessage(), "RFTrendDefinitionService - getTrendResult");
          return {
              "status": RFConstants.ERROR,
              "errorCode": errorDetails.getCode(),
              "errorMessage": errorDetails.getMessage()
          };
      }
      return {
          "status": RFConstants.SUCCESS,
          "trendData": this.trendData
      };
  },

  _buildRecordsAgeMap: function() {
      this.recordsAgeMap = {};
      var gr = new GlideRecord(this.getTable());
      gr.addQuery("sys_id", "IN", this.recordSysIds);
      gr.query();
      while (gr.next()) {
          this.recordsAgeMap[gr.getValue("sys_id")] = gr.getValue("sys_created_on");
      }
  },

  _processCommonValue: function() {
      var commonField = this.getCommonField();
      this._getCommonValueOrCommonReferenceResults(this.getTable(), commonField, "sys_id");
  },

  _processCommonReference: function() {
      var referenceType = this.getReferenceType();
      if (referenceType == RFConstants.trendReferenceTypes.SIMPLE) {
          var trendFieldReferenceTable = RFUtils.getFieldReferenceTable(this.getTable(), this.getTrendField());
          if (!trendFieldReferenceTable) {
              this.isTrendConfigValid = false;
              return;
          }
          this.sourceTable = trendFieldReferenceTable;
          this._getCommonValueOrCommonReferenceResults(this.getTable(), this.getTrendField(), "sys_id");
      } else if (referenceType == RFConstants.trendReferenceTypes.M2M) {
          var m2mToFieldReferenceTable = RFUtils.getFieldReferenceTable(this.getM2MTable(), this.getM2MToField());
          var m2mFromFieldReferenceTable = RFUtils.getFieldReferenceTable(this.getM2MTable(), this.getM2MFromField());
          if (!m2mToFieldReferenceTable || !m2mFromFieldReferenceTable) {
              this.isTrendConfigValid = false;
              return;
          }
          this.sourceTable = m2mToFieldReferenceTable;
          this._getCommonValueOrCommonReferenceResults(this.getM2MTable(), this.getM2MToField(), this.getM2MFromField());
      }
  },

  _processCommonCondition: function() {
      var condition = this.getCondition();
      var recordsGr = new GlideRecord(this.getTable());
      recordsGr.addQuery("sys_id", "IN", this.recordSysIds);
      if (condition) {
          recordsGr.addEncodedQuery(condition);
      }
      recordsGr.query();
      if (recordsGr.getRowCount() >= this.getThreshold()) {
          this._getCommonConditionResults(recordsGr);
      }
  },

  _getCommonConditionResults: function(recordsGr) {
      var ids = [];
      while (recordsGr.next()) {
          var sysId = recordsGr.getValue("sys_id");
          ids.push(sysId);
      }
      var groupDetails = this._getGroupDetails(ids);
      var groupData = {};
      groupData[RFConstants.evaluationOutputNames.OLDEST_CREATED_AT] = groupDetails[RFConstants.evaluationOutputNames.OLDEST_CREATED_AT];
      groupData[RFConstants.evaluationOutputNames.COUNT] = ids.length;
      groupData[RFConstants.evaluationOutputNames.HIGHEST_CONFIDENCE_RECORD] = groupDetails[RFConstants.evaluationOutputNames.HIGHEST_CONFIDENCE_RECORD];
      groupData[RFConstants.evaluationOutputNames.TREND_DEFINITION_ID] = this.getTrendDefinitionId();
      groupData[RFConstants.evaluationOutputNames.QUALIFIED_RECORD_IDS] = JSON.stringify(ids);
      this.trendData = [groupData];
  },

  _getCommonValueOrCommonReferenceResults: function(queryTable, groupField, queryField) {
      var groupMap = {};
      var gr = new GlideRecord(queryTable);
      gr.addQuery(queryField, "IN", this.recordSysIds);
      gr.addNotNullQuery(groupField);
      gr.query();
      while (gr.next()) {
          var groupKey = gr.getElement(groupField).toString();
          if (!groupMap[groupKey]) {
              groupMap[groupKey] = {};
              groupMap[groupKey][this.ids] = [];
              groupMap[groupKey][RFConstants.evaluationOutputNames.DISPLAY_VALUE] = gr.getElement(groupField).getDisplayValue();
          }
          groupMap[groupKey][this.ids].push(gr.getValue(queryField));
      }

      var resultList = [];
      for (var key in groupMap) {
          if (groupMap[key][this.ids].length < this.getThreshold()) {
              continue;
          }
          var ids = groupMap[key][this.ids];
          var groupDetails = this._getGroupDetails(ids);
          var groupData = {};
          groupData[RFConstants.evaluationOutputNames.OLDEST_CREATED_AT] = groupDetails[RFConstants.evaluationOutputNames.OLDEST_CREATED_AT];
          groupData[RFConstants.evaluationOutputNames.COUNT] = ids.length;
          if (this.sourceTable) {
              groupData[RFConstants.evaluationOutputNames.TREND_RECORD] = key;
          } else {
              groupData[RFConstants.evaluationOutputNames.DISPLAY_VALUE] = groupMap[key][RFConstants.evaluationOutputNames.DISPLAY_VALUE];
              groupData[RFConstants.evaluationOutputNames.VALUE] = key;
          }
          groupData[RFConstants.evaluationOutputNames.HIGHEST_CONFIDENCE_RECORD] = groupDetails[RFConstants.evaluationOutputNames.HIGHEST_CONFIDENCE_RECORD];
          groupData[RFConstants.evaluationOutputNames.QUALIFIED_RECORD_IDS] = JSON.stringify(ids);
          resultList.push(groupData);
      }
      resultList.sort(function(a, b) {
          return b.count - a.count;
      });
      this.trendData = resultList;
  },

  _getGroupDetails: function(ids) {
      //It returns the oldest record creation time and the highest ML confidence record in trend group
      var oldestCreatedAt = (new GlideDateTime()).toString();
      var highestConfidenceRecordIdx = this.recordSysIds.length;
      for (var i = 0; i < ids.length; i++) {
          var sysId = ids[i];
          if (this.recordsAgeMap[sysId] < oldestCreatedAt) {
              oldestCreatedAt = this.recordsAgeMap[sysId];
          }
          // Find index of the record in 'recordSysIds' list, having highest ML confidence among all of the records in 'ids' list
          if (this.recordSysIds.indexOf(sysId) < highestConfidenceRecordIdx) {
              highestConfidenceRecordIdx = this.recordSysIds.indexOf(sysId);
          }
      }
      var groupDetails = {};
      groupDetails[RFConstants.evaluationOutputNames.OLDEST_CREATED_AT] = oldestCreatedAt;
      groupDetails[RFConstants.evaluationOutputNames.HIGHEST_CONFIDENCE_RECORD] = this.recordSysIds[highestConfidenceRecordIdx];
      return groupDetails;
  },

  type: "RFTrendDefinitionService"
};

Sys ID

ecc89a05534330107234ddeeff7b1200

Offical Documentation

Official Docs: