package models import ( "github.com/TeaOSLab/EdgeAPI/internal/errors" _ "github.com/go-sql-driver/mysql" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/dbs" ) const ( NodeClusterMetricItemStateEnabled = 1 // 已启用 NodeClusterMetricItemStateDisabled = 0 // 已禁用 ) type NodeClusterMetricItemDAO dbs.DAO func NewNodeClusterMetricItemDAO() *NodeClusterMetricItemDAO { return dbs.NewDAO(&NodeClusterMetricItemDAO{ DAOObject: dbs.DAOObject{ DB: Tea.Env, Table: "edgeNodeClusterMetricItems", Model: new(NodeClusterMetricItem), PkName: "id", }, }).(*NodeClusterMetricItemDAO) } var SharedNodeClusterMetricItemDAO *NodeClusterMetricItemDAO func init() { dbs.OnReady(func() { SharedNodeClusterMetricItemDAO = NewNodeClusterMetricItemDAO() }) } // EnableNodeClusterMetricItem 启用条目 func (this *NodeClusterMetricItemDAO) EnableNodeClusterMetricItem(tx *dbs.Tx, id uint32) error { _, err := this.Query(tx). Pk(id). Set("state", NodeClusterMetricItemStateEnabled). Update() return err } // DisableNodeClusterMetricItem 禁用条目 func (this *NodeClusterMetricItemDAO) DisableNodeClusterMetricItem(tx *dbs.Tx, id uint32) error { _, err := this.Query(tx). Pk(id). Set("state", NodeClusterMetricItemStateDisabled). Update() return err } // FindEnabledNodeClusterMetricItem 查找启用中的条目 func (this *NodeClusterMetricItemDAO) FindEnabledNodeClusterMetricItem(tx *dbs.Tx, id uint32) (*NodeClusterMetricItem, error) { result, err := this.Query(tx). Pk(id). Attr("state", NodeClusterMetricItemStateEnabled). Find() if result == nil { return nil, err } return result.(*NodeClusterMetricItem), err } // DisableClusterItem 禁用某个集群的指标 func (this *NodeClusterMetricItemDAO) DisableClusterItem(tx *dbs.Tx, clusterId int64, itemId int64) error { return this.Query(tx). Attr("clusterId", clusterId). Attr("itemId", itemId). State(NodeClusterMetricItemStateEnabled). Set("state", NodeClusterMetricItemStateDisabled). UpdateQuickly() } // EnableClusterItem 启用某个集群的指标 func (this *NodeClusterMetricItemDAO) EnableClusterItem(tx *dbs.Tx, clusterId int64, itemId int64) error { if clusterId <= 0 || itemId <= 0 { return errors.New("clusterId or itemId should not be 0") } var op = NewNodeClusterMetricItemOperator() op.ClusterId = clusterId op.ItemId = itemId op.IsOn = true return this.Save(tx, op) } // FindAllClusterItems 查找某个集群的指标 func (this *NodeClusterMetricItemDAO) FindAllClusterItems(tx *dbs.Tx, clusterId int64) (result []*NodeClusterMetricItem, err error) { _, err = this.Query(tx). Attr("clusterId", clusterId). State(NodeClusterMetricItemStateEnabled). DescPk(). Slice(&result). FindAll() return } // CountAllClusterItems 计算集群中指标数量 func (this *NodeClusterMetricItemDAO) CountAllClusterItems(tx *dbs.Tx, clusterId int64) (int64, error) { return this.Query(tx). Attr("clusterId", clusterId). State(NodeClusterMetricItemStateEnabled). Count() }