GroupedRows.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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: BI Dashboard
  7. *| (C) Copyright IBM Corp. 2019
  8. *|
  9. *| US Government Users Restricted Rights - Use, duplication or disclosure
  10. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  11. *+------------------------------------------------------------------------+
  12. */
  13. define(['underscore'], function (_) {
  14. var GroupedRows = function () {
  15. /**
  16. * @param {Object} groupIndex A category index to group members by.
  17. * @param {Object} aggregateIndex Index of measure to aggregate on.
  18. */
  19. function GroupedRows(groupIndex, aggregateIndex) {
  20. _classCallCheck(this, GroupedRows);
  21. this.groupIndex = groupIndex; // The grouped column index to the data item array.
  22. this.aggregateIndex = aggregateIndex; // The index of the aggregate column in the data item array.
  23. this.bins = {};
  24. this.binKeys = [];
  25. }
  26. /**
  27. * Extracts from a single row the group attribute members
  28. * - this represents a single 'bin' to aggregate values by.
  29. * @param {Object} row A data point row from the query result data.
  30. * @return {Integer} A key which represent the group attribute members (index into the data item array).
  31. * Example: groupIndex = [0]
  32. * row.pt = [1, { v: 1234 }, 0]
  33. * _rowToKey = 1
  34. */
  35. GroupedRows.prototype._rowToKey = function _rowToKey(row) {
  36. return row.pt[this.groupIndex];
  37. };
  38. /**
  39. * Gets the bin for the supplied key.
  40. * If the bin doesn't already exist it will be created.
  41. * Example: key [1] and bins did not exist
  42. * bins (after creating them) = { 1: { rows: [], aggregate: 0 } }
  43. * returns { rows: [], aggregate: 0 }
  44. * @param {Object} key An index into the map of bins.
  45. * @return {Object} The bin for the given key.
  46. */
  47. GroupedRows.prototype._getBin = function _getBin(key) {
  48. var bin = this.bins;
  49. if (!bin[key]) {
  50. bin[key] = {};
  51. }
  52. bin = bin[key];
  53. if (!bin.rows) {
  54. // Initialize bin
  55. bin.rows = []; // Track all rows in this bin
  56. bin.aggregate = 0; // Keep a running aggregate for this bin
  57. this.binKeys.push(key); // Ensure the bin key is recorded
  58. }
  59. return bin;
  60. };
  61. /**
  62. * Add row to the grouped bin and update the aggregate for the bin.
  63. * @param {Object} bin A grouping of data point rows to aggregate on.
  64. * @param {Object} row A data point row from the query result data.
  65. */
  66. GroupedRows.prototype._aggregate = function _aggregate(bin, row) {
  67. var aggregate = row.pt[this.aggregateIndex],
  68. aggregateValue = aggregate && aggregate.v;
  69. aggregateValue = aggregateValue === null || isNaN(aggregateValue) ? 0 : Number(aggregateValue);
  70. bin.aggregate += aggregateValue;
  71. bin.rows.push(row);
  72. };
  73. /**
  74. * Adds a query result data point to the grouped rows.
  75. * This will put the item into the appropriated grouped bin and update the aggregrate of the bin based on the new row.
  76. * @param {Object} row A data point row from the query result data.
  77. */
  78. GroupedRows.prototype.addRow = function addRow(row) {
  79. var key = this._rowToKey(row);
  80. var bin = this._getBin(key);
  81. this._aggregate(bin, row);
  82. };
  83. /**
  84. * Sort the keys for the grouped bins using the aggregateIteratee function.
  85. * @param {function} aggregateIteratee Function used to compare aggregate bins during sort.
  86. */
  87. GroupedRows.prototype.sort = function sort(aggregateIteratee) {
  88. var _this = this;
  89. this.binKeys = _.sortBy(this.binKeys, function (key) {
  90. var bin = _this._getBin(key);
  91. return aggregateIteratee(bin.aggregate);
  92. });
  93. };
  94. /**
  95. * Re-Index the bin row/datapoint to the new value at the index.
  96. * Used to align the data row to the appropriate grouped data item it references.
  97. * @param {Object} bin A grouping of data point rows.
  98. * @param {Object} index The attribute member index to re-index.
  99. * @param {Object} value The new index value of the attribute member.
  100. */
  101. GroupedRows.prototype._reIndex = function _reIndex(bin, index, value) {
  102. bin.rows.forEach(function (row) {
  103. row.pt[index] = value;
  104. });
  105. };
  106. /**
  107. * Flatten the ordered grouped bins of rows into one array of data points.
  108. * The grouped data items for each binned row will also be updated.
  109. */
  110. GroupedRows.prototype.flatten = function flatten() {
  111. var _this2 = this;
  112. return _.chain(this.binKeys).map(function (key, index) {
  113. var bin = _this2._getBin(key);
  114. _this2._reIndex(bin, _this2.groupIndex, index);
  115. return bin.rows;
  116. }).flatten().value();
  117. };
  118. return GroupedRows;
  119. }();
  120. return GroupedRows;
  121. });
  122. //# sourceMappingURL=GroupedRows.js.map