heatmap_data_converter.ts 11 KB

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