diff --git a/pkg/apis/monitor/alertrecord.go b/pkg/apis/monitor/alertrecord.go index 353a482886..9daaab15db 100644 --- a/pkg/apis/monitor/alertrecord.go +++ b/pkg/apis/monitor/alertrecord.go @@ -33,13 +33,14 @@ type AlertRecordListInput struct { apis.EnabledResourceBaseListInput apis.StatusStandaloneResourceListInput - AlertId string `json:"alert_id"` - Level string `json:"level"` - State string `json:"state"` - ResType string `json:"res_type"` - Alerting bool `json:"alerting"` - ResName string `json:"res_name"` - ResId string `json:"res_id"` + AlertId string `json:"alert_id"` + AlertName string `json:"alert_name"` + Level string `json:"level"` + State string `json:"state"` + 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 b3e90dfd48..8f3e645607 100644 --- a/pkg/mcclient/options/monitor/alertrecord.go +++ b/pkg/mcclient/options/monitor/alertrecord.go @@ -27,13 +27,14 @@ import ( type AlertRecordListOptions struct { options.BaseListOptions - AlertId string `help:"id of alert"` - 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"` + AlertId string `help:"id of alert"` + 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"` + AlertName string `json:"alert_name"` } func (o *AlertRecordListOptions) Params() (jsonutils.JSONObject, error) { diff --git a/pkg/monitor/models/alertrecord.go b/pkg/monitor/models/alertrecord.go index a66337ca97..3eb322fd02 100644 --- a/pkg/monitor/models/alertrecord.go +++ b/pkg/monitor/models/alertrecord.go @@ -135,6 +135,10 @@ func (manager *SAlertRecordManager) ListItemFilter( } else { q = q.IsNotEmpty("res_type").IsNotNull("res_type") } + if len(query.AlertName) != 0 { + alertQ := GetCommonAlertManager().Query("id").Contains("name", query.AlertName).SubQuery() + q = q.In("alert_id", alertQ) + } if len(query.ResType) != 0 { q.Filter(sqlchemy.Equals(q.Field("res_type"), query.ResType)) } @@ -170,14 +174,14 @@ 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) { +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 != "" { + if input.ResName != "" { filters.Append(func(item jsonutils.JSONObject) (bool, error) { evalMatchs := make([]monitor.EvalMatch, 0) if item.Contains("eval_data") { @@ -187,7 +191,7 @@ func (man *SAlertRecordManager) getAlertingRecordQuery() *sqlchemy.SQuery { } for _, match := range evalMatchs { for k, v := range match.Tags { - if strings.HasSuffix(k, "_id") && v == input.ResId { + if strings.Contains(k, "name") && strings.Contains(v, input.ResName) { return true, nil } } @@ -197,7 +201,7 @@ func (man *SAlertRecordManager) getAlertingRecordQuery() *sqlchemy.SQuery { }) } return filters, nil -}*/ +} func (man *SAlertRecordManager) GetAlertRecord(id string) (*SAlertRecord, error) { obj, err := man.FetchById(id)