You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import KFModal from '@/components/KFModal';
  2. import { ServiceRunStatus } from '@/enums';
  3. import { useComputingResource } from '@/hooks/resource';
  4. import { type ServiceVersionData } from '@/pages/ModelDeployment/types';
  5. import { getServiceVersionCompareReq } from '@/services/modelDeployment';
  6. import { isEmpty } from '@/utils';
  7. import { to } from '@/utils/promise';
  8. import { Typography, type ModalProps } from 'antd';
  9. import classNames from 'classnames';
  10. import { useEffect, useMemo, useState } from 'react';
  11. import { statusInfo } from '../ModelDeployStatusCell';
  12. import styles from './index.less';
  13. type CompareData = {
  14. differences: Record<string, any>;
  15. version1: ServiceVersionData;
  16. version2: ServiceVersionData;
  17. };
  18. type ServiceVersionDataKey = keyof ServiceVersionData;
  19. type FiledType = {
  20. key: ServiceVersionDataKey;
  21. text: string;
  22. format?: (data: any) => any;
  23. };
  24. interface VersionCompareModalProps extends Omit<ModalProps, 'onOk'> {
  25. version1: string;
  26. version2: string;
  27. }
  28. // 格式化环境变量
  29. const formatEnvText = (env: Record<string, string>) => {
  30. if (!env || Object.keys(env).length === 0) {
  31. return '--';
  32. }
  33. return Object.entries(env)
  34. .map(([key, value]) => `${key} = ${value}`)
  35. .join(',');
  36. };
  37. function VersionCompareModal({ version1, version2, ...rest }: VersionCompareModalProps) {
  38. const [compareData, setCompareData] = useState<CompareData | undefined>(undefined);
  39. const getResourceDescription = useComputingResource()[2];
  40. const fields: FiledType[] = useMemo(
  41. () => [
  42. {
  43. key: 'service_name',
  44. text: '服务名称',
  45. },
  46. {
  47. key: 'run_state',
  48. text: '状态',
  49. format: (data: any) => {
  50. return data ? statusInfo[data as ServiceRunStatus].text : '--';
  51. },
  52. },
  53. {
  54. key: 'image',
  55. text: '镜像',
  56. },
  57. {
  58. key: 'code_config',
  59. text: '代码配置',
  60. format: (data: any) => {
  61. return data?.show_value;
  62. },
  63. },
  64. {
  65. key: 'model',
  66. text: '模型',
  67. format: (data: any) => {
  68. return data?.show_value;
  69. },
  70. },
  71. {
  72. key: 'resource',
  73. text: '资源规格',
  74. format: getResourceDescription,
  75. },
  76. {
  77. key: 'replicas',
  78. text: '副本数',
  79. },
  80. {
  81. key: 'mount_path',
  82. text: '挂载路径',
  83. },
  84. {
  85. key: 'url',
  86. text: '服务URL',
  87. },
  88. {
  89. key: 'env_variables',
  90. text: '环境变量',
  91. format: formatEnvText,
  92. },
  93. {
  94. key: 'description',
  95. text: '描述',
  96. },
  97. ],
  98. [getResourceDescription],
  99. );
  100. useEffect(() => {
  101. getServiceVersionCompare();
  102. }, []);
  103. // 获取对比数据
  104. const getServiceVersionCompare = async () => {
  105. const params = {
  106. id1: version1,
  107. id2: version2,
  108. };
  109. const [res] = await to(getServiceVersionCompareReq(params));
  110. if (res && res.data) {
  111. setCompareData(res.data);
  112. }
  113. };
  114. const {
  115. version1: v1 = {} as ServiceVersionData,
  116. version2: v2 = {} as ServiceVersionData,
  117. differences = {},
  118. } = compareData || {};
  119. const isDifferent = (key: ServiceVersionDataKey) => {
  120. const keys = Object.keys(differences);
  121. return keys.includes(key);
  122. };
  123. return (
  124. <KFModal
  125. {...rest}
  126. title="服务版本对比"
  127. width={825}
  128. footer={null}
  129. className={styles['version-compare']}
  130. >
  131. <div className={styles['version-compare__container']}>
  132. <div className={styles['version-compare__fields']}>
  133. <div className={styles['version-compare__fields__title']}>基础版本号</div>
  134. {fields.map(({ key, text }) => (
  135. <div
  136. className={classNames(styles['version-compare__fields__text'], {
  137. [styles['version-compare__fields__text--different']]: isDifferent(key),
  138. })}
  139. key={key}
  140. >
  141. {text}
  142. </div>
  143. ))}
  144. </div>
  145. <div className={styles['version-compare__left']}>
  146. <div className={styles['version-compare__left__title']}>{v1.version}</div>
  147. {fields.map(({ key, format }) => {
  148. const text = format ? format(v1[key]) : v1[key];
  149. return (
  150. <div
  151. key={key}
  152. className={classNames(styles['version-compare__left__text'], {
  153. [styles['version-compare__left__text--different']]: isDifferent(key),
  154. })}
  155. >
  156. <Typography.Text ellipsis={{ tooltip: text }} style={{ color: 'inherit' }}>
  157. {isEmpty(text) ? '--' : text}
  158. </Typography.Text>
  159. </div>
  160. );
  161. })}
  162. </div>
  163. <div className={styles['version-compare__right']}>
  164. <div className={styles['version-compare__right__title']}>{v2.version}</div>
  165. {fields.map(({ key, format }) => {
  166. const text = format ? format(v2[key]) : v2[key];
  167. return (
  168. <div
  169. key={key}
  170. className={classNames(styles['version-compare__right__text'], {
  171. [styles['version-compare__right__text--different']]: isDifferent(key),
  172. })}
  173. >
  174. <Typography.Text ellipsis={{ tooltip: text }} style={{ color: 'inherit' }}>
  175. {isEmpty(text) ? '--' : text}
  176. </Typography.Text>
  177. </div>
  178. );
  179. })}
  180. </div>
  181. </div>
  182. </KFModal>
  183. );
  184. }
  185. export default VersionCompareModal;