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.

format.ts 5.0 kB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { BasicInfoLink } from '@/components/BasicInfo/types';
  2. import { ResourceSelectorResponse } from '@/components/ResourceSelectorModal';
  3. import { ResourceInfoTabKeys } from '@/pages/Dataset/components/ResourceInfo';
  4. import {
  5. DataSource,
  6. DatasetData,
  7. ModelData,
  8. ProjectDependency,
  9. TrainTask,
  10. } from '@/pages/Dataset/config';
  11. import { getGitUrl } from '@/utils';
  12. // 格式化日期
  13. export { formatDate } from '@/utils/date';
  14. type SelectedCodeConfig = {
  15. code_path: string;
  16. branch: string;
  17. showValue?: string; // 前端使用的
  18. show_value?: string; // 后端使用的
  19. };
  20. /**
  21. * 格式化数据集数组
  22. *
  23. * @param datasets - 数据集数组
  24. * @return 基本信息链接对象数组
  25. */
  26. export const formatDatasets = (datasets?: DatasetData[]): BasicInfoLink[] | undefined => {
  27. if (!datasets || datasets.length === 0) {
  28. return undefined;
  29. }
  30. return datasets.map((item) => ({
  31. value: item.name,
  32. link: `/dataset/dataset/info/${item.id}?tab=${ResourceInfoTabKeys.Introduction}&version=${item.version}&name=${item.name}&owner=${item.owner}&identifier=${item.identifier}`,
  33. }));
  34. };
  35. /**
  36. * 格式化数据集
  37. *
  38. * @param dataset - 数据集
  39. * @return 基本信息链接对象
  40. */
  41. export const formatDataset = (dataset?: DatasetData): BasicInfoLink | undefined => {
  42. if (!dataset) {
  43. return undefined;
  44. }
  45. return {
  46. value: dataset.name,
  47. link: `/dataset/dataset/info/${dataset.id}?tab=${ResourceInfoTabKeys.Introduction}&version=${dataset.version}&name=${dataset.name}&owner=${dataset.owner}&identifier=${dataset.identifier}`,
  48. };
  49. };
  50. /**
  51. * 格式化模型
  52. *
  53. * @param model - 模型
  54. * @return 基本信息链接对象
  55. */
  56. export const formatModel = (model: ModelData): BasicInfoLink | undefined => {
  57. if (!model) {
  58. return undefined;
  59. }
  60. return {
  61. value: model.name,
  62. link: `/dataset/model/info/${model.id}?tab=${ResourceInfoTabKeys.Introduction}&version=${model.version}&name=${model.name}&owner=${model.owner}&identifier=${model.identifier}`,
  63. };
  64. };
  65. /**
  66. * 格式化镜像
  67. *
  68. * @param mirror - 选择的镜像
  69. * @return 镜像地址
  70. */
  71. export const formatMirror = (mirror: ResourceSelectorResponse): string | undefined => {
  72. if (!mirror) {
  73. return undefined;
  74. }
  75. return mirror.path;
  76. };
  77. /**
  78. * 格式化代码配置
  79. *
  80. * @param project - 代码配置或者选择的代码配置
  81. * @return 基本信息链接对象
  82. */
  83. export const formatCodeConfig = (
  84. project?: ProjectDependency | SelectedCodeConfig,
  85. ): BasicInfoLink | undefined => {
  86. if (!project) {
  87. return undefined;
  88. }
  89. // 创建表单,CodeSelect 组件返回,目前有流水线、模型部署、超参数自动寻优创建时选择了代码配置
  90. if ('code_path' in project) {
  91. const { showValue, show_value, code_path, branch } = project;
  92. return {
  93. value: showValue || show_value,
  94. url: getGitUrl(code_path, branch),
  95. };
  96. } else {
  97. // 数据集和模型的代码配置
  98. const { url, branch, name } = project;
  99. return {
  100. value: name,
  101. url: getGitUrl(url, branch),
  102. };
  103. }
  104. };
  105. /**
  106. * 格式化训练任务(实验实例)
  107. *
  108. * @param task - 训练任务
  109. * @return 基本信息链接对象
  110. */
  111. export const formatTrainTask = (task?: TrainTask) => {
  112. if (!task) {
  113. return undefined;
  114. }
  115. return {
  116. value: task.name,
  117. url: `/pipeline/experiment/instance/${task.workflow_id}/${task.ins_id}`,
  118. };
  119. };
  120. /**
  121. * 格式化数据来源
  122. *
  123. * @param source - 数据来源枚举值
  124. * @return 数据来源中文名称
  125. */
  126. export const formatSource = (source?: string): string | undefined => {
  127. if (source === DataSource.Create) {
  128. return '用户上传';
  129. } else if (source === DataSource.HandExport) {
  130. return '手动导入';
  131. } else if (source === DataSource.AutoExport) {
  132. return '实验自动导入';
  133. } else if (source === DataSource.LabelStudioExport) {
  134. return '数据标注导入';
  135. }
  136. return source;
  137. };
  138. /**
  139. * 格式化字符串数组,以逗号分隔
  140. *
  141. * @param value - 字符串数组
  142. * @return 字符串,以逗号分隔
  143. */
  144. export const formatList = (value: string[] | null | undefined): string => {
  145. if (
  146. value === undefined ||
  147. value === null ||
  148. Array.isArray(value) === false ||
  149. value.length === 0
  150. ) {
  151. return '--';
  152. }
  153. return value.join(',');
  154. };
  155. /**
  156. * 格式化布尔值
  157. *
  158. * @param value - 布尔值
  159. * @return true 为 "是",false 为 "否"
  160. */
  161. export const formatBoolean = (value: boolean): string => {
  162. return value ? '是' : '否';
  163. };
  164. type FormatEnumFunc = (value: string | number) => React.ReactNode;
  165. /**
  166. * 格式化枚举
  167. *
  168. * @param options - 枚举选项数组
  169. * @return 一个函数,参数是枚举值,从选项数组中找到对应的项,然后返回该项的 label
  170. */
  171. export const formatEnum = (
  172. options: { value?: string | number | null; label?: React.ReactNode }[],
  173. ): FormatEnumFunc => {
  174. return (value: string | number) => {
  175. const option = options.find((item) => item.value === value);
  176. return option && option.label ? option.label : '--';
  177. };
  178. };