fix(monitor): filter alertrecords by alert_name and res_name (#23865)

This commit is contained in:
Zexi Li
2025-11-28 18:58:16 +08:00
committed by GitHub
parent 8778a2d83c
commit f95c341f44
3 changed files with 24 additions and 18 deletions
+8 -7
View File
@@ -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 {
+8 -7
View File
@@ -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) {
+8 -4
View File
@@ -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)