transformMetricsResponseToTable.tsx 968 B

1234567891011121314151617181920212223242526272829303132333435
  1. import {MetricsApiResponse} from 'sentry/types';
  2. import {TableData} from '../discover/discoverQuery';
  3. import {getAggregateArg} from '../discover/fields';
  4. import {METRIC_TO_COLUMN_TYPE} from './fields';
  5. function changeObjectValuesToTypes(
  6. obj: Record<string, number | string | null> | undefined
  7. ) {
  8. return Object.keys(obj ?? {}).reduce((acc, key) => {
  9. acc[key] = METRIC_TO_COLUMN_TYPE[getAggregateArg(key) ?? key] ?? 'string';
  10. return acc;
  11. }, {});
  12. }
  13. export function transformMetricsResponseToTable(
  14. response: MetricsApiResponse | null
  15. ): TableData {
  16. const data =
  17. response?.groups.map((group, index) => ({
  18. id: String(index),
  19. ...group.by,
  20. ...group.totals,
  21. })) ?? [];
  22. const singleRow = response?.groups[0];
  23. // TODO(metrics): these should come from the API in the future
  24. const meta = {
  25. ...changeObjectValuesToTypes(singleRow?.by),
  26. ...changeObjectValuesToTypes(singleRow?.totals),
  27. };
  28. return {meta, data};
  29. }