utils.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. };
  149. jest.mock('sentry/views/settings/project/server-side-sampling/utils/useProjectStats');
  150. const useProjectStats = importedUseProjectStats as jest.MockedFunction<
  151. typeof importedUseProjectStats
  152. >;
  153. useProjectStats.mockImplementation(() => ({
  154. projectStats: TestStubs.OutcomesWithReason(),
  155. loading: false,
  156. error: undefined,
  157. projectStatsSeries: [],
  158. }));
  159. jest.mock(
  160. 'sentry/views/settings/project/server-side-sampling/utils/useRecommendedSdkUpgrades'
  161. );
  162. const useRecommendedSdkUpgrades =
  163. importedUseRecommendedSdkUpgrades as jest.MockedFunction<
  164. typeof importedUseRecommendedSdkUpgrades
  165. >;
  166. useRecommendedSdkUpgrades.mockImplementation(() => ({
  167. recommendedSdkUpgrades: [
  168. {
  169. project: mockedProjects[1],
  170. latestSDKName: mockedSamplingSdkVersions[1].latestSDKName,
  171. latestSDKVersion: mockedSamplingSdkVersions[1].latestSDKVersion,
  172. },
  173. ],
  174. incompatibleProjects: [],
  175. fetching: false,
  176. }));
  177. export function getMockData({
  178. projects,
  179. access,
  180. }: {access?: string[]; projects?: Project[]} = {}) {
  181. return initializeOrg({
  182. ...initializeOrg(),
  183. organization: {
  184. ...initializeOrg().organization,
  185. features: ['server-side-sampling', 'server-side-sampling-ui'],
  186. access: access ?? initializeOrg().organization.access,
  187. projects,
  188. },
  189. projects,
  190. });
  191. }
  192. export function TestComponent({
  193. router,
  194. project,
  195. organization,
  196. withModal,
  197. }: {
  198. organization: Organization;
  199. project: Project;
  200. router: InjectedRouter;
  201. withModal?: boolean;
  202. }) {
  203. return (
  204. <RouteContext.Provider
  205. value={{
  206. router,
  207. location: router.location,
  208. params: {
  209. orgId: organization.slug,
  210. projectId: project.slug,
  211. },
  212. routes: [],
  213. }}
  214. >
  215. {withModal && <GlobalModal />}
  216. <OrganizationContext.Provider value={organization}>
  217. <ServerSideSampling project={project} />
  218. </OrganizationContext.Provider>
  219. </RouteContext.Provider>
  220. );
  221. }