SummaryQueryResult.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict';
  2. /*
  3. *+------------------------------------------------------------------------+
  4. *| Licensed Materials - Property of IBM
  5. *| IBM Cognos Products: BI Dashboard
  6. *| (C) Copyright IBM Corp. 2018, 2020
  7. *|
  8. *| US Government Users Restricted Rights - Use, duplication or disclosure
  9. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  10. *+------------------------------------------------------------------------+
  11. */
  12. /**
  13. * This Class offers functions to access query result data by keys.
  14. * @example query result from DSS: dashboard-analytics/widgets/livewidget/features/summary/mocks/SummaryQueryResult.json
  15. **/
  16. define(['../../../lib/@waca/core-client/js/core-client/ui/core/Class', 'underscore'], function (BaseClass, _) {
  17. 'use strict';
  18. var SummaryQueryResult = BaseClass.extend({
  19. init: function init(queryResult, logger) {
  20. this._aDataItems = null; // Array of result data items. Each element is an instance of ResultItem
  21. this._oDataItemsIndexMap = {}; // Data items map of a summary result for easy access to data items index
  22. this._oDataPointsMap = {}; // Data points map for easy access to data point by data items index
  23. this._queryResult = queryResult; // QueryResultData object
  24. if (this._queryResult.getRowCount() > 0) {
  25. this._buildDataItemsMap();
  26. this._buildDataPointsMap();
  27. }
  28. this.logger = logger;
  29. },
  30. /**
  31. * @override
  32. * @public
  33. * @function getValue
  34. * @param {string[]} categoryUIds - array of unique category data item ids to identify a summary query result
  35. * @param {string} measureUId - the unique id of the measure to get the summary
  36. * @param {string[]} tupleIds - array of unique tuple id to get a summary tuple cell
  37. * @returns the summary value by category keys (member unique names)
  38. */
  39. getValue: function getValue(categoryUIds, measureUId, tupleIds) {
  40. var _this = this;
  41. if (!categoryUIds || !tupleIds || this._queryResult.getRowCount() === 0) {
  42. return null;
  43. }
  44. var aCellIdx = []; // Indexes of category member unique ids
  45. var u;
  46. var validIdx = true;
  47. categoryUIds.forEach(function (categoryItemUid, idx) {
  48. u = tupleIds[idx]; // A category member unique id of the tuple
  49. var dataIdx = _this._oDataItemsIndexMap[categoryItemUid][u];
  50. if (dataIdx === undefined) {
  51. validIdx = false;
  52. _this.logger.error('Could not find index for: ' + u);
  53. return;
  54. }
  55. aCellIdx.push(dataIdx);
  56. });
  57. if (!validIdx) {
  58. return null;
  59. }
  60. var rowIdx = aCellIdx.length ? this._oDataPointsMap[aCellIdx.join()] : 0;
  61. var measureIdx = this._oDataItemsIndexMap[measureUId]; // Column index in the result
  62. return this._queryResult.getValue(rowIdx, measureIdx).value;
  63. },
  64. /**
  65. * Build data items map based on item unique IDs and member unique IDs, and store the index of each member of the data item.
  66. * The indexes will be used to get data point value of the summary query result.
  67. * @example this._oDataItemsIndexMap:
  68. * {
  69. 'model0000016393c2398a_00000001': {
  70. 'sales_and_marketing_csv.Year_->[2005]': 0,
  71. 'sales_and_marketing_csv.Year_->[2006]': 1
  72. },
  73. 'model00000163b272e450_00000000': 1,
  74. 'model0000016393c25183_00000000': {
  75. 'sales_and_marketing_csv.Product_line->[Personal Accessories]': 0,
  76. 'sales_and_marketing_csv.Product_line->[Camping Equipment]': 1
  77. },
  78. 'model0000016393cba0a9_00000003': {
  79. 'sales_and_marketing_csv.Product_type->[Eyewear]': 0,
  80. 'sales_and_marketing_csv.Product_type->[Cooking Gear]': 1
  81. },
  82. 'model0000016393cc8a97_00000000': {
  83. 'sales_and_marketing_csv.Order_method_type->[Web]': 0
  84. },
  85. 'model0000016393c2744a_00000000': 5
  86. }
  87. */
  88. _buildDataItemsMap: function _buildDataItemsMap() {
  89. var _this2 = this;
  90. this._aDataItems = this._aDataItems || this._queryResult.getResultItemList();
  91. this._aDataItems.forEach(function (resultDataItem, itemIdx) {
  92. var itemUID = resultDataItem.getId();
  93. var rowCount = resultDataItem.getRowCount();
  94. if (rowCount !== 0) {
  95. _this2._oDataItemsIndexMap[itemUID] = {};
  96. for (var rowIdx = 0; rowIdx < rowCount; rowIdx++) {
  97. _this2._oDataItemsIndexMap[itemUID][resultDataItem.getValue(rowIdx)[0].value] = rowIdx;
  98. }
  99. } else if (resultDataItem.getDataItemList()[0].getAggregation()) {
  100. // When there is no tuple and has aggregate, it's a measure. Store the index
  101. // to be used later to get data point value of the measure.
  102. _this2._oDataItemsIndexMap[itemUID] = itemIdx;
  103. }
  104. });
  105. },
  106. /**
  107. * Build data points map based on item indexes to provide quick access to data point rows
  108. * @example this._oDataPointsMap
  109. * {
  110. '0,0,0,0': 0,
  111. '1,1,1,0': 1,
  112. '1,0,0,0': 2
  113. }
  114. */
  115. _buildDataPointsMap: function _buildDataPointsMap() {
  116. var _this3 = this;
  117. var dataPoints = this._queryResult.getDataPointValueList();
  118. dataPoints.forEach(function (dataPoint, idx) {
  119. /**
  120. * @example dataPoint: the integers corresponding to category member indexes,
  121. * and the value object corresponding to measure values, such as avg of Quantity and avg of Revenue.
  122. * {
  123. 'pt': [
  124. 1,
  125. {
  126. 'v': 16393.466666666667
  127. },
  128. 1,
  129. 1,
  130. 0,
  131. {
  132. 'v': 338947.1332380951
  133. }
  134. ]
  135. }
  136. */
  137. var categoryMemberIndexes = _.filter(dataPoint.pt, function (entry) {
  138. return typeof entry === 'number';
  139. });
  140. _this3._oDataPointsMap[categoryMemberIndexes.join()] = idx;
  141. });
  142. }
  143. });
  144. return SummaryQueryResult;
  145. });
  146. //# sourceMappingURL=SummaryQueryResult.js.map