Compare commits

...

12 Commits

32 changed files with 1449 additions and 114 deletions

View File

@ -0,0 +1,57 @@
import random
import sys
def quick_sort(arr):
"""
快速排序主函数,支持空数组处理
"""
if len(arr) <= 1:
return arr
def partition(low, high):
pivot_index = random.randint(low, high) # 随机选择基准
arr[pivot_index], arr[high] = arr[high], arr[pivot_index]
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i+1], arr[high] = arr[high], arr[i+1]
return i + 1
def recur_sort(low, high):
if low < high:
pi = partition(low, high)
recur_sort(low, pi - 1)
recur_sort(pi + 1, high)
recur_sort(0, len(arr) - 1)
return arr
if __name__ == "__main__":
# 从命令行参数读取数据
if len(sys.argv) > 1:
try:
# 处理多种输入格式:逗号分隔、空格分隔或混合分隔
input_str = " ".join(sys.argv[1:])
input_data = [float(x) if '.' in x else int(x)
for x in input_str.replace(',', ' ').split()]
print("原始输入:", sys.argv[1:])
print("解析数据:", input_data)
sorted_arr = quick_sort(input_data.copy())
print("排序结果:", sorted_arr)
except ValueError:
print("错误:输入数据包含非数字字符,请确保只输入数字")
print("用法: python script.py [数字1 数字2 ...]")
print("示例: python script.py 3 0 8 7 2 1 9 4")
else:
print("未提供输入数据,使用默认测试用例")
test_case = [3, 0, 8, 7, 2, 1, 9, 4]
print("测试数据:", test_case)
print("排序结果:", quick_sort(test_case.copy()))

View File

@ -0,0 +1,57 @@
import random
import sys
def quick_sort(arr):
"""
快速排序主函数,支持空数组处理
"""
if len(arr) <= 1:
return arr
def partition(low, high):
pivot_index = random.randint(low, high) # 随机选择基准
arr[pivot_index], arr[high] = arr[high], arr[pivot_index]
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i+1], arr[high] = arr[high], arr[i+1]
return i + 1
def recur_sort(low, high):
if low < high:
pi = partition(low, high)
recur_sort(low, pi - 1)
recur_sort(pi + 1, high)
recur_sort(0, len(arr) - 1)
return arr
if __name__ == "__main__":
# 从命令行参数读取数据
if len(sys.argv) > 1:
try:
# 处理多种输入格式:逗号分隔、空格分隔或混合分隔
input_str = " ".join(sys.argv[1:])
input_data = [float(x) if '.' in x else int(x)
for x in input_str.replace(',', ' ').split()]
print("原始输入:", sys.argv[1:])
print("解析数据:", input_data)
sorted_arr = quick_sort(input_data.copy())
print("排序结果:", sorted_arr)
except ValueError:
print("错误:输入数据包含非数字字符,请确保只输入数字")
print("用法: python script.py [数字1 数字2 ...]")
print("示例: python script.py 3 0 8 7 2 1 9 4")
else:
print("未提供输入数据,使用默认测试用例")
test_case = [3, 0, 8, 7, 2, 1, 9, 4]
print("测试数据:", test_case)
print("排序结果:", quick_sort(test_case.copy()))

View File

