initializePerformanceData.ts 4.0 KB

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