feat(monitor): list alertrecords by res_id (#21664)

This commit is contained in:
Zexi Li
2024-11-22 16:54:23 +08:00
committed by GitHub
parent 096a56ffc6
commit 905cf7aa4a
3 changed files with 31 additions and 0 deletions
+1
View File
@@ -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 {
@@ -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"`
}
+29
View File
@@ -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 {