@ -0,0 +1,412 @@
### 零、数据库测试表test_simple)
#### 1.基本信息
所属模块:仅供创建数据库时,测试是否连接成功
负责人:宁欢
创建时间2025-05-13
#### 2.表结构
| 序号 | 字段名 | 中文名称 | 数据类型 |
| ---- | ---------- | -------------- | ------------- |
| 1 | id | id唯一主键 | Int |
| 2 | name | 名称 | VARCHAR(255) |
| 3 | created_at | 创建时间 | LocalDateTime |
#### 3.SQL脚本
```
-- 创建极简测试表
CREATE TABLE IF NOT EXISTS `test_simple` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 插入测试数据
INSERT INTO `test_simple` (`name`) VALUES ('测试数据');
-- 查询验证
SELECT * FROM `test_simple`;
```
#### 4.变更记录
| 版本 | 变更时间 | 变更人 | 变更内容 |
| ---- | ---------- | ------ | -------- |
| v1.0 | 2025-05-13 | nh | 初始创建 |
### 一、模型信息表model_info)
#### 1.基本信息
所属模块:智能应用服务管理
负责人:宁欢
创建时间: 2025-05-14
#### 2.表结构
| 序号 | 数据表名 | 中文名称 | 数据类型 |
| ---- | ---------- | ------------------ | ------------ |
| 1 | id | 模型id唯一主键 | Long |
| 2 | model_name | 模型名称 | VARCHAR(255) |
#### 3.SQL脚本
```
CREATE TABLE model_info (
id BIGINT PRIMARY KEY COMMENT '模型id唯一主键',
model_name VARCHAR(255) COMMENT '模型名称'
);
-- 1. 删除现有主键约束
ALTER TABLE model_info DROP CONSTRAINT IF EXISTS model_info_pkey;
-- 2. 创建序列(如果不存在)
CREATE SEQUENCE IF NOT EXISTS model_info_id_seq;
-- 3. 将 id 字段设置为使用序列自增
ALTER TABLE model_info
ALTER COLUMN id SET DEFAULT nextval('model_info_id_seq');
-- 4. 重新添加主键约束
ALTER TABLE model_info ADD PRIMARY KEY (id);
-- 5. 将序列的当前值设置为表中现有最大 id 值 +1确保不自增冲突
SELECT setval('model_info_id_seq', COALESCE((SELECT MAX(id)+1 FROM model_info), 1), false);
```
#### 4.变更记录
| 版本 | 变更时间 | 变更人 | 变更内容 |
| ---- | ---------- | ------ | ---------------------- |
| v1.0 | 2025-05-13 | 宁欢 | 初始创建 |
| v1.1 | 2025-05-20 | 宁欢 | 将主键约束条件改为自增 |
### 二、日志表model_log)
#### 1.基本信息
所属模块:智能应用服务管理
负责人:宁欢
创建时间2025-05-14
#### 2.表结构
| 序号 | 数据表名 | 中文名称 | 数据类型 |
| ---- | ---------------- | --------------------- | ------------- |
| 1 | id | 日志id唯一主键 | Long |
| 2 | model_version_id | 关联模型版本id | Long |
| 3 | log_type | 日志类型123...) | Int |
| 4 | log_path | 日志存储路径 | VARCHAR(255) |
| 5 | log_time | 日志生成时间 | LocalDateTime |
#### 3.SQL脚本
```
-- 创建model_log表
CREATE TABLE model_log (
id BIGINT PRIMARY KEY COMMENT '日志id唯一主键',
model_id BIGINT COMMENT '关联模型id',
log_type INT COMMENT '日志类型123...',
log_path VARCHAR(255) COMMENT '日志存储路径',
log_time TIMESTAMP COMMENT '日志生成时间'
);
```
#### 4.变更记录
| 版本 | 变更时间 | 变更人 | 变更内容 |
| ---- | ---------- | ------ | ------------------------------------------ |
| v1.0 | 2025-05-14 | 宁欢 | 初始创建 |
| v2.0 | 2025-06-30 | 宁欢 | 模型日志修改成绑定模型版本id而不是模型id |
### 三、模型评估记录表model_evaluation)
#### 1.基本信息
所属模块:智能应用服务管理
负责人:宁欢
创建时间2025-05-14
#### 2.表结构
| 序号 | 数据表名 | 中文名称 | 数据类型 |
| ---- | ----------------- | ---------------------- | ------------- |
| 1 | id | 评估记录id唯一主键 | Long |
| 2 | model_id | 关联模型id | Long |
| 3 | evaluation_time | 评估时间 | LocalDateTime |
| 4 | evaluation_result | 评估结果 | VARCHAR(255) |
| 5 | operator | 评估操作人员 | VARCHAR(255) |
#### 3.SQL脚本
```
-- 创建model_evaluation表
CREATE TABLE model_evaluation (
id BIGINT PRIMARY KEY COMMENT '评估记录id唯一主键',
model_id BIGINT COMMENT '关联模型id',
evaluation_time TIMESTAMP COMMENT '评估时间',
evaluation_result VARCHAR(255) COMMENT '评估结果',
operator VARCHAR(255) COMMENT '评估操作人员'
);
```
#### 4.变更记录
| 版本 | 变更时间 | 变更人 | 变更内容 |
| ---- | ---------- | ------ | -------- |
| v1.0 | 2025-05-14 | 宁欢 | 初始创建 |
### 四、模型版本信息model_version)
#### 1.基本信息
所属模块:智能应用服务管理
负责人:宁欢
创建时间2025-05-14
#### 2.表结构
| 序号 | 数据表名 | 中文名称 | 数据类型 |
| ---- | --------------------- | ---------------------------------- | ------------- |
| 1 | id | 版本信息表id唯一主键 | Long |
| 2 | model_id | 关联模型id | Long |
| 3 | version | 模型版本 | VARCHAR(255) |
| 4 | dataset_id | 数据集id | Int |
| 5 | model_config | 模型配置信息 | VARCHAR(255) |
| 6 | model_path | 模型存储路径 | VARCHAR(255) |
| 7 | status | 模型状态1代表上线0代表不上线 | Int |
| 8 | create_time | 创建时间 | LocalDateTime |
| 9 | update_time | 更新时间 | LocalDateTime |
| 10 | model_size | 模型大小 | Integer |
| 11 | data_pre_handle_file | 数据预处理文件存储路径 | VARCHAR(255) |
| 12 | model_super_args | 模型超参数 | VARCHAR(255) |
| 13 | model_args_size | 模型参数量 | VARCHAR(255) |
| 14 | model_source_code_url | 模型源代码路径 | VARCHAR(255) |
| 15 | model_file | 模型文件存储路径 | VARCHAR(255) |
| 16 | model_design_document | 模型设计文档存储路径 | VARCHAR(255) |
| 17 | life_cycle | 模型生命周期 | VARCHAR(255) |
#### 3.SQL脚本
```
-- 创建model_version表
CREATE TABLE model_version (
id BIGINT PRIMARY KEY COMMENT '版本信息表id唯一主键',
model_id BIGINT COMMENT '关联模型id',
version VARCHAR(255) COMMENT '模型版本',
dataset_id INT COMMENT '数据集id',
model_config VARCHAR(255) COMMENT '模型配置信息',
model_path VARCHAR(255) COMMENT '模型存储路径',
status INT COMMENT '模型状态1代表上线0代表不上线',
create_time TIMESTAMP COMMENT '创建时间'
);
-- 添加更新时间字段类型为TIMESTAMP与Java的LocalDateTime对应
ALTER TABLE model_version ADD COLUMN update_time TIMESTAMP COMMENT '更新时间';
-- 添加模型大小字段类型用INT与Java的Integer对应
ALTER TABLE model_version ADD COLUMN model_size INT COMMENT '模型大小';
-- 数据预处理文件存储路径用VARCHAR存储路径信息
ALTER TABLE model_version ADD COLUMN data_pre_handle_file VARCHAR(255) COMMENT '数据预处理文件存储路径';
-- 模型超参数用VARCHAR存储文本信息
ALTER TABLE model_version ADD COLUMN model_super_args VARCHAR(255) COMMENT '模型超参数';
-- 模型参数量用VARCHAR存储文本格式的数量信息
ALTER TABLE model_version ADD COLUMN model_args_size VARCHAR(255) COMMENT '模型参数量';
-- 模型源代码路径用VARCHAR存储路径信息
ALTER TABLE model_version ADD COLUMN model_source_code_url VARCHAR(255) COMMENT '模型源代码路径';
-- 模型文件存储路径用VARCHAR存储路径信息
ALTER TABLE model_version ADD COLUMN model_file VARCHAR(255) COMMENT '模型文件存储路径';
-- 模型设计文档存储路径用VARCHAR存储路径信息
ALTER TABLE model_version ADD COLUMN model_design_document VARCHAR(255) COMMENT '模型设计文档存储路径';
-- 模型生命周期用VARCHAR存储文本信息
ALTER TABLE model_version ADD COLUMN life_cycle VARCHAR(255) COMMENT '模型生命周期';
-- 1. 删除现有主键约束
ALTER TABLE model_version DROP CONSTRAINT IF EXISTS model_version_pkey;
-- 2. 创建序列(如果不存在)
CREATE SEQUENCE IF NOT EXISTS model_version_id_seq;
-- 3. 将 id 字段设置为使用序列自增
ALTER TABLE model_version
ALTER COLUMN id SET DEFAULT nextval('model_version_id_seq');
-- 4. 重新添加主键约束
ALTER TABLE model_version ADD PRIMARY KEY (id);
-- 5. 将序列的当前值设置为表中现有最大 id 值 +1确保不自增冲突
SELECT setval('model_version_id_seq', COALESCE((SELECT MAX(id)+1 FROM model_version), 1), false);
```
#### 4.变更记录
| 版本 | 变更时间 | 变更人 | 变更内容 |
| ---- | ---------- | ------ | -------------------------------------------------- |
| v1.0 | 2025-05-14 | 宁欢 | 初始创建 |
| v1.1 | 2025-05-20 | 宁欢 | 新增了一些字段(从update_time到life_cycle共9个字段) |
| v1.3 | 2025-05-20 | 宁欢 | 将主键约束条件改为自增 |
### 五、数据集表dataset )
#### 1.基本信息
所属模块:智能应用服务管理
负责人:胡楷沅
创建时间2025-05-13
#### 2.表结构
| 序号 | 数据表名 | 中文名称 | 数据类型 |
| ---- | -------------- | ---------------------------------------------------- | ------------ |
| 1 | dataset_id | 数据集id唯一主键 | Int |
| 2 | dataset_name | 数据集名称 | VARCHAR(255) |
| 3 | dataset_type | 数据集类型0 表示用户上传1 表示来源于数据库) | Int |
| 4 | dataset_status | 数据集状态0 表示停用1 表示启用) | Int |
| 5 | ds_path | 分布式存储路径(存入分布式文件系统的路径) | VARCHAR(255) |
| 6 | args | 过滤参数(存储为 JSON 格式对应 Map<String, String> | JSON |
| 7 | create_time | 创建时间 | TIMESTAMP |
| 8 | update_time | 更新时间 | TIMESTAMP |
#### 3.SQL脚本
```
CREATE TABLE dataset (
dataset_id INT PRIMARY KEY AUTO_INCREMENT COMMENT '数据集ID',
dataset_name VARCHAR(255) COMMENT '数据集名称',
dataset_type INT COMMENT '数据集类型0表示用户上传1表示来源于数据库',
dataset_status INT COMMENT '数据集状态0表示停用1表示启用',
ds_path VARCHAR(255) COMMENT '分布式存储路径,存入分布式文件系统的路径',
args JSON COMMENT '过滤参数存储为JSON格式对应Map<String, String>',
create_time TIMESTAMP COMMENT '创建时间',
update_time TIMESTAMP COMMENT '更新时间'
);
```
#### 4.变更记录
| 版本 | 变更时间 | 变更人 | 变更内容 |
| ---- | ---------- | ------ | -------- |
| v1.0 | 2025-05-13 | 胡楷沅 | 初始创建 |
### 六、GPU资源表gpu_resource)
#### 1.基本信息
所属模块:智能应用服务管理
负责人:杜冲
创建时间2025-05-14
#### 2.表结构
| 字段名 | 数据类型 | 约束 | 字段描述 |
| :-----------: | :---------: | :--------: | :-------------------: |
| GPUId | BIGINT | 主键,自增 | GPU的ID全局唯一标识 |
| GPUModel | VARCHAR(64) | NOT NULL | GPU的型号 |
| GPUMemorySize | INT | NOT NULL | GPU内存大小 |
| Ip | VARCHAR(15) | NOT NULL | GPU所在ip |
| CreatedTime | DATETIME | NULL | GPU添加时间可为空 |
#### 3.SQL脚本
```
-- 创建gpu_resource表
CREATE TABLE gpu_resource (
GPUId BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
GPUModel VARCHAR(64) NOT NULL,
GPUMemorySize INT NOT NULL,
Ip VARCHAR(15) NOT NULL CHECK (Ip ~ '^\\d+\\.\\d+\\.\\d+\\.\\d+$'),
CreatedTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
```
#### 4.变更记录
| 版本 | 变更时间 | 变更人 | 变更内容 |
| ---- | ---------- | ------ | -------- |
| v1.0 | 2025-05-14 | 杜冲 | 初始创建 |
### 七、算法基础信息表algorithm_info)
#### 1.基本信息
所属模块:智能应用服务管理
负责人:孙一城
创建时间2025-05-14
#### 2.表结构
| 字段名 | 类型 | 是否为空 | 默认值 | 说明 |
| -------------- | ------------ | -------- | --------------------------- | -------------- |
| id | BIGINT | NOT NULL | AUTO_INCREMENT | 算法ID |
| algorithm_name | VARCHAR(100) | NOT NULL | | 算法名称(唯一) |
| algorithm_file | VARCHAR(255) | NOT NULL | | 算法文件路径 |
| algorithm_type | VARCHAR(50) | NOT NULL | | 算法分类 |
| description | TEXT | NULL | | 算法描述 |
| created_by | VARCHAR(50) | NOT NULL | | 创建人 |
| create_time | DATETIME | NOT NULL | CURRENT_TIMESTAMP | 创建时间 |
| update_time | DATETIME | NOT NULL | CURRENT_TIMESTAMP ON UPDATE | 更新时间 |
| file_size | BIGINT | NULL | | 文件大小(字节) |
#### 3.SQL脚本
```
-- 创建算法信息表Kingbase 兼容版)
CREATE TABLE algorithm_info (
id BIGSERIAL PRIMARY KEY, -- 自增主键
algorithm_name VARCHAR(100) NOT NULL, -- 算法名称
algorithm_file VARCHAR(255) NOT NULL, -- 算法文件路径
algorithm_type VARCHAR(50) NOT NULL, -- 算法分类
description TEXT, -- 算法描述
created_by VARCHAR(50) NOT NULL, -- 创建人
create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建时间
update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 更新时间(需触发器自动更新)
file_size BIGINT -- 文件大小(字节)
);
-- 添加唯一约束
ALTER TABLE algorithm_info ADD CONSTRAINT uk_algorithm_name UNIQUE (algorithm_name);
-- 这部份没有成功运行,存在问题
-- 为 update_time 添加自动更新触发器
CREATE OR REPLACE FUNCTION update_modified_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.update_time = NOW(); -- 语句以分号结尾
RETURN NEW;
END;
$$ LANGUAGE plpgsql; -- 美元引号正确闭合
-- 创建触发器
CREATE TRIGGER update_algorithm_info_modtime
BEFORE UPDATE ON algorithm_info
FOR EACH ROW EXECUTE FUNCTION update_modified_column();
-- 创建索引(语法与 MySQL 相同,无需修改)
CREATE INDEX idx_algorithm_type ON algorithm_info(algorithm_type);
CREATE INDEX idx_created_by ON algorithm_info(created_by);
CREATE INDEX idx_create_time ON algorithm_info(create_time);
```
#### 4.变更记录
| 版本 | 变更时间 | 变更人 | 变更内容 |
| ---- | ---------- | ------ | -------- |
| v1.0 | 2025-05-14 | 孙一城 | 初始创建 |

View File

@ -185,7 +185,12 @@
<artifactId>jaxb-runtime</artifactId>
<version>2.3.3</version> <!-- 注意版本不超过2.3.3 -->
</dependency>
</dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -14,7 +14,10 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Tag(name ="算法创建相关接口")
@RestController
@RequestMapping("/api/algorithm")
@ -31,16 +34,16 @@ public class AlgorithmInfoController {
@GetMapping("/{id}")
public ResponseEntity<AlgorithmInfo> getById(@PathVariable Long id) {
AlgorithmInfo algorithmInfo = algorithmInfoService.getById(id);
return algorithmInfo != null ?
ResponseEntity.ok(algorithmInfo) :
return algorithmInfo != null ?
ResponseEntity.ok(algorithmInfo) :
ResponseEntity.notFound().build();
}
@GetMapping("/name/{algorithmName}")
public ResponseEntity<AlgorithmInfo> getByName(@PathVariable String algorithmName) {
AlgorithmInfo algorithmInfo = algorithmInfoService.getByName(algorithmName);
return algorithmInfo != null ?
ResponseEntity.ok(algorithmInfo) :
return algorithmInfo != null ?
ResponseEntity.ok(algorithmInfo) :
ResponseEntity.notFound().build();
}
@ -56,18 +59,18 @@ public class AlgorithmInfoController {
if (!algorithmInfoService.validateAlgorithmInfo(algorithmInfo)) {
return ResponseEntity.badRequest().body("Invalid algorithm information");
}
boolean success = algorithmInfoService.update(algorithmInfo);
return success ?
ResponseEntity.ok("Update successful") :
return success ?
ResponseEntity.ok("Update successful") :
ResponseEntity.badRequest().body("Update failed");
}
@DeleteMapping("/{id}")
public ResponseEntity<String> delete(@PathVariable Long id) {
boolean success = algorithmInfoService.delete(id);
return success ?
ResponseEntity.ok("Delete successful") :
return success ?
ResponseEntity.ok("Delete successful") :
ResponseEntity.badRequest().body("Delete failed");
}
@ -103,11 +106,37 @@ public class AlgorithmInfoController {
* 算法运行
*/
@PostMapping("/run/{id}")
@Operation(summary = "运行")
public OptResult run(@PathVariable Long id,@RequestBody String param){
log.info("运行",id);
String result = algorithmInfoService.run(id,param);
return OptResult.success("运行成功"+result);
@Operation(summary = "运行算法")
public OptResult run(@PathVariable Long id, @RequestBody String param) {
log.info("运行算法 ID: {}", id);
try {
AlgorithmInfo algorithm = algorithmInfoService.getById(id);
if (algorithm == null) {
return OptResult.error("算法不存在");
}
// 1. 解析前端传入的参数JSON格式
Map<String, Object> paramMap = objectMapper.readValue(param, Map.class);
// 2. 从参数中提取实际需要传递给Python脚本的参数列表
// 示例:假设前端传入 {"args": [3, 0, 8, 7, 2, 1, 9, 4]}
List<String> args = new ArrayList<>();
if (paramMap.containsKey("args")) {
List<Object> argList = (List<Object>) paramMap.get("args");
for (Object arg : argList) {
args.add(arg.toString());
}
}
// 3. 调用Service执行Python脚本并获取结果
String result = algorithmInfoService.run(algorithm.getFilePath(), args);
// 4. 返回结构化结果
return OptResult.success("运行结果"+result);
} catch (Exception e) {
log.error("算法运行失败", e);
return OptResult.error("算法运行失败: " + e.getMessage());
}
}
/**
* 前端列表返回算法名称
@ -118,4 +147,4 @@ public class AlgorithmInfoController {
return algorithmInfoService.getAllNames();
}
}
}

View File

@ -102,5 +102,31 @@ public class ModelController {
return OptResult.success(datasetList);
}
@Operation(summary = "获取模型训练信息")
@GetMapping("/getModelTrainInfo")
public OptResult getModelTrainInfo(Long id){
log.info("获取模型训练信息");
ModelTrainInfoVO modelTrainInfo = modelService.getModelTrainInfo(id);
return OptResult.success(modelTrainInfo);
}
@Operation(summary = "模型修改成训练中")
@PutMapping("/updateModelTrain")
public OptResult updateModelTrain(Long id){
log.info("模型修改成训练中");
modelService.updateModelTrain(id);
return OptResult.success();
}
@Operation(summary = "模型更新小版本")
@PutMapping("/updateModelVersionMinor")
public OptResult updateModelVersionMinor(@RequestBody ModelVersionDTO dto){
log.info("模型更新小版本");
modelService.updateModelVersionMinor(dto);
return OptResult.success();
}
}

View File

@ -2,7 +2,10 @@ package com.bipt.intelligentapplicationorchestrationservice.controller;
import com.bipt.intelligentapplicationorchestrationservice.config.IpConfig;
import com.bipt.intelligentapplicationorchestrationservice.entity.DeployRequest;
import com.bipt.intelligentapplicationorchestrationservice.entity.ModelSelectVO;
import com.bipt.intelligentapplicationorchestrationservice.enumeration.ServiceStatus;
import com.bipt.intelligentapplicationorchestrationservice.mapper.ModelMapper;
import com.bipt.intelligentapplicationorchestrationservice.mapper.PublishMapper;
import com.bipt.intelligentapplicationorchestrationservice.pojo.*;
import com.bipt.intelligentapplicationorchestrationservice.service.ModelDeployer;
import com.bipt.intelligentapplicationorchestrationservice.service.PublishService;
@ -38,20 +41,26 @@ public class PublishController {
@Autowired
private ModelDeployer modelDeployer;
@Autowired
private PublishMapper publishMapper;
@PostMapping
@Operation(summary ="新增发布请求")
@Transactional
public OptResult<List<ServicePublishVO>> save(@RequestBody ServicePublishDTO servicePublishDTO) {
log.info("模型发布请求:{}", servicePublishDTO);
Long id = servicePublishDTO.getModelId();
Long ModelId = publishService.getModelId(id);
servicePublishDTO.setModelId(ModelId);
servicePublishDTO.setStatus(ServiceStatus.ONLINE.getCode());
publishService.save(servicePublishDTO);
//调用模型部署
DeployRequest request = new DeployRequest();
Long modelId = servicePublishDTO.getModelId();
ModelVersion modelVersion = modelMapper.selectById(modelId);
/* Long modelId = servicePublishDTO.getModelId();*/
ModelVersion modelVersion = publishMapper.selectByModelVersionId(id);
String modelConfig = modelVersion.getModelConfig();
//假设modelConfig只存GPU数据
request.setModelId(String.valueOf(modelId));
request.setModelId(String.valueOf(ModelId));
request.setRequiredMemory(Integer.parseInt(modelConfig));
modelDeployer.deploy(request);
// 获取前端传来的IP字符串
@ -107,4 +116,143 @@ public class PublishController {
log.info("返回列表;{}",ips);
return OptResult.success(ips);
}
@GetMapping("/config/ids")
public OptResult<List<ModelSelectVO>> getModelNames() {
// 只获取状态为“在线”的模型列表(筛掉已下线的服务)
List<ModelSelectVO> modelSelectVOS = publishService.getOnlineModelNames();
/*List<ModelSelectVO> modelNames = publishService.getModelNames();*/
log.info("获取到在线模型列表:{}", modelSelectVOS);
return OptResult.success(modelSelectVOS);
}
@PostMapping("/online/{serviceId}")
@Operation(summary = "上线已下线的服务")
@Transactional
public OptResult<String> onlineService(@PathVariable Long serviceId) {
log.info("上线服务请求: {}", serviceId);
// 1. 从数据库获取服务信息,验证状态
ServicePublishVO service = publishService.getServiceById(serviceId);
if (service == null) {
return OptResult.error("服务不存在");
}
if (service.getStatus() == ServiceStatus.ONLINE.getCode()) {
return OptResult.error("服务已处于上线状态");
}
if (service.getStatus() != ServiceStatus.OFFLINE.getCode()) {
return OptResult.error("服务当前状态不支持上线操作");
}
// 2. 调用Nacos重新注册服务
try {
String[] ipArray = service.getIp().split(",");
for (String ip : ipArray) {
String trimmedIp = ip.trim();
if (!trimmedIp.isEmpty()) {
nacosServiceUtil.registerService(
service.getModelId().toString(),
trimmedIp,
8080,
service.getApiUrl()
);
log.info("Nacos服务重新注册成功: {}", trimmedIp);
}
}
} catch (Exception e) {
log.error("Nacos服务注册失败", e);
return OptResult.error("Nacos服务注册失败");
}
// 3. 更新数据库状态为“在线”
ServicePublishDTO updateDto = new ServicePublishDTO();
BeanUtils.copyProperties(service, updateDto);
updateDto.setStatus(ServiceStatus.ONLINE.getCode()); // 假设ONLINE状态码为1
publishService.updateServiceStatus(updateDto);
return OptResult.success("服务上线成功");
}
// 新增:服务下线接口
@DeleteMapping("/{serviceId}")
@Operation(summary = "下线已发布的服务")
@Transactional
public OptResult<String> offlineService(@PathVariable Long serviceId) {
log.info("下线服务请求: {}", serviceId);
// 1. 从数据库获取服务信息
ServicePublishVO service = publishService.getServiceById(serviceId);
if (service == null) {
return OptResult.error("服务不存在");
}
// 2. 调用 Nacos 下线服务
try {
String[] ipArray = service.getIp().split(",");
for (String ip : ipArray) {
String trimmedIp = ip.trim();
if (!trimmedIp.isEmpty()) {
nacosServiceUtil.deregisterService(
service.getModelId().toString(),
trimmedIp,
8080
);
log.info("Nacos服务下线成功: {}", trimmedIp);
}
}
} catch (Exception e) {
log.error("Nacos服务下线失败", e);
return OptResult.error("Nacos服务下线失败");
}
// 3. 修改数据库记录状态为0下线
ServicePublishDTO updateDto = new ServicePublishDTO();
BeanUtils.copyProperties(service, updateDto);
updateDto.setStatus(ServiceStatus.OFFLINE.getCode()); // 假设OFFLINE状态码为0
publishService.updateServiceStatus(updateDto);
return OptResult.success("服务下线成功");
}
// 新增:服务状态同步接口
@GetMapping("/sync")
@Operation(summary = "同步服务状态")
public OptResult<String> syncServiceStatus() {
log.info("开始同步服务状态...");
try {
// 1. 获取数据库中所有已上线的服务
List<ServicePublishVO> dbServices = publishService.listPublishedServicesByStatus(ServiceStatus.ONLINE.getCode());
// 2. 遍历每个服务,检查 Nacos 注册状态
for (ServicePublishVO service : dbServices) {
String serviceName = service.getModelId().toString();
String[] ipArray = service.getIp().split(",");
// 获取 Nacos 中注册的实例
List<String> nacosInstances = nacosServiceUtil.getServiceInstances(serviceName);
// 检查每个 IP 是否都在 Nacos 中注册
for (String ip : ipArray) {
String trimmedIp = ip.trim();
if (!trimmedIp.isEmpty() && !nacosInstances.contains(trimmedIp)) {
// 如果数据库中有但 Nacos 中没有,则重新注册
nacosServiceUtil.registerService(
serviceName,
trimmedIp,
8080,
service.getApiUrl()
);
log.info("重新注册服务到 Nacos: {}", trimmedIp);
}
}
}
log.info("服务状态同步完成");
return OptResult.success("服务状态同步完成");
} catch (Exception e) {
log.error("服务状态同步失败", e);
return OptResult.error("服务状态同步失败");
}
}
}

View File

@ -1,20 +1,15 @@
package com.bipt.intelligentapplicationorchestrationservice.controller;
import com.bipt.intelligentapplicationorchestrationservice.pojo.OptResult;
import com.bipt.intelligentapplicationorchestrationservice.service.ServiceAPIService;
import com.bipt.intelligentapplicationorchestrationservice.util.NacosServiceUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Set;
@ -34,12 +29,18 @@ public class ServiceAPIController {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@PostMapping("/release")
@Operation(summary = "结束访问")
@Transactional
public OptResult releaseResource(@PathVariable Long modelId) {
String key = "modelId:" + modelId;
String modelConfig = (String) redisTemplate.opsForValue().get(key);
if (modelConfig == null) {
log.warn("尝试释放不存在的模型资源: {}", modelId);
return OptResult.error("模型资源不存在");
}
int userMemorySize = parseGpuMemorySize(modelConfig);
List<String> instanceIps;
try {
@ -48,18 +49,33 @@ public class ServiceAPIController {
log.error("获取Nacos实例失败", e);
return OptResult.error("获取实例失败");
}
int memorySize;
boolean released = false;
for (String ip : instanceIps) {
String ipKey = "ip:" + ip;
Integer nowMemorySizeOBJ = (Integer) redisTemplate.opsForValue().get(ipKey);
// 如果该IP没有记录则跳过可能资源分配记录已过期
if (nowMemorySizeOBJ == null) {
log.warn("IP {} 的资源记录不存在,可能已过期", ip);
continue;
}
int nowMemorySize = nowMemorySizeOBJ;
memorySize = nowMemorySize + userMemorySize;
int newMemorySize = nowMemorySize + userMemorySize;
// 更新IP对应的资源值
redisTemplate.opsForValue().set(ipKey, memorySize);
redisTemplate.opsForValue().set(ipKey, newMemorySize);
// 设置缓存过期时间3600秒
redisTemplate.expire(ipKey, 3600, TimeUnit.SECONDS);
log.info("IP {} 释放 {} MB 资源,当前可用: {} MB", ip, userMemorySize, newMemorySize);
released = true;
}
if (!released) {
return OptResult.error("未找到匹配的资源记录");
}
// 处理等待队列(先来先服务)
String waitQueueKey = "waitQueue:" + modelId;
// 取出队列头部的任务(最早加入的)
@ -81,11 +97,13 @@ public class ServiceAPIController {
// 1. 存储modelConfig到缓存
String modelConfig = serviceAPIService.getByModelId(modelId);
int requestMemorySize = parseGpuMemorySize(modelConfig);
if (requestMemorySize == -1){
if (requestMemorySize == -1) {
return OptResult.error("解析配置失败,请检查模型:" + modelId +"是否存在");
}
String modelConfigKey = "modelConfig:" + modelId;
redisTemplate.opsForValue().set(modelConfigKey, modelConfig);
// 2. 获取Nacos实例IP列表
List<String> instanceIps;
try {
@ -94,8 +112,14 @@ public class ServiceAPIController {
log.error("获取Nacos实例失败", e);
return OptResult.error("获取实例失败");
}
Set<String> gpuKeys = redisTemplate.keys("gpu:*");
//根据IP列表查找资源
if (gpuKeys == null || gpuKeys.isEmpty()) {
log.error("未找到可用的GPU资源");
return OptResult.error("系统无可用GPU资源");
}
// 根据IP列表查找资源
for (String instanceIp : instanceIps) {
for (String gpuKey : gpuKeys) {
String GPUConfig = (String) redisTemplate.opsForValue().get(gpuKey);
@ -103,7 +127,7 @@ public class ServiceAPIController {
// 分割键值对
String[] pairs = GPUConfig.split(",");
String ip = null;
int memorySize = 0;
int totalMemorySize = 0;
for (String pair : pairs) {
String[] keyValue = pair.split(":", 2);
if (keyValue.length == 2) {
@ -112,40 +136,62 @@ public class ServiceAPIController {
if ("IP".equalsIgnoreCase(key)) {
ip = value;
} else if ("GPUMemorySize".equalsIgnoreCase(key)) {
memorySize = Integer.parseInt(value);
totalMemorySize = Integer.parseInt(value);
}
}
}
// 检查解析出的 IP 是否在 Nacos 实例列表中
if (instanceIp.equals(ip)) {
log.info("找到 IP {} 对应的 GPU 内存: {} ", ip, memorySize);
if (memorySize>=requestMemorySize){
int newMemorySize = memorySize - requestMemorySize;
String ipKey = "ip:" + ip;
redisTemplate.opsForValue().set(ipKey,newMemorySize);
//访问请求最大时间为3600s
redisTemplate.expire(ipKey, 3600, TimeUnit.SECONDS);
log.info("找到 IP {} 对应的 GPU 内存: {} MB", ip, totalMemorySize);
// 获取当前可用内存
String ipKey = "ip:" + ip;
Integer currentAvailable = (Integer) redisTemplate.opsForValue().get(ipKey);
// 如果没有记录,则初始化为总内存
if (currentAvailable == null) {
currentAvailable = totalMemorySize;
redisTemplate.opsForValue().set(ipKey, currentAvailable);
log.info("IP {} 首次使用,初始可用内存: {} MB", ip, currentAvailable);
}
// 检查可用内存是否足够
if (currentAvailable >= requestMemorySize) {
int newMemorySize = currentAvailable - requestMemorySize;
redisTemplate.opsForValue().set(ipKey, newMemorySize);
// 访问请求最大时间为3600s
redisTemplate.expire(ipKey, 3600, TimeUnit.SECONDS);
// 记录模型与IP的绑定关系
redisTemplate.opsForValue().set("modelId:" + modelId, modelConfig);
log.info("IP {} 分配成功,分配前可用: {} MB分配后可用: {} MB",
ip, currentAvailable, newMemorySize);
return OptResult.success("资源分配成功使用ip:" + ip);
} else {
log.info("IP {} 资源不足,当前可用: {} MB请求: {} MB",
ip, currentAvailable, requestMemorySize);
}
return OptResult.success("资源分配成功使用ip:" + ip);
}else {
log.info("资源不足");
}
}
}
}
// 所有实例检查完毕未找到足够资源
String waitQueueKey = "waitQueue:" + modelId;
// 改为右插入保证队列顺序为FIFO最早的任务在列表头部
redisTemplate.opsForList().rightPush(waitQueueKey, modelId);
redisTemplate.opsForList().rightPush(waitQueueKey, modelId);
log.info("未找到足够资源,任务 {} 加入等待队列", modelId);
return OptResult.error("资源不足,等待中");
}
/**
* 从模型配置字符串中解析GPU内存需求
* @param modelConfig 模型配置字符串,格式如 "GPUMemorySize:8000,version:1"
* @return 解析到的GPU内存大小MB若解析失败返回-1
*/
private int parseGpuMemorySize(String modelConfig) {
public int parseGpuMemorySize(String modelConfig) {
if (modelConfig == null || modelConfig.isEmpty()) {
log.error("模型配置为空无法解析GPU内存需求");
return -1;
@ -177,5 +223,4 @@ public class ServiceAPIController {
}
return requestMemorySize;
}
}
}

View File

@ -0,0 +1,10 @@
package com.bipt.intelligentapplicationorchestrationservice.entity;
import lombok.Data;
@Data
public class ModelSelectVO {
private Long modelId; // 模型ID即modelId对应ModelVersion的id
private String modelName; // 模型名称(如"图像识别模型"
private String version; // 版本信息(如"v1.0.0"
}

View File

@ -0,0 +1,31 @@
package com.bipt.intelligentapplicationorchestrationservice.enumeration;
public enum ServiceStatus {
OFFLINE(0, "下线"),
ONLINE(1, "上线");
private final int code;
private final String description;
ServiceStatus(int code, String description) {
this.code = code;
this.description = description;
}
public int getCode() {
return code;
}
public String getDescription() {
return description;
}
public static ServiceStatus fromCode(int code) {
for (ServiceStatus status : ServiceStatus.values()) {
if (status.code == code) {
return status;
}
}
throw new IllegalArgumentException("未知的状态码: " + code);
}
}

View File

@ -5,14 +5,14 @@ import com.bipt.intelligentapplicationorchestrationservice.pojo.ModelLogVO;
public interface EvaluationMapper {
/*
* 查询模型评估日志详情
* @param id 模型评估日志id
* @param id 模型版本id
* @return 模型评估日志详情
*/
ModelLogVO selectLogDetail(Long id);
/*
* 更新模型评估日志状态(评估通过则上线)
* @param id 模型评估日志id
* @param id 模型版本id
* @param status 模型评估日志状态
*/
void update(Long id, Integer status);

View File

@ -76,4 +76,19 @@ public interface ModelMapper {
*/
@Select("select dataset_id,dataset_name from dataset")
List<DatasetEntity> listDataset();
/**
* 获取模型训练信息
* @param id 模型版本表id
* 返回模型训练信息
*/
ModelTrainInfoVO getModelTrainInfo(Long id);
/**
* 获取模型版本信息
* @param modelId
* @return
*/
ModelVersion selectByModelId(Long modelId);
}

View File

@ -1,10 +1,13 @@
package com.bipt.intelligentapplicationorchestrationservice.mapper;
import com.bipt.intelligentapplicationorchestrationservice.entity.ModelSelectVO;
import com.bipt.intelligentapplicationorchestrationservice.pojo.ModelVersion;
import com.bipt.intelligentapplicationorchestrationservice.pojo.ServicePublishDTO;
import com.bipt.intelligentapplicationorchestrationservice.pojo.ServicePublishVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
@ -17,6 +20,46 @@ public interface PublishMapper {
Long getByApiUrl(String apiUrl);
@Select("SELECT model_id,api_url,ip FROM service_publish")
@Select("SELECT * FROM service_publish")
List<ServicePublishVO> listPublishedServices();
@Select("SELECT " +
"mv.model_id AS modelId, " +
"m.model_name AS modelName, " +
"mv.version AS version " +
"FROM model_version mv " +
"LEFT JOIN model_info m ON mv.model_id = m.id")
List<ModelSelectVO> selectModelSelectList();
// 根据ID查询服务移除update_time和deleted字段
@Select("SELECT id, model_id, api_url, ip, create_time " +
"FROM service_publish WHERE id = #{serviceId}")
ServicePublishVO getServiceById(Long serviceId);
void updateStatus(Long id, int status);
List<ServicePublishVO> selectByStatus(Integer status);
@Select("SELECT " +
"mv.id AS modelId, " +
/*"mv.model_id AS modelId, " +*/
"m.model_name AS modelName, " +
"mv.version AS version " +
"FROM model_version mv " +
"LEFT JOIN model_info m ON mv.model_id = m.id " +
"WHERE mv.model_id NOT IN ( " +
" SELECT DISTINCT model_id " +
" FROM service_publish " +
" WHERE status = #{code} " +
")")
List<ModelSelectVO> selectModelNamesByStatus(int code);
@Select("select model_id from model_version where id=#{id}")
Long getByMdVersionId(Long id);
/**
* 根据modelversionId查询Modelversion信息
* @param id
* @return
*/
@Select("select * from model_version where id = #{id}")
ModelVersion selectByModelVersionId(Long id);
}

View File

@ -84,4 +84,8 @@ public class AlgorithmInfo {
public void setFileSize(Long fileSize) {
this.fileSize = fileSize;
}
}
public String getFilePath() {
return algorithmFile;
}
}

View File

@ -17,7 +17,7 @@ import java.time.LocalDateTime;
@AllArgsConstructor
public class ModelEvaluation implements Serializable {
private Long id; // 评估记录id
private Long modelId; // 关联模型id
private Long modelVersionId; // 关联模型id,后续修改成了模型版本id
private LocalDateTime evaluationTime; // 评估时间
private String evaluationResult; // 评估结果
private String operator; // 评估操作人员

View File

@ -0,0 +1,18 @@
package com.bipt.intelligentapplicationorchestrationservice.pojo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ModelTrainInfoVO {
private Long id;
private Integer datasetId; // 数据集id
private String modelConfig; // 模型配置信息
private String dsPath;// 版本信息表id
private String dataPreHandleFile; // 数据预处理文件存储路径
}

View File

@ -12,6 +12,7 @@ import java.time.LocalDateTime;
@AllArgsConstructor
public class ModelVersionDTO {
private Long id; // 模型版本id
private Long modelId; // 模型id
private String version; // 模型版本
private Integer datasetId; // 数据集id
private String modelConfig; // 模型配置信息

View File

@ -22,4 +22,5 @@ public class ServicePublishDTO implements Serializable {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private LocalDateTime createTime;
private String ip;
private int status;
}

View File

@ -16,11 +16,13 @@ import java.time.LocalDateTime;
@NoArgsConstructor
@AllArgsConstructor
public class ServicePublishVO implements Serializable {
private Long id;
private Long modelId;
/*private String GPUModel;*/
private String ip;
/* private String GPUMemorySize;*/
private String apiUrl;
private int status;
}

View File

@ -3,6 +3,7 @@ package com.bipt.intelligentapplicationorchestrationservice.service;
import com.bipt.intelligentapplicationorchestrationservice.pojo.AlgorithmInfo;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
public interface AlgorithmInfoService {
@ -15,7 +16,7 @@ public interface AlgorithmInfoService {
void save(AlgorithmInfo algorithmInfo, MultipartFile file);
String run(Long id, String param);
String run(String scriptPath, List<String> args) throws IOException, InterruptedException;
List<String> getAllNames();

View File

@ -3,6 +3,7 @@ package com.bipt.intelligentapplicationorchestrationservice.service.Impl;
import com.bipt.intelligentapplicationorchestrationservice.mapper.AlgorithmInfoMapper;
import com.bipt.intelligentapplicationorchestrationservice.pojo.AlgorithmInfo;
import com.bipt.intelligentapplicationorchestrationservice.service.AlgorithmInfoService;
import jakarta.servlet.ServletContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@ -11,10 +12,15 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Service
@Slf4j
@ -23,8 +29,10 @@ public class AlgorithmInfoServiceImpl implements AlgorithmInfoService {
@Autowired
private AlgorithmInfoMapper algorithmInfoMapper;
@Value("${algorithm.upload.dir:/tmp/algorithm-files/}") // 默认上传目录
// 从配置文件读取上传目录
@Value("${algorithm.upload.dir:algorithm_files}")
private String uploadDir;
@Override
public AlgorithmInfo getById(Long id) {
return algorithmInfoMapper.selectById(id);
@ -70,6 +78,11 @@ public class AlgorithmInfoServiceImpl implements AlgorithmInfoService {
return true;
}
/**
* 新增算法
* @param algorithmInfo
* @param file
*/
@Override
@Transactional
public void save(AlgorithmInfo algorithmInfo, MultipartFile file) {
@ -80,57 +93,124 @@ public class AlgorithmInfoServiceImpl implements AlgorithmInfoService {
throw new RuntimeException("算法已存在,请去修改算法");
}
// 只接收文件但不进行保存操作
if (file != null && !file.isEmpty()) {
log.info("已接收文件: {}", file.getOriginalFilename());
log.info("文件大小: {} 字节", file.getSize());
log.info("文件类型: {}", file.getContentType());
// 临时设置一个空路径(避免数据库保存空值)
//todo 保存到分布式存储
algorithmInfo.setAlgorithmFile("");
try {
// 获取文件原始名称
String originalFilename = file.getOriginalFilename();
if (originalFilename == null || originalFilename.isEmpty()) {
throw new RuntimeException("文件名称为空");
}
// 生成唯一文件名,避免冲突
String fileName = UUID.randomUUID().toString() + "_" + originalFilename;
// 构建相对路径(相对于项目根目录)
Path relativePath = Paths.get(uploadDir, fileName);
// 获取当前应用的运行目录(兼容开发和部署环境)
Path basePath = Paths.get("").toAbsolutePath();
Path absolutePath = basePath.resolve(relativePath);
// 确保目录存在
Path parentDir = absolutePath.getParent();
if (!Files.exists(parentDir)) {
Files.createDirectories(parentDir);
log.info("已创建存储目录: {}", parentDir);
}
// 保存文件到指定路径
file.transferTo(absolutePath);
// 存储相对路径到数据库
algorithmInfo.setAlgorithmFile(relativePath.toString());
// 设置文件大小
algorithmInfo.setFileSize(Files.size(absolutePath));
log.info("文件保存成功 - 相对路径: {}, 绝对路径: {}",
relativePath, absolutePath);
} catch (Exception e) {
log.error("文件保存失败", e);
throw new RuntimeException("文件保存失败: " + e.getMessage(), e);
}
} else {
// 文件为空的处理逻辑
algorithmInfo.setAlgorithmFile(null);
algorithmInfo.setFileSize(0L);
}
algorithmInfo.setCreateTime(LocalDateTime.now());
// 保存算法信息到数据库注意此时algorithmFile字段为空
// 保存算法信息到数据库
algorithmInfoMapper.insert(algorithmInfo);
}
@Override
public String run(Long id, String param) {
//todo从分布式存储中拿到文件以下是示例
String file = algorithmInfoMapper.getFileById(id);
StringBuilder result = new StringBuilder(); // 用于存储结果
try {
// 构建命令,将 param 作为参数传递给 Python 脚本
ProcessBuilder pb = new ProcessBuilder("python", file, param);
Process process = pb.start();
// 读取标准输出(脚本执行结果)
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
result.append(line).append("\n");
}
// 读取错误输出
BufferedReader errorReader = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
String errorLine;
while ((errorLine = errorReader.readLine()) != null) {
result.append("Error: ").append(errorLine).append("\n");
}
int exitCode = process.waitFor();
result.append("Exit Code: ").append(exitCode);
} catch (Exception e) {
result.append("执行异常: ").append(e.getMessage());
e.printStackTrace();
/**
* 执行Python算法脚本并返回结果
* @param scriptPath Python脚本路径数据库中存储的相对路径
* @param args 命令行参数列表
* @return 脚本执行结果
*/
public String run(String scriptPath, List<String> args) throws IOException, InterruptedException {
if (scriptPath == null || scriptPath.isEmpty()) {
throw new IllegalArgumentException("脚本路径不能为空");
}
return result.toString(); // 返回完整结果
// 获取当前应用的运行目录(兼容开发和部署环境)
Path basePath = Paths.get("").toAbsolutePath();
Path absoluteScriptPath = basePath.resolve(scriptPath);
// 验证文件是否存在
if (!Files.exists(absoluteScriptPath)) {
throw new FileNotFoundException("脚本文件不存在: " + absoluteScriptPath);
}
// 验证文件是否可执行针对Python脚本
if (!Files.isReadable(absoluteScriptPath)) {
throw new IOException("脚本文件不可读: " + absoluteScriptPath);
}
// 构建命令python [脚本绝对路径] [参数1] [参数2] ...
List<String> command = new ArrayList<>();
command.add("python"); // Python解释器路径可配置在application.properties中
command.add(absoluteScriptPath.toString()); // 使用绝对路径执行脚本
command.addAll(args); // 添加所有参数
// 打印完整命令(用于调试)
log.info("执行命令: {}", String.join(" ", command));
// 创建进程并执行命令
ProcessBuilder processBuilder = new ProcessBuilder(command);
// 设置工作目录为脚本所在目录
processBuilder.directory(absoluteScriptPath.getParent().toFile());
processBuilder.redirectErrorStream(true); // 将错误输出合并到标准输出
Process process = processBuilder.start();
// 读取脚本输出使用UTF-8编码避免中文乱码
StringBuilder output = new StringBuilder();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
}
// 等待进程执行完成并获取退出码
int exitCode = process.waitFor();
// 检查脚本是否成功执行
if (exitCode != 0) {
// 捕获详细的错误信息
String errorMsg = "脚本执行失败,退出码: " + exitCode +
"\n命令: " + String.join(" ", command) +
"\n输出: " + output.toString();
log.error(errorMsg);
throw new RuntimeException(errorMsg);
}
return output.toString();
}
@Override

View File

@ -72,6 +72,10 @@ public class ModelServiceImpl implements ModelService {
return modelVOList;
}
/**
* 查询模型详情
* @param id
*/
@Override
public ModelVersion detail(Long id) {
log.info("查询模型详情");
@ -79,13 +83,23 @@ public class ModelServiceImpl implements ModelService {
return modelVersion;
}
/**
* 更新模型
* @param dto
*/
@Override
public void updateModel(ModelVersionDTO dto) {
// 更新模型还需要更新操作人和时间
// TODO: 更新模型还需要更新操作人和时间
log.info("更新模型");
dto.setCreateTime(LocalDateTime.now());
dto.setUpdateTime(LocalDateTime.now());
modelMapper.update(dto);
}
/**
* 删除模型版本
* @param id
*/
@Override
public void deleteModelVersion(Long id) {
log.info("删除模型版本");
@ -149,6 +163,9 @@ public class ModelServiceImpl implements ModelService {
log.info("模型生命周期更新成功,新状态为: {}", targetLifeCycle);
}
/**
* 获取模型生命周期列表
*/
@Override
public List<Map<String, String>> listLifeCycle() {
return Arrays.stream(ModelLifecycle.values())
@ -159,6 +176,9 @@ public class ModelServiceImpl implements ModelService {
.collect(Collectors.toList());
}
/**
* 获取模型数据集列表
*/
@Override
public List<DatasetEntity> listDataset() {
List<DatasetEntity> datasetEntityList = modelMapper.listDataset();
@ -166,4 +186,41 @@ public class ModelServiceImpl implements ModelService {
}
/**
* 获取模型训练信息
* @param id
*/
@Override
public ModelTrainInfoVO getModelTrainInfo(Long id) {
ModelTrainInfoVO modelTrainInfoVO = modelMapper.getModelTrainInfo(id);
return modelTrainInfoVO;
}
/**
* 模型训练(把模型修改成训练中)
* @param id
*/
@Override
public void updateModelTrain(Long id) {
// 更新当前模型的生命周期为训练中
modelMapper.updateLifeCycleById(id, ModelLifecycle.TRAINING.getDbValue());
}
/**
* 模型小版本更新
* @param dto
*/
@Override
public void updateModelVersionMinor(ModelVersionDTO dto) {
// 更新模型小版本(其实是新增一个小版本)
ModelVersion modelVersion = new ModelVersion();
BeanUtils.copyProperties(dto, modelVersion, "id", "modelId");
modelVersion.setModelId(dto.getModelId()); // 把模型id设置成该模型版本关联的模型id
modelVersion.setCreateTime(LocalDateTime.now());
modelVersion.setUpdateTime(LocalDateTime.now());
modelVersion.setOperateUser("zs");
// TODO: 后续可能还需要更新操作人
modelMapper.insertModelVersion(modelVersion);
}
}

View File

@ -1,5 +1,7 @@
package com.bipt.intelligentapplicationorchestrationservice.service.Impl;
import com.bipt.intelligentapplicationorchestrationservice.entity.ModelSelectVO;
import com.bipt.intelligentapplicationorchestrationservice.enumeration.ServiceStatus;
import com.bipt.intelligentapplicationorchestrationservice.mapper.PublishMapper;
import com.bipt.intelligentapplicationorchestrationservice.pojo.ServicePublishDTO;
import com.bipt.intelligentapplicationorchestrationservice.pojo.ServicePublishVO;
@ -33,8 +35,6 @@ public class PublishServiceImpl implements PublishService {
throw new IllegalArgumentException("请求已存在: " + apiUrl);
}
//todo调用服务部署
publishMapper.insert(servicePublishDTO);
}
@ -43,5 +43,35 @@ public class PublishServiceImpl implements PublishService {
return publishMapper.listPublishedServices();
}
@Override
public List<ModelSelectVO> getModelNames() {
return publishMapper.selectModelSelectList();
}
@Override
public ServicePublishVO getServiceById(Long serviceId) {
return publishMapper.getServiceById(serviceId);
}
@Override
public void updateServiceStatus(ServicePublishDTO servicePublishDTO) {
publishMapper.updateStatus(servicePublishDTO.getId(), servicePublishDTO.getStatus());
}
@Override
public List<ServicePublishVO> listPublishedServicesByStatus(Integer status) {
return publishMapper.selectByStatus(status);
}
@Override
public List<ModelSelectVO> getOnlineModelNames() {
// 调用Mapper查询状态为“在线”的模型ServiceStatus.ONLINE.getCode() 假设为1
return publishMapper.selectModelNamesByStatus(ServiceStatus.OFFLINE.getCode());
}
@Override
public Long getModelId(Long id) {
return publishMapper.getByMdVersionId(id);
}
}

View File

@ -23,4 +23,10 @@ public interface ModelService {
List<Map<String, String>> listLifeCycle();
List<DatasetEntity> listDataset();
ModelTrainInfoVO getModelTrainInfo(Long id);
void updateModelTrain(Long id);
void updateModelVersionMinor(ModelVersionDTO dto);
}

View File

@ -1,5 +1,6 @@
package com.bipt.intelligentapplicationorchestrationservice.service;
import com.bipt.intelligentapplicationorchestrationservice.entity.ModelSelectVO;
import com.bipt.intelligentapplicationorchestrationservice.pojo.ServicePublishDTO;
import com.bipt.intelligentapplicationorchestrationservice.pojo.ServicePublishVO;
@ -9,6 +10,17 @@ public interface PublishService {
void save(ServicePublishDTO servicePublishDTO);
List<ServicePublishVO> listPublishedServices();
List<ModelSelectVO> getModelNames();
ServicePublishVO getServiceById(Long serviceId);
void updateServiceStatus(ServicePublishDTO updateDto);
List<ServicePublishVO> listPublishedServicesByStatus(Integer status);
List<ModelSelectVO> getOnlineModelNames();
Long getModelId(Long id);
}

View File

@ -1,14 +1,17 @@
package com.bipt.intelligentapplicationorchestrationservice.util;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
@Component
@ -17,26 +20,102 @@ public class NacosServiceUtil {
@Value("${spring.cloud.nacos.discovery.server-addr}")
private String nacosServerAddr;
public void registerService(String serviceName, String ip, int port, String url) throws Exception { // 新增url参数
NamingService naming = NamingFactory.createNamingService(nacosServerAddr);
private NamingService namingService;
/**
* 获取NamingService实例线程安全
*/
private NamingService getNamingService() throws Exception {
if (namingService == null) {
synchronized (this) {
if (namingService == null) {
namingService = NacosFactory.createNamingService(nacosServerAddr);
}
}
}
return namingService;
}
/**
* 注册服务到Nacos
*/
public void registerService(String serviceName, String ip, int port, String url) throws Exception {
NamingService naming = getNamingService();
Instance instance = new Instance();
instance.setIp(ip);
instance.setPort(port);
// 添加元数据存储URL
instance.setWeight(1.0);
instance.setHealthy(true);
// 添加元数据
Map<String, String> metadata = new HashMap<>();
metadata.put("url", url); // 将URL存入元数据
metadata.put("url", url);
metadata.put("registerTime", String.valueOf(System.currentTimeMillis()));
instance.setMetadata(metadata);
naming.registerInstance(serviceName, instance);
}
/**
* 从Nacos注销服务
*/
public void deregisterService(String serviceName, String ip, int port) throws Exception {
NamingService naming = getNamingService();
naming.deregisterInstance(serviceName, ip, port);
}
/**
* 获取服务所有实例
*/
public List<Instance> getAllInstances(String serviceName) throws Exception {
NamingService naming = getNamingService();
return naming.getAllInstances(serviceName);
}
/**
* 获取服务所有实例IP
*/
public List<String> getServiceInstances(String serviceName) throws Exception {
NamingService naming = NamingFactory.createNamingService(nacosServerAddr);
List<Instance> instances = naming.getAllInstances(serviceName);
return instances.stream()
return getAllInstances(serviceName).stream()
.map(Instance::getIp)
.collect(Collectors.toList());
}
/**
* 获取服务信息适配Nacos 2.x
*/
public ServiceInfo getServiceInfo(String serviceName) throws Exception {
NamingService naming = getNamingService();
// 使用selectInstances替代getServiceInfo
List<Instance> instances = naming.selectInstances(serviceName, true);
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setName(serviceName);
serviceInfo.setHosts(instances);
return serviceInfo;
}
/**
* 根据IP和端口查询实例是否存在
*/
public boolean isInstanceExists(String serviceName, String ip, int port) throws Exception {
List<Instance> instances = getAllInstances(serviceName);
return instances.stream()
.anyMatch(instance ->
Objects.equals(instance.getIp(), ip) &&
instance.getPort() == port
);
}
/**
* 更新服务实例元数据
*/
public void updateInstanceMetadata(String serviceName, String ip, int port, Map<String, String> metadata) throws Exception {
NamingService naming = getNamingService();
Instance instance = new Instance();
instance.setIp(ip);
instance.setPort(port);
instance.setMetadata(metadata);
naming.registerInstance(serviceName, instance);
}
}

View File

@ -0,0 +1,5 @@
# 阿里云OSS配置
aliyun.oss.endpoint=oss-cn-beijing.aliyuncs.com
aliyun.oss.bucketName=ipz-nh
aliyun.oss.accessKeyId=LTAI5tBeto7V7BPWBcCjeP7A
aliyun.oss.accessKeySecret=bjQGt2G4J5yetxuY5cT5ZnKnIOqe4O

View File

@ -60,3 +60,6 @@ spring.profiles.active=dev
#配置IP列表后续根据需求修改ip数据以下仅为测试用例
available.ips=192.168.1.100,192.168.1.101,192.168.1.102
# 算法文件上传目录(相对于项目根目录)
algorithm.upload.dir=algorithm_files

View File

@ -9,7 +9,7 @@
from model_log m1,
model_info m2,
model_version m3
where m1.model_id=m2.id and m3.model_id=m2.id and m1.model_id = #{id}
where m1.model_version_id=m3.id and m3.model_id=m2.id and m1.model_version_id = #{id}
</select>
<!--更新模型信息(目前只更新模型是否上线,后续如果更多需求可优化>-->

View File

@ -30,6 +30,17 @@
<!--查询模型详细信息-->
<select id="selectById" resultType="com.bipt.intelligentapplicationorchestrationservice.pojo.ModelVersion">
SELECT
t1.model_name, t1.id modelId,
t2.version, t2.dataset_id, t2.model_config, t2.id,
t2.model_path, t2.status, t2.create_time, t2.update_time, t2.model_size,
t2.data_pre_handle_file, t2.model_super_args, t2.model_args_size, t2.model_source_code_url, t2.model_file,
t2.model_design_document, t2.life_cycle, t2.operate_user
FROM model_info t1 JOIN model_version t2 ON t1.id = t2.model_id
where t2.id = #{id}
</select>
<select id="selectByModelId"
resultType="com.bipt.intelligentapplicationorchestrationservice.pojo.ModelVersion">
SELECT
t1.model_name,
t2.version, t2.dataset_id, t2.model_config,
@ -37,7 +48,7 @@
t2.data_pre_handle_file, t2.model_super_args, t2.model_args_size, t2.model_source_code_url, t2.model_file,
t2.model_design_document, t2.life_cycle, t2.operate_user
FROM model_info t1 JOIN model_version t2 ON t1.id = t2.model_id
where t2.id = #{id}
where t2.model_id = #{id}
</select>
<!--更新模型信息-->
@ -62,4 +73,15 @@
</set>
WHERE id = #{id}
</update>
<!--获取模型训练信息-->
<select id="getModelTrainInfo" resultType="com.bipt.intelligentapplicationorchestrationservice.pojo.ModelTrainInfoVO">
select m1.dataset_id,
m1.id,
m1.model_config,
d2.ds_path,
m1.data_pre_handle_file
from model_version m1,dataset d2
where m1.dataset_id=d2.dataset_id and m1.id=#{id}
</select>
</mapper>

View File

@ -3,9 +3,14 @@
<mapper namespace="com.bipt.intelligentapplicationorchestrationservice.mapper.PublishMapper">
<insert id="insert">
INSERT INTO service_publish
(id,model_id,api_url,create_time,ip)
values (#{id}, #{modelId}, #{apiUrl}, #{createTime},#{ip})
(id,model_id,api_url,create_time,ip,status)
values (#{id}, #{modelId}, #{apiUrl}, #{createTime},#{ip},#{status})
</insert>
<update id="updateStatus">
UPDATE service_publish
SET status = #{status}
WHERE id = #{id}
</update>
<select id="getByApiUrl" resultType="java.lang.Long">
SELECT id FROM service_publish WHERE api_url = #{apiUrl};
@ -17,4 +22,10 @@
mv.*
from model_version mv join service_publish sp on mv.model_id = sp.model_id
</select>
<select id="selectByStatus"
resultType="com.bipt.intelligentapplicationorchestrationservice.pojo.ServicePublishVO">
SELECT *
FROM service_publish
WHERE status = #{status}
</select>
</mapper>

View File

@ -0,0 +1,125 @@
package com.bipt.intelligentapplicationorchestrationservice;
import com.bipt.intelligentapplicationorchestrationservice.controller.ServiceAPIController;
import com.bipt.intelligentapplicationorchestrationservice.pojo.OptResult;
import com.bipt.intelligentapplicationorchestrationservice.service.ServiceAPIService;
import com.bipt.intelligentapplicationorchestrationservice.util.NacosServiceUtil;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
@RunWith(SpringRunner.class)
public class ServiceAPIControllerTest {
@Mock
private ServiceAPIService serviceAPIService;
@Mock
private NacosServiceUtil nacosServiceUtil;
@Mock
private RedisTemplate<String, Object> redisTemplate;
@Mock
private ValueOperations<String, Object> valueOperations;
@Mock
private ListOperations<String, Object> listOperations;
@InjectMocks
private ServiceAPIController serviceAPIController;
@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
when(redisTemplate.opsForValue()).thenReturn(valueOperations);
when(redisTemplate.opsForList()).thenReturn(listOperations);
}
@Test
public void testMultiResourceAllocation() throws Exception {
System.out.println("===== 开始测试多资源分配 =====");
// 环境初始化
String ip1 = "192.168.1.1";
String ip2 = "192.168.1.2";
List<String> instanceIps = Arrays.asList(ip1, ip2);
Set<String> gpuKeys = new HashSet<>(Arrays.asList("gpu:1", "gpu:2"));
// 模拟两个GPU的总内存配置
when(valueOperations.get("gpu:1")).thenReturn("IP:" + ip1 + ",GPUMemorySize:8000");
when(valueOperations.get("gpu:2")).thenReturn("IP:" + ip2 + ",GPUMemorySize:10000");
// 第一个请求分配到IP1需要3000MB
System.out.println("\n=== 第一个请求分配到IP1 ===");
Long modelId1 = 1L;
String modelConfig1 = "GPUMemorySize:3000,version:1";
when(serviceAPIService.getByModelId(modelId1)).thenReturn(modelConfig1);
when(nacosServiceUtil.getServiceInstances(modelId1.toString())).thenReturn(instanceIps);
when(redisTemplate.keys("gpu:*")).thenReturn(gpuKeys);
// IP1首次使用无需提前设置ip:ip1默认用总内存8000
OptResult result1 = serviceAPIController.schedule(modelId1);
// 验证结果
assertTrue("第一个请求应成功", result1.isSuccess());
assertEquals("资源分配成功使用ip:" + ip1, result1.getData());
verify(valueOperations, times(1)).set("ip:" + ip1, 5000); // 8000-3000
System.out.println("IP1 可用内存=5000MB, IP2 可用内存=10000MB初始");
// 第二个请求分配到IP2需要6000MB
System.out.println("\n=== 第二个请求分配到IP2 ===");
Long modelId2 = 2L;
String modelConfig2 = "GPUMemorySize:6000,version:1";
when(serviceAPIService.getByModelId(modelId2)).thenReturn(modelConfig2);
when(nacosServiceUtil.getServiceInstances(modelId2.toString())).thenReturn(instanceIps);
when(valueOperations.get("ip:" + ip1)).thenReturn(5000); // IP1当前可用5000不足6000
// IP2首次使用无需提前设置ip:ip2默认用总内存10000
OptResult result2 = serviceAPIController.schedule(modelId2);
// 验证结果
assertTrue("第二个请求应成功", result2.isSuccess());
assertEquals("资源分配成功使用ip:" + ip2, result2.getData());
verify(valueOperations, times(1)).set("ip:" + ip2, 4000); // 10000-6000
System.out.println("IP1 可用内存=5000MB, IP2 可用内存=4000MB");
// 第三个请求(资源不足)
System.out.println("\n=== 第三个请求:资源不足 ===");
Long modelId3 = 3L;
String modelConfig3 = "GPUMemorySize:7000,version:1";
when(serviceAPIService.getByModelId(modelId3)).thenReturn(modelConfig3);
when(valueOperations.get("ip:" + ip1)).thenReturn(5000); // IP1可用5000 <7000
when(valueOperations.get("ip:" + ip2)).thenReturn(4000); // IP2可用4000 <7000
OptResult result3 = serviceAPIController.schedule(modelId3);
// 验证结果
assertFalse("第三个请求应失败", result3.isSuccess());
assertEquals("资源不足,等待中", result3.getErrorInfo());
verify(listOperations, times(1)).rightPush("waitQueue:" + modelId3, modelId3);
System.out.println("模型ID=" + modelId3 + " 加入等待队列");
System.out.println("===== 多资源分配测试完成 =====");
}
}