RankProcessor.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. 'use strict';
  2. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. /*
  5. *+------------------------------------------------------------------------+
  6. *| Licensed Materials - Property of IBM
  7. *| IBM Cognos Products: BI Dashboard
  8. *| (C) Copyright IBM Corp. 2019
  9. *|
  10. *| US Government Users Restricted Rights - Use, duplication or disclosure
  11. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  12. *+------------------------------------------------------------------------+
  13. */
  14. define(['underscore'], function (_) {
  15. 'use strict';
  16. /**
  17. * This Class processes query response results and extracts the top-bottom rank to a separate result data
  18. * Used by JQGrid.js during rendering process
  19. */
  20. var RankProcessor = function () {
  21. /**
  22. * @Constructor
  23. * @param {Object} options
  24. */
  25. function RankProcessor(queryResult, findSlotDetailsByRealColumnIndex) {
  26. _classCallCheck(this, RankProcessor);
  27. this._queryResult = queryResult;
  28. this.dataRows = this._queryResult.getDataPointValueList();
  29. this.dataItems = this._queryResult.getResultItemList();
  30. this.findSlotDetailsByRealColumnIndex = findSlotDetailsByRealColumnIndex;
  31. // Array of item ids that are being referenced by rank columns
  32. this.rankRefIds = [];
  33. this.rankDataItems = [];
  34. this._prepareRankDataItems();
  35. }
  36. /**
  37. * Called during initialization. Looks at the original data
  38. * and detects the rank columns. For each rank column, generates a
  39. * mock rankDataItem which will later be used while rendering the JQGrid.
  40. */
  41. RankProcessor.prototype._prepareRankDataItems = function _prepareRankDataItems() {
  42. var _this = this;
  43. // Look at first row to determine if each column is a rank column
  44. this.dataRows[0] && this.dataRows[0].pt.forEach(function (colData, colIndex) {
  45. if (_this._getRankValue(colData, colIndex)) {
  46. _this.rankRefIds.push(_this.dataItems[colIndex].getId());
  47. var _findSlotDetailsByRea = _this.findSlotDetailsByRealColumnIndex(colIndex),
  48. slot = _findSlotDetailsByRea.slot,
  49. mapIndex = _findSlotDetailsByRea.mapIndex;
  50. var rankDataItemOpt = {
  51. refColumnIndex: colIndex,
  52. resultDataItem: _this.dataItems[colIndex],
  53. aggregationType: slot.getDataItemList()[mapIndex].getAggregation()
  54. };
  55. _this.rankDataItems.push(new RankDataItem(rankDataItemOpt));
  56. }
  57. });
  58. };
  59. /**
  60. * Process Query result based on the passed options to constructor.
  61. * @return {Array} rankDataItems
  62. */
  63. RankProcessor.prototype.processData = function processData() {
  64. if (this.dataRows.length) {
  65. this._processDataRows();
  66. this._processDataItems();
  67. }
  68. return this.rankDataItems;
  69. };
  70. RankProcessor.prototype._processDataRows = function _processDataRows() {
  71. _.each(this.dataRows, function (dataRow) {
  72. this._processDataPoint(dataRow.pt);
  73. }.bind(this));
  74. };
  75. RankProcessor.prototype._processDataPoint = function _processDataPoint(rowData) {
  76. var ranks = [];
  77. for (var col = 0; col < rowData.length; col++) {
  78. var colData = rowData[col];
  79. var rank = this._getRankValue(colData, col);
  80. if (rank > 0) {
  81. // obtain the reference unique id from the dataitems
  82. var ref = this.dataItems[col].getId();
  83. // order of the rank is determined by the order of the references mapped in the rank slot
  84. var rankIndex = this.rankRefIds.indexOf(ref);
  85. // collect the ranks from each decoration
  86. ranks[rankIndex] = {
  87. v: rank
  88. };
  89. }
  90. }
  91. // prepare the parameters for splice
  92. ranks.splice(0, 0, 0, 0);
  93. // prepend all collected ranks to the row
  94. Array.prototype.splice.apply(rowData, ranks);
  95. };
  96. RankProcessor.prototype._processDataItems = function _processDataItems() {
  97. for (var index = 0; index < this.rankRefIds.length; index++) {
  98. // append an empty place holder for the ranks append to the datapoints
  99. var rankedDataItem = _.find(this.dataItems, function (dataItem) {
  100. return dataItem.getId() === this.rankRefIds[index];
  101. }.bind(this));
  102. this.dataItems.splice(index, 0, rankedDataItem);
  103. }
  104. };
  105. /**
  106. * Tries to read the rank value from column data (i.e. each entry inside dataRow.pt)
  107. * If finds, returns the rank value, if not returns 0
  108. *
  109. * @param {*} colData
  110. * @param {*} colIndex
  111. * @return {Number} rank value from the data point
  112. */
  113. RankProcessor.prototype._getRankValue = function _getRankValue(colData, colIndex) {
  114. var rankValue = 0;
  115. if ((typeof colData === 'undefined' ? 'undefined' : _typeof(colData)) === 'object') {
  116. // continuous data with rank decoration
  117. if (colData.deco && colData.deco.rank) {
  118. rankValue = colData.deco.rank;
  119. }
  120. } else {
  121. // reference the categorical data items to obtain the rank decoration
  122. var tupleItem = this.dataItems[colIndex].getValue(colData)[0];
  123. if (tupleItem.deco && tupleItem.deco.rank) {
  124. rankValue = tupleItem.deco.rank;
  125. }
  126. }
  127. return rankValue;
  128. };
  129. return RankProcessor;
  130. }();
  131. var RankDataItem = function () {
  132. /**
  133. * @Constructor
  134. * @param {Object} options
  135. */
  136. function RankDataItem(options) {
  137. _classCallCheck(this, RankDataItem);
  138. this._refColumnIndex = options.refColumnIndex;
  139. this._resultDataItem = options.resultDataItem;
  140. this._aggregationType = options.aggregationType;
  141. }
  142. RankDataItem.prototype.isRank = function isRank() {
  143. return true;
  144. };
  145. RankDataItem.prototype.getType = function getType() {
  146. return 'fact';
  147. };
  148. RankDataItem.prototype.getLabel = function getLabel() {
  149. // There should be only one column
  150. return this._resultDataItem.getDataItemList()[0].getLabel();
  151. };
  152. RankDataItem.prototype.getId = function getId() {
  153. return this._resultDataItem.getId();
  154. };
  155. RankDataItem.prototype.getAggregation = function getAggregation() {
  156. return this._aggregationType;
  157. };
  158. RankDataItem.prototype.getRefColumnIndex = function getRefColumnIndex() {
  159. return this._refColumnIndex;
  160. };
  161. return RankDataItem;
  162. }();
  163. return RankProcessor;
  164. });
  165. //# sourceMappingURL=RankProcessor.js.map