From c068badd3630ddabddf30ee8f479110e9f837253 Mon Sep 17 00:00:00 2001 From: zhaoxiangchun <1422928955@qq.com> Date: Tue, 2 Mar 2021 11:52:44 +0800 Subject: [PATCH] fix(monitor): fix monitor export err MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.修复监控导出报错 2.db_dispatcher.ListItems 方法中对exportKeys的处理新增了一个方法 --- pkg/cloudcommon/db/db_dispatcher.go | 18 +++++++++++++++--- pkg/monitor/models/commonalert.go | 21 ++++++++++++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/pkg/cloudcommon/db/db_dispatcher.go b/pkg/cloudcommon/db/db_dispatcher.go index 52b37fe778..2da3323881 100644 --- a/pkg/cloudcommon/db/db_dispatcher.go +++ b/pkg/cloudcommon/db/db_dispatcher.go @@ -425,9 +425,6 @@ func Query2List(manager IModelManager, ctx context.Context, userCred mcclient.To extraResults[i].Update(jsonDict) jsonDict = extraResults[i] } - if len(exportKeys) > 0 { - jsonDict = jsonDict.CopyIncludes(exportKeys...) - } if len(fieldFilter) > 0 { jsonDict = jsonDict.CopyIncludes(fieldFilter...) } @@ -791,6 +788,9 @@ func ListItems(manager IModelManager, ctx context.Context, userCred mcclient.Tok if len(retList) != retCount { totalCnt = len(retList) } + if query.Contains("export_keys") { + retList = getExportCols(query, retList) + } paginate := false if !customizeFilters.IsEmpty() { // query not use Limit and Offset, do manual pagination @@ -822,6 +822,18 @@ func calculateListResult(data []jsonutils.JSONObject, total, limit, offset int64 return &retResult } +func getExportCols(query jsonutils.JSONObject, retList []jsonutils.JSONObject) []jsonutils.JSONObject { + var exportKeys stringutils2.SSortedStrings + exportKeyStr, _ := query.GetString("export_keys") + exportKeys = stringutils2.NewSortedStrings(strings.Split(exportKeyStr, ",")) + for i := range retList { + if len(exportKeys) > 0 { + retList[i] = retList[i].(*jsonutils.JSONDict).CopyIncludes(exportKeys...) + } + } + return retList +} + func (dispatcher *DBModelDispatcher) List(ctx context.Context, query jsonutils.JSONObject, ctxIds []dispatcher.SResourceContext) (*modulebase.ListResult, error) { userCred := fetchUserCredential(ctx) diff --git a/pkg/monitor/models/commonalert.go b/pkg/monitor/models/commonalert.go index 7e357750f5..72480afd19 100644 --- a/pkg/monitor/models/commonalert.go +++ b/pkg/monitor/models/commonalert.go @@ -355,6 +355,25 @@ func (man *SCommonAlertManager) ListItemFilter( return q, nil } +func (manager *SCommonAlertManager) GetExportExtraKeys(ctx context.Context, keys stringutils2.SSortedStrings, rowMap map[string]string) *jsonutils.JSONDict { + res := manager.SResourceBaseManager.GetExportExtraKeys(ctx, keys, rowMap) + if keys.Contains("tenant") { + if projectId, ok := rowMap["tenant_id"]; ok && projectId != "" { + tenant, err := db.TenantCacheManager.FetchTenantById(ctx, projectId) + if err == nil { + res.Set("tenant", jsonutils.NewString(fmt.Sprintf("%s/%s", tenant.GetName(), + tenant.GetProjectDomain()))) + } + } else { + tenant, err := db.TenantCacheManager.FetchTenantById(ctx, rowMap["domain_id"]) + if err == nil { + res.Set("tenant", jsonutils.NewString(tenant.GetProjectDomain())) + } + } + } + return res +} + func (man *SCommonAlertManager) CustomizeFilterList( ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, query jsonutils.JSONObject) ( @@ -371,7 +390,7 @@ func (man *SCommonAlertManager) CustomizeFilterList( return func(data jsonutils.JSONObject) (bool, error) { id, err := data.GetString("id") if err != nil { - return false, err + return true, nil } obj, err := man.GetAlert(id) if err != nil {