From 1adf085b4b26df32ea7e5256d3de13638f6faaf2 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Thu, 25 Apr 2024 17:07:12 +0800 Subject: [PATCH] fix(monitor): filter alerts by monitor_resource_id (#20137) --- pkg/apis/monitor/alert.go | 1 + pkg/mcclient/options/monitor/commonalert.go | 5 +++-- pkg/monitor/models/alert.go | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/apis/monitor/alert.go b/pkg/apis/monitor/alert.go index 7bc0e7664e..c6eacc1aa5 100644 --- a/pkg/apis/monitor/alert.go +++ b/pkg/apis/monitor/alert.go @@ -155,6 +155,7 @@ type AlertListInput struct { apis.StatusStandaloneResourceListInput // 以报警是否启用/禁用过滤列表 // Enabled *bool `json:"enabled"` + MonitorResourceId []string `json:"monitor_resource_id"` } type AlertDetails struct { diff --git a/pkg/mcclient/options/monitor/commonalert.go b/pkg/mcclient/options/monitor/commonalert.go index 01f15ed2e7..0eac9487af 100644 --- a/pkg/mcclient/options/monitor/commonalert.go +++ b/pkg/mcclient/options/monitor/commonalert.go @@ -27,8 +27,9 @@ import ( type CommonAlertListOptions struct { options.BaseListOptions // 报警类型 - AlertType string `help:"common alert type" choices:"normal|system"` - Level string `help:"common alert notify level" choices:"normal|important|fatal"` + AlertType string `help:"common alert type" choices:"normal|system"` + Level string `help:"common alert notify level" choices:"normal|important|fatal"` + MonitorResourceId []string `help:"monitor resource id"` } func (o *CommonAlertListOptions) Params() (jsonutils.JSONObject, error) { diff --git a/pkg/monitor/models/alert.go b/pkg/monitor/models/alert.go index a5084a15e1..c85ab0bcc3 100644 --- a/pkg/monitor/models/alert.go +++ b/pkg/monitor/models/alert.go @@ -292,6 +292,10 @@ func (man *SAlertManager) ListItemFilter( if err != nil { return nil, errors.Wrap(err, "SEnabledResourceBaseManager.ListItemFilter") } + if len(input.MonitorResourceId) != 0 { + sq := MonitorResourceAlertManager.Query("alert_id").In("monitor_resource_id", input.MonitorResourceId).SubQuery() + q = q.In("id", sq) + } return q, nil }