utils.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import {t} from 'sentry/locale';
  2. const AGGREGATE_ALIAS_VALUE_EXTRACT_PATTERN = /(\d+)$/;
  3. const FUNCTION_FIELD_VALUE_EXTRACT_PATTERN = /(\d+)\)$/;
  4. /**
  5. * Convert a discover response into a barchart compatible series
  6. */
  7. export function transformData(
  8. data: Record<string, number>[],
  9. useAggregateAlias: boolean = true
  10. ) {
  11. const extractedData = Object.keys(data[0])
  12. .map((key: string) => {
  13. const nameMatch = (
  14. useAggregateAlias
  15. ? AGGREGATE_ALIAS_VALUE_EXTRACT_PATTERN
  16. : FUNCTION_FIELD_VALUE_EXTRACT_PATTERN
  17. ).exec(key);
  18. if (!nameMatch) {
  19. return [-1, -1];
  20. }
  21. let nameValue = Number(nameMatch[1]);
  22. if (nameValue > 100) {
  23. nameValue /= 10;
  24. }
  25. return [nameValue, data[0][key]];
  26. })
  27. .filter(i => i[0] > 0);
  28. extractedData.sort((a, b) => {
  29. if (a[0] > b[0]) {
  30. return 1;
  31. }
  32. if (a[0] < b[0]) {
  33. return -1;
  34. }
  35. return 0;
  36. });
  37. return [
  38. {
  39. seriesName: t('Duration'),
  40. data: extractedData.map(i => ({value: i[1], name: `${i[0].toLocaleString()}%`})),
  41. },
  42. ];
  43. }