feat(monitor): filter monitor_resource_alert by ip (#21221)

This commit is contained in:
Zexi Li
2024-09-12 11:01:21 +08:00
committed by GitHub
parent 0bd46675ef
commit 861717e8d6
3 changed files with 27 additions and 1 deletions
@@ -42,7 +42,7 @@ func init() {
func NewMonitorResourceManager() *SMonitorResourceManager {
man := modules.NewMonitorV2Manager("monitorresource", "monitorresources",
[]string{"id", "name", "res_type", "res_id", "alert_state", "status"},
[]string{"id", "name", "res_type", "res_id", "alert_state", "status", "data"},
[]string{})
return &SMonitorResourceManager{
ResourceManager: &man,
@@ -27,6 +27,7 @@ type MonitorResourceAlertListOptions struct {
Alerting bool `help:"search alerting resource" json:"alerting"`
SendState string `json:"send_state"`
AllState bool `help:"Show all state" json:"all_state"`
Ip string `help:"IP address" json:"ip"`
}
func (o *MonitorResourceAlertListOptions) GetMasterOpt() string {
@@ -16,6 +16,7 @@ package models
import (
"context"
"strings"
"time"
"yunion.io/x/jsonutils"
@@ -229,6 +230,30 @@ func (m *SMonitorResourceAlertManager) ListItemFilter(ctx context.Context, q *sq
return q, nil
}
func (m *SMonitorResourceAlertManager) CustomizeFilterList(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (*db.CustomizeListFilters, error) {
filters := db.NewCustomizeListFilters()
if query.Contains("ip") {
ip, _ := query.GetString("ip")
ipF := func(obj jsonutils.JSONObject) (bool, error) {
match := new(monitor.EvalMatch)
if err := obj.Unmarshal(match, "data"); err != nil {
log.Warningf("unmarshal data of object %s: %v", obj, err)
return false, nil
}
if tagIp, ok := match.Tags["ip"]; ok {
if strings.Contains(tagIp, ip) {
return true, nil
}
}
return false, nil
}
filters.Append(ipF)
}
return filters, nil
}
func (man *SMonitorResourceAlertManager) FetchCustomizeColumns(
ctx context.Context,
userCred mcclient.TokenCredential,