[REV] optimize paged resource

This commit is contained in:
bay1ts 2017-12-06 23:51:34 +08:00
parent ed6e5b8ce5
commit d694c8db60
6 changed files with 46 additions and 12 deletions

View File

@ -185,10 +185,20 @@ func (t *TenantStruct) TenantsWithResource(w http.ResponseWriter, r *http.Reques
// description: 统一返回格式
pageLen:=25
curPage:=2
pageLenStr := strings.TrimSpace(chi.URLParam(r, "pageLen"))
curPageStr := strings.TrimSpace(chi.URLParam(r, "curPage"))
pageLen,err:=strconv.Atoi(pageLenStr)
if err != nil {
httputil.ReturnError(r, w, 400, fmt.Sprintf("bad request, %v", err))
return
}
curPage,err:=strconv.Atoi(curPageStr)
if err != nil {
httputil.ReturnError(r, w, 400, fmt.Sprintf("bad request, %v", err))
return
}
rep, err := handler.GetTenantManager().GetTenants()
if err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("get tenants error, %v", err))
@ -215,7 +225,12 @@ func (t *TenantStruct) TenantsWithResource(w http.ResponseWriter, r *http.Reques
}
pList := api_model.TenantResList(result)
sort.Sort(pList)
httputil.ReturnSuccess(r, w, pList)
resultList:=pList[(curPage-1)*pageLen:curPage*pageLen-1]
var ret api_model.PagedTenantResList
ret.List=resultList
ret.Length=len(rep)
httputil.ReturnSuccess(r, w, ret)
return
}

View File

@ -28,6 +28,7 @@ import (
//TenantHandler tenant handler
type TenantHandler interface {
GetTenants() ([]*dbmodel.Tenants, error)
//GetPagedTenants(page,pageSize int) (*api_model.StatsInfo, error)
GetTenantsName() ([]string, error)
StatsMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error)
TotalMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error)

View File

@ -83,7 +83,7 @@ func CreateTenManager(conf option.Config) (*TenantAction, error) {
}, nil
}
//GetTenants get tenants
//GetTenantsName get tenants name
func (t *TenantAction) GetTenantsName() ([]string, error) {
tenants, err := db.GetManager().TenantDao().GetALLTenants()
if err != nil {
@ -96,6 +96,19 @@ func (t *TenantAction) GetTenantsName() ([]string, error) {
return result, err
}
//GetPagedTenants get tenants paged
//func (t *TenantAction) GetPagedTenants(page,pageSize int) (*api_model.StatsInfo, error) {
// tenants, err := db.GetManager().TenantDao().GetTenantsOrderByUsedMemPaged(page,pageSize)
// if err != nil {
// return nil, err
// }
// var result []string
// for _,v:=range tenants{
// result=append(result,strings.ToLower(v.Name))
// }
// return result, err
//}
//GetTenants get tenants
func (t *TenantAction) GetTenants() ([]*dbmodel.Tenants, error) {
tenants, err := db.GetManager().TenantDao().GetALLTenants()

View File

@ -20,6 +20,11 @@ package model
type TenantResList []*TenantResource
type PagedTenantResList struct {
List []*TenantResource `json:"list"`
Length int `json:"length"`
}
//TenantResource path参数
//swagger:parameters getVolumes getDepVolumes
type TenantResource struct {

View File

@ -37,7 +37,7 @@ type TenantDao interface {
GetTenantByUUID(uuid string) (*model.Tenants, error)
GetTenantIDByName(tenantName string) (*model.Tenants, error)
GetALLTenants() ([]*model.Tenants, error)
GetTenantsOrderByUsedMemPaged(page,pagesize int) ([]*model.Tenants, error)
//GetTenantsOrderByUsedMemPaged(page,pagesize int) ([]*model.Tenants, error)
}
//LicenseDao LicenseDao

View File

@ -78,13 +78,13 @@ func (t *TenantDaoImpl) GetTenantIDByName(name string) (*model.Tenants, error) {
}
//GetTenantsOrderByUsedMemPaged 获取租户分页
func (t *TenantDaoImpl) GetTenantsOrderByUsedMemPaged(page,pagesize int) ([]*model.Tenants, error) {
var tenant []*model.Tenants
if err := t.DB.Select("sum(container_cpu), sum(container_memory)").Where("cur_status = ?", "running").Find(&tenant).Error; err != nil {
return nil, err
}
return &tenant, nil
}
//func (t *TenantDaoImpl) GetTenantsOrderByUsedMemPaged(page,pagesize int) ([]*model.Tenants, error) {
// var tenant []*model.Tenants
// if err := t.DB.Select("sum(container_cpu), sum(container_memory)").Where("cur_status = ?", "running").Group("tenant_id").Find(&tenant).Error; err != nil {
// return nil, err
// }
// return tenant, nil
//}
//GetALLTenants GetALLTenants
func (t *TenantDaoImpl) GetALLTenants() ([]*model.Tenants, error) {