thunks.ts 7.8 KB

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