serverSideSamplingStore.tsx 4.8 KB

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