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

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.