thunks.ts 8.8 KB

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