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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { ResourceSelectorResponse } from '@/components/ResourceSelectorModal';
  2. import { ResourceInfoTabKeys } from '@/pages/Dataset/components/ResourceInfo';
  3. import {
  4. DataSource,
  5. DatasetData,
  6. ModelData,
  7. ProjectDependency,
  8. TrainTask,
  9. } from '@/pages/Dataset/config';
  10. import { getGitUrl } from '@/utils';
  11. // 格式化日期
  12. export { formatDate } from '@/utils/date';
  13. type SelectedCodeConfig = {
  14. code_path: string;
  15. branch: string;
  16. showValue?: string; // 前端使用的
  17. show_value?: string; // 后端使用的
  18. };
  19. // 格式化数据集数组
  20. export const formatDatasets = (datasets?: DatasetData[]) => {
  21. if (!datasets || datasets.length === 0) {
  22. return undefined;
  23. }
  24. return datasets.map((item) => ({
  25. value: item.name,
  26. link: `/dataset/dataset/info/${item.id}?tab=${ResourceInfoTabKeys.Introduction}&version=${item.version}&name=${item.name}&owner=${item.owner}&identifier=${item.identifier}`,
  27. }));
  28. };
  29. // 格式化数据集
  30. export const formatDataset = (dataset?: DatasetData) => {
  31. if (!dataset) {
  32. return undefined;
  33. }
  34. return {
  35. value: dataset.name,
  36. link: `/dataset/dataset/info/${dataset.id}?tab=${ResourceInfoTabKeys.Introduction}&version=${dataset.version}&name=${dataset.name}&owner=${dataset.owner}&identifier=${dataset.identifier}`,
  37. };
  38. };
  39. // 格式化模型
  40. export const formatModel = (model: ModelData) => {
  41. if (!model) {
  42. return undefined;
  43. }
  44. return {
  45. value: model.name,
  46. link: `/dataset/model/info/${model.id}?tab=${ResourceInfoTabKeys.Introduction}&version=${model.version}&name=${model.name}&owner=${model.owner}&identifier=${model.identifier}`,
  47. };
  48. };
  49. // 格式化镜像
  50. export const formatMirror = (mirror: ResourceSelectorResponse) => {
  51. if (!mirror) {
  52. return undefined;
  53. }
  54. return mirror.path;
  55. };
  56. // 格式化代码配置
  57. export const formatCodeConfig = (project?: ProjectDependency | SelectedCodeConfig) => {
  58. if (!project) {
  59. return undefined;
  60. }
  61. // 创建表单,CodeSelect 组件返回,目前有流水线、模型部署、超参数自动寻优创建时选择了代码配置
  62. if ('code_path' in project) {
  63. const { showValue, show_value, code_path, branch } = project;
  64. return {
  65. value: showValue || show_value,
  66. url: getGitUrl(code_path, branch),
  67. };
  68. } else {
  69. // 数据集和模型的代码配置
  70. const { url, branch, name } = project;
  71. return {
  72. value: name,
  73. url: getGitUrl(url, branch),
  74. };
  75. }
  76. };
  77. // 格式化训练任务(实验实例)
  78. export const formatTrainTask = (task?: TrainTask) => {
  79. if (!task) {
  80. return undefined;
  81. }
  82. return {
  83. value: task.name,
  84. url: `/pipeline/experiment/instance/${task.workflow_id}/${task.ins_id}`,
  85. };
  86. };
  87. // 格式化数据来源
  88. export const formatSource = (source?: string) => {
  89. if (source === DataSource.Create) {
  90. return '用户上传';
  91. } else if (source === DataSource.HandExport) {
  92. return '手动导入';
  93. } else if (source === DataSource.AutoExport) {
  94. return '实验自动导入';
  95. } else if (source === DataSource.LabelStudioExport) {
  96. return '数据标注导入';
  97. }
  98. return source;
  99. };
  100. // 格式化字符串数组,以逗号分隔
  101. export const formatList = (value: string[] | null | undefined): string => {
  102. if (
  103. value === undefined ||
  104. value === null ||
  105. Array.isArray(value) === false ||
  106. value.length === 0
  107. ) {
  108. return '--';
  109. }
  110. return value.join(',');
  111. };
  112. // 格式化布尔值
  113. export const formatBoolean = (value: boolean): string => {
  114. return value ? '是' : '否';
  115. };
  116. type FormatEnumFunc = (value: string | number) => React.ReactNode;
  117. // 格式化枚举
  118. export const formatEnum = (
  119. options: { value?: string | number | null; label?: React.ReactNode }[],
  120. ): FormatEnumFunc => {
  121. return (value: string | number) => {
  122. const option = options.find((item) => item.value === value);
  123. return option && option.label ? option.label : '--';
  124. };
  125. };