utils.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import {t} from 'sentry/locale';
  2. import type {Organization} from 'sentry/types/organization';
  3. import {canUseMetricsData} from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  4. const AGGREGATE_ALIAS_VALUE_EXTRACT_PATTERN = /(\d+)$/;
  5. const FUNCTION_FIELD_VALUE_EXTRACT_PATTERN = /(\d+)\)$/;
  6. /**
  7. * Convert a discover response into a barchart compatible series
  8. */
  9. export function transformData(
  10. data: Record<string, number>[],
  11. useAggregateAlias: boolean = true
  12. ) {
  13. const extractedData = Object.keys(data[0])
  14. .map((key: string) => {
  15. const nameMatch = (
  16. useAggregateAlias
  17. ? AGGREGATE_ALIAS_VALUE_EXTRACT_PATTERN
  18. : FUNCTION_FIELD_VALUE_EXTRACT_PATTERN
  19. ).exec(key);
  20. if (!nameMatch) {
  21. return [-1, -1];
  22. }
  23. let nameValue = Number(nameMatch[1]);
  24. if (nameValue > 100) {
  25. nameValue /= 10;
  26. }
  27. return [nameValue, data[0][key]];
  28. })
  29. .filter(i => i[0] > 0);
  30. extractedData.sort((a, b) => {
  31. if (a[0] > b[0]) {
  32. return 1;
  33. }
  34. if (a[0] < b[0]) {
  35. return -1;
  36. }
  37. return 0;
  38. });
  39. return [
  40. {
  41. seriesName: t('Duration'),
  42. data: extractedData.map(i => ({value: i[1], name: `${i[0].toLocaleString()}%`})),
  43. },
  44. ];
  45. }
  46. export function getPercentiles(organization: Organization) {
  47. const isUsingMetrics = canUseMetricsData(organization);
  48. const METRICS_PERCENTILES = ['0.25', '0.50', '0.75', '0.90', '0.95', '0.99', '1'];
  49. const INDEXED_PERCENTILES = [
  50. '0.10',
  51. '0.25',
  52. '0.50',
  53. '0.75',
  54. '0.90',
  55. '0.95',
  56. '0.99',
  57. '0.995',
  58. '0.999',
  59. '1',
  60. ];
  61. return isUsingMetrics ? METRICS_PERCENTILES : INDEXED_PERCENTILES;
  62. }