fix(monitor): fix monitor export err

1.修复监控导出报错
2.db_dispatcher.ListItems 方法中对exportKeys的处理新增了一个方法
This commit is contained in:
zhaoxiangchun
2021-03-02 11:52:44 +08:00
parent 0e7a043502
commit 97c107f7e9
2 changed files with 35 additions and 4 deletions
+15 -3
View File
@@ -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)
+20 -1
View File
@@ -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 {