initializePerformanceData.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {Project} from 'sentry/types';
  3. import {defined} from 'sentry/utils';
  4. import EventView from 'sentry/utils/discover/eventView';
  5. import {
  6. ExampleSpan,
  7. ExampleTransaction,
  8. SuspectSpan,
  9. } from 'sentry/utils/performance/suspectSpans/types';
  10. export interface initializeDataSettings {
  11. features?: string[];
  12. project?: any; // TODO(k-fish): Fix this project type.
  13. projects?: Project[];
  14. query?: {};
  15. }
  16. export function initializeData(settings?: initializeDataSettings) {
  17. const _defaultProject = TestStubs.Project();
  18. const _settings = {
  19. query: {},
  20. features: [],
  21. projects: [_defaultProject],
  22. project: _defaultProject,
  23. ...settings,
  24. };
  25. const {query, features, projects, project} = _settings;
  26. const organization = TestStubs.Organization({
  27. features,
  28. projects,
  29. });
  30. const routerLocation: {query: {project?: number}} = {
  31. query: {
  32. ...query,
  33. },
  34. };
  35. if (settings?.project) {
  36. routerLocation.query.project = project;
  37. }
  38. const router = {
  39. location: routerLocation,
  40. };
  41. const initialData = initializeOrg({organization, projects, project, router});
  42. const location = initialData.router.location;
  43. const eventView = EventView.fromLocation(location);
  44. return {...initialData, location, eventView};
  45. }
  46. export const SAMPLE_SPANS = [
  47. {
  48. op: 'op1',
  49. group: 'aaaaaaaaaaaaaaaa',
  50. description: 'span-1',
  51. examples: [
  52. {
  53. id: 'abababababababab',
  54. description: 'span-1',
  55. spans: [{id: 'ababab11'}, {id: 'ababab22'}],
  56. },
  57. {
  58. id: 'acacacacacacacac',
  59. description: 'span-2',
  60. spans: [{id: 'acacac11'}, {id: 'acacac22'}],
  61. },
  62. {
  63. id: 'adadadadadadadad',
  64. description: 'span-3',
  65. spans: [{id: 'adadad11'}, {id: 'adadad22'}],
  66. },
  67. ],
  68. },
  69. {
  70. op: 'op2',
  71. group: 'bbbbbbbbbbbbbbbb',
  72. description: 'span-4',
  73. examples: [
  74. {
  75. id: 'bcbcbcbcbcbcbcbc',
  76. description: 'span-4',
  77. spans: [{id: 'bcbcbc11'}, {id: 'bcbcbc11'}],
  78. },
  79. {
  80. id: 'bdbdbdbdbdbdbdbd',
  81. description: 'span-5',
  82. spans: [{id: 'bdbdbd11'}, {id: 'bdbdbd22'}],
  83. },
  84. {
  85. id: 'bebebebebebebebe',
  86. description: 'span-6',
  87. spans: [{id: 'bebebe11'}, {id: 'bebebe22'}],
  88. },
  89. ],
  90. },
  91. ];
  92. type SpanOpt = {
  93. id: string;
  94. };
  95. type ExampleOpt = {
  96. description: string;
  97. id: string;
  98. spans: SpanOpt[];
  99. };
  100. type SuspectOpt = {
  101. description: string;
  102. examples: ExampleOpt[];
  103. group: string;
  104. op: string;
  105. };
  106. function makeSpan(opt: SpanOpt): ExampleSpan {
  107. const {id} = opt;
  108. return {
  109. id,
  110. startTimestamp: 10100,
  111. finishTimestamp: 10200,
  112. exclusiveTime: 100,
  113. };
  114. }
  115. function makeExample(opt: ExampleOpt): ExampleTransaction {
  116. const {id, description, spans} = opt;
  117. return {
  118. id,
  119. description,
  120. startTimestamp: 10000,
  121. finishTimestamp: 12000,
  122. nonOverlappingExclusiveTime: 2000,
  123. spans: spans.map(makeSpan),
  124. };
  125. }
  126. export function makeSuspectSpan(opt: SuspectOpt): SuspectSpan {
  127. const {op, group, description, examples} = opt;
  128. return {
  129. op,
  130. group,
  131. description,
  132. frequency: 1,
  133. count: 1,
  134. avgOccurrences: 1,
  135. sumExclusiveTime: 5,
  136. p50ExclusiveTime: 1,
  137. p75ExclusiveTime: 2,
  138. p95ExclusiveTime: 3,
  139. p99ExclusiveTime: 4,
  140. examples: examples.map(makeExample),
  141. };
  142. }
  143. export function generateSuspectSpansResponse(opts?: {
  144. examples?: number;
  145. examplesOnly?: boolean;
  146. }) {
  147. const {examples, examplesOnly} = opts ?? {};
  148. return SAMPLE_SPANS.map(sampleSpan => {
  149. const span = {...sampleSpan};
  150. if (defined(examples)) {
  151. span.examples = span.examples.slice(0, examples);
  152. }
  153. const suspectSpans = makeSuspectSpan(span);
  154. if (examplesOnly) {
  155. return {
  156. op: suspectSpans.op,
  157. group: suspectSpans.group,
  158. examples: suspectSpans.examples,
  159. };
  160. }
  161. return suspectSpans;
  162. });
  163. }