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 20 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * @Author: 赵伟
  3. * @Date: 2024-04-16 13:58:08
  4. * @Description: 创建服务版本
  5. */
  6. import PageTitle from '@/components/PageTitle';
  7. import ParameterSelect from '@/components/ParameterSelect';
  8. import ResourceSelect, {
  9. ResourceSelectorType,
  10. requiredValidator,
  11. type ParameterInputObject,
  12. } from '@/components/ResourceSelect';
  13. import SubAreaTitle from '@/components/SubAreaTitle';
  14. import {
  15. createServiceVersionReq,
  16. getServiceInfoReq,
  17. updateServiceVersionReq,
  18. } from '@/services/modelDeployment';
  19. import { changePropertyName } from '@/utils';
  20. import { to } from '@/utils/promise';
  21. import SessionStorage from '@/utils/sessionStorage';
  22. import { removeFormListItem } from '@/utils/ui';
  23. import { MinusCircleOutlined, PlusCircleOutlined, PlusOutlined } from '@ant-design/icons';
  24. import { useNavigate, useParams } from '@umijs/max';
  25. import { App, Button, Col, Flex, Form, Input, InputNumber, Row } from 'antd';
  26. import { omit, pick } from 'lodash';
  27. import { useEffect, useState } from 'react';
  28. import { CreateServiceVersionFrom, ServiceOperationType, ServiceVersionData } from '../types';
  29. import styles from './index.less';
  30. // 表单数据
  31. export type FormData = {
  32. service_name: string; // 服务名称
  33. version: string; // 服务版本
  34. description: string; // 描述
  35. model: ParameterInputObject; // 模型
  36. image: ParameterInputObject; // 镜像
  37. // code_config: ParameterInputObject; // 代码
  38. resource: string; // 资源规格
  39. replicas: string; // 副本数量
  40. mount_path: string; // 模型路径
  41. env_variables: { key: string; value: string }[]; // 环境变量
  42. };
  43. type ServiceVersionCache = ServiceVersionData & {
  44. operationType: ServiceOperationType;
  45. lastPage: CreateServiceVersionFrom;
  46. };
  47. // 参数表单数据
  48. export type FormEnvVariable = {
  49. key: string; // 参数名
  50. value: string; // 参数值
  51. };
  52. function CreateServiceVersion() {
  53. const navigate = useNavigate();
  54. const [form] = Form.useForm();
  55. const [operationType, setOperationType] = useState(ServiceOperationType.Create);
  56. const [lastPage, setLastPage] = useState(CreateServiceVersionFrom.ServiceInfo);
  57. const { message } = App.useApp();
  58. const [versionInfo, setVersionInfo] = useState<ServiceVersionData | undefined>(undefined);
  59. const params = useParams();
  60. const serviceId = params.serviceId;
  61. // 因为没有服务版本详情接口,需要从缓存中获取
  62. useEffect(() => {
  63. const res: ServiceVersionCache | undefined = SessionStorage.getItem(
  64. SessionStorage.serviceVersionInfoKey,
  65. true,
  66. );
  67. if (res) {
  68. setOperationType(res.operationType);
  69. setLastPage(res.lastPage);
  70. setVersionInfo(res);
  71. let model, envVariables;
  72. // 模型
  73. if (res.model && typeof res.model === 'object') {
  74. model = changePropertyName(res.model, { show_value: 'showValue' });
  75. // 接口返回是数据没有 value 值,但是 form 需要 value
  76. model.value = model.showValue;
  77. }
  78. // 环境变量
  79. if (res.env_variables && typeof res.env_variables === 'object') {
  80. envVariables = Object.entries(res.env_variables).map(([key, value]) => ({
  81. key,
  82. value,
  83. }));
  84. }
  85. const formData = {
  86. ...omit(res, 'model', 'env_variables'),
  87. model: model,
  88. env_variables: envVariables,
  89. };
  90. form.setFieldsValue(formData);
  91. }
  92. return () => {
  93. SessionStorage.removeItem(SessionStorage.serviceVersionInfoKey);
  94. };
  95. }, [form]);
  96. useEffect(() => {
  97. // 获取服务详情,设置服务名称
  98. const getServiceInfo = async () => {
  99. const [res] = await to(getServiceInfoReq(serviceId));
  100. if (res && res.data) {
  101. form.setFieldsValue({
  102. service_name: res.data.service_name,
  103. });
  104. }
  105. };
  106. getServiceInfo();
  107. }, [serviceId, form]);
  108. // 创建版本
  109. const createServiceVersion = async (formData: FormData) => {
  110. const envList = formData['env_variables'];
  111. const model = formData['model'];
  112. const envVariables = envList?.reduce((acc, cur) => {
  113. acc[cur.key] = cur.value;
  114. return acc;
  115. }, {} as Record<string, string>);
  116. // 根据后台要求,修改表单数据
  117. const object = {
  118. ...omit(formData, ['replicas', 'env_variables', 'model']),
  119. replicas: Number(formData.replicas),
  120. env_variables: envVariables,
  121. model: changePropertyName(
  122. pick(model, ['id', 'name', 'version', 'path', 'identifier', 'owner', 'showValue']),
  123. { showValue: 'show_value' },
  124. ),
  125. service_id: serviceId,
  126. };
  127. const params =
  128. operationType === ServiceOperationType.Create
  129. ? {
  130. ...object,
  131. deploy_type: 'web',
  132. }
  133. : {
  134. id: versionInfo?.id,
  135. rerun: operationType === ServiceOperationType.Restart ? true : false,
  136. deployment_name: versionInfo?.deployment_name,
  137. ...object,
  138. };
  139. const request =
  140. operationType === ServiceOperationType.Create
  141. ? createServiceVersionReq
  142. : updateServiceVersionReq;
  143. const [res] = await to(request(params));
  144. if (res) {
  145. message.success('操作成功');
  146. if (lastPage === CreateServiceVersionFrom.ServiceInfo) {
  147. navigate(-1);
  148. } else {
  149. navigate(`/dataset/modelDeployment/serviceInfo/${serviceId}`, { replace: true });
  150. }
  151. }
  152. };
  153. // 提交
  154. const handleSubmit = (values: FormData) => {
  155. createServiceVersion(values);
  156. };
  157. // 取消
  158. const cancel = () => {
  159. navigate(-1);
  160. };
  161. const disabled = operationType !== ServiceOperationType.Create;
  162. let buttonText = '新建';
  163. let title = '新增服务版本';
  164. if (operationType === ServiceOperationType.Update) {
  165. title = '更新服务版本';
  166. buttonText = '更新';
  167. } else if (operationType === ServiceOperationType.Restart) {
  168. title = '重启服务版本';
  169. buttonText = '重启';
  170. }
  171. return (
  172. <div className={styles['create-service-version']}>
  173. <PageTitle title={title}></PageTitle>
  174. <div className={styles['create-service-version__content']}>
  175. <div>
  176. <Form
  177. name="create-service-version"
  178. labelCol={{ flex: '100px' }}
  179. labelAlign="left"
  180. form={form}
  181. onFinish={handleSubmit}
  182. size="large"
  183. autoComplete="off"
  184. >
  185. <SubAreaTitle
  186. title="基本信息"
  187. image={require('@/assets/img/mirror-basic.png')}
  188. style={{ marginBottom: '26px' }}
  189. ></SubAreaTitle>
  190. <Row gutter={8}>
  191. <Col span={10}>
  192. <Form.Item
  193. label="服务名称"
  194. name="service_name"
  195. rules={[
  196. {
  197. required: true,
  198. message: '请输入服务名称',
  199. },
  200. ]}
  201. >
  202. <Input
  203. placeholder="请输入服务名称"
  204. maxLength={30}
  205. disabled
  206. showCount
  207. allowClear
  208. />
  209. </Form.Item>
  210. </Col>
  211. </Row>
  212. <Row gutter={8}>
  213. <Col span={10}>
  214. <Form.Item
  215. label="服务版本"
  216. name="version"
  217. rules={[
  218. {
  219. required: true,
  220. message: '请输入服务版本',
  221. },
  222. {
  223. pattern: /^[a-zA-Z0-9._-]+$/,
  224. message: '服务支持字母、数字、点(.)、下划线(_)、中横线(-)',
  225. },
  226. ]}
  227. >
  228. <Input
  229. placeholder="请输入服务版本"
  230. maxLength={30}
  231. disabled={disabled}
  232. showCount
  233. allowClear
  234. />
  235. </Form.Item>
  236. </Col>
  237. </Row>
  238. <Row gutter={8}>
  239. <Col span={20}>
  240. <Form.Item
  241. label="版本描述"
  242. name="description"
  243. rules={[
  244. {
  245. required: true,
  246. message: '请输入版本描述',
  247. },
  248. ]}
  249. >
  250. <Input.TextArea
  251. autoSize={{ minRows: 2, maxRows: 6 }}
  252. placeholder="请输入版本描述,最长128字符"
  253. maxLength={128}
  254. showCount
  255. allowClear
  256. />
  257. </Form.Item>
  258. </Col>
  259. </Row>
  260. <SubAreaTitle
  261. title="部署构建"
  262. image={require('@/assets/img/model-deployment.png')}
  263. style={{ marginTop: '20px', marginBottom: '24px' }}
  264. ></SubAreaTitle>
  265. <Row gutter={8}>
  266. <Col span={10}>
  267. <Form.Item
  268. label="选择模型"
  269. name="model"
  270. rules={[
  271. {
  272. validator: requiredValidator,
  273. message: '请选择模型',
  274. },
  275. ]}
  276. required
  277. >
  278. <ResourceSelect
  279. type={ResourceSelectorType.Model}
  280. placeholder="请选择模型"
  281. disabled={disabled}
  282. canInput={false}
  283. />
  284. </Form.Item>
  285. </Col>
  286. </Row>
  287. <Row gutter={8}>
  288. <Col span={10}>
  289. <Form.Item
  290. label="选择镜像"
  291. name="image"
  292. rules={[
  293. {
  294. validator: requiredValidator,
  295. message: '请选择镜像',
  296. },
  297. ]}
  298. required
  299. >
  300. <ResourceSelect
  301. type={ResourceSelectorType.Mirror}
  302. placeholder="请选择镜像"
  303. canInput={false}
  304. disabled={disabled}
  305. />
  306. </Form.Item>
  307. </Col>
  308. </Row>
  309. {/* <Row gutter={8}>
  310. <Col span={10}>
  311. <Form.Item
  312. label="代码配置"
  313. name="code_config"
  314. tooltip="此处代码配置为可引用的代码配置,具体代码编写指南见用户手册中服务部署模块"
  315. >
  316. <CodeSelect
  317. placeholder="请选择代码配置"
  318. canInput={false}
  319. size="large"
  320. disabled={disabled}
  321. />
  322. </Form.Item>
  323. </Col>
  324. </Row> */}
  325. <Row gutter={8}>
  326. <Col span={10}>
  327. <Form.Item
  328. label="资源规格"
  329. name="computing_resource_id"
  330. rules={[
  331. {
  332. required: true,
  333. message: '请选择资源规格',
  334. },
  335. ]}
  336. >
  337. <ParameterSelect dataType="resource" placeholder="请选择资源规格" />
  338. </Form.Item>
  339. </Col>
  340. </Row>
  341. <Row gutter={8}>
  342. <Col span={10}>
  343. <Form.Item
  344. label="副本数量"
  345. name="replicas"
  346. rules={[
  347. {
  348. required: true,
  349. message: '请输入副本数量',
  350. },
  351. {
  352. pattern: /^[1-9]\d*$/,
  353. message: '副本数量必须是正整数',
  354. },
  355. ]}
  356. >
  357. <InputNumber
  358. style={{ width: '100%' }}
  359. placeholder="请输入副本数量"
  360. min={1}
  361. precision={0}
  362. />
  363. </Form.Item>
  364. </Col>
  365. </Row>
  366. <Row gutter={8}>
  367. <Col span={10}>
  368. <Form.Item
  369. label="挂载路径"
  370. name="mount_path"
  371. rules={[
  372. {
  373. required: true,
  374. message: '请输入模型挂载路径',
  375. },
  376. {
  377. pattern: /^\/[a-zA-Z0-9._/-]+$/,
  378. message:
  379. '请输入正确的挂载路径,以斜杠(/)开头,只支持字母、数字、点(.)、下划线(_)、中横线(-)、斜杠(/)',
  380. },
  381. ]}
  382. >
  383. <Input
  384. placeholder="请输入模型挂载路径"
  385. disabled={disabled}
  386. maxLength={64}
  387. showCount
  388. allowClear
  389. />
  390. </Form.Item>
  391. </Col>
  392. </Row>
  393. <Row gutter={8}>
  394. <Col span={10}>
  395. <Form.Item label="环境变量">
  396. <Form.List name="env_variables">
  397. {(fields, { add, remove }) => (
  398. <>
  399. {fields.map(({ key, name, ...restField }, index) => (
  400. <Flex
  401. key={key}
  402. gap="0 8px"
  403. style={{
  404. position: 'relative',
  405. }}
  406. >
  407. <Form.Item
  408. {...restField}
  409. name={[name, 'key']}
  410. style={{ flex: 1 }}
  411. rules={[
  412. {
  413. validator: (_, value) => {
  414. if (!value) {
  415. return Promise.reject(new Error('请输入变量名'));
  416. }
  417. if (!/^[a-zA-Z_][a-zA-Z0-9_-]*$/.test(value)) {
  418. return Promise.reject(
  419. new Error(
  420. '变量名只支持字母、数字、下划线、中横线并且必须以字母或下划线开头',
  421. ),
  422. );
  423. }
  424. // 判断不能重名
  425. const list = form
  426. .getFieldValue('env_variables')
  427. .filter(
  428. (item: FormEnvVariable | undefined) =>
  429. item !== undefined && item !== null,
  430. );
  431. const names = list.map((item: FormEnvVariable) => item.key);
  432. if (new Set(names).size !== names.length) {
  433. return Promise.reject(new Error('名称不能重复'));
  434. }
  435. return Promise.resolve();
  436. },
  437. },
  438. ]}
  439. >
  440. <Input placeholder="请输入变量名" disabled={disabled} allowClear />
  441. </Form.Item>
  442. <span style={{ lineHeight: '46px' }}>=</span>
  443. <Form.Item
  444. {...restField}
  445. name={[name, 'value']}
  446. style={{ flex: 1 }}
  447. rules={[{ required: true, message: '请输入变量值' }]}
  448. >
  449. <Input placeholder="请输入变量值" disabled={disabled} allowClear />
  450. </Form.Item>
  451. <Flex
  452. style={{
  453. width: '76px',
  454. left: 'calc(100% + 10px)',
  455. height: '46px',
  456. marginBottom: '24px',
  457. position: 'absolute',
  458. }}
  459. align="center"
  460. >
  461. <Button
  462. style={{
  463. marginRight: '3px',
  464. }}
  465. shape="circle"
  466. size="middle"
  467. type="text"
  468. icon={<MinusCircleOutlined />}
  469. disabled={disabled}
  470. onClick={() => {
  471. removeFormListItem(
  472. form,
  473. 'env_variables',
  474. name,
  475. remove,
  476. ['key', 'value'],
  477. '删除后,该环境变量将不可恢复',
  478. );
  479. }}
  480. ></Button>
  481. {index === fields.length - 1 && (
  482. <Button
  483. shape="circle"
  484. size="middle"
  485. type="text"
  486. disabled={disabled}
  487. icon={<PlusCircleOutlined />}
  488. onClick={() => add()}
  489. ></Button>
  490. )}
  491. </Flex>
  492. </Flex>
  493. ))}
  494. {fields.length === 0 && (
  495. <Form.Item className={styles['create-service-version__content__env']}>
  496. <Button
  497. className={styles['create-service-version__content__env__add-btn']}
  498. color="primary"
  499. variant="dashed"
  500. disabled={disabled}
  501. icon={<PlusOutlined />}
  502. block
  503. onClick={() => add()}
  504. >
  505. 环境变量
  506. </Button>
  507. </Form.Item>
  508. )}
  509. </>
  510. )}
  511. </Form.List>
  512. </Form.Item>
  513. </Col>
  514. </Row>
  515. <Form.Item wrapperCol={{ offset: 0, span: 16 }} style={{ marginTop: '20px' }}>
  516. <Button type="primary" htmlType="submit">
  517. {buttonText}
  518. </Button>
  519. <Button
  520. type="default"
  521. htmlType="button"
  522. onClick={cancel}
  523. style={{ marginLeft: '20px' }}
  524. >
  525. 取消
  526. </Button>
  527. </Form.Item>
  528. </Form>
  529. </div>
  530. </div>
  531. </div>
  532. );
  533. }
  534. export default CreateServiceVersion;