heatmap_data_converter.ts 11 KB

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