utils.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import {DURATION_UNITS} from 'sentry/utils/discover/fieldRenderers';
  2. import type {DiscoverDatasets} from 'sentry/utils/discover/types';
  3. import {VitalState} from 'sentry/views/performance/vitalDetail/utils';
  4. // maps to PERFORMANCE_SCORE_COLORS keys
  5. export enum PerformanceScore {
  6. GOOD = 'good',
  7. NEEDS_IMPROVEMENT = 'needsImprovement',
  8. BAD = 'bad',
  9. NONE = 'none',
  10. }
  11. export type VitalStatus = {
  12. description: string | undefined;
  13. score: PerformanceScore;
  14. };
  15. export type VitalItem = {
  16. dataset: DiscoverDatasets;
  17. description: string;
  18. field: string;
  19. getStatus: (value: MetricValue) => VitalStatus;
  20. title: string;
  21. };
  22. export type MetricValue = {
  23. // the field type if defined, e.g. duration
  24. type: string | undefined;
  25. // the unit of the value, e.g. milliseconds
  26. unit: string | undefined;
  27. // the actual value
  28. value: string | number | undefined;
  29. };
  30. export const STATUS_UNKNOWN: VitalStatus = {
  31. description: undefined,
  32. score: PerformanceScore.NONE,
  33. };
  34. export function getColdAppStartPerformance(metric: MetricValue) {
  35. let description = '';
  36. let status = PerformanceScore.NONE;
  37. if (typeof metric.value === 'number' && metric.unit) {
  38. const durationMs = metric.value * DURATION_UNITS[metric.unit];
  39. // TODO should be platform dependant
  40. if (durationMs > 5000) {
  41. status = PerformanceScore.BAD;
  42. description = VitalState.POOR;
  43. } else if (durationMs > 3000) {
  44. status = PerformanceScore.NEEDS_IMPROVEMENT;
  45. description = VitalState.MEH;
  46. } else if (durationMs > 0) {
  47. status = PerformanceScore.GOOD;
  48. description = VitalState.GOOD;
  49. }
  50. }
  51. return {
  52. score: status,
  53. description: description,
  54. };
  55. }
  56. export function getWarmAppStartPerformance(metric: MetricValue) {
  57. let description = '';
  58. let status = PerformanceScore.NONE;
  59. if (typeof metric.value === 'number' && metric.unit) {
  60. const durationMs = metric.value * DURATION_UNITS[metric.unit];
  61. // TODO should be platform dependant
  62. if (durationMs > 2000) {
  63. status = PerformanceScore.BAD;
  64. description = VitalState.POOR;
  65. } else if (durationMs > 1000) {
  66. status = PerformanceScore.NEEDS_IMPROVEMENT;
  67. description = VitalState.MEH;
  68. } else if (durationMs > 0) {
  69. status = PerformanceScore.GOOD;
  70. description = VitalState.GOOD;
  71. }
  72. }
  73. return {
  74. score: status,
  75. description: description,
  76. };
  77. }
  78. export function getDefaultMetricPerformance(_: MetricValue) {
  79. return STATUS_UNKNOWN;
  80. }