Skip to content

Commit 6259647

Browse files
authored
feat(monitor): support listing monitor_resource by alert_state (#22289)
1 parent 58fcc3b commit 6259647

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

pkg/apis/monitor/monitor_resource.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ type MonitorResourceListInput struct {
3939
apis.EnabledResourceBaseListInput
4040
compute.ManagedResourceListInput
4141

42-
ResId []string `json:"res_id"`
43-
ResType string `json:"res_type"`
44-
OnlyResId bool `json:"only_res_id"`
42+
ResId []string `json:"res_id"`
43+
ResType string `json:"res_type"`
44+
OnlyResId bool `json:"only_res_id"`
45+
AlertStates []string `json:"alert_states"`
4546

4647
ResName string `json:"res_name"`
4748
}

pkg/mcclient/options/monitor/monitor_resource.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ func (o *MonitorResourceJointAlertOptions) Property() string {
3333

3434
type MonitorResourceListOptions struct {
3535
options.BaseListOptions
36-
ResType string `help:"filter by resource type" json:"res_type"`
37-
ResId []string `help:"filter by resource id" json:"res_id"`
38-
ResName string `help:"filter by resource name" json:"res_name"`
36+
ResType string `help:"filter by resource type" json:"res_type"`
37+
ResId []string `help:"filter by resource id" json:"res_id"`
38+
ResName string `help:"filter by resource name" json:"res_name"`
39+
AlertStates []string `help:"filter by alert state" json:"alert_states"`
3940
}
4041

4142
func (o *MonitorResourceListOptions) Params() (jsonutils.JSONObject, error) {

pkg/monitor/alerting/conditions/query.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,15 @@ func (c FormatCond) String() string {
110110
return "no_data"
111111
}
112112

113-
func (c *QueryCondition) filterTags(tags map[string]string, details monitor.CommonAlertMetricDetails) map[string]string {
113+
func (c *QueryCondition) filterTags(tags map[string]string, cloudTags map[string]string, details monitor.CommonAlertMetricDetails) map[string]string {
114114
ret := make(map[string]string)
115-
for key, val := range tags {
115+
allTags := make(map[string]string)
116+
for _, ts := range []map[string]string{tags, cloudTags} {
117+
for k, v := range ts {
118+
allTags[k] = v
119+
}
120+
}
121+
for key, val := range allTags {
116122
//if strings.HasSuffix(key, "_id") {
117123
// continue
118124
//}
@@ -303,7 +309,7 @@ func (c *QueryCondition) NewEvalMatch(
303309
queryKeyInfo = evalMatch.Metric
304310
}
305311
evalMatch.Unit = alertDetails.FieldDescription.Unit
306-
evalMatch.Tags = c.filterTags(series.Tags, *alertDetails)
312+
evalMatch.Tags = c.filterTags(series.Tags, series.CloudTags, *alertDetails)
307313
evalMatch.Value = value
308314
evalMatch.ValueStr = models.RationalizeValueFromUnit(*value, alertDetails.FieldDescription.Unit,
309315
alertDetails.FieldOpt)

pkg/monitor/models/monitor_resource.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ func (manager *SMonitorResourceManager) FieldListFilter(q *sqlchemy.SQuery, quer
203203
if len(query.ResName) != 0 {
204204
q.Contains("name", query.ResName)
205205
}
206+
if len(query.AlertStates) != 0 {
207+
q.In("alert_state", query.AlertStates)
208+
}
206209
return q
207210
}
208211

0 commit comments

Comments
 (0)