diff --git a/cmd/climc/shell/monitor/alertrecord.go b/cmd/climc/shell/monitor/alertrecord.go index 1e20c94454..4446621164 100644 --- a/cmd/climc/shell/monitor/alertrecord.go +++ b/cmd/climc/shell/monitor/alertrecord.go @@ -10,4 +10,5 @@ func init() { cmd := shell.NewResourceCmd(modules.AlertRecordManager) cmd.List(new(options.AlertRecordListOptions)) cmd.Show(new(options.AlertRecordShowOptions)) + cmd.Get("", new(options.AlertRecordTotalOptions)) } diff --git a/pkg/mcclient/options/monitor/alertrecord.go b/pkg/mcclient/options/monitor/alertrecord.go index ad61dad073..e95305bd25 100644 --- a/pkg/mcclient/options/monitor/alertrecord.go +++ b/pkg/mcclient/options/monitor/alertrecord.go @@ -29,3 +29,16 @@ func (o *AlertRecordShowOptions) Params() (jsonutils.JSONObject, error) { func (o *AlertRecordShowOptions) GetId() string { return o.ID } + +type AlertRecordTotalOptions struct { + ID string `help:"total-alert" json:"-"` + options.BaseListOptions +} + +func (o *AlertRecordTotalOptions) Params() (jsonutils.JSONObject, error) { + return options.ListStructToParams(o) +} + +func (o *AlertRecordTotalOptions) GetId() string { + return o.ID +} diff --git a/pkg/monitor/models/alertrecord.go b/pkg/monitor/models/alertrecord.go index bc30c5a985..d82373103f 100644 --- a/pkg/monitor/models/alertrecord.go +++ b/pkg/monitor/models/alertrecord.go @@ -297,5 +297,50 @@ func (manager *SAlertRecordManager) DeleteRecordsOfThirtyDaysAgo(ctx context.Con log.Errorf("delete expire record:%s err:%v", records[i].GetId(), err) } } +} +func (manager *SAlertRecordManager) AllowGetPropertyTotalAlert(ctx context.Context, userCred mcclient.TokenCredential, + query jsonutils.JSONObject) bool { + return true +} + +func (manager *SAlertRecordManager) GetPropertyTotalAlert(ctx context.Context, userCred mcclient.TokenCredential, + query jsonutils.JSONObject) (jsonutils.JSONObject, error) { + + alertRecords, err := manager.getNowAlertingRecord(ctx, userCred, query) + if err != nil { + return nil, errors.Wrap(err, "getNowAlertingRecord error") + } + 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) + count = count + resTypeCount + } + alertCountMap.Set(record.ResType, jsonutils.NewInt(count)) + } + return alertCountMap, nil +} + +func (manager *SAlertRecordManager) getNowAlertingRecord(ctx context.Context, userCred mcclient.TokenCredential, + param jsonutils.JSONObject) ([]SAlertRecord, error) { + now := time.Now() + startTime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 1, now.Location()) + query := manager.Query() + scope, _ := param.GetString("scope") + query = manager.FilterByOwner(query, userCred, rbacutils.String2Scope(scope)) + query = query.GE("created_at", startTime.UTC().Format(timeutils.MysqlTimeFormat)) + sQuery := CommonAlertManager.Query("id").Equals("state", monitor.AlertStateAlerting).IsNull("used_by").SubQuery() + query = query.In("alert_id", sQuery).IsNotNull("res_type").IsNotEmpty("res_type").GroupBy("alert_id") + records := make([]SAlertRecord, 0) + err := db.FetchModelObjects(manager, query, &records) + if err != nil { + return nil, err + } + return records, nil }