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.

aiStorage.go 21 kB

5 months ago
6 months ago
6 months ago
11 months ago
5 months ago
5 months ago
5 months ago
5 months ago
10 months ago
11 months ago
5 months ago
5 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. package database
  2. import (
  3. "github.com/zeromicro/go-zero/core/logx"
  4. clientCore "gitlink.org.cn/JointCloud/pcm-coordinator/client"
  5. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/schedulers/option"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  9. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result"
  10. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/tracker"
  11. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
  12. "gorm.io/gorm"
  13. "strconv"
  14. "time"
  15. )
  16. type AiStorage struct {
  17. DbEngin *gorm.DB
  18. }
  19. func (s *AiStorage) GetParticipants() (*types.ClusterListResp, error) {
  20. var resp types.ClusterListResp
  21. tx := s.DbEngin.Raw("select * from t_cluster where `deleted_at` IS NULL ORDER BY create_time Desc").Scan(&resp.List)
  22. if tx.Error != nil {
  23. logx.Errorf(tx.Error.Error())
  24. return nil, tx.Error
  25. }
  26. return &resp, nil
  27. }
  28. func (s *AiStorage) GetClustersByAdapterId(id string) (*types.ClusterListResp, error) {
  29. var resp types.ClusterListResp
  30. tx := s.DbEngin.Raw("select * from t_cluster where `deleted_at` IS NULL and `adapter_id` = ? ORDER BY create_time Desc", id).Scan(&resp.List)
  31. if tx.Error != nil {
  32. logx.Errorf(tx.Error.Error())
  33. return nil, tx.Error
  34. }
  35. return &resp, nil
  36. }
  37. func (s *AiStorage) GetClusterNameById(id string) (string, error) {
  38. var name string
  39. tx := s.DbEngin.Raw("select `description` from t_cluster where `id` = ?", id).Scan(&name)
  40. if tx.Error != nil {
  41. logx.Errorf(tx.Error.Error())
  42. return "", tx.Error
  43. }
  44. return name, nil
  45. }
  46. func (s *AiStorage) GetAdapterNameById(id string) (string, error) {
  47. var name string
  48. tx := s.DbEngin.Raw("select `name` from t_adapter where `id` = ?", id).Scan(&name)
  49. if tx.Error != nil {
  50. logx.Errorf(tx.Error.Error())
  51. return "", tx.Error
  52. }
  53. return name, nil
  54. }
  55. func (s *AiStorage) GetAdapterIdsByType(adapterType string) ([]string, error) {
  56. var list []types.AdapterInfo
  57. var ids []string
  58. db := s.DbEngin.Model(&types.AdapterInfo{}).Table("t_adapter")
  59. db = db.Where("type = ?", adapterType)
  60. err := db.Order("create_time desc").Find(&list).Error
  61. if err != nil {
  62. return nil, err
  63. }
  64. for _, info := range list {
  65. ids = append(ids, info.Id)
  66. }
  67. return ids, nil
  68. }
  69. func (s *AiStorage) GetAdaptersByType(adapterType string) ([]*types.AdapterInfo, error) {
  70. var list []*types.AdapterInfo
  71. db := s.DbEngin.Model(&types.AdapterInfo{}).Table("t_adapter")
  72. db = db.Where("type = ?", adapterType)
  73. err := db.Order("create_time desc").Find(&list).Error
  74. if err != nil {
  75. return nil, err
  76. }
  77. return list, nil
  78. }
  79. func (s *AiStorage) GetAiTasksByAdapterId(adapterId string) ([]*models.TaskAi, error) {
  80. var resp []*models.TaskAi
  81. db := s.DbEngin.Model(&models.TaskAi{}).Table("task_ai")
  82. db = db.Where("adapter_id = ?", adapterId)
  83. err := db.Order("commit_time desc").Find(&resp).Error
  84. if err != nil {
  85. return nil, err
  86. }
  87. return resp, nil
  88. }
  89. func (s *AiStorage) GetAiTaskListById(id int64) ([]*models.TaskAi, error) {
  90. var aiTaskList []*models.TaskAi
  91. tx := s.DbEngin.Raw("select * from task_ai where `task_id` = ? ", id).Scan(&aiTaskList)
  92. if tx.Error != nil {
  93. return nil, tx.Error
  94. }
  95. return aiTaskList, nil
  96. }
  97. func (s *AiStorage) DoesTaskNameExist(name string, taskType string) (bool, error) {
  98. var total int32
  99. switch taskType {
  100. case "training":
  101. tx := s.DbEngin.Raw("select count(*) from task where `name` = ?", name).Scan(&total)
  102. if tx.Error != nil {
  103. logx.Errorf(tx.Error.Error())
  104. return false, tx.Error
  105. }
  106. case "inference":
  107. tx := s.DbEngin.Raw("select count(*) from ai_deploy_instance_task where `name` = ?", name).Scan(&total)
  108. if tx.Error != nil {
  109. logx.Errorf(tx.Error.Error())
  110. return false, tx.Error
  111. }
  112. }
  113. return total > 0, nil
  114. }
  115. func (s *AiStorage) SaveTask(name string, desc string, userId int64, strategyCode int64, synergyStatus int64, aiType string, yaml string, saveToChain func(task models.Task, id int64) error) (int64, error) {
  116. startTime := time.Now()
  117. // 构建主任务结构体
  118. taskModel := models.Task{
  119. Id: utils.GenSnowflakeID(),
  120. Status: constants.Saved,
  121. Description: desc,
  122. Name: name,
  123. UserId: userId,
  124. SynergyStatus: synergyStatus,
  125. Strategy: strategyCode,
  126. AdapterTypeDict: "1",
  127. TaskTypeDict: aiType,
  128. YamlString: yaml,
  129. StartTime: &startTime,
  130. CommitTime: time.Now(),
  131. }
  132. // 保存任务数据到数据库
  133. tx := s.DbEngin.Create(&taskModel)
  134. if tx.Error != nil {
  135. return 0, tx.Error
  136. }
  137. id := taskModel.Id
  138. // 数据上链
  139. if saveToChain != nil {
  140. err := saveToChain(taskModel, id)
  141. if err != nil {
  142. logx.Error(err)
  143. }
  144. }
  145. return id, nil
  146. }
  147. func (s *AiStorage) UpdateTask(task *types.TaskModel) error {
  148. task.UpdatedTime = time.Now().Format(constants.Layout)
  149. tx := s.DbEngin.Table("task").Model(task).Updates(task)
  150. if tx.Error != nil {
  151. logx.Errorf(tx.Error.Error())
  152. return tx.Error
  153. }
  154. return nil
  155. }
  156. func (s *AiStorage) AllTaskLastMonth() ([]*types.TaskModel, error) {
  157. var list []*types.TaskModel
  158. // 构建数据库查询
  159. db := s.DbEngin.Model(&types.TaskModel{}).Table("task")
  160. now := time.Now()
  161. lastMonth := now.AddDate(0, -1, 0)
  162. db = db.Where("created_time >= ?", lastMonth)
  163. // 查询任务列表
  164. if err := db.Order("created_time desc").Find(&list).Error; err != nil {
  165. return nil, result.NewDefaultError(err.Error())
  166. }
  167. return list, nil
  168. }
  169. func (s *AiStorage) SaveAiTask(taskId int64, opt option.Option, adapterName string, clusterId string, clusterName string, jobId string, status string, msg string) error {
  170. var aiOpt *option.AiOption
  171. switch (opt).(type) {
  172. case *option.AiOption:
  173. aiOpt = (opt).(*option.AiOption)
  174. case *option.InferOption:
  175. inferOpt := (opt).(*option.InferOption)
  176. aiOpt = &option.AiOption{}
  177. aiOpt.TaskName = inferOpt.TaskName
  178. aiOpt.Replica = inferOpt.Replica
  179. aiOpt.AdapterId = inferOpt.AdapterId
  180. aiOpt.TaskType = inferOpt.ModelType
  181. aiOpt.ModelName = inferOpt.ModelName
  182. aiOpt.StrategyName = inferOpt.Strategy
  183. }
  184. // 构建主任务结构体
  185. aId, err := strconv.ParseInt(aiOpt.AdapterId, 10, 64)
  186. if err != nil {
  187. return err
  188. }
  189. cId, err := strconv.ParseInt(clusterId, 10, 64)
  190. if err != nil {
  191. return err
  192. }
  193. aiTaskModel := models.TaskAi{
  194. TaskId: taskId,
  195. AdapterId: aId,
  196. AdapterName: adapterName,
  197. ClusterId: cId,
  198. ClusterName: clusterName,
  199. Name: aiOpt.TaskName,
  200. Replica: int64(aiOpt.Replica),
  201. JobId: jobId,
  202. TaskType: aiOpt.TaskType,
  203. ModelName: aiOpt.ModelName,
  204. Strategy: aiOpt.StrategyName,
  205. Status: status,
  206. Msg: msg,
  207. Output: aiOpt.Output,
  208. Card: aiOpt.ComputeCard,
  209. StartTime: time.Now().Format(time.RFC3339),
  210. CommitTime: time.Now(),
  211. }
  212. // 保存任务数据到数据库
  213. tx := s.DbEngin.Create(&aiTaskModel)
  214. if tx.Error != nil {
  215. return tx.Error
  216. }
  217. return nil
  218. }
  219. func (s *AiStorage) SaveAiTaskImageSubTask(ta *models.TaskAiSub) error {
  220. tx := s.DbEngin.Table("task_ai_sub").Create(ta)
  221. if tx.Error != nil {
  222. return tx.Error
  223. }
  224. return nil
  225. }
  226. func (s *AiStorage) SaveClusterTaskQueue(adapterId string, clusterId string, queueNum int64) error {
  227. aId, err := strconv.ParseInt(adapterId, 10, 64)
  228. if err != nil {
  229. return err
  230. }
  231. cId, err := strconv.ParseInt(clusterId, 10, 64)
  232. if err != nil {
  233. return err
  234. }
  235. taskQueue := models.TClusterTaskQueue{
  236. AdapterId: aId,
  237. ClusterId: cId,
  238. QueueNum: queueNum,
  239. }
  240. tx := s.DbEngin.Create(&taskQueue)
  241. if tx.Error != nil {
  242. return tx.Error
  243. }
  244. return nil
  245. }
  246. func (s *AiStorage) GetClusterTaskQueues(adapterId string, clusterId string) ([]*models.TClusterTaskQueue, error) {
  247. var taskQueues []*models.TClusterTaskQueue
  248. tx := s.DbEngin.Raw("select * from t_cluster_task_queue where `adapter_id` = ? and `cluster_id` = ?", adapterId, clusterId).Scan(&taskQueues)
  249. if tx.Error != nil {
  250. logx.Errorf(tx.Error.Error())
  251. return nil, tx.Error
  252. }
  253. return taskQueues, nil
  254. }
  255. func (s *AiStorage) GetAiTaskIdByClusterIdAndTaskId(clusterId string, taskId string) (string, error) {
  256. var aiTask models.TaskAi
  257. tx := s.DbEngin.Raw("select * from task_ai where `cluster_id` = ? and `task_id` = ?", clusterId, taskId).Scan(&aiTask)
  258. if tx.Error != nil {
  259. logx.Errorf(tx.Error.Error())
  260. return "", tx.Error
  261. }
  262. return aiTask.JobId, nil
  263. }
  264. func (s *AiStorage) GetClusterResourcesById(clusterId string) (*models.TClusterResource, error) {
  265. var clusterResource models.TClusterResource
  266. tx := s.DbEngin.Raw("select * from t_cluster_resource where `cluster_id` = ?", clusterId).Scan(&clusterResource)
  267. if tx.Error != nil {
  268. logx.Errorf(tx.Error.Error())
  269. return nil, tx.Error
  270. }
  271. return &clusterResource, nil
  272. }
  273. func (s *AiStorage) SaveClusterResources(adapterId string, clusterId string, clusterName string, clusterType int64, cpuAvail float64, cpuTotal float64,
  274. memAvail float64, memTotal float64, diskAvail float64, diskTotal float64, gpuAvail float64, gpuTotal float64, cardTotal int64, topsTotal float64, cardHours float64,
  275. balance float64, taskCompleted int64) error {
  276. cId, err := strconv.ParseInt(clusterId, 10, 64)
  277. if err != nil {
  278. return err
  279. }
  280. aId, err := strconv.ParseInt(adapterId, 10, 64)
  281. if err != nil {
  282. return err
  283. }
  284. clusterResource := models.TClusterResource{
  285. AdapterId: aId,
  286. ClusterId: cId,
  287. ClusterName: clusterName,
  288. ClusterType: clusterType,
  289. CpuAvail: cpuAvail,
  290. CpuTotal: cpuTotal,
  291. MemAvail: memAvail,
  292. MemTotal: memTotal,
  293. DiskAvail: diskAvail,
  294. DiskTotal: diskTotal,
  295. GpuAvail: gpuAvail,
  296. GpuTotal: gpuTotal,
  297. CardTotal: cardTotal,
  298. CardTopsTotal: topsTotal,
  299. CardHours: cardHours,
  300. Balance: balance,
  301. TaskCompleted: taskCompleted,
  302. }
  303. tx := s.DbEngin.Create(&clusterResource)
  304. if tx.Error != nil {
  305. return tx.Error
  306. }
  307. // prometheus
  308. param := tracker.ClusterLoadRecord{
  309. AdapterId: aId,
  310. ClusterName: clusterName,
  311. CpuAvail: cpuAvail,
  312. CpuTotal: cpuTotal,
  313. CpuUtilisation: clusterResource.CpuAvail / clusterResource.CpuTotal,
  314. MemoryAvail: memAvail,
  315. MemoryTotal: memTotal,
  316. MemoryUtilisation: clusterResource.MemAvail / clusterResource.MemTotal,
  317. DiskAvail: diskAvail,
  318. DiskTotal: diskTotal,
  319. DiskUtilisation: clusterResource.DiskAvail / clusterResource.DiskTotal,
  320. }
  321. tracker.SyncClusterLoad(param)
  322. return nil
  323. }
  324. func (s *AiStorage) UpdateClusterResources(clusterResource *models.TClusterResource) error {
  325. tx := s.DbEngin.Where("cluster_id = ?", clusterResource.ClusterId).Updates(clusterResource)
  326. if tx.Error != nil {
  327. return tx.Error
  328. }
  329. // prometheus
  330. param := tracker.ClusterLoadRecord{
  331. AdapterId: clusterResource.AdapterId,
  332. ClusterName: clusterResource.ClusterName,
  333. CpuAvail: clusterResource.CpuAvail,
  334. CpuTotal: clusterResource.CpuTotal,
  335. CpuUtilisation: clusterResource.CpuAvail / clusterResource.CpuTotal,
  336. MemoryAvail: clusterResource.MemAvail,
  337. MemoryTotal: clusterResource.MemTotal,
  338. MemoryUtilisation: clusterResource.MemAvail / clusterResource.MemTotal,
  339. DiskAvail: clusterResource.DiskAvail,
  340. DiskTotal: clusterResource.DiskTotal,
  341. DiskUtilisation: clusterResource.DiskAvail / clusterResource.DiskTotal,
  342. }
  343. tracker.SyncClusterLoad(param)
  344. return nil
  345. }
  346. func (s *AiStorage) UpdateAiTask(task *models.TaskAi) error {
  347. tx := s.DbEngin.Updates(task)
  348. if tx.Error != nil {
  349. return tx.Error
  350. }
  351. return nil
  352. }
  353. func (s *AiStorage) UpdateTaskByModel(task *models.Task) error {
  354. tx := s.DbEngin.Updates(task)
  355. if tx.Error != nil {
  356. return tx.Error
  357. }
  358. return nil
  359. }
  360. func (s *AiStorage) GetStrategyCode(name string) (int64, error) {
  361. var strategy int64
  362. sqlStr := `select t_dict_item.item_value
  363. from t_dict
  364. left join t_dict_item on t_dict.id = t_dict_item.dict_id
  365. where item_text = ?
  366. and t_dict.dict_code = 'schedule_Strategy'`
  367. //查询调度策略
  368. err := s.DbEngin.Raw(sqlStr, name).Scan(&strategy).Error
  369. if err != nil {
  370. return strategy, nil
  371. }
  372. return strategy, nil
  373. }
  374. func (s *AiStorage) AddNoticeInfo(adapterId string, adapterName string, clusterId string, clusterName string, taskName string, noticeType string, incident string) {
  375. aId, err := strconv.ParseInt(adapterId, 10, 64)
  376. if err != nil {
  377. logx.Errorf("adapterId convert failure, err: %v", err)
  378. }
  379. var cId int64
  380. if clusterId != "" {
  381. cId, err = strconv.ParseInt(clusterId, 10, 64)
  382. if err != nil {
  383. logx.Errorf("clusterId convert failure, err: %v", err)
  384. }
  385. }
  386. noticeInfo := clientCore.NoticeInfo{
  387. AdapterId: aId,
  388. AdapterName: adapterName,
  389. ClusterId: cId,
  390. ClusterName: clusterName,
  391. NoticeType: noticeType,
  392. TaskName: taskName,
  393. Incident: incident,
  394. CreatedTime: time.Now(),
  395. }
  396. result := s.DbEngin.Table("t_notice").Create(&noticeInfo)
  397. if result.Error != nil {
  398. logx.Errorf("Task creation failure, err: %v", result.Error)
  399. }
  400. }
  401. func (s *AiStorage) SaveInferDeployInstance(taskId int64, instanceId string, instanceName string, adapterId int64,
  402. adapterName string, clusterId int64, clusterName string, modelName string, modelType string, inferCard string, clusterType string) (int64, error) {
  403. startTime := time.Now().Format(time.RFC3339)
  404. // 构建主任务结构体
  405. insModel := models.AiInferDeployInstance{
  406. DeployInstanceTaskId: taskId,
  407. InstanceId: instanceId,
  408. InstanceName: instanceName,
  409. AdapterId: adapterId,
  410. AdapterName: adapterName,
  411. ClusterId: clusterId,
  412. ClusterName: clusterName,
  413. ModelName: modelName,
  414. ModelType: modelType,
  415. InferCard: inferCard,
  416. ClusterType: clusterType,
  417. Status: constants.Deploying,
  418. CreateTime: startTime,
  419. UpdateTime: startTime,
  420. }
  421. // 保存任务数据到数据库
  422. tx := s.DbEngin.Table("ai_infer_deploy_instance").Create(&insModel)
  423. if tx.Error != nil {
  424. return 0, tx.Error
  425. }
  426. return insModel.Id, nil
  427. }
  428. func (s *AiStorage) UpdateInferDeployInstance(instance *models.AiInferDeployInstance, needUpdateTime bool) error {
  429. if needUpdateTime {
  430. instance.UpdateTime = time.Now().Format(time.RFC3339)
  431. }
  432. tx := s.DbEngin.Table("ai_infer_deploy_instance").Updates(instance)
  433. if tx.Error != nil {
  434. logx.Errorf(tx.Error.Error())
  435. return tx.Error
  436. }
  437. return nil
  438. }
  439. func (s *AiStorage) GetTaskById(id int64) (*models.Task, error) {
  440. var task *models.Task
  441. tx := s.DbEngin.Raw("select * from task where `id` = ?", id).Scan(&task)
  442. if tx.Error != nil {
  443. logx.Errorf(tx.Error.Error())
  444. return nil, tx.Error
  445. }
  446. return task, nil
  447. }
  448. func (s *AiStorage) GetInferDeployInstanceById(id int64) (*models.AiInferDeployInstance, error) {
  449. var deployIns *models.AiInferDeployInstance
  450. tx := s.DbEngin.Raw("select * from ai_infer_deploy_instance where `id` = ?", id).Scan(&deployIns)
  451. if tx.Error != nil {
  452. logx.Errorf(tx.Error.Error())
  453. return nil, tx.Error
  454. }
  455. return deployIns, nil
  456. }
  457. func (s *AiStorage) GetDeployTaskById(id int64) (*models.AiDeployInstanceTask, error) {
  458. var task *models.AiDeployInstanceTask
  459. tx := s.DbEngin.Raw("select * from ai_deploy_instance_task where `id` = ?", id).Scan(&task)
  460. if tx.Error != nil {
  461. logx.Errorf(tx.Error.Error())
  462. return nil, tx.Error
  463. }
  464. return task, nil
  465. }
  466. func (s *AiStorage) GetDeployTaskListByType(modelType string) ([]*models.AiDeployInstanceTask, error) {
  467. var tasks []*models.AiDeployInstanceTask
  468. tx := s.DbEngin.Raw("select * from ai_deploy_instance_task where `model_type` = ?", modelType).Scan(&tasks)
  469. if tx.Error != nil {
  470. logx.Errorf(tx.Error.Error())
  471. return nil, tx.Error
  472. }
  473. return tasks, nil
  474. }
  475. func (s *AiStorage) GetAllDeployTasks() ([]*models.AiDeployInstanceTask, error) {
  476. var tasks []*models.AiDeployInstanceTask
  477. tx := s.DbEngin.Raw("select * from ai_deploy_instance_task").Scan(&tasks)
  478. if tx.Error != nil {
  479. logx.Errorf(tx.Error.Error())
  480. return nil, tx.Error
  481. }
  482. return tasks, nil
  483. }
  484. func (s *AiStorage) UpdateDeployTask(task *models.AiDeployInstanceTask, needUpdateTime bool) error {
  485. if needUpdateTime {
  486. task.UpdateTime = time.Now().Format(time.RFC3339)
  487. }
  488. tx := s.DbEngin.Table("ai_deploy_instance_task").Updates(task)
  489. if tx.Error != nil {
  490. logx.Errorf(tx.Error.Error())
  491. return tx.Error
  492. }
  493. return nil
  494. }
  495. func (s *AiStorage) DeleteDeployTaskById(id int64) error {
  496. tx := s.DbEngin.Delete(&models.AiDeployInstanceTask{}, id)
  497. if tx.Error != nil {
  498. logx.Errorf(tx.Error.Error())
  499. return tx.Error
  500. }
  501. return nil
  502. }
  503. func (s *AiStorage) UpdateDeployTaskById(id int64) error {
  504. task, err := s.GetDeployTaskById(id)
  505. if err != nil {
  506. return err
  507. }
  508. err = s.UpdateDeployTask(task, true)
  509. if err != nil {
  510. return err
  511. }
  512. return nil
  513. }
  514. func (s *AiStorage) GetInstanceListByDeployTaskId(id int64) ([]*models.AiInferDeployInstance, error) {
  515. var list []*models.AiInferDeployInstance
  516. tx := s.DbEngin.Raw("select * from ai_infer_deploy_instance where `deploy_instance_task_id` = ?", id).Scan(&list)
  517. if tx.Error != nil {
  518. logx.Errorf(tx.Error.Error())
  519. return nil, tx.Error
  520. }
  521. return list, nil
  522. }
  523. func (s *AiStorage) GetInferDeployInstanceListLastMonth() ([]*models.AiInferDeployInstance, error) {
  524. var list []*models.AiInferDeployInstance
  525. now := time.Now()
  526. lastMonth := now.AddDate(0, -1, 0)
  527. tx := s.DbEngin.Raw("select * from ai_infer_deploy_instance where `create_time` >= ?", lastMonth).Scan(&list)
  528. if tx.Error != nil {
  529. logx.Errorf(tx.Error.Error())
  530. return nil, tx.Error
  531. }
  532. return list, nil
  533. }
  534. func (s *AiStorage) GetDeployTaskList() ([]*models.AiDeployInstanceTask, error) {
  535. var list []*models.AiDeployInstanceTask
  536. tx := s.DbEngin.Raw("select * from ai_deploy_instance_task").Scan(&list)
  537. if tx.Error != nil {
  538. logx.Errorf(tx.Error.Error())
  539. return nil, tx.Error
  540. }
  541. return list, nil
  542. }
  543. func (s *AiStorage) GetInferDeployInstanceTotalNum() (int32, error) {
  544. var total int32
  545. tx := s.DbEngin.Raw("select count(*) from ai_infer_deploy_instance").Scan(&total)
  546. if tx.Error != nil {
  547. logx.Errorf(tx.Error.Error())
  548. return 0, tx.Error
  549. }
  550. return total, nil
  551. }
  552. func (s *AiStorage) GetInferDeployInstanceRunningNum() (int32, error) {
  553. var total int32
  554. tx := s.DbEngin.Raw("select count(*) from ai_infer_deploy_instance where `status` = 'Running'").Scan(&total)
  555. if tx.Error != nil {
  556. logx.Errorf(tx.Error.Error())
  557. return 0, tx.Error
  558. }
  559. return total, nil
  560. }
  561. func (s *AiStorage) GetInferenceTaskTotalNum() (int32, error) {
  562. var total int32
  563. tx := s.DbEngin.Raw("select count(*) from task where `task_type_dict` = 11 or `task_type_dict` = 12").Scan(&total)
  564. if tx.Error != nil {
  565. logx.Errorf(tx.Error.Error())
  566. return 0, tx.Error
  567. }
  568. return total, nil
  569. }
  570. func (s *AiStorage) GetInferenceTaskRunningNum() (int32, error) {
  571. var total int32
  572. tx := s.DbEngin.Raw("select count(*) from task where `task_type_dict` = 11 and `status` = 'Running'").Scan(&total)
  573. if tx.Error != nil {
  574. logx.Errorf(tx.Error.Error())
  575. return 0, tx.Error
  576. }
  577. return total, nil
  578. }
  579. func (s *AiStorage) GetTrainingTaskTotalNum() (int32, error) {
  580. var total int32
  581. tx := s.DbEngin.Raw("select count(*) from task where `task_type_dict` = 10").Scan(&total)
  582. if tx.Error != nil {
  583. logx.Errorf(tx.Error.Error())
  584. return 0, tx.Error
  585. }
  586. return total, nil
  587. }
  588. func (s *AiStorage) GetTrainingTaskRunningNum() (int32, error) {
  589. var total int32
  590. tx := s.DbEngin.Raw("select count(*) from task where `task_type_dict` = 10 and `status` = 'Running'").Scan(&total)
  591. if tx.Error != nil {
  592. logx.Errorf(tx.Error.Error())
  593. return 0, tx.Error
  594. }
  595. return total, nil
  596. }
  597. func (s *AiStorage) SaveInferDeployTask(taskName string, userId int64, modelName string, modelType string, desc string) (int64, error) {
  598. startTime := time.Now().Format(time.RFC3339)
  599. // 构建主任务结构体
  600. taskModel := models.AiDeployInstanceTask{
  601. Id: utils.GenSnowflakeID(),
  602. Name: taskName,
  603. UserId: userId,
  604. ModelName: modelName,
  605. ModelType: modelType,
  606. Desc: desc,
  607. CreateTime: startTime,
  608. UpdateTime: startTime,
  609. }
  610. // 保存任务数据到数据库
  611. tx := s.DbEngin.Table("ai_deploy_instance_task").Create(&taskModel)
  612. if tx.Error != nil {
  613. return 0, tx.Error
  614. }
  615. return taskModel.Id, nil
  616. }
  617. func (s *AiStorage) GetRunningDeployInstanceById(id int64, adapterId string) ([]*models.AiInferDeployInstance, error) {
  618. var list []*models.AiInferDeployInstance
  619. tx := s.DbEngin.Raw("select * from ai_infer_deploy_instance where `deploy_instance_task_id` = ? and `adapter_id` = ? and `status` = 'Running'", id, adapterId).Scan(&list)
  620. if tx.Error != nil {
  621. logx.Errorf(tx.Error.Error())
  622. return nil, tx.Error
  623. }
  624. return list, nil
  625. }
  626. func (s *AiStorage) IsDeployTaskNameDuplicated(name string) (bool, error) {
  627. var total int32
  628. tx := s.DbEngin.Raw("select count(*) from ai_deploy_instance_task where `name` = ?", name).Scan(&total)
  629. if tx.Error != nil {
  630. return false, tx.Error
  631. }
  632. if total == 0 {
  633. return false, nil
  634. }
  635. return true, nil
  636. }
  637. func (s *AiStorage) GetClustersById(id string) (*types.ClusterInfo, error) {
  638. var resp types.ClusterInfo
  639. tx := s.DbEngin.Raw("select * from t_cluster where `id` = ? ", id).Scan(&resp)
  640. if tx.Error != nil {
  641. logx.Errorf(tx.Error.Error())
  642. return nil, tx.Error
  643. }
  644. return &resp, nil
  645. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.