From 8a0c2d357b9bdfea4877291a32b15286fd3cf61c Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Tue, 28 May 2024 09:59:49 +0800 Subject: [PATCH] fix(monitor): detach alert (#20373) --- pkg/monitor/models/monitor_resource.go | 15 ++++++++++++++- pkg/monitor/models/monitor_resource_alert.go | 11 +++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/pkg/monitor/models/monitor_resource.go b/pkg/monitor/models/monitor_resource.go index 1c1e9c43c1..2d1415aee9 100644 --- a/pkg/monitor/models/monitor_resource.go +++ b/pkg/monitor/models/monitor_resource.go @@ -416,7 +416,20 @@ func (manager *SMonitorResourceManager) UpdateMonitorResourceAttachJoint(ctx con } deleteJointIds := make([]int64, 0) for _, joint := range resourceAlerts { - if utils.IsInStringArray(joint.MonitorResourceId, matchResourceIds) { + evalData, err := joint.GetData() + if err != nil { + log.Warningf("get data of monitor_resource_alert %s: %s", jsonutils.Marshal(joint), err) + continue + } + metricName := evalData.Metric + isMetricFound := false + for _, match := range matches { + if match.Metric == metricName { + isMetricFound = true + break + } + } + if utils.IsInStringArray(joint.MonitorResourceId, matchResourceIds) && isMetricFound { continue } deleteJointIds = append(deleteJointIds, joint.RowId) diff --git a/pkg/monitor/models/monitor_resource_alert.go b/pkg/monitor/models/monitor_resource_alert.go index cd77c76f2a..e70e2fb627 100644 --- a/pkg/monitor/models/monitor_resource_alert.go +++ b/pkg/monitor/models/monitor_resource_alert.go @@ -152,6 +152,17 @@ func (obj *SMonitorResourceAlert) UpdateAlertRecordData(record *SAlertRecord, ma return nil } +func (obj *SMonitorResourceAlert) GetData() (*monitor.EvalMatch, error) { + if obj.Data == nil { + return nil, errors.Errorf("data is nil") + } + match := new(monitor.EvalMatch) + if err := obj.Data.Unmarshal(match); err != nil { + return nil, errors.Wrap(err, "unmarshal to monitor.EvalMatch") + } + return match, nil +} + func (m *SMonitorResourceAlertManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, input *monitor.MonitorResourceJointListInput) (*sqlchemy.SQuery, error) { var err error q, err = m.SJointResourceBaseManager.ListItemFilter(ctx, q, userCred, input.JointResourceBaseListInput)