thunks.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import {
  2. ApiClustersApi,
  3. Configuration,
  4. Cluster,
  5. Topic,
  6. TopicFormData,
  7. TopicConfig,
  8. NewSchemaSubject,
  9. SchemaSubject,
  10. } from 'generated-sources';
  11. import {
  12. ConsumerGroupID,
  13. PromiseThunkResult,
  14. ClusterName,
  15. BrokerId,
  16. TopicName,
  17. TopicMessageQueryParams,
  18. TopicFormFormattedParams,
  19. TopicFormDataRaw,
  20. SchemaName,
  21. } from 'redux/interfaces';
  22. import { BASE_PARAMS } from 'lib/constants';
  23. import * as actions from './actions';
  24. const apiClientConf = new Configuration(BASE_PARAMS);
  25. export const apiClient = new ApiClustersApi(apiClientConf);
  26. export const fetchClustersList = (): PromiseThunkResult => async (dispatch) => {
  27. dispatch(actions.fetchClusterListAction.request());
  28. try {
  29. const clusters: Cluster[] = await apiClient.getClusters();
  30. dispatch(actions.fetchClusterListAction.success(clusters));
  31. } catch (e) {
  32. dispatch(actions.fetchClusterListAction.failure());
  33. }
  34. };
  35. export const fetchClusterStats = (
  36. clusterName: ClusterName
  37. ): PromiseThunkResult => async (dispatch) => {
  38. dispatch(actions.fetchClusterStatsAction.request());
  39. try {
  40. const payload = await apiClient.getClusterStats({ clusterName });
  41. dispatch(actions.fetchClusterStatsAction.success(payload));
  42. } catch (e) {
  43. dispatch(actions.fetchClusterStatsAction.failure());
  44. }
  45. };
  46. export const fetchClusterMetrics = (
  47. clusterName: ClusterName
  48. ): PromiseThunkResult => async (dispatch) => {
  49. dispatch(actions.fetchClusterMetricsAction.request());
  50. try {
  51. const payload = await apiClient.getClusterMetrics({ clusterName });
  52. dispatch(actions.fetchClusterMetricsAction.success(payload));
  53. } catch (e) {
  54. dispatch(actions.fetchClusterMetricsAction.failure());
  55. }
  56. };
  57. export const fetchBrokers = (
  58. clusterName: ClusterName
  59. ): PromiseThunkResult => async (dispatch) => {
  60. dispatch(actions.fetchBrokersAction.request());
  61. try {
  62. const payload = await apiClient.getBrokers({ clusterName });
  63. dispatch(actions.fetchBrokersAction.success(payload));
  64. } catch (e) {
  65. dispatch(actions.fetchBrokersAction.failure());
  66. }
  67. };
  68. export const fetchBrokerMetrics = (
  69. clusterName: ClusterName,
  70. brokerId: BrokerId
  71. ): PromiseThunkResult => async (dispatch) => {
  72. dispatch(actions.fetchBrokerMetricsAction.request());
  73. try {
  74. const payload = await apiClient.getBrokersMetrics({
  75. clusterName,
  76. id: brokerId,
  77. });
  78. dispatch(actions.fetchBrokerMetricsAction.success(payload));
  79. } catch (e) {
  80. dispatch(actions.fetchBrokerMetricsAction.failure());
  81. }
  82. };
  83. export const fetchTopicsList = (
  84. clusterName: ClusterName
  85. ): PromiseThunkResult => async (dispatch) => {
  86. dispatch(actions.fetchTopicsListAction.request());
  87. try {
  88. const topics = await apiClient.getTopics({ clusterName });
  89. dispatch(actions.fetchTopicsListAction.success(topics));
  90. } catch (e) {
  91. dispatch(actions.fetchTopicsListAction.failure());
  92. }
  93. };
  94. export const fetchTopicMessages = (
  95. clusterName: ClusterName,
  96. topicName: TopicName,
  97. queryParams: Partial<TopicMessageQueryParams>
  98. ): PromiseThunkResult => async (dispatch) => {
  99. dispatch(actions.fetchTopicMessagesAction.request());
  100. try {
  101. const messages = await apiClient.getTopicMessages({
  102. clusterName,
  103. topicName,
  104. ...queryParams,
  105. });
  106. dispatch(actions.fetchTopicMessagesAction.success(messages));
  107. } catch (e) {
  108. dispatch(actions.fetchTopicMessagesAction.failure());
  109. }
  110. };
  111. export const fetchTopicDetails = (
  112. clusterName: ClusterName,
  113. topicName: TopicName
  114. ): PromiseThunkResult => async (dispatch) => {
  115. dispatch(actions.fetchTopicDetailsAction.request());
  116. try {
  117. const topicDetails = await apiClient.getTopicDetails({
  118. clusterName,
  119. topicName,
  120. });
  121. dispatch(
  122. actions.fetchTopicDetailsAction.success({
  123. topicName,
  124. details: topicDetails,
  125. })
  126. );
  127. } catch (e) {
  128. dispatch(actions.fetchTopicDetailsAction.failure());
  129. }
  130. };
  131. export const fetchTopicConfig = (
  132. clusterName: ClusterName,
  133. topicName: TopicName
  134. ): PromiseThunkResult => async (dispatch) => {
  135. dispatch(actions.fetchTopicConfigAction.request());
  136. try {
  137. const config = await apiClient.getTopicConfigs({ clusterName, topicName });
  138. dispatch(actions.fetchTopicConfigAction.success({ topicName, config }));
  139. } catch (e) {
  140. dispatch(actions.fetchTopicConfigAction.failure());
  141. }
  142. };
  143. const formatTopicFormData = (form: TopicFormDataRaw): TopicFormData => {
  144. const {
  145. name,
  146. partitions,
  147. replicationFactor,
  148. cleanupPolicy,
  149. retentionBytes,
  150. retentionMs,
  151. maxMessageBytes,
  152. minInSyncReplicas,
  153. customParams,
  154. } = form;
  155. return {
  156. name,
  157. partitions,
  158. replicationFactor,
  159. configs: {
  160. 'cleanup.policy': cleanupPolicy,
  161. 'retention.ms': retentionMs,
  162. 'retention.bytes': retentionBytes,
  163. 'max.message.bytes': maxMessageBytes,
  164. 'min.insync.replicas': minInSyncReplicas,
  165. ...Object.values(customParams || {}).reduce(
  166. (result: TopicFormFormattedParams, customParam: TopicConfig) => {
  167. return {
  168. ...result,
  169. [customParam.name]: customParam.value,
  170. };
  171. },
  172. {}
  173. ),
  174. },
  175. };
  176. };
  177. export const createTopic = (
  178. clusterName: ClusterName,
  179. form: TopicFormDataRaw
  180. ): PromiseThunkResult => async (dispatch) => {
  181. dispatch(actions.createTopicAction.request());
  182. try {
  183. const topic: Topic = await apiClient.createTopic({
  184. clusterName,
  185. topicFormData: formatTopicFormData(form),
  186. });
  187. dispatch(actions.createTopicAction.success(topic));
  188. } catch (e) {
  189. dispatch(actions.createTopicAction.failure());
  190. }
  191. };
  192. export const updateTopic = (
  193. clusterName: ClusterName,
  194. form: TopicFormDataRaw
  195. ): PromiseThunkResult => async (dispatch) => {
  196. dispatch(actions.updateTopicAction.request());
  197. try {
  198. const topic: Topic = await apiClient.updateTopic({
  199. clusterName,
  200. topicName: form.name,
  201. topicFormData: formatTopicFormData(form),
  202. });
  203. dispatch(actions.updateTopicAction.success(topic));
  204. } catch (e) {
  205. dispatch(actions.updateTopicAction.failure());
  206. }
  207. };
  208. export const fetchConsumerGroupsList = (
  209. clusterName: ClusterName
  210. ): PromiseThunkResult => async (dispatch) => {
  211. dispatch(actions.fetchConsumerGroupsAction.request());
  212. try {
  213. const consumerGroups = await apiClient.getConsumerGroups({ clusterName });
  214. dispatch(actions.fetchConsumerGroupsAction.success(consumerGroups));
  215. } catch (e) {
  216. dispatch(actions.fetchConsumerGroupsAction.failure());
  217. }
  218. };
  219. export const fetchConsumerGroupDetails = (
  220. clusterName: ClusterName,
  221. consumerGroupID: ConsumerGroupID
  222. ): PromiseThunkResult => async (dispatch) => {
  223. dispatch(actions.fetchConsumerGroupDetailsAction.request());
  224. try {
  225. const consumerGroupDetails = await apiClient.getConsumerGroup({
  226. clusterName,
  227. id: consumerGroupID,
  228. });
  229. dispatch(
  230. actions.fetchConsumerGroupDetailsAction.success({
  231. consumerGroupID,
  232. details: consumerGroupDetails,
  233. })
  234. );
  235. } catch (e) {
  236. dispatch(actions.fetchConsumerGroupDetailsAction.failure());
  237. }
  238. };
  239. export const fetchSchemasByClusterName = (
  240. clusterName: ClusterName
  241. ): PromiseThunkResult<void> => async (dispatch) => {
  242. dispatch(actions.fetchSchemasByClusterNameAction.request());
  243. try {
  244. const schemas = await apiClient.getSchemas({ clusterName });
  245. dispatch(actions.fetchSchemasByClusterNameAction.success(schemas));
  246. } catch (e) {
  247. dispatch(actions.fetchSchemasByClusterNameAction.failure());
  248. }
  249. };
  250. export const fetchSchemaVersions = (
  251. clusterName: ClusterName,
  252. subject: SchemaName
  253. ): PromiseThunkResult<void> => async (dispatch) => {
  254. if (!subject) return;
  255. dispatch(actions.fetchSchemaVersionsAction.request());
  256. try {
  257. const versions = await apiClient.getAllVersionsBySubject({
  258. clusterName,
  259. subject,
  260. });
  261. dispatch(actions.fetchSchemaVersionsAction.success(versions));
  262. } catch (e) {
  263. dispatch(actions.fetchSchemaVersionsAction.failure());
  264. }
  265. };
  266. export const createSchema = (
  267. clusterName: ClusterName,
  268. subject: SchemaName,
  269. newSchemaSubject: NewSchemaSubject
  270. ): PromiseThunkResult => async (dispatch) => {
  271. dispatch(actions.createSchemaAction.request());
  272. try {
  273. const schema: SchemaSubject = await apiClient.createNewSchema({
  274. clusterName,
  275. subject,
  276. newSchemaSubject,
  277. });
  278. dispatch(actions.createSchemaAction.success(schema));
  279. } catch (e) {
  280. dispatch(actions.createSchemaAction.failure());
  281. throw e;
  282. }
  283. };