From da7e2a010a8e6e8533c0beb6e8db0601f7242564 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Fri, 22 Nov 2024 16:54:37 +0800 Subject: [PATCH] feat(monitor): list alertrecords by res_id (#21667) --- pkg/apis/monitor/alertrecord.go | 1 + pkg/mcclient/options/monitor/alertrecord.go | 1 + pkg/monitor/models/alertrecord.go | 29 +++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/pkg/apis/monitor/alertrecord.go b/pkg/apis/monitor/alertrecord.go index 1dc9e95032..3dbaaacb2e 100644 --- a/pkg/apis/monitor/alertrecord.go +++ b/pkg/apis/monitor/alertrecord.go @@ -39,6 +39,7 @@ type AlertRecordListInput struct { ResType string `json:"res_type"` Alerting bool `json:"alerting"` ResName string `json:"res_name"` + ResId string `json:"res_id"` } type AlertRecordDetails struct { diff --git a/pkg/mcclient/options/monitor/alertrecord.go b/pkg/mcclient/options/monitor/alertrecord.go index bb2019e602..1bc7567352 100644 --- a/pkg/mcclient/options/monitor/alertrecord.go +++ b/pkg/mcclient/options/monitor/alertrecord.go @@ -31,6 +31,7 @@ type AlertRecordListOptions struct { Level string `help:"alert level"` State string `help:"alert state"` ResTypes []string `json:"res_types"` + ResId string `json:"res_id"` ResName string `json:"res_name"` Alerting bool `json:"alerting"` } diff --git a/pkg/monitor/models/alertrecord.go b/pkg/monitor/models/alertrecord.go index e4966f0b9a..756a9a4ff2 100644 --- a/pkg/monitor/models/alertrecord.go +++ b/pkg/monitor/models/alertrecord.go @@ -159,6 +159,35 @@ func (man *SAlertRecordManager) getAlertingRecordQuery() *sqlchemy.SQuery { } +func (man *SAlertRecordManager) CustomizeFilterList(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (*db.CustomizeListFilters, error) { + filters := db.NewCustomizeListFilters() + + input := new(monitor.AlertRecordListInput) + if err := query.Unmarshal(input); err != nil { + return nil, errors.Wrap(err, "AlertRecordManager.CustomizeFilterList.Unmarshal") + } + if input.ResId != "" { + filters.Append(func(item jsonutils.JSONObject) (bool, error) { + evalMatchs := make([]monitor.EvalMatch, 0) + if item.Contains("eval_data") { + err := item.Unmarshal(&evalMatchs, "eval_data") + if err != nil { + return false, errors.Wrap(err, "record Unmarshal evalMatchs error") + } + for _, match := range evalMatchs { + for k, v := range match.Tags { + if strings.HasSuffix(k, "_id") && v == input.ResId { + return true, nil + } + } + } + } + return false, nil + }) + } + return filters, nil +} + func (man *SAlertRecordManager) GetAlertRecord(id string) (*SAlertRecord, error) { obj, err := man.FetchById(id) if err != nil {