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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package database
  2. import (
  3. "github.com/zeromicro/go-zero/core/logx"
  4. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/schedulers/option"
  5. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  8. "gorm.io/gorm"
  9. "strconv"
  10. "time"
  11. )
  12. type AiStorage struct {
  13. DbEngin *gorm.DB
  14. }
  15. func (s *AiStorage) GetParticipants() (*types.ClusterListResp, error) {
  16. var resp types.ClusterListResp
  17. tx := s.DbEngin.Raw("select * from t_cluster where `deleted_at` IS NULL ORDER BY create_time Desc").Scan(&resp.List)
  18. if tx.Error != nil {
  19. logx.Errorf(tx.Error.Error())
  20. return nil, tx.Error
  21. }
  22. return &resp, nil
  23. }
  24. func (s *AiStorage) GetClustersByAdapterId(id string) (*types.ClusterListResp, error) {
  25. var resp types.ClusterListResp
  26. 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)
  27. if tx.Error != nil {
  28. logx.Errorf(tx.Error.Error())
  29. return nil, tx.Error
  30. }
  31. return &resp, nil
  32. }
  33. func (s *AiStorage) GetClusterNameById(id string) (string, error) {
  34. var name string
  35. tx := s.DbEngin.Raw("select `description` from t_cluster where `id` = ?", id).Scan(&name)
  36. if tx.Error != nil {
  37. logx.Errorf(tx.Error.Error())
  38. return "", tx.Error
  39. }
  40. return name, nil
  41. }
  42. func (s *AiStorage) GetAdapterIdsByType(adapterType string) ([]string, error) {
  43. var list []types.AdapterInfo
  44. var ids []string
  45. db := s.DbEngin.Model(&types.AdapterInfo{}).Table("t_adapter")
  46. db = db.Where("type = ?", adapterType)
  47. err := db.Order("create_time desc").Find(&list).Error
  48. if err != nil {
  49. return nil, err
  50. }
  51. for _, info := range list {
  52. ids = append(ids, info.Id)
  53. }
  54. return ids, nil
  55. }
  56. func (s *AiStorage) GetAdaptersByType(adapterType string) ([]*types.AdapterInfo, error) {
  57. var list []*types.AdapterInfo
  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. return list, nil
  65. }
  66. func (s *AiStorage) GetAiTasksByAdapterId(adapterId string) ([]*models.TaskAi, error) {
  67. var resp []*models.TaskAi
  68. db := s.DbEngin.Model(&models.TaskAi{}).Table("task_ai")
  69. db = db.Where("adapter_id = ?", adapterId)
  70. err := db.Order("commit_time desc").Find(&resp).Error
  71. if err != nil {
  72. return nil, err
  73. }
  74. return resp, nil
  75. }
  76. func (s *AiStorage) SaveTask(name string, strategyCode int64, synergyStatus int64) (int64, error) {
  77. // 构建主任务结构体
  78. taskModel := models.Task{
  79. Status: constants.Saved,
  80. Description: "ai task",
  81. Name: name,
  82. SynergyStatus: synergyStatus,
  83. Strategy: strategyCode,
  84. AdapterTypeDict: 1,
  85. CommitTime: time.Now(),
  86. }
  87. // 保存任务数据到数据库
  88. tx := s.DbEngin.Create(&taskModel)
  89. if tx.Error != nil {
  90. return 0, tx.Error
  91. }
  92. return taskModel.Id, nil
  93. }
  94. func (s *AiStorage) SaveAiTask(taskId int64, option *option.AiOption, clusterId string, clusterName string, jobId string, status string, msg string) error {
  95. // 构建主任务结构体
  96. aId, err := strconv.ParseInt(option.AdapterId, 10, 64)
  97. if err != nil {
  98. return err
  99. }
  100. cId, err := strconv.ParseInt(clusterId, 10, 64)
  101. if err != nil {
  102. return err
  103. }
  104. del, _ := time.Parse(constants.Layout, constants.Layout)
  105. aiTaskModel := models.TaskAi{
  106. TaskId: taskId,
  107. AdapterId: aId,
  108. ClusterId: cId,
  109. ClusterName: clusterName,
  110. Name: option.TaskName,
  111. Replica: int64(option.Replica),
  112. JobId: jobId,
  113. TaskType: option.TaskType,
  114. Strategy: option.StrategyName,
  115. Status: status,
  116. Msg: msg,
  117. Card: option.ComputeCard,
  118. DeletedAt: del,
  119. CommitTime: time.Now(),
  120. }
  121. // 保存任务数据到数据库
  122. tx := s.DbEngin.Create(&aiTaskModel)
  123. if tx.Error != nil {
  124. return tx.Error
  125. }
  126. return nil
  127. }
  128. func (s *AiStorage) SaveClusterTaskQueue(adapterId string, clusterId string, queueNum int64) error {
  129. aId, err := strconv.ParseInt(adapterId, 10, 64)
  130. if err != nil {
  131. return err
  132. }
  133. cId, err := strconv.ParseInt(clusterId, 10, 64)
  134. if err != nil {
  135. return err
  136. }
  137. taskQueue := models.TClusterTaskQueue{
  138. AdapterId: aId,
  139. ClusterId: cId,
  140. QueueNum: queueNum,
  141. }
  142. tx := s.DbEngin.Create(&taskQueue)
  143. if tx.Error != nil {
  144. return tx.Error
  145. }
  146. return nil
  147. }
  148. func (s *AiStorage) GetClusterTaskQueues(adapterId string, clusterId string) ([]*models.TClusterTaskQueue, error) {
  149. var taskQueues []*models.TClusterTaskQueue
  150. tx := s.DbEngin.Raw("select * from t_cluster_task_queue where `adapter_id` = ? and `cluster_id` = ?", adapterId, clusterId).Scan(&taskQueues)
  151. if tx.Error != nil {
  152. logx.Errorf(tx.Error.Error())
  153. return nil, tx.Error
  154. }
  155. return taskQueues, nil
  156. }
  157. func (s *AiStorage) GetAiTaskIdByClusterIdAndTaskId(clusterId string, taskId string) (string, error) {
  158. var aiTask models.TaskAi
  159. tx := s.DbEngin.Raw("select * from task_ai where `cluster_id` = ? and `task_id` = ?", clusterId, taskId).Scan(&aiTask)
  160. if tx.Error != nil {
  161. logx.Errorf(tx.Error.Error())
  162. return "", tx.Error
  163. }
  164. return aiTask.JobId, nil
  165. }
  166. func (s *AiStorage) GetClusterResourcesById(clusterId string) (*models.TClusterResource, error) {
  167. var clusterResource models.TClusterResource
  168. tx := s.DbEngin.Raw("select * from t_cluster_resource where `cluster_id` = ?", clusterId).Scan(&clusterResource)
  169. if tx.Error != nil {
  170. logx.Errorf(tx.Error.Error())
  171. return nil, tx.Error
  172. }
  173. return &clusterResource, nil
  174. }
  175. func (s *AiStorage) SaveClusterResources(clusterId string, clusterName string, clusterType int64, cpuAvail float64, cpuTotal float64,
  176. memAvail float64, memTotal float64, diskAvail float64, diskTotal float64, gpuAvail float64, gpuTotal float64, cardTotal int64, topsTotal float64) error {
  177. cId, err := strconv.ParseInt(clusterId, 10, 64)
  178. if err != nil {
  179. return err
  180. }
  181. clusterResource := models.TClusterResource{
  182. ClusterId: cId,
  183. ClusterName: clusterName,
  184. ClusterType: clusterType,
  185. CpuAvail: cpuAvail,
  186. CpuTotal: cpuTotal,
  187. MemAvail: memAvail,
  188. MemTotal: memTotal,
  189. DiskAvail: diskAvail,
  190. DiskTotal: diskTotal,
  191. GpuAvail: gpuAvail,
  192. GpuTotal: gpuTotal,
  193. CardTotal: cardTotal,
  194. CardTopsTotal: topsTotal,
  195. }
  196. tx := s.DbEngin.Create(&clusterResource)
  197. if tx.Error != nil {
  198. return tx.Error
  199. }
  200. return nil
  201. }
  202. func (s *AiStorage) UpdateClusterResources(clusterResource *models.TClusterResource) error {
  203. tx := s.DbEngin.Updates(clusterResource)
  204. if tx.Error != nil {
  205. return tx.Error
  206. }
  207. return nil
  208. }
  209. func (s *AiStorage) UpdateAiTask(task *models.TaskAi) error {
  210. tx := s.DbEngin.Updates(task)
  211. if tx.Error != nil {
  212. return tx.Error
  213. }
  214. return nil
  215. }
  216. func (s *AiStorage) GetStrategyCode(name string) (int64, error) {
  217. var strategy int64
  218. sqlStr := `select t_dict_item.item_value
  219. from t_dict
  220. left join t_dict_item on t_dict.id = t_dict_item.dict_id
  221. where item_text = ?
  222. and t_dict.dict_code = 'schedule_Strategy'`
  223. //查询调度策略
  224. err := s.DbEngin.Raw(sqlStr, name).Scan(&strategy).Error
  225. if err != nil {
  226. return strategy, nil
  227. }
  228. return strategy, nil
  229. }

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.