fix(monitor): add order index for monitor panel (#21528)

This commit is contained in:
屈轩
2024-11-05 17:31:45 +08:00
committed by GitHub
parent 9402c6c605
commit 8895e6c87b
7 changed files with 72 additions and 35 deletions
+16
View File
@@ -15,10 +15,12 @@
package monitor
import (
"reflect"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/gotypes"
"yunion.io/x/onecloud/pkg/apis"
)
@@ -84,6 +86,14 @@ type AlertSetting struct {
Conditions []AlertCondition `json:"conditions"`
}
func (s AlertSetting) String() string {
return jsonutils.Marshal(s).String()
}
func (s AlertSetting) IsZero() bool {
return len(s.Conditions) == 0
}
type AlertCondition struct {
Type string `json:"type"`
Query AlertQuery `json:"query"`
@@ -212,3 +222,9 @@ type AlertPauseInput struct {
Paused bool `json:"paused"`
}
func init() {
gotypes.RegisterSerializable(reflect.TypeOf(&AlertSetting{}), func() gotypes.ISerializable {
return &AlertSetting{}
})
}
+8 -3
View File
@@ -15,8 +15,6 @@
package monitor
import (
"yunion.io/x/jsonutils"
"yunion.io/x/onecloud/pkg/apis"
)
@@ -41,7 +39,7 @@ type AlertPanelDetail struct {
PanelName string `json:"panel_name"`
PanelId string `json:"panel_id"`
Refresh string `json:"refresh"`
Setting jsonutils.JSONObject
Setting *AlertSetting
PanelDetails
}
@@ -53,3 +51,10 @@ type AlertClonePanelInput struct {
type AlertCloneDashboardInput struct {
CloneName string `json:"clone_name"`
}
type AlertPanelSetOrderInput struct {
Order []struct {
PanelId string `json:"panel_id"`
Index int `json:"index"`
} `json:"order"`
}
+1 -1
View File
@@ -30,7 +30,7 @@ type SAlert struct {
SMonitorScopedResource
// Frequency is evaluate period
Frequency int64 `json:"frequency"`
Settings jsonutils.JSONObject `json:"settings"`
Settings *AlertSetting `json:"settings"`
Level string `json:"level"`
Message string `json:"message"`
UsedBy string `json:"used_by"`
+34 -6
View File
@@ -215,12 +215,10 @@ func (dash *SAlertDashBoard) getJointPanels() ([]SAlertDashboardPanel, error) {
func (dash *SAlertDashBoard) getAttachPanels() ([]SAlertPanel, error) {
panels := make([]SAlertPanel, 0)
panelQuery := AlertPanelManager.Query()
sq := AlertDashBoardPanelManager.Query(AlertDashBoardPanelManager.GetSlaveFieldName()).Equals(
AlertDashBoardPanelManager.GetMasterFieldName(), dash.Id).SubQuery()
panelQuery = panelQuery.In("id", sq)
panelQuery = panelQuery.Desc("created_at")
err := db.FetchModelObjects(AlertPanelManager, panelQuery, &panels)
q := AlertPanelManager.Query()
sq := AlertDashBoardPanelManager.Query().Equals("dashboard_id", dash.Id).SubQuery()
q = q.Join(sq, sqlchemy.Equals(q.Field("id"), sq.Field("panel_id"))).Asc(sq.Field("index")).Desc("created_at")
err := db.FetchModelObjects(AlertPanelManager, q, &panels)
if err != nil {
return panels, errors.Wrapf(err, "dashboard:%s get attach panels error", dash.Name)
}
@@ -290,6 +288,36 @@ func (dash *SAlertDashBoard) PerformClonePanel(ctx context.Context, userCred mcc
return output, nil
}
func (dash *SAlertDashBoard) PerformSetPanelOrder(
ctx context.Context,
userCred mcclient.TokenCredential,
query jsonutils.JSONObject,
input monitor.AlertPanelSetOrderInput,
) (jsonutils.JSONObject, error) {
panels, err := dash.getJointPanels()
if err != nil {
return nil, errors.Wrapf(err, "getJointPanels")
}
order := map[string]int{}
for _, v := range input.Order {
order[v.PanelId] = v.Index
}
for i := range panels {
panel := panels[i]
index, ok := order[panel.PanelId]
if ok {
_, err := db.Update(&panel, func() error {
panel.Index = index
return nil
})
if err != nil {
return nil, errors.Wrapf(err, "update index")
}
}
}
return nil, nil
}
func (dash *SAlertDashBoard) PerformCloneDashboard(ctx context.Context, userCred mcclient.TokenCredential,
query jsonutils.JSONObject, input monitor.AlertCloneDashboardInput) (jsonutils.JSONObject, error) {
iModel, err := db.NewModelObject(AlertDashBoardManager)
+2 -1
View File
@@ -51,10 +51,11 @@ type SAlertDashboardPanel struct {
DashboardId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required" index:"true"`
PanelId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required" index:"true"`
Index int `nullable:"true" default:"0" list:"user" create:"optional"`
}
func (man *SAlertDashboardPanelManager) GetMasterFieldName() string {
return "dashboard_Id"
return "dashboard_id"
}
func (man *SAlertDashboardPanelManager) GetSlaveFieldName() string {
+7 -22
View File
@@ -58,8 +58,8 @@ type SAlertPanel struct {
db.SStatusStandaloneResourceBase
db.SScopedResourceBase
Settings jsonutils.JSONObject `nullable:"false" list:"user" create:"required" update:"user"`
Message string `charset:"utf8" list:"user" create:"optional" update:"user"`
Settings *monitor.AlertSetting `nullable:"false" list:"user" create:"required" update:"user"`
Message string `charset:"utf8" list:"user" create:"optional" update:"user"`
}
func (manager *SAlertPanelManager) NamespaceScope() rbacscope.TRbacScope {
@@ -231,21 +231,17 @@ func (man *SAlertPanelManager) FetchCustomizeColumns(
}
func (panel *SAlertPanel) GetMoreDetails(out monitor.PanelDetails) (monitor.PanelDetails, error) {
setting, err := panel.GetSettings()
if err != nil {
return out, err
}
if len(setting.Conditions) == 0 {
if panel.Settings == nil || len(panel.Settings.Conditions) == 0 {
return out, nil
}
out.CommonAlertMetricDetails = make([]*monitor.CommonAlertMetricDetails, len(setting.Conditions))
for i, cond := range setting.Conditions {
out.CommonAlertMetricDetails = make([]*monitor.CommonAlertMetricDetails, len(panel.Settings.Conditions))
for i, cond := range panel.Settings.Conditions {
metricDetails := panel.GetCommonAlertMetricDetailsFromAlertCondition(i, &cond)
out.CommonAlertMetricDetails[i] = metricDetails
setting.Conditions[i] = cond
panel.Settings.Conditions[i] = cond
}
panel.Settings = jsonutils.Marshal(setting)
out.Settings = panel.Settings
return out, nil
}
@@ -257,17 +253,6 @@ func (dash *SAlertPanel) GetCommonAlertMetricDetailsFromAlertCondition(index int
return metricDetails
}
func (dash *SAlertPanel) GetSettings() (*monitor.AlertSetting, error) {
setting := new(monitor.AlertSetting)
if dash.Settings == nil {
return setting, nil
}
if err := dash.Settings.Unmarshal(setting); err != nil {
return nil, errors.Wrapf(err, "dashboard %s unmarshal", dash.GetId())
}
return setting, nil
}
func (dash *SAlertPanel) ValidateUpdateData(
ctx context.Context,
userCred mcclient.TokenCredential,
+4 -2
View File
@@ -792,8 +792,10 @@ func (alert *SCommonAlert) GetCommonAlertMetricDetailsFromAlertCondition(index i
return metricDetails
}
func getCommonAlertMetricDetailsFromCondition(cond *monitor.AlertCondition,
metricDetails *monitor.CommonAlertMetricDetails) {
func getCommonAlertMetricDetailsFromCondition(
cond *monitor.AlertCondition,
metricDetails *monitor.CommonAlertMetricDetails,
) {
cmp := ""
switch cond.Evaluator.Type {
case "gt":