package database import ( "github.com/zeromicro/go-zero/core/logx" clientCore "gitlink.org.cn/JointCloud/pcm-coordinator/client" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/entity" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" "gorm.io/gorm" "strconv" "time" ) type CloudStorage struct { DbEngin *gorm.DB } func (c *CloudStorage) GetProviderParams() ([]entity.ProviderParams, error) { var proParams []entity.ProviderParams sqlstr := "SELECT SUM(a.disk_avail) as disk_avail,SUM(a.mem_avail) as mem_avail,SUM(a.cpu_total * a.cpu_usable) as cpu_avail,participant_id from (SELECT * from sc_node_avail_info where created_time in (SELECT MAX(created_time) as time from sc_node_avail_info where deleted_flag = 0 GROUP BY participant_id,node_name)) a GROUP BY a.participant_id" c.DbEngin.Raw(sqlstr).Scan(&proParams) if len(proParams) == 0 { return nil, nil } return proParams, nil } func (c *CloudStorage) FindAvailableParticipants() ([]entity.Participant, error) { var parts []entity.Participant sqlstr := "select id as participant_id, name as name from sc_participant_phy_info" c.DbEngin.Raw(sqlstr).Scan(&parts) if len(parts) == 0 { return nil, nil } return parts, nil } func (c *CloudStorage) GetClustersByAdapterId(id string) (*types.ClusterListResp, error) { var resp types.ClusterListResp tx := c.DbEngin.Raw("select * from t_cluster where `deleted_at` IS NULL and `adapter_id` = ? ORDER BY create_time Desc", id).Scan(&resp.List) if tx.Error != nil { logx.Errorf(tx.Error.Error()) return nil, tx.Error } return &resp, nil } func (s *CloudStorage) GetAdaptersByType(adapterType string) ([]*types.AdapterInfo, error) { var list []*types.AdapterInfo db := s.DbEngin.Model(&types.AdapterInfo{}).Table("t_adapter") db = db.Where("type = ?", adapterType) err := db.Order("create_time desc").Find(&list).Error if err != nil { return nil, err } return list, nil } func (c *CloudStorage) AddNoticeInfo(adapterId string, adapterName string, clusterId string, clusterName string, taskName string, noticeType string, incident string) { aId, err := strconv.ParseInt(adapterId, 10, 64) if err != nil { logx.Errorf("adapterId convert failure, err: %v", err) } var cId int64 if clusterId != "" { cId, err = strconv.ParseInt(clusterId, 10, 64) if err != nil { logx.Errorf("clusterId convert failure, err: %v", err) } } noticeInfo := clientCore.NoticeInfo{ AdapterId: aId, AdapterName: adapterName, ClusterId: cId, ClusterName: clusterName, NoticeType: noticeType, TaskName: taskName, Incident: incident, CreatedTime: time.Now(), } result := c.DbEngin.Table("t_notice").Create(¬iceInfo) if result.Error != nil { logx.Errorf("Task creation failure, err: %v", result.Error) } }