deprecatedTransformMetricsResponseToTable.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import {MetricsApiResponse} from 'sentry/types';
  2. import {TableData} from '../discover/discoverQuery';
  3. import {getAggregateAlias, getAggregateArg} from '../discover/fields';
  4. import {
  5. convertMetricsGroupBysToDiscoverFields,
  6. convertMetricsToDiscoverFields,
  7. METRIC_TO_COLUMN_TYPE,
  8. } from './fields';
  9. type Arguments = {
  10. response: MetricsApiResponse | null;
  11. };
  12. function renameObjectKeys(
  13. obj: Record<string, number | string | null>,
  14. renameFunc: (key: string) => string
  15. ) {
  16. return Object.keys(obj).reduce((acc, key) => {
  17. const discoverKey = getAggregateAlias(renameFunc(key));
  18. if (discoverKey) {
  19. acc[discoverKey] = obj[key];
  20. }
  21. return acc;
  22. }, {});
  23. }
  24. function changeObjectValuesToTypes(obj: Record<string, number | string | null>) {
  25. return Object.keys(obj).reduce((acc, key) => {
  26. acc[key] = METRIC_TO_COLUMN_TYPE[getAggregateArg(key) ?? key] ?? 'number';
  27. return acc;
  28. }, {});
  29. }
  30. /**
  31. * This function was used for performance table on metrics PoC.
  32. * The transform that renames the fields will be handled on the API layer.
  33. * @deprecated
  34. */
  35. export function deprecatedTransformMetricsResponseToTable({
  36. response,
  37. }: Arguments): TableData {
  38. const data =
  39. response?.groups.map((group, index) => ({
  40. id: String(index),
  41. ...renameObjectKeys(group.by, convertMetricsGroupBysToDiscoverFields),
  42. ...renameObjectKeys(group.totals, convertMetricsToDiscoverFields),
  43. })) ?? [];
  44. const meta = {
  45. ...renameObjectKeys(
  46. changeObjectValuesToTypes(response?.groups[0]?.by ?? {}),
  47. convertMetricsGroupBysToDiscoverFields
  48. ),
  49. ...renameObjectKeys(
  50. changeObjectValuesToTypes(response?.groups[0]?.totals ?? {}),
  51. convertMetricsToDiscoverFields
  52. ),
  53. };
  54. return {
  55. data,
  56. meta,
  57. };
  58. }