mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-19 10:46:58 +08:00
fix(monitor): fetch total alert resources from monitor_resource_alert (#23276)
This commit is contained in:
@@ -443,30 +443,23 @@ func (manager *SAlertRecordManager) GetPropertyTotalAlert(ctx context.Context, u
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Unmarshal AlertRecordListInput error")
|
||||
}
|
||||
alertRecords, err := manager.getNowAlertingRecord(ctx, userCred, *input)
|
||||
alertRess, err := MonitorResourceAlertManager.GetNowAlertingAlerts(ctx, userCred, input)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "getNowAlertingRecord error")
|
||||
}
|
||||
if input.Details != nil && *input.Details {
|
||||
|
||||
}
|
||||
alertCountMap := jsonutils.NewDict()
|
||||
for _, record := range alertRecords {
|
||||
evalMatches, err := record.GetEvalData()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "get record:%s evalData error", record.GetId())
|
||||
}
|
||||
count := int64(len(evalMatches))
|
||||
if alertCountMap.Contains(record.ResType) {
|
||||
resTypeCount, _ := alertCountMap.Int(record.ResType)
|
||||
for _, res := range alertRess {
|
||||
var count int64 = 1
|
||||
if alertCountMap.Contains(res.ResType) {
|
||||
resTypeCount, _ := alertCountMap.Int(res.ResType)
|
||||
count = count + resTypeCount
|
||||
}
|
||||
alertCountMap.Set(record.ResType, jsonutils.NewInt(count))
|
||||
alertCountMap.Set(res.ResType, jsonutils.NewInt(count))
|
||||
}
|
||||
return alertCountMap, nil
|
||||
}
|
||||
|
||||
func (manager *SAlertRecordManager) getNowAlertingRecord(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
/*func (manager *SAlertRecordManager) getNowAlertingRecord(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
input monitor.AlertRecordListInput) ([]SAlertRecord, error) {
|
||||
//now := time.Now()
|
||||
//startTime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 1, now.Location())
|
||||
@@ -500,11 +493,13 @@ func (manager *SAlertRecordManager) getNowAlertingRecord(ctx context.Context, us
|
||||
tmp := *query
|
||||
recordModel, err := db.NewModelObject(manager)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.Wrapf(err, "NewModelObject %s", manager.Keyword())
|
||||
}
|
||||
if err := (&tmp).Equals("alert_id", alert.GetId()).First(recordModel); err == nil {
|
||||
records = append(records, *(recordModel.(*SAlertRecord)))
|
||||
} else {
|
||||
log.Warningf("get records of alert_id %s error: %v", alert.GetId(), err)
|
||||
}
|
||||
}
|
||||
return records, nil
|
||||
}
|
||||
}*/
|
||||
|
||||
@@ -176,6 +176,29 @@ func (obj *SMonitorResourceAlert) GetData() (*monitor.EvalMatch, error) {
|
||||
return match, nil
|
||||
}
|
||||
|
||||
func (m *SMonitorResourceAlertManager) GetNowAlertingAlerts(ctx context.Context, userCred mcclient.TokenCredential, input *monitor.AlertRecordListInput) ([]SMonitorResourceAlert, error) {
|
||||
ownerId, err := m.FetchOwnerId(ctx, jsonutils.Marshal(&input))
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "FetchOwnerId by input: %s", jsonutils.Marshal(input))
|
||||
}
|
||||
if ownerId == nil {
|
||||
ownerId = userCred
|
||||
}
|
||||
|
||||
alertsQuery := CommonAlertManager.Query("id").Equals("state", monitor.AlertStateAlerting).IsTrue("enabled").
|
||||
IsNull("used_by")
|
||||
alertsQuery = CommonAlertManager.FilterByOwner(ctx, alertsQuery, CommonAlertManager, userCred, ownerId, rbacscope.String2Scope(input.Scope))
|
||||
|
||||
alertRess := make([]SMonitorResourceAlert, 0)
|
||||
q := m.Query()
|
||||
q = q.Equals("alert_state", monitor.AlertStateAlerting)
|
||||
q = q.In("alert_id", alertsQuery)
|
||||
if err := db.FetchModelObjects(m, q, &alertRess); err != nil {
|
||||
return nil, errors.Wrapf(err, "FetchModelObjects by GetNowAlertingAlerts err")
|
||||
}
|
||||
return alertRess, nil
|
||||
}
|
||||
|
||||
func (m *SMonitorResourceAlertManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, input *monitor.MonitorResourceJointListInput) (*sqlchemy.SQuery, error) {
|
||||
var err error
|
||||
q, err = m.SJointResourceBaseManager.ListItemFilter(ctx, q, userCred, input.JointResourceBaseListInput)
|
||||
|
||||
@@ -41,7 +41,6 @@ func (m *SMonitorScopedResourceManager) FilterByOwner(ctx context.Context, q *sq
|
||||
case rbacscope.ScopeDomain:
|
||||
q = q.Equals("domain_id", ownerId.GetProjectDomainId())
|
||||
case rbacscope.ScopeProject:
|
||||
|
||||
q = q.Equals("tenant_id", ownerId.GetProjectId())
|
||||
}
|
||||
return q
|
||||
|
||||
Reference in New Issue
Block a user