From d7a8c3c6463233e01bbbcf53e5eff4b09fc8f46d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Tue, 5 Nov 2024 17:31:37 +0800 Subject: [PATCH] fix(monitor): add order index for monitor panel (#21527) --- pkg/apis/monitor/alert.go | 16 +++++++++ pkg/apis/monitor/alertdashboard.go | 11 ++++-- pkg/apis/monitor/zz_generated.model.go | 2 +- pkg/monitor/models/alertdashboard.go | 40 ++++++++++++++++++---- pkg/monitor/models/alertdashboard_panel.go | 3 +- pkg/monitor/models/alertpannel.go | 29 ++++------------ pkg/monitor/models/commonalert.go | 6 ++-- 7 files changed, 72 insertions(+), 35 deletions(-) diff --git a/pkg/apis/monitor/alert.go b/pkg/apis/monitor/alert.go index 612762ce78..6ab9a04cb9 100644 --- a/pkg/apis/monitor/alert.go +++ b/pkg/apis/monitor/alert.go @@ -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{} + }) +} diff --git a/pkg/apis/monitor/alertdashboard.go b/pkg/apis/monitor/alertdashboard.go index 0c2622c3cc..4df79b052a 100644 --- a/pkg/apis/monitor/alertdashboard.go +++ b/pkg/apis/monitor/alertdashboard.go @@ -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"` +} diff --git a/pkg/apis/monitor/zz_generated.model.go b/pkg/apis/monitor/zz_generated.model.go index 55178c1598..be76e420d5 100644 --- a/pkg/apis/monitor/zz_generated.model.go +++ b/pkg/apis/monitor/zz_generated.model.go @@ -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"` diff --git a/pkg/monitor/models/alertdashboard.go b/pkg/monitor/models/alertdashboard.go index fb27df05f1..87c74bf2df 100644 --- a/pkg/monitor/models/alertdashboard.go +++ b/pkg/monitor/models/alertdashboard.go @@ -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) diff --git a/pkg/monitor/models/alertdashboard_panel.go b/pkg/monitor/models/alertdashboard_panel.go index 8dad930b6c..a3a13f6c51 100644 --- a/pkg/monitor/models/alertdashboard_panel.go +++ b/pkg/monitor/models/alertdashboard_panel.go @@ -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 { diff --git a/pkg/monitor/models/alertpannel.go b/pkg/monitor/models/alertpannel.go index 1d63554d5a..1e825a8340 100644 --- a/pkg/monitor/models/alertpannel.go +++ b/pkg/monitor/models/alertpannel.go @@ -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, diff --git a/pkg/monitor/models/commonalert.go b/pkg/monitor/models/commonalert.go index dbce756166..2db599b5c8 100644 --- a/pkg/monitor/models/commonalert.go +++ b/pkg/monitor/models/commonalert.go @@ -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":