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

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.