QueryResultDataUtils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /*
  4. *+------------------------------------------------------------------------+
  5. *| Licensed Materials - Property of IBM
  6. *| IBM Cognos Products: Dashboard
  7. *| (C) Copyright IBM Corp. 2017, 2020
  8. *|
  9. *| US Government Users Restricted Rights - Use, duplication or disclosure
  10. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  11. *+------------------------------------------------------------------------+
  12. */
  13. /**
  14. * This Utils Class processes Data Item from Query Result
  15. **/
  16. define(['underscore'], function (_) {
  17. var Utils = function () {
  18. function Utils() {
  19. _classCallCheck(this, Utils);
  20. }
  21. /**
  22. * Normalize a dataItem to make sure it has a complete format of {d:xxx, u:xxx, p:{d:xxx, u:xxxx}} (if p is present)
  23. * @param {QueryResultDataItem} dataItem
  24. */
  25. Utils.normalizeData = function normalizeData(dataItem) {
  26. this._normalizeTuplePart(dataItem);
  27. if (dataItem && dataItem.p !== undefined) {
  28. this._normalizeTuplePart(dataItem.p);
  29. }
  30. };
  31. Utils.isRectifyNeeded = function isRectifyNeeded(tuplePart) {
  32. return tuplePart === null; //Augment this if necessary
  33. };
  34. /**
  35. * Only create a JS Object if the tuple part is returned not as an Object
  36. */
  37. Utils.getRectifiedData = function getRectifiedData(tuplePart) {
  38. return {
  39. 'u': tuplePart
  40. };
  41. };
  42. /**
  43. * @param @param {Object} part is normalized only if it is an Object
  44. */
  45. Utils._normalizeTuplePart = function _normalizeTuplePart(part) {
  46. if (_.isObject(part)) {
  47. //TODO: once the queryResult Api is fully hooked up, we should clean here using dataCell.value only
  48. if (!part.hasOwnProperty('d')) {
  49. part.d = part.hasOwnProperty('u') ? part.u : part.hasOwnProperty('v') ? part.v : part.hasOwnProperty('value') ? part.value : part;
  50. }
  51. if (!part.hasOwnProperty('u')) {
  52. part.u = part.hasOwnProperty('d') ? part.d : part.hasOwnProperty('v') ? part.v : part.hasOwnProperty('value') ? part.value : part;
  53. }
  54. }
  55. };
  56. /**
  57. * TODO Need to review whether to expose this in QueryResultAPI or keep it here.
  58. * @return two dimensions array that stores the resolved values from query result data data points
  59. */
  60. Utils.getResolvedDataRows = function getResolvedDataRows(resultAPI) {
  61. var itemList = resultAPI.getResultItemList();
  62. var aResult = [];
  63. for (var i = 0; i < resultAPI.getRowCount(); i++) {
  64. aResult[i] = [];
  65. for (var j = 0; j < itemList.length; j++) {
  66. aResult[i].push(resultAPI.getValue(i, j));
  67. }
  68. }
  69. return aResult;
  70. };
  71. Utils.getDataItemIndex = function getDataItemIndex(queryResultData, dataItemRefID) {
  72. var nIndex = -1;
  73. if (queryResultData) {
  74. _.find(queryResultData.dataItems, function (dataItem, index) {
  75. var id = dataItem.itemClass && dataItem.itemClass['id'];
  76. if (id === dataItemRefID) {
  77. nIndex = index;
  78. return true;
  79. }
  80. });
  81. }
  82. return nIndex;
  83. };
  84. /**
  85. * Returns total number of data items
  86. *
  87. * @param resultItemList
  88. * @return Number - the dataitem count in all resultItems
  89. */
  90. Utils.getDataItemCount = function getDataItemCount(resultItemList) {
  91. return resultItemList.reduce(function (accumulator, currentValue) {
  92. return accumulator + currentValue.getDataItemList().length;
  93. }, 0);
  94. };
  95. /**
  96. * Returns array of data items
  97. *
  98. * @param resultItemList
  99. * @return array - the data items returned from query result
  100. */
  101. Utils.getResultDataItemList = function getResultDataItemList(resultItemList) {
  102. var dataItemList = [];
  103. resultItemList.forEach(function (item) {
  104. dataItemList.push.apply(dataItemList, item.getDataItemList());
  105. });
  106. return dataItemList;
  107. };
  108. return Utils;
  109. }();
  110. return Utils;
  111. });
  112. //# sourceMappingURL=QueryResultDataUtils.js.map