Dashboard增加指标图表

This commit is contained in:
刘祥超
2021-07-19 17:58:16 +08:00
parent f52ea4fa4f
commit c6a09e131b
9 changed files with 431 additions and 279 deletions

View File

@@ -49,7 +49,15 @@ func (this *MetricItemDAO) EnableMetricItem(tx *dbs.Tx, id int64) error {
// DisableMetricItem 禁用条目
func (this *MetricItemDAO) DisableMetricItem(tx *dbs.Tx, itemId int64) error {
_, err := this.Query(tx).
isPublic, err := this.Query(tx).
Pk(itemId).
Result("isPublic").
FindIntCol(0)
if err != nil {
return err
}
_, err = this.Query(tx).
Pk(itemId).
Set("state", MetricItemStateDisabled).
Update()
@@ -58,7 +66,7 @@ func (this *MetricItemDAO) DisableMetricItem(tx *dbs.Tx, itemId int64) error {
}
// 通知更新
err = this.NotifyUpdate(tx, itemId)
err = this.NotifyUpdate(tx, itemId, isPublic == 1)
if err != nil {
return err
}
@@ -92,7 +100,7 @@ func (this *MetricItemDAO) FindMetricItemName(tx *dbs.Tx, id int64) (string, err
}
// CreateItem 创建指标
func (this *MetricItemDAO) CreateItem(tx *dbs.Tx, code string, category string, name string, keys []string, period int32, periodUnit string, value string) (int64, error) {
func (this *MetricItemDAO) CreateItem(tx *dbs.Tx, code string, category string, name string, keys []string, period int32, periodUnit string, value string, isPublic bool) (int64, error) {
sort.Strings(keys)
op := NewMetricItemOperator()
@@ -111,13 +119,26 @@ func (this *MetricItemDAO) CreateItem(tx *dbs.Tx, code string, category string,
op.Period = period
op.PeriodUnit = periodUnit
op.Value = value
op.IsPublic = isPublic
op.IsOn = true
op.State = MetricItemStateEnabled
return this.SaveInt64(tx, op)
itemId, err := this.SaveInt64(tx, op)
if err != nil {
return 0, err
}
if isPublic {
err = this.NotifyUpdate(tx, itemId, isPublic)
if err != nil {
return 0, err
}
}
return itemId, nil
}
// UpdateItem 修改\指标
func (this *MetricItemDAO) UpdateItem(tx *dbs.Tx, itemId int64, name string, keys []string, period int32, periodUnit string, value string, isOn bool) error {
func (this *MetricItemDAO) UpdateItem(tx *dbs.Tx, itemId int64, name string, keys []string, period int32, periodUnit string, value string, isOn bool, isPublic bool) error {
if itemId <= 0 {
return errors.New("invalid itemId")
}
@@ -132,6 +153,7 @@ func (this *MetricItemDAO) UpdateItem(tx *dbs.Tx, itemId int64, name string, key
if oldItem == nil {
return nil
}
oldIsPublic := oldItem.IsPublic == 1
var versionChanged = false
if strings.Join(oldItem.DecodeKeys(), "&") != strings.Join(keys, "&") || types.Int32(oldItem.Period) != period || oldItem.PeriodUnit != periodUnit || oldItem.Value != value {
versionChanged = true
@@ -157,14 +179,15 @@ func (this *MetricItemDAO) UpdateItem(tx *dbs.Tx, itemId int64, name string, key
if versionChanged {
op.Version = dbs.SQL("version+1")
}
op.IsPublic = isPublic
err = this.Save(tx, op)
if err != nil {
return err
}
// 通知更新
if versionChanged || (oldItem.IsOn == 0 && isOn) || (oldItem.IsOn == 1 && !isOn) {
err := this.NotifyUpdate(tx, itemId)
if versionChanged || (oldItem.IsOn == 0 && isOn) || (oldItem.IsOn == 1 && !isOn) || oldIsPublic != isPublic {
err := this.NotifyUpdate(tx, itemId, isPublic || oldIsPublic)
if err != nil {
return err
}
@@ -204,6 +227,18 @@ func (this *MetricItemDAO) ListEnabledItems(tx *dbs.Tx, category serverconfigs.M
return
}
// FindAllPublicItems 取得公用的指标
func (this *MetricItemDAO) FindAllPublicItems(tx *dbs.Tx) (result []*MetricItem, err error) {
_, err = this.Query(tx).
State(MetricItemStateEnabled).
Attr("userId", 0).
Attr("isPublic", true).
DescPk().
Slice(&result).
FindAll()
return
}
// ComposeItemConfig 组合指标配置
func (this *MetricItemDAO) ComposeItemConfig(tx *dbs.Tx, itemId int64) (*serverconfigs.MetricItemConfig, error) {
if itemId <= 0 {
@@ -234,6 +269,25 @@ func (this *MetricItemDAO) ComposeItemConfig(tx *dbs.Tx, itemId int64) (*serverc
return config, nil
}
// ComposeItemConfigWithItem 根据Item信息组合指标
func (this *MetricItemDAO) ComposeItemConfigWithItem(item *MetricItem) *serverconfigs.MetricItemConfig {
if item == nil {
return nil
}
var config = &serverconfigs.MetricItemConfig{
Id: int64(item.Id),
IsOn: item.IsOn == 1,
Period: types.Int(item.Period),
PeriodUnit: item.PeriodUnit,
Category: item.Category,
Value: item.Value,
Keys: item.DecodeKeys(),
Version: types.Int32(item.Version),
}
return config
}
// FindItemVersion 获取指标的版本号
func (this *MetricItemDAO) FindItemVersion(tx *dbs.Tx, itemId int64) (int32, error) {
version, err := this.Query(tx).
@@ -247,7 +301,20 @@ func (this *MetricItemDAO) FindItemVersion(tx *dbs.Tx, itemId int64) (int32, err
}
// NotifyUpdate 通知更新
func (this *MetricItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64) error {
func (this *MetricItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64, isPublic bool) error {
if isPublic {
clusterIds, err := SharedNodeClusterDAO.FindAllEnableClusterIds(tx)
if err != nil {
return err
}
for _, clusterId := range clusterIds {
err = SharedNodeTaskDAO.CreateClusterTask(tx, clusterId, NodeTaskTypeConfigChanged)
if err != nil {
return err
}
}
return nil
}
clusterIds, err := SharedNodeClusterMetricItemDAO.FindAllClusterIdsWithItemId(tx, itemId)
if err != nil {
return err