histogram.ts 1.4 KB

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