utils.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import {DURATION_UNITS} from 'sentry/utils/discover/fieldRenderers';
  2. import type {DiscoverDatasets} from 'sentry/utils/discover/types';
  3. import getDuration from 'sentry/utils/duration/getDuration';
  4. import {VitalState} from 'sentry/views/performance/vitalDetail/utils';
  5. const formatMetricValue = (metric: MetricValue): string => {
  6. if (typeof metric.value === 'number' && metric.type === 'duration' && metric.unit) {
  7. const seconds =
  8. (metric.value * ((metric.unit && DURATION_UNITS[metric.unit]) ?? 1)) / 1000;
  9. return getDuration(seconds, 2, true);
  10. }
  11. if (typeof metric.value === 'number' && metric.type === 'number') {
  12. return metric.value.toFixed(2);
  13. }
  14. return String(metric.value);
  15. };
  16. // maps to PERFORMANCE_SCORE_COLORS keys
  17. export enum PerformanceScore {
  18. GOOD = 'good',
  19. NEEDS_IMPROVEMENT = 'needsImprovement',
  20. BAD = 'bad',
  21. NONE = 'none',
  22. }
  23. export type VitalStatus = {
  24. description: string | undefined;
  25. formattedValue: string | undefined;
  26. score: PerformanceScore;
  27. value: MetricValue | undefined;
  28. };
  29. export type VitalItem = {
  30. dataset: DiscoverDatasets;
  31. description: string;
  32. docs: React.ReactNode;
  33. field: string;
  34. getStatus: (value: MetricValue) => VitalStatus;
  35. platformDocLinks: Record<string, string>;
  36. sdkDocLinks: Record<string, string>;
  37. setup: React.ReactNode | undefined;
  38. title: string;
  39. };
  40. export type MetricValue = {
  41. // the field type if defined, e.g. duration
  42. type: string | undefined;
  43. // the unit of the value, e.g. milliseconds
  44. unit: string | undefined;
  45. // the actual value
  46. value: string | number | undefined;
  47. };
  48. export const STATUS_UNKNOWN: VitalStatus = {
  49. description: undefined,
  50. formattedValue: undefined,
  51. value: undefined,
  52. score: PerformanceScore.NONE,
  53. };
  54. export function getColdAppStartPerformance(metric: MetricValue): VitalStatus {
  55. let description = '';
  56. let status = PerformanceScore.NONE;
  57. if (typeof metric.value === 'number' && metric.unit) {
  58. const durationMs = metric.value * DURATION_UNITS[metric.unit];
  59. // TODO should be platform dependant
  60. if (durationMs > 5000) {
  61. status = PerformanceScore.BAD;
  62. description = VitalState.POOR;
  63. } else if (durationMs > 3000) {
  64. status = PerformanceScore.NEEDS_IMPROVEMENT;
  65. description = VitalState.MEH;
  66. } else if (durationMs > 0) {
  67. status = PerformanceScore.GOOD;
  68. description = VitalState.GOOD;
  69. }
  70. }
  71. return {
  72. value: metric,
  73. formattedValue: formatMetricValue(metric),
  74. score: status,
  75. description: description,
  76. };
  77. }
  78. export function getWarmAppStartPerformance(metric: MetricValue): VitalStatus {
  79. let description = '';
  80. let status = PerformanceScore.NONE;
  81. if (typeof metric.value === 'number' && metric.unit) {
  82. const durationMs = metric.value * DURATION_UNITS[metric.unit];
  83. // TODO should be platform dependant
  84. if (durationMs > 2000) {
  85. status = PerformanceScore.BAD;
  86. description = VitalState.POOR;
  87. } else if (durationMs > 1000) {
  88. status = PerformanceScore.NEEDS_IMPROVEMENT;
  89. description = VitalState.MEH;
  90. } else if (durationMs > 0) {
  91. status = PerformanceScore.GOOD;
  92. description = VitalState.GOOD;
  93. }
  94. }
  95. return {
  96. value: metric,
  97. formattedValue: formatMetricValue(metric),
  98. score: status,
  99. description: description,
  100. };
  101. }
  102. export function getDefaultMetricPerformance(metric: MetricValue): VitalStatus {
  103. return {
  104. description: undefined,
  105. formattedValue: formatMetricValue(metric),
  106. value: metric,
  107. score: PerformanceScore.NONE,
  108. };
  109. }