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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. tx := s.DbEngin.Raw("select * from task_ai where `adapter_id` = ? ", adapterId).Order("commit_time desc").Scan(&resp)
  69. if tx.Error != nil {
  70. logx.Errorf(tx.Error.Error())
  71. return nil, tx.Error
  72. }
  73. return resp, nil
  74. }
  75. func (s *AiStorage) SaveTask(name string, strategyCode int64, synergyStatus int64) (int64, error) {
  76. // 构建主任务结构体
  77. taskModel := models.Task{
  78. Status: constants.Saved,
  79. Description: "ai task",
  80. Name: name,
  81. SynergyStatus: synergyStatus,
  82. Strategy: strategyCode,
  83. AdapterTypeDict: 1,
  84. CommitTime: time.Now(),
  85. }
  86. // 保存任务数据到数据库
  87. tx := s.DbEngin.Create(&taskModel)
  88. if tx.Error != nil {
  89. return 0, tx.Error
  90. }
  91. return taskModel.Id, nil
  92. }
  93. func (s *AiStorage) SaveAiTask(taskId int64, option *option.AiOption, clusterId string, clusterName string, jobId string, status string, msg string) error {
  94. // 构建主任务结构体
  95. aId, err := strconv.ParseInt(option.AdapterId, 10, 64)
  96. if err != nil {
  97. return err
  98. }
  99. cId, err := strconv.ParseInt(clusterId, 10, 64)
  100. if err != nil {
  101. return err
  102. }
  103. del, _ := time.Parse(constants.Layout, constants.Layout)
  104. aiTaskModel := models.TaskAi{
  105. TaskId: taskId,
  106. AdapterId: aId,
  107. ClusterId: cId,
  108. ClusterName: clusterName,
  109. Name: option.TaskName,
  110. Replica: option.Replica,
  111. JobId: jobId,
  112. TaskType: option.TaskType,
  113. Strategy: option.StrategyName,
  114. Status: status,
  115. Msg: msg,
  116. Card: option.ComputeCard,
  117. DeletedAt: del,
  118. CommitTime: time.Now(),
  119. }
  120. // 保存任务数据到数据库
  121. tx := s.DbEngin.Create(&aiTaskModel)
  122. if tx.Error != nil {
  123. return tx.Error
  124. }
  125. return nil
  126. }
  127. func (s *AiStorage) SaveClusterTaskQueue(adapterId string, clusterId string, queueNum int64) error {
  128. aId, err := strconv.ParseInt(adapterId, 10, 64)
  129. if err != nil {
  130. return err
  131. }
  132. cId, err := strconv.ParseInt(clusterId, 10, 64)
  133. if err != nil {
  134. return err
  135. }
  136. taskQueue := models.TClusterTaskQueue{
  137. AdapterId: aId,
  138. ClusterId: cId,
  139. QueueNum: queueNum,
  140. }
  141. tx := s.DbEngin.Create(&taskQueue)
  142. if tx.Error != nil {
  143. return tx.Error
  144. }
  145. return nil
  146. }
  147. func (s *AiStorage) GetClusterTaskQueues(adapterId string, clusterId string) ([]*models.TClusterTaskQueue, error) {
  148. var taskQueues []*models.TClusterTaskQueue
  149. tx := s.DbEngin.Raw("select * from t_cluster_task_queue where `adapter_id` = ? and `cluster_id` = ?", adapterId, clusterId).Scan(&taskQueues)
  150. if tx.Error != nil {
  151. logx.Errorf(tx.Error.Error())
  152. return nil, tx.Error
  153. }
  154. return taskQueues, nil
  155. }
  156. func (s *AiStorage) GetAiTaskIdByClusterIdAndTaskId(clusterId string, taskId string) (string, error) {
  157. var aiTask models.TaskAi
  158. tx := s.DbEngin.Raw("select * from task_ai where `cluster_id` = ? and `task_id` = ?", clusterId, taskId).Scan(&aiTask)
  159. if tx.Error != nil {
  160. logx.Errorf(tx.Error.Error())
  161. return "", tx.Error
  162. }
  163. return aiTask.JobId, nil
  164. }
  165. func (s *AiStorage) GetClusterResourcesById(clusterId string) (*models.TClusterResource, error) {
  166. var clusterResource models.TClusterResource
  167. tx := s.DbEngin.Raw("select * from t_cluster_resource where `cluster_id` = ?", clusterId).Scan(&clusterResource)
  168. if tx.Error != nil {
  169. logx.Errorf(tx.Error.Error())
  170. return nil, tx.Error
  171. }
  172. return &clusterResource, nil
  173. }
  174. func (s *AiStorage) SaveClusterResources(clusterId string, clusterName string, clusterType int64, cpuAvail float64, cpuTotal float64,
  175. memAvail float64, memTotal float64, diskAvail float64, diskTotal float64, gpuAvail float64, gpuTotal float64, cardTotal int64, topsTotal float64) error {
  176. cId, err := strconv.ParseInt(clusterId, 10, 64)
  177. if err != nil {
  178. return err
  179. }
  180. clusterResource := models.TClusterResource{
  181. ClusterId: cId,
  182. ClusterName: clusterName,
  183. ClusterType: clusterType,
  184. CpuAvail: cpuAvail,
  185. CpuTotal: cpuTotal,
  186. MemAvail: memAvail,
  187. MemTotal: memTotal,
  188. DiskAvail: diskAvail,
  189. DiskTotal: diskTotal,
  190. GpuAvail: gpuAvail,
  191. GpuTotal: gpuTotal,
  192. CardTotal: cardTotal,
  193. CardTopsTotal: topsTotal,
  194. }
  195. tx := s.DbEngin.Create(&clusterResource)
  196. if tx.Error != nil {
  197. return tx.Error
  198. }
  199. return nil
  200. }
  201. func (s *AiStorage) UpdateClusterResources(clusterResource *models.TClusterResource) error {
  202. tx := s.DbEngin.Updates(clusterResource)
  203. if tx.Error != nil {
  204. return tx.Error
  205. }
  206. return nil
  207. }
  208. func (s *AiStorage) UpdateAiTask(task *models.TaskAi) error {
  209. tx := s.DbEngin.Updates(task)
  210. if tx.Error != nil {
  211. return tx.Error
  212. }
  213. return nil
  214. }
  215. func (s *AiStorage) GetStrategyCode(name string) (int64, error) {
  216. var strategy int64
  217. sqlStr := `select t_dict_item.item_value
  218. from t_dict
  219. left join t_dict_item on t_dict.id = t_dict_item.dict_id
  220. where item_text = ?
  221. and t_dict.dict_code = 'schedule_Strategy'`
  222. //查询调度策略
  223. err := s.DbEngin.Raw(sqlStr, name).Scan(&strategy).Error
  224. if err != nil {
  225. return strategy, nil
  226. }
  227. return strategy, nil
  228. }

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.