Name

sn_sd.ContentTreeUtil

Description

The script include is used to create content tree, type of indicators, name of indicators, drilldown level of indicators.

Script

var ContentTreeUtil = Class.create();

ContentTreeUtil.prototype = {
  initialize: function() {
      this.input = {};
      this.alreadyVisitedNodes = [];
      this.SD_DETAILS_READ_ROLE = "sn_sd.success_dashboard_details_read";
      this.PRIMARY_INDICATOR_TABLE = "sn_sd_primary_indicator";
      this.CONTRIBUTING_INDICATOR_REGISTRY_TABLE = "sn_sd_contributing_indicator_registry";
      this.type = '';
      this.count = 0;
  },

  // ! - The function checks if the node has already been visited in the N-Ary tree (cycle & duplicate detetction)
  duplicateDetected: function(kpiSysId) {
      if (this.alreadyVisitedNodes.indexOf(kpiSysId) === -1) {
          this.alreadyVisitedNodes.push(kpiSysId);
          return false;
      } else {
          return true;
      }
  },

  // ! - Create Leaf nodes and leaf-1 nodes
  getLeafNodesFromContributingIndicators: function(kpiSysId, tree, level) {
      var grPrimaryIndicator, grContributingIndicator;
      var objSDHelperUtils = new sn_sd.SDHelperUtils();
      var curNode = {
          id: kpiSysId,
          label: objSDHelperUtils.getIndicatorLabelPrimary(kpiSysId),
          level: level,
          index: this.count++,
          children: []
      };

      if (!this.duplicateDetected(kpiSysId)) {
          if (gs.hasRole(this.SD_DETAILS_READ_ROLE) && this.type == "count" && this.input.playbook.toLowerCase() == "false") {
              grPrimaryIndicator = new GlideRecord(this.PRIMARY_INDICATOR_TABLE);
              grPrimaryIndicator.addQuery("indicator", kpiSysId);
              grPrimaryIndicator.query();

              if (grPrimaryIndicator.next()) {
                  grContributingIndicator = new GlideRecord(this.CONTRIBUTING_INDICATOR_REGISTRY_TABLE);
                  grContributingIndicator.addQuery("primary_kpi", grPrimaryIndicator.primary_indicator);
                  grContributingIndicator.addQuery("active", true);
                  if (this.input.buValue != "" && this.input.buValue != null && this.input.buValue != undefined && this.input.buValue != "All") {
                      grContributingIndicator.addQuery("service_group", this.input.buValue);
                  }
                  grContributingIndicator.orderBy("order");
                  grContributingIndicator.query();

                  while (grContributingIndicator.next()) {
                      curNode.children.push({
                          id: grContributingIndicator.contributing_indicator.toString(),
                          label: grContributingIndicator.name.getDisplayValue(),
                          level: level + 1,
                          index: this.count++
                      });
                  }
              }
          }

          tree.push(curNode);
      }
  },

  // ! - Create all the nodes except leaf & leaf-1
  createNAryTree: function(kpiSysId, tree, level) {
      var curNode = {};
      var paformula, showDenominator;
      var objSDHelperUtils = new sn_sd.SDHelperUtils();

      var grPrimaryIndicator = new GlideRecord(this.PRIMARY_INDICATOR_TABLE);
      grPrimaryIndicator.addQuery("indicator", kpiSysId);
      grPrimaryIndicator.query();

      if (grPrimaryIndicator.next()) {
          paformula = grPrimaryIndicator.indicator.formula;
          showDenominator = grPrimaryIndicator.show_denominator_in_ui ? grPrimaryIndicator.show_denominator_in_ui : null;

          if (grPrimaryIndicator.primary_indicator.drilldown_type == "registered indicators") {
              this.getLeafNodesFromContributingIndicators(kpiSysId, tree, level);
          } else if (grPrimaryIndicator.primary_indicator.drilldown_type == "formula numerators" && paformula != "" && !this.duplicateDetected(kpiSysId)) {
              if (level !== 0) {
                  curNode = {
                      id: kpiSysId,
                      label: objSDHelperUtils.getIndicatorLabelPrimary(kpiSysId),
                      unit: grPrimaryIndicator.indicator.unit.getDisplayValue(),
                      level: level,
                      index: this.count++,
                      children: []
                  };
              } else {
                  if (grPrimaryIndicator.indicator.unit.getDisplayValue() == "%") {
                      curNode = {
                          id: kpiSysId,
                          label: objSDHelperUtils.getIndicatorLabelPrimary(kpiSysId),
                          unit: grPrimaryIndicator.indicator.unit.getDisplayValue(),
                          level: level,
                          index: this.count++,
                          children: [],
                          skipFlag: true
                      };
                  } else {
                      curNode = {
                          id: kpiSysId,
                          label: objSDHelperUtils.getIndicatorLabelPrimary(kpiSysId),
                          unit: grPrimaryIndicator.indicator.unit.getDisplayValue(),
                          level: level,
                          index: this.count++,
                          children: []
                      };
                  }
              }

              var pattern = /[0-9a-f]{32}/g;
              var formulaSysIds = {
                  numerator: paformula.split("/")[0].match(pattern),
                  denominator: showDenominator && paformula.split("/")[1] ? paformula.split("/")[1].match(pattern) : []
              };

              var self = this;
              formulaSysIds.numerator.forEach(function(eachNumerator) {
                  self.createNAryTree(eachNumerator, curNode.children, level + 1);
              });
              formulaSysIds.denominator.forEach(function(eachDenominator) {
                  self.createNAryTree(eachDenominator, curNode.children, level + 1);
              });

              tree.push(curNode);

          } else {
              this.getLeafNodesFromContributingIndicators(kpiSysId, tree, level);
          }
      }
  },

  // ! - The function creates the count Content Tree and return the object
  createCountContentTree: function(input) {
      var result = [];
      var content_tree = [];
      var drilldown_view = "";
      var sysId = input.uuid;
      this.input = input;
      var objSDHelperUtils = new sn_sd.SDHelperUtils();
      var typeOfIndicator = objSDHelperUtils.getIndicatorType(sysId);

      if (sysId !== "") {
          this.type = typeOfIndicator;
          this.createNAryTree(sysId, content_tree, 0);
      }

      drilldown_view = content_tree.length === 0 ? null : objSDHelperUtils.getIndicatorDrilldownLevel(input.selectedId);

      result.push({
          type: typeOfIndicator,
          content_tree: content_tree[0] && content_tree[0].skipFlag ? content_tree[0].children : content_tree,
          details_role: !gs.hasRole(this.SD_DETAILS_READ_ROLE),
          drilldown_view: drilldown_view
      });

      return result;
  },

  type: 'ContentTreeUtil'
};

Sys ID

468395b243b061107f9fbbfd6bb8f22b

Offical Documentation

Official Docs: