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

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

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.