queryToSeries.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import moment from 'moment';
  2. import {Series} from 'sentry/types/echarts';
  3. import {zeroFillSeries} from 'sentry/views/starfish/utils/zeroFillSeries';
  4. export const queryToSeries = (
  5. data: (Record<string, any> & {interval: string})[],
  6. groupByProperty: string,
  7. seriesValueProperty: string,
  8. startTime?: moment.Moment,
  9. endTime?: moment.Moment,
  10. interval?: number,
  11. zerofillValue?: any
  12. ): Series[] => {
  13. const seriesMap: Record<string, Series> = {};
  14. data.forEach(row => {
  15. const dataEntry = {value: row[seriesValueProperty], name: row.interval};
  16. if (!seriesMap[row[groupByProperty]]) {
  17. seriesMap[row[groupByProperty]] = {
  18. seriesName: row[groupByProperty],
  19. data: [],
  20. };
  21. }
  22. if (dataEntry.value !== undefined) {
  23. seriesMap[row[groupByProperty]].data.push(dataEntry);
  24. }
  25. });
  26. if (!startTime || !endTime || !interval) {
  27. return Object.values(seriesMap);
  28. }
  29. return Object.values(seriesMap).map(series =>
  30. zeroFillSeries(
  31. series,
  32. moment.duration(interval, 'hours'),
  33. startTime,
  34. endTime,
  35. zerofillValue
  36. )
  37. );
  38. };