feat(monitor): get records of monitor_resource_alert (#23825)

This commit is contained in:
Zexi Li
2025-11-24 21:34:26 +08:00
committed by GitHub
parent 2188834cd6
commit 067f2773e6
2 changed files with 28 additions and 2 deletions
+12 -2
View File
@@ -131,6 +131,17 @@ func (manager *SMonitorResourceManager) GetMonitorResources(input monitor.Monito
return monitorResources, nil
}
func (man *SMonitorResourceManager) GetMonitorResourceByResId(id string) (*SMonitorResource, error) {
resources, err := MonitorResourceManager.GetMonitorResources(monitor.MonitorResourceListInput{ResId: []string{id}})
if err != nil {
return nil, errors.Wrapf(err, "SMonitorResourceManager GetMonitorResources by resId: %s", id)
}
if len(resources) == 0 {
return nil, errors.Errorf("SMonitorResourceManager GetMonitorResources by resId: %s not found", id)
}
return &resources[0], nil
}
type SdeleteRes struct {
resType string
notIn []string
@@ -326,8 +337,7 @@ func (self *SMonitorResource) DetachJoint(ctx context.Context, userCred mcclient
}
func (self *SMonitorResource) getMoreDetails(out monitor.MonitorResourceDetails) monitor.MonitorResourceDetails {
joints, err := MonitorResourceAlertManager.GetJoinsByListInput(monitor.
MonitorResourceJointListInput{MonitorResourceId: self.ResId})
joints, err := MonitorResourceAlertManager.GetJoinsByListInput(monitor.MonitorResourceJointListInput{MonitorResourceId: self.ResId})
if err != nil {
log.Errorf("getMoreDetails err:%v", err)
}
@@ -374,3 +374,19 @@ func (m *SMonitorResourceAlertManager) GetResourceAlert(alertId string, resource
}
return obj, nil
}
// GetAlertRecords 通过 MonitorResourceAlert 查询历史的 AlertRecord
// 查询条件:相同的 AlertId 且 ResIds 包含该 MonitorResourceId 的所有记录
func (obj *SMonitorResourceAlert) GetAlertRecords() ([]SAlertRecord, error) {
records := make([]SAlertRecord, 0)
q := AlertRecordManager.Query()
q = q.Equals("alert_id", obj.AlertId)
// ResIds 字段存储的是逗号分隔的资源ID列表,使用 ContainsAny 查询
q = q.Filter(sqlchemy.ContainsAny(q.Field("res_ids"), []string{obj.MonitorResourceId}))
q = q.Desc("created_at") // 按创建时间倒序,最新的在前
err := db.FetchModelObjects(AlertRecordManager, q, &records)
if err != nil {
return nil, errors.Wrapf(err, "get historical alert records by alert_id: %q, monitor_resource_id: %q", obj.AlertId, obj.MonitorResourceId)
}
return records, nil
}