utils.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import {InjectedRouter} from 'react-router';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import GlobalModal from 'sentry/components/globalModal';
  4. import {Organization, Project} from 'sentry/types';
  5. import {
  6. RecommendedSdkUpgrade,
  7. SamplingConditionOperator,
  8. SamplingDistribution,
  9. SamplingInnerName,
  10. SamplingInnerOperator,
  11. SamplingRule,
  12. SamplingRuleType,
  13. SamplingSdkVersion,
  14. } from 'sentry/types/sampling';
  15. import {OrganizationContext} from 'sentry/views/organizationContext';
  16. import {Outcome} from 'sentry/views/organizationStats/types';
  17. import {RouteContext} from 'sentry/views/routeContext';
  18. import ServerSideSampling from 'sentry/views/settings/project/server-side-sampling';
  19. import importedUseProjectStats from 'sentry/views/settings/project/server-side-sampling/utils/useProjectStats';
  20. import {useRecommendedSdkUpgrades as importedUseRecommendedSdkUpgrades} from 'sentry/views/settings/project/server-side-sampling/utils/useRecommendedSdkUpgrades';
  21. export const outcomesWithoutClientDiscarded = {
  22. ...TestStubs.OutcomesWithReason(),
  23. groups: TestStubs.OutcomesWithReason().groups.filter(
  24. group => group.by.outcome !== Outcome.CLIENT_DISCARD
  25. ),
  26. };
  27. export const uniformRule: SamplingRule = {
  28. sampleRate: 0.5,
  29. type: SamplingRuleType.TRACE,
  30. active: false,
  31. condition: {
  32. op: SamplingConditionOperator.AND,
  33. inner: [],
  34. },
  35. id: 1,
  36. };
  37. export const specificRule: SamplingRule = {
  38. sampleRate: 0.2,
  39. active: false,
  40. type: SamplingRuleType.TRACE,
  41. condition: {
  42. op: SamplingConditionOperator.AND,
  43. inner: [
  44. {
  45. op: SamplingInnerOperator.GLOB_MATCH,
  46. name: SamplingInnerName.TRACE_RELEASE,
  47. value: ['1.2.2'],
  48. },
  49. ],
  50. },
  51. id: 2,
  52. };
  53. export const mockedProjects = [
  54. TestStubs.Project({
  55. name: 'javascript',
  56. slug: 'javascript',
  57. id: 1,
  58. }),
  59. TestStubs.Project({
  60. name: 'sentry',
  61. slug: 'sentry',
  62. platform: 'python',
  63. id: 2,
  64. }),
  65. TestStubs.Project({
  66. id: 4,
  67. dynamicSampling: {
  68. rules: [
  69. {
  70. sampleRate: 1,
  71. type: 'trace',
  72. active: false,
  73. condition: {
  74. op: 'and',
  75. inner: [],
  76. },
  77. id: 1,
  78. },
  79. ],
  80. },
  81. }),
  82. ];
  83. export const mockedSamplingSdkVersions: SamplingSdkVersion[] = [
  84. {
  85. project: mockedProjects[0].slug,
  86. latestSDKVersion: '1.0.3',
  87. latestSDKName: 'sentry.javascript.react',
  88. isSendingSampleRate: true,
  89. isSendingSource: true,
  90. isSupportedPlatform: true,
  91. },
  92. {
  93. project: mockedProjects[1].slug,
  94. latestSDKVersion: '1.0.2',
  95. latestSDKName: 'sentry.python',
  96. isSendingSampleRate: false,
  97. isSendingSource: false,
  98. isSupportedPlatform: true,
  99. },
  100. {
  101. project: 'java',
  102. latestSDKVersion: '1.0.2',
  103. latestSDKName: 'sentry.java',
  104. isSendingSampleRate: true,
  105. isSendingSource: false,
  106. isSupportedPlatform: true,
  107. },
  108. {
  109. project: 'angular',
  110. latestSDKVersion: '1.0.2',
  111. latestSDKName: 'sentry.javascript.angular',
  112. isSendingSampleRate: false,
  113. isSendingSource: false,
  114. isSupportedPlatform: false,
  115. },
  116. ];
  117. export const recommendedSdkUpgrades: RecommendedSdkUpgrade[] = [
  118. {
  119. project: mockedProjects[1],
  120. latestSDKName: mockedSamplingSdkVersions[1].latestSDKName,
  121. latestSDKVersion: mockedSamplingSdkVersions[1].latestSDKVersion,
  122. },
  123. ];
  124. export const mockedSamplingDistribution: SamplingDistribution = {
  125. project_breakdown: [
  126. {
  127. project: mockedProjects[0].slug,
  128. project_id: mockedProjects[0].id,
  129. 'count()': 888,
  130. },
  131. {
  132. project: mockedProjects[1].slug,
  133. project_id: mockedProjects[1].id,
  134. 'count()': 100,
  135. },
  136. ],
  137. sample_size: 100,
  138. null_sample_rate_percentage: 98,
  139. sample_rate_distributions: {
  140. min: 1,
  141. max: 1,
  142. avg: 1,
  143. p50: 1,
  144. p90: 1,
  145. p95: 1,
  146. p99: 1,
  147. },
  148. startTimestamp: '2017-08-04T07:52:11Z',
  149. endTimestamp: '2017-08-05T07:52:11Z',
  150. };
  151. jest.mock('sentry/views/settings/project/server-side-sampling/utils/useProjectStats');
  152. const useProjectStats = importedUseProjectStats as jest.MockedFunction<
  153. typeof importedUseProjectStats
  154. >;
  155. useProjectStats.mockImplementation(() => ({
  156. projectStats: TestStubs.OutcomesWithReason(),
  157. loading: false,
  158. error: undefined,
  159. projectStatsSeries: [],
  160. onRefetch: jest.fn(),
  161. }));
  162. jest.mock(
  163. 'sentry/views/settings/project/server-side-sampling/utils/useRecommendedSdkUpgrades'
  164. );
  165. const useRecommendedSdkUpgrades =
  166. importedUseRecommendedSdkUpgrades as jest.MockedFunction<
  167. typeof importedUseRecommendedSdkUpgrades
  168. >;
  169. useRecommendedSdkUpgrades.mockImplementation(() => ({
  170. recommendedSdkUpgrades: [
  171. {
  172. project: mockedProjects[1],
  173. latestSDKName: mockedSamplingSdkVersions[1].latestSDKName,
  174. latestSDKVersion: mockedSamplingSdkVersions[1].latestSDKVersion,
  175. },
  176. ],
  177. incompatibleProjects: [],
  178. isProjectIncompatible: true,
  179. affectedProjects: [mockedProjects[1]],
  180. fetching: false,
  181. }));
  182. export function getMockData({
  183. projects,
  184. access,
  185. }: {access?: string[]; projects?: Project[]} = {}) {
  186. return initializeOrg({
  187. ...initializeOrg(),
  188. organization: {
  189. ...initializeOrg().organization,
  190. features: ['server-side-sampling', 'server-side-sampling-ui'],
  191. access: access ?? initializeOrg().organization.access,
  192. projects,
  193. },
  194. projects,
  195. });
  196. }
  197. export function TestComponent({
  198. router,
  199. project,
  200. organization,
  201. withModal,
  202. }: {
  203. organization: Organization;
  204. project: Project;
  205. router: InjectedRouter;
  206. withModal?: boolean;
  207. }) {
  208. return (
  209. <RouteContext.Provider
  210. value={{
  211. router,
  212. location: router.location,
  213. params: {
  214. orgId: organization.slug,
  215. projectId: project.slug,
  216. },
  217. routes: [],
  218. }}
  219. >
  220. {withModal && <GlobalModal />}
  221. <OrganizationContext.Provider value={organization}>
  222. <ServerSideSampling project={project} />
  223. </OrganizationContext.Provider>
  224. </RouteContext.Provider>
  225. );
  226. }