utils.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.Outcomes(),
  23. groups: TestStubs.Outcomes().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. },
  91. {
  92. project: mockedProjects[1].slug,
  93. latestSDKVersion: '1.0.2',
  94. latestSDKName: 'sentry.python',
  95. isSendingSampleRate: false,
  96. isSendingSource: false,
  97. },
  98. {
  99. project: 'java',
  100. latestSDKVersion: '1.0.2',
  101. latestSDKName: 'sentry.java',
  102. isSendingSampleRate: true,
  103. isSendingSource: false,
  104. },
  105. ];
  106. export const recommendedSdkUpgrades: RecommendedSdkUpgrade[] = [
  107. {
  108. project: mockedProjects[1],
  109. latestSDKName: mockedSamplingSdkVersions[1].latestSDKName,
  110. latestSDKVersion: mockedSamplingSdkVersions[1].latestSDKVersion,
  111. },
  112. ];
  113. export const mockedSamplingDistribution: SamplingDistribution = {
  114. project_breakdown: [
  115. {
  116. project: mockedProjects[0].slug,
  117. project_id: mockedProjects[0].id,
  118. 'count()': 888,
  119. },
  120. {
  121. project: mockedProjects[1].slug,
  122. project_id: mockedProjects[1].id,
  123. 'count()': 100,
  124. },
  125. ],
  126. sample_size: 100,
  127. null_sample_rate_percentage: 98,
  128. sample_rate_distributions: {
  129. min: 1,
  130. max: 1,
  131. avg: 1,
  132. p50: 1,
  133. p90: 1,
  134. p95: 1,
  135. p99: 1,
  136. },
  137. };
  138. jest.mock('sentry/views/settings/project/server-side-sampling/utils/useProjectStats');
  139. const useProjectStats = importedUseProjectStats as jest.MockedFunction<
  140. typeof importedUseProjectStats
  141. >;
  142. useProjectStats.mockImplementation(() => ({
  143. projectStats: TestStubs.Outcomes(),
  144. loading: false,
  145. error: undefined,
  146. projectStatsSeries: [],
  147. }));
  148. jest.mock(
  149. 'sentry/views/settings/project/server-side-sampling/utils/useRecommendedSdkUpgrades'
  150. );
  151. const useRecommendedSdkUpgrades =
  152. importedUseRecommendedSdkUpgrades as jest.MockedFunction<
  153. typeof importedUseRecommendedSdkUpgrades
  154. >;
  155. useRecommendedSdkUpgrades.mockImplementation(() => ({
  156. recommendedSdkUpgrades: [
  157. {
  158. project: mockedProjects[1],
  159. latestSDKName: mockedSamplingSdkVersions[1].latestSDKName,
  160. latestSDKVersion: mockedSamplingSdkVersions[1].latestSDKVersion,
  161. },
  162. ],
  163. fetching: false,
  164. }));
  165. export function getMockData({
  166. projects,
  167. access,
  168. }: {access?: string[]; projects?: Project[]} = {}) {
  169. return initializeOrg({
  170. ...initializeOrg(),
  171. organization: {
  172. ...initializeOrg().organization,
  173. features: ['server-side-sampling'],
  174. access: access ?? initializeOrg().organization.access,
  175. projects,
  176. },
  177. projects,
  178. });
  179. }
  180. export function TestComponent({
  181. router,
  182. project,
  183. organization,
  184. withModal,
  185. }: {
  186. organization: Organization;
  187. project: Project;
  188. router: InjectedRouter;
  189. withModal?: boolean;
  190. }) {
  191. return (
  192. <RouteContext.Provider
  193. value={{
  194. router,
  195. location: router.location,
  196. params: {
  197. orgId: organization.slug,
  198. projectId: project.slug,
  199. },
  200. routes: [],
  201. }}
  202. >
  203. {withModal && <GlobalModal />}
  204. <OrganizationContext.Provider value={organization}>
  205. <ServerSideSampling project={project} />
  206. </OrganizationContext.Provider>
  207. </RouteContext.Provider>
  208. );
  209. }