topicParamsTransformer.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { TopicWithDetailedInfo } from 'redux/interfaces';
  2. import {
  3. MILLISECONDS_IN_WEEK,
  4. TOPIC_CUSTOM_PARAMS,
  5. TOPIC_CUSTOM_PARAMS_PREFIX,
  6. } from 'lib/constants';
  7. import { DEFAULTS } from 'components/Topics/Topic/Edit/Edit';
  8. export const getValue = (
  9. topic: TopicWithDetailedInfo,
  10. fieldName: string,
  11. defaultValue?: number
  12. ) =>
  13. Number(topic?.config?.find((config) => config.name === fieldName)?.value) ||
  14. defaultValue;
  15. const topicParamsTransformer = (topic?: TopicWithDetailedInfo) => {
  16. if (!topic) {
  17. return DEFAULTS;
  18. }
  19. const { name, replicationFactor } = topic;
  20. return {
  21. ...DEFAULTS,
  22. name,
  23. replicationFactor,
  24. partitions: topic.partitionCount || DEFAULTS.partitions,
  25. maxMessageBytes: getValue(topic, 'max.message.bytes', 1000012),
  26. minInSyncReplicas: getValue(topic, 'min.insync.replicas', 1),
  27. retentionBytes: getValue(topic, 'retention.bytes', -1),
  28. retentionMs: getValue(topic, 'retention.ms', MILLISECONDS_IN_WEEK),
  29. [TOPIC_CUSTOM_PARAMS_PREFIX]: topic.config
  30. ?.filter(
  31. (el) =>
  32. el.value !== el.defaultValue &&
  33. Object.keys(TOPIC_CUSTOM_PARAMS).includes(el.name)
  34. )
  35. .map((el) => ({ name: el.name, value: el.value })),
  36. };
  37. };
  38. export default topicParamsTransformer;