queryDataToChartData.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import moment, {Moment} from 'moment';
  2. import {Series} from 'sentry/types/echarts';
  3. import {zeroFillSeries} from 'sentry/views/starfish/utils/zeroFillSeries';
  4. export function queryDataToChartData<T>(
  5. data: ({interval: string} & T)[],
  6. startTime: Moment,
  7. endTime: Moment,
  8. seriesOptions: Partial<Series> = {}
  9. ): Record<keyof T, Series> {
  10. const series: Record<string, Series> = {};
  11. if (data.length > 0) {
  12. Object.keys(data[0])
  13. .filter(key => key !== 'interval')
  14. .forEach(key => {
  15. series[key] = {data: [], seriesName: `${key}()`, ...seriesOptions};
  16. });
  17. }
  18. data.forEach(point => {
  19. Object.keys(point).forEach(key => {
  20. if (key !== 'interval') {
  21. series[key].data.push({
  22. name: point.interval,
  23. value: point[key],
  24. });
  25. }
  26. });
  27. });
  28. Object.entries(series).forEach(([seriesKey, s]) => {
  29. series[seriesKey] = zeroFillSeries(
  30. s,
  31. moment.duration(12, 'hours'),
  32. startTime,
  33. endTime
  34. );
  35. });
  36. return series as Record<keyof T, Series>;
  37. }