heatmap_data_converter.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. import _ from 'lodash';
  2. let VALUE_INDEX = 0;
  3. let TIME_INDEX = 1;
  4. interface XBucket {
  5. x: number;
  6. buckets: any;
  7. }
  8. interface YBucket {
  9. y: number;
  10. values: number[];
  11. }
  12. function elasticHistogramToHeatmap(seriesList) {
  13. let heatmap = {};
  14. for (let series of seriesList) {
  15. let bound = Number(series.alias);
  16. if (isNaN(bound)) {
  17. return heatmap;
  18. }
  19. for (let point of series.datapoints) {
  20. let count = point[VALUE_INDEX];
  21. let time = point[TIME_INDEX];
  22. if (!_.isNumber(count)) {
  23. continue;
  24. }
  25. let bucket = heatmap[time];
  26. if (!bucket) {
  27. bucket = heatmap[time] = {x: time, buckets: {}};
  28. }
  29. bucket.buckets[bound] = {y: bound, count: count, values: [], points: []};
  30. }
  31. }
  32. return heatmap;
  33. }
  34. /**
  35. * Convert buckets into linear array of "cards" - objects, represented heatmap elements.
  36. * @param {Object} buckets
  37. * @return {Array} Array of "card" objects
  38. */
  39. function convertToCards(buckets) {
  40. let min = 0, max = 0;
  41. let cards = [];
  42. _.forEach(buckets, xBucket => {
  43. _.forEach(xBucket.buckets, yBucket=> {
  44. let card = {
  45. x: xBucket.x,
  46. y: yBucket.y,
  47. yBounds: yBucket.bounds,
  48. values: yBucket.values,
  49. count: yBucket.count,
  50. };
  51. cards.push(card);
  52. if (cards.length === 1) {
  53. min = yBucket.count;
  54. max = yBucket.count;
  55. }
  56. min = yBucket.count < min ? yBucket.count : min;
  57. max = yBucket.count > max ? yBucket.count : max;
  58. });
  59. });
  60. let cardStats = {min, max};
  61. return {cards, cardStats};
  62. }
  63. /**
  64. * Special method for log scales. When series converted into buckets with log scale,
  65. * for simplification, 0 values are converted into 0, not into -Infinity. On the other hand, we mean
  66. * that all values less than series minimum, is 0 values, and we create special "minimum" bucket for
  67. * that values (actually, there're no values less than minimum, so this bucket is empty).
  68. * 8-16| | ** | | * | **|
  69. * 4-8| * |* *|* |** *| * |
  70. * 2-4| * *| | ***| |* |
  71. * 1-2|* | | | | | This bucket contains minimum series value
  72. * 0.5-1|____|____|____|____|____| This bucket should be displayed as 0 on graph
  73. * 0|____|____|____|____|____| This bucket is for 0 values (should actually be -Infinity)
  74. * So we should merge two bottom buckets into one (0-value bucket).
  75. *
  76. * @param {Object} buckets Heatmap buckets
  77. * @param {Number} minValue Minimum series value
  78. * @return {Object} Transformed buckets
  79. */
  80. function mergeZeroBuckets(buckets, minValue) {
  81. _.forEach(buckets, xBucket => {
  82. let yBuckets = xBucket.buckets;
  83. let emptyBucket = {
  84. bounds: {bottom: 0, top: 0},
  85. values: [],
  86. points: [],
  87. count: 0,
  88. };
  89. let nullBucket = yBuckets[0] || emptyBucket;
  90. let minBucket = yBuckets[minValue] || emptyBucket;
  91. let newBucket = {
  92. y: 0,
  93. bounds: {bottom: minValue, top: minBucket.bounds.top || minValue},
  94. values: [],
  95. points: [],
  96. count: 0,
  97. };
  98. newBucket.points = nullBucket.points.concat(minBucket.points);
  99. newBucket.values = nullBucket.values.concat(minBucket.values);
  100. newBucket.count = newBucket.values.length;
  101. if (newBucket.count === 0) {
  102. return;
  103. }
  104. delete yBuckets[minValue];
  105. yBuckets[0] = newBucket;
  106. });
  107. return buckets;
  108. }
  109. /**
  110. * Convert set of time series into heatmap buckets
  111. * @return {Object} Heatmap object:
  112. * {
  113. * xBucketBound_1: {
  114. * x: xBucketBound_1,
  115. * buckets: {
  116. * yBucketBound_1: {
  117. * y: yBucketBound_1,
  118. * bounds: {bottom, top}
  119. * values: [val_1, val_2, ..., val_K],
  120. * points: [[val_Y, val_X, series_name], ..., [...]],
  121. * seriesStat: {seriesName_1: val_1, seriesName_2: val_2}
  122. * },
  123. * ...
  124. * yBucketBound_M: {}
  125. * },
  126. * values: [val_1, val_2, ..., val_K],
  127. * points: [
  128. * [val_Y, val_X, series_name], (point_1)
  129. * ...
  130. * [...] (point_K)
  131. * ]
  132. * },
  133. * xBucketBound_2: {},
  134. * ...
  135. * xBucketBound_N: {}
  136. * }
  137. */
  138. function convertToHeatMap(seriesList, yBucketSize, xBucketSize, logBase = 1) {
  139. let heatmap = {};
  140. for (let series of seriesList) {
  141. let datapoints = series.datapoints;
  142. let seriesName = series.label;
  143. // Slice series into X axis buckets
  144. // | | ** | | * | **|
  145. // | * |* *|* |** *| * |
  146. // |** *| | ***| |* |
  147. // |____|____|____|____|____|_
  148. //
  149. _.forEach(datapoints, point => {
  150. let bucketBound = getBucketBound(point[TIME_INDEX], xBucketSize);
  151. pushToXBuckets(heatmap, point, bucketBound, seriesName);
  152. });
  153. }
  154. // Slice X axis buckets into Y (value) buckets
  155. // | **| |2|,
  156. // | * | --\ |1|,
  157. // |* | --/ |1|,
  158. // |____| |0|
  159. //
  160. _.forEach(heatmap, xBucket => {
  161. if (logBase !== 1) {
  162. xBucket.buckets = convertToLogScaleValueBuckets(xBucket, yBucketSize, logBase);
  163. } else {
  164. xBucket.buckets = convertToValueBuckets(xBucket, yBucketSize);
  165. }
  166. });
  167. return heatmap;
  168. }
  169. function pushToXBuckets(buckets, point, bucketNum, seriesName) {
  170. let value = point[VALUE_INDEX];
  171. if (value === null || value === undefined || isNaN(value)) { return; }
  172. // Add series name to point for future identification
  173. let point_ext = _.concat(point, seriesName);
  174. if (buckets[bucketNum] && buckets[bucketNum].values) {
  175. buckets[bucketNum].values.push(value);
  176. buckets[bucketNum].points.push(point_ext);
  177. } else {
  178. buckets[bucketNum] = {
  179. x: bucketNum,
  180. values: [value],
  181. points: [point_ext]
  182. };
  183. }
  184. }
  185. function pushToYBuckets(buckets, bucketNum, value, point, bounds) {
  186. var count = 1;
  187. // Use the 3rd argument as scale/count
  188. if (point.length > 3) {
  189. count = parseInt(point[2]);
  190. }
  191. if (buckets[bucketNum]) {
  192. buckets[bucketNum].values.push(value);
  193. buckets[bucketNum].points.push(point);
  194. buckets[bucketNum].count += count;
  195. } else {
  196. buckets[bucketNum] = {
  197. y: bucketNum,
  198. bounds: bounds,
  199. values: [value],
  200. points: [point],
  201. count: count,
  202. };
  203. }
  204. }
  205. function getValueBucketBound(value, yBucketSize, logBase) {
  206. if (logBase === 1) {
  207. return getBucketBound(value, yBucketSize);
  208. } else {
  209. return getLogScaleBucketBound(value, yBucketSize, logBase);
  210. }
  211. }
  212. /**
  213. * Find bucket for given value (for linear scale)
  214. */
  215. function getBucketBounds(value, bucketSize) {
  216. let bottom, top;
  217. bottom = Math.floor(value / bucketSize) * bucketSize;
  218. top = (Math.floor(value / bucketSize) + 1) * bucketSize;
  219. return {bottom, top};
  220. }
  221. function getBucketBound(value, bucketSize) {
  222. let bounds = getBucketBounds(value, bucketSize);
  223. return bounds.bottom;
  224. }
  225. function convertToValueBuckets(xBucket, bucketSize) {
  226. let values = xBucket.values;
  227. let points = xBucket.points;
  228. let buckets = {};
  229. _.forEach(values, (val, index) => {
  230. let bounds = getBucketBounds(val, bucketSize);
  231. let bucketNum = bounds.bottom;
  232. pushToYBuckets(buckets, bucketNum, val, points[index], bounds);
  233. });
  234. return buckets;
  235. }
  236. /**
  237. * Find bucket for given value (for log scales)
  238. */
  239. function getLogScaleBucketBounds(value, yBucketSplitFactor, logBase) {
  240. let top, bottom;
  241. if (value === 0) {
  242. return {bottom: 0, top: 0};
  243. }
  244. let value_log = logp(value, logBase);
  245. let pow, powTop;
  246. if (yBucketSplitFactor === 1 || !yBucketSplitFactor) {
  247. pow = Math.floor(value_log);
  248. powTop = pow + 1;
  249. } else {
  250. let additional_bucket_size = 1 / yBucketSplitFactor;
  251. let additional_log = value_log - Math.floor(value_log);
  252. additional_log = Math.floor(additional_log / additional_bucket_size) * additional_bucket_size;
  253. pow = Math.floor(value_log) + additional_log;
  254. powTop = pow + additional_bucket_size;
  255. }
  256. bottom = Math.pow(logBase, pow);
  257. top = Math.pow(logBase, powTop);
  258. return {bottom, top};
  259. }
  260. function getLogScaleBucketBound(value, yBucketSplitFactor, logBase) {
  261. let bounds = getLogScaleBucketBounds(value, yBucketSplitFactor, logBase);
  262. return bounds.bottom;
  263. }
  264. function convertToLogScaleValueBuckets(xBucket, yBucketSplitFactor, logBase) {
  265. let values = xBucket.values;
  266. let points = xBucket.points;
  267. let buckets = {};
  268. _.forEach(values, (val, index) => {
  269. let bounds = getLogScaleBucketBounds(val, yBucketSplitFactor, logBase);
  270. let bucketNum = bounds.bottom;
  271. pushToYBuckets(buckets, bucketNum, val, points[index], bounds);
  272. });
  273. return buckets;
  274. }
  275. /**
  276. * Logarithm for custom base
  277. * @param value
  278. * @param base logarithm base
  279. */
  280. function logp(value, base) {
  281. return Math.log(value) / Math.log(base);
  282. }
  283. /**
  284. * Calculate size of Y bucket from given buckets bounds.
  285. * @param bounds Array of Y buckets bounds
  286. * @param logBase Logarithm base
  287. */
  288. function calculateBucketSize(bounds: number[], logBase = 1): number {
  289. let bucketSize = Infinity;
  290. if (bounds.length === 0) {
  291. return 0;
  292. } else if (bounds.length === 1) {
  293. return bounds[0];
  294. } else {
  295. bounds = _.sortBy(bounds);
  296. for (let i = 1; i < bounds.length; i++) {
  297. let distance = getDistance(bounds[i], bounds[i - 1], logBase);
  298. bucketSize = distance < bucketSize ? distance : bucketSize;
  299. }
  300. }
  301. return bucketSize;
  302. }
  303. /**
  304. * Calculate distance between two numbers in given scale (linear or logarithmic).
  305. * @param a
  306. * @param b
  307. * @param logBase
  308. */
  309. function getDistance(a: number, b: number, logBase = 1): number {
  310. if (logBase === 1) {
  311. // Linear distance
  312. return Math.abs(b - a);
  313. } else {
  314. // logarithmic distance
  315. let ratio = Math.max(a, b) / Math.min(a, b);
  316. return logp(ratio, logBase);
  317. }
  318. }
  319. /**
  320. * Compare two heatmap data objects
  321. * @param objA
  322. * @param objB
  323. */
  324. function isHeatmapDataEqual(objA: any, objB: any): boolean {
  325. let is_eql = !emptyXOR(objA, objB);
  326. _.forEach(objA, (xBucket: XBucket, x) => {
  327. if (objB[x]) {
  328. if (emptyXOR(xBucket.buckets, objB[x].buckets)) {
  329. is_eql = false;
  330. return false;
  331. }
  332. _.forEach(xBucket.buckets, (yBucket: YBucket, y) => {
  333. if (objB[x].buckets && objB[x].buckets[y]) {
  334. if (objB[x].buckets[y].values) {
  335. is_eql = _.isEqual(_.sortBy(yBucket.values), _.sortBy(objB[x].buckets[y].values));
  336. if (!is_eql) {
  337. return false;
  338. } else {
  339. return true;
  340. }
  341. } else {
  342. is_eql = false;
  343. return false;
  344. }
  345. } else {
  346. is_eql = false;
  347. return false;
  348. }
  349. });
  350. if (!is_eql) {
  351. return false;
  352. } else {
  353. return true;
  354. }
  355. } else {
  356. is_eql = false;
  357. return false;
  358. }
  359. });
  360. return is_eql;
  361. }
  362. function emptyXOR(foo: any, bar: any): boolean {
  363. return (_.isEmpty(foo) || _.isEmpty(bar)) && !(_.isEmpty(foo) && _.isEmpty(bar));
  364. }
  365. export {
  366. convertToHeatMap,
  367. elasticHistogramToHeatmap,
  368. convertToCards,
  369. mergeZeroBuckets,
  370. getValueBucketBound,
  371. isHeatmapDataEqual,
  372. calculateBucketSize
  373. };