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

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

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.