histogram.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import _ from 'lodash';
  2. /**
  3. * Convert series into array of series values.
  4. * @param data Array of series
  5. */
  6. export function getSeriesValues(data: any): number[] {
  7. let values = [];
  8. // Count histogam stats
  9. for (let i = 0; i < data.length; i++) {
  10. let series = data[i];
  11. for (let j = 0; j < series.data.length; j++) {
  12. if (series.data[j][1] !== null) {
  13. values.push(series.data[j][1]);
  14. }
  15. }
  16. }
  17. return values;
  18. }
  19. /**
  20. * Convert array of values into timeseries-like histogram:
  21. * [[val_1, count_1], [val_2, count_2], ..., [val_n, count_n]]
  22. * @param values
  23. * @param bucketSize
  24. */
  25. export function convertValuesToHistogram(values: number[], bucketSize: number): any[] {
  26. let histogram = {};
  27. for (let i = 0; i < values.length; i++) {
  28. let bound = getBucketBound(values[i], bucketSize);
  29. if (histogram[bound]) {
  30. histogram[bound] = histogram[bound] + 1;
  31. } else {
  32. histogram[bound] = 1;
  33. }
  34. }
  35. return _.map(histogram, (count, bound) => {
  36. return [Number(bound), count];
  37. });
  38. }
  39. function getBucketBound(value: number, bucketSize: number): number {
  40. return Math.floor(value / bucketSize) * bucketSize;
  41. }