serverSideSamplingStore.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import {createStore} from 'reflux';
  2. import {SeriesApi} from 'sentry/types';
  3. import {SamplingDistribution, SamplingSdkVersion} from 'sentry/types/sampling';
  4. import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
  5. import {CommonStoreDefinition} from './types';
  6. type ProjectStats = {
  7. data: SeriesApi | undefined;
  8. error: string | undefined;
  9. loading: boolean;
  10. };
  11. type Distribution = {
  12. data: SamplingDistribution | undefined;
  13. error: string | undefined;
  14. loading: boolean;
  15. };
  16. type SdkVersions = {
  17. data: SamplingSdkVersion[] | undefined;
  18. error: string | undefined;
  19. loading: boolean;
  20. };
  21. type State = {
  22. distribution: Distribution;
  23. projectStats30d: ProjectStats;
  24. projectStats48h: ProjectStats;
  25. sdkVersions: SdkVersions;
  26. };
  27. const initialState: State = {
  28. projectStats48h: {
  29. error: undefined,
  30. loading: false,
  31. data: undefined,
  32. },
  33. projectStats30d: {
  34. error: undefined,
  35. loading: false,
  36. data: undefined,
  37. },
  38. distribution: {
  39. error: undefined,
  40. loading: false,
  41. data: undefined,
  42. },
  43. sdkVersions: {
  44. error: undefined,
  45. loading: false,
  46. data: undefined,
  47. },
  48. };
  49. interface ServerSideSamplingStoreDefinition extends CommonStoreDefinition<State> {
  50. fetchDistribution(): void;
  51. fetchDistributionError(error: string): void;
  52. fetchDistributionSuccess(data: SamplingDistribution): void;
  53. fetchProjectStats30d(): void;
  54. fetchProjectStats30dError: (error: string) => void;
  55. fetchProjectStats30dSuccess: (data: SeriesApi) => void;
  56. fetchProjectStats48h(): void;
  57. fetchProjectStats48hError: (error: string) => void;
  58. fetchProjectStats48hSuccess: (data: SeriesApi) => void;
  59. fetchSdkVersions(): void;
  60. fetchSdkVersionsError(error: string): void;
  61. fetchSdkVersionsSuccess(data: SamplingSdkVersion[]): void;
  62. reset(): void;
  63. }
  64. const storeConfig: ServerSideSamplingStoreDefinition = {
  65. state: initialState,
  66. reset() {
  67. this.state = initialState;
  68. this.trigger(this.state);
  69. },
  70. getState() {
  71. return this.state;
  72. },
  73. fetchProjectStats48h() {
  74. this.state = {
  75. ...this.state,
  76. projectStats48h: {
  77. error: undefined,
  78. loading: true,
  79. data: undefined,
  80. },
  81. };
  82. this.trigger(this.state);
  83. },
  84. fetchProjectStats48hSuccess(data: SeriesApi) {
  85. this.state = {
  86. ...this.state,
  87. projectStats48h: {
  88. error: undefined,
  89. loading: false,
  90. data,
  91. },
  92. };
  93. this.trigger(this.state);
  94. },
  95. fetchProjectStats48hError(error: string) {
  96. this.state = {
  97. ...this.state,
  98. projectStats48h: {
  99. error,
  100. loading: false,
  101. data: undefined,
  102. },
  103. };
  104. this.trigger(this.state);
  105. },
  106. fetchProjectStats30d() {
  107. this.state = {
  108. ...this.state,
  109. projectStats30d: {
  110. error: undefined,
  111. loading: true,
  112. data: undefined,
  113. },
  114. };
  115. this.trigger(this.state);
  116. },
  117. fetchProjectStats30dSuccess(data: SeriesApi) {
  118. this.state = {
  119. ...this.state,
  120. projectStats30d: {
  121. error: undefined,
  122. loading: false,
  123. data,
  124. },
  125. };
  126. this.trigger(this.state);
  127. },
  128. fetchProjectStats30dError(error: string) {
  129. this.state = {
  130. ...this.state,
  131. projectStats30d: {
  132. error,
  133. loading: false,
  134. data: undefined,
  135. },
  136. };
  137. this.trigger(this.state);
  138. },
  139. fetchDistribution() {
  140. this.state = {
  141. ...this.state,
  142. distribution: {
  143. error: undefined,
  144. loading: true,
  145. data: undefined,
  146. },
  147. };
  148. this.trigger(this.state);
  149. },
  150. fetchDistributionSuccess(data: SamplingDistribution) {
  151. this.state = {
  152. ...this.state,
  153. distribution: {
  154. error: undefined,
  155. loading: false,
  156. data,
  157. },
  158. };
  159. this.trigger(this.state);
  160. },
  161. fetchDistributionError(error: string) {
  162. this.state = {
  163. ...this.state,
  164. distribution: {
  165. error,
  166. loading: false,
  167. data: undefined,
  168. },
  169. };
  170. this.trigger(this.state);
  171. },
  172. fetchSdkVersions() {
  173. this.state = {
  174. ...this.state,
  175. sdkVersions: {
  176. error: undefined,
  177. loading: true,
  178. data: undefined,
  179. },
  180. };
  181. this.trigger(this.state);
  182. },
  183. fetchSdkVersionsSuccess(data: SamplingSdkVersion[]) {
  184. this.state = {
  185. ...this.state,
  186. sdkVersions: {
  187. error: undefined,
  188. loading: false,
  189. data,
  190. },
  191. };
  192. this.trigger(this.state);
  193. },
  194. fetchSdkVersionsError(error: string) {
  195. this.state = {
  196. ...this.state,
  197. sdkVersions: {
  198. error,
  199. loading: false,
  200. data: undefined,
  201. },
  202. };
  203. this.trigger(this.state);
  204. },
  205. };
  206. export const ServerSideSamplingStore = createStore(makeSafeRefluxStore(storeConfig));