Skip to content

Commit

Permalink
fix: i18n; text error; accurary; nodes calculations (#2605)
Browse files Browse the repository at this point in the history
* fix: i18n; text error; accurary; nodes calculations

* fix: typo
  • Loading branch information
dspo authored Oct 27, 2021
1 parent 9194610 commit 6cf5f64
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 13 deletions.
1 change: 1 addition & 0 deletions apistructs/cmp_resource_overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type ResourceOverviewReportDataItem struct {
ProjectID int64 `json:"projectID"`
ProjectName string `json:"projectName"`
ProjectDisplayName string `json:"projectDisplayName"`
ProjectDesc string `json:"projectDesc"`
OwnerUserID int64 `json:"ownerUserID"`
OwnerUserName string `json:"ownerUserName"`
OwnerUserNickName string `json:"ownerUserNickname"`
Expand Down
1 change: 1 addition & 0 deletions apistructs/core_services_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ type ProjectNamespaces struct {
ProjectID uint `json:"projectID"`
ProjectName string `json:"projectName"`
ProjectDisplayName string `json:"projectDisplayName"`
ProjectDesc string `json:"projectDesc"`
OwnerUserID uint `json:"ownerUserID"`
OwnerUserName string `json:"ownerUserName"`
OwnerUserNickname string `json:"ownerUserNickname"`
Expand Down
2 changes: 2 additions & 0 deletions conf/cmp/i18n/component-protocol.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ zh:
quota in use: 配额已使用
cluster trend: 集群资源变化趋势图
project trend: 项目资源变化趋势图
SharedResources: 共享资源
resourceNodeCount:

en:
Expand Down Expand Up @@ -384,6 +385,7 @@ en:
cpu-size: CPU Cores
memory-size: Memory Size
undrained: There are still unevicted Pods in this node!
SharedResources: Shared Resources

# cmp-dashboard-resource
resourceNodeCount: nodes
3 changes: 3 additions & 0 deletions modules/cmp/endpoints/resource_overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/sirupsen/logrus"

"github.com/erda-project/erda-infra/providers/legacy/httpendpoints/i18n"
"github.com/erda-project/erda/pkg/http/httpserver"
"github.com/erda-project/erda/pkg/http/httpserver/errorresp"
"github.com/erda-project/erda/pkg/http/httputil"
Expand All @@ -29,6 +30,8 @@ import (
var resourceOverviewReportError = errorresp.New(errorresp.WithTemplateMessage("ErrResourceOverviewReport", "查询项目资源分配情况失败"))

func (e *Endpoints) ResourceOverviewReport(ctx context.Context, r *http.Request, vars map[string]string) (httpserver.Responser, error) {
langCodes := i18n.Language(r)
ctx = context.WithValue(ctx, "lang_codes", langCodes)
logrus.Debugln("ResourceOverviewReport")

orgIDStr := r.Header.Get(httputil.OrgHeader)
Expand Down
1 change: 1 addition & 0 deletions modules/cmp/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func (p *provider) do(ctx context.Context) (*httpserver.Server, error) {
resourceTable := resource.NewReportTable(
resource.ReportTableWithBundle(bdl),
resource.ReportTableWithCMP(p),
resource.ReportTableWithTrans(p.Tran),
)

ep, err := initEndpoints(ctx, db, js, cachedJs, bdl, o, p.Credential, resourceTable)
Expand Down
33 changes: 24 additions & 9 deletions modules/cmp/resource/report_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/rancher/apiserver/pkg/types"
"github.com/sirupsen/logrus"

"github.com/erda-project/erda-infra/providers/i18n"
"github.com/erda-project/erda-proto-go/cmp/dashboard/pb"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/bundle"
Expand All @@ -34,6 +35,7 @@ type ReportTable struct {
ListSteveResource(ctx context.Context, req *apistructs.SteveRequest) ([]types.APIObject, error)
GetNamespacesResources(ctx context.Context, nReq *pb.GetNamespacesResourcesRequest) (*pb.GetNamespacesResourcesResponse, error)
}
trans i18n.Translator
}

func NewReportTable(opts ...ReportTableOption) *ReportTable {
Expand All @@ -47,6 +49,9 @@ func NewReportTable(opts ...ReportTableOption) *ReportTable {
func (rt *ReportTable) GetResourceOverviewReport(ctx context.Context, orgID int64, clusterNames []string,
cpuPerNode, memPerNode uint64) (*apistructs.ResourceOverviewReportData, error) {
logrus.Debugln("GetResourceOverviewReport", "query all namespaces")

langCodes, _ := ctx.Value("lang_codes").(i18n.LanguageCodes)

// 1) 查找所有 namespaces
var namespacesM = make(map[string][]string)
orgIDStr := strconv.FormatInt(orgID, 10)
Expand Down Expand Up @@ -129,6 +134,7 @@ func (rt *ReportTable) GetResourceOverviewReport(ctx context.Context, orgID int6
ProjectID: int64(projectItem.ProjectID),
ProjectName: projectItem.ProjectName,
ProjectDisplayName: projectItem.ProjectDisplayName,
ProjectDesc: projectItem.ProjectDesc,
OwnerUserID: int64(projectItem.OwnerUserID),
OwnerUserName: projectItem.OwnerUserName,
OwnerUserNickName: projectItem.OwnerUserNickname,
Expand All @@ -139,32 +145,35 @@ func (rt *ReportTable) GetResourceOverviewReport(ctx context.Context, orgID int6
Nodes: 0,
}
if projectItem.CPUQuota != 0 {
item.CPUWaterLevel = float64(projectItem.GetCPUReqeust()) / float64(projectItem.CPUQuota)
item.CPUWaterLevel = float64(projectItem.GetCPUReqeust()) / float64(projectItem.CPUQuota) * 100
}
if projectItem.MemQuota != 0 {
item.MemWaterLevel = float64(projectItem.GetMemRequest()) / float64(projectItem.MemQuota)
item.MemWaterLevel = float64(projectItem.GetMemRequest()) / float64(projectItem.MemQuota) * 100
}
item.Nodes = item.CPUQuota / float64(cpuPerNode)
if nodes := item.MemQuota / float64(memPerNode); nodes > item.Nodes {
item.Nodes = nodes
}
item.Nodes = calcu.Accuracy(item.Nodes, 1)
data.List = append(data.List, &item)
}
sharedNodes := float64(sharedResource[0]) / float64(cpuPerNode)
if nodes := float64(sharedResource[1]) / float64(memPerNode); nodes > sharedNodes {
sharedNodes := float64(sharedResource[0]) / float64(cpuPerNode*1000)
if nodes := float64(sharedResource[1]) / float64(memPerNode*1024*1024*1024); nodes > sharedNodes {
sharedNodes = nodes
}
sharedNodes = calcu.Accuracy(sharedNodes, 1)
data.List = append(data.List, &apistructs.ResourceOverviewReportDataItem{
ProjectID: 0,
ProjectName: "",
ProjectDisplayName: "共享资源", // todo: i18n
ProjectName: "-",
ProjectDisplayName: "-",
ProjectDesc: rt.trans.Text(langCodes, "SharedResources"),
OwnerUserID: 0,
OwnerUserName: "",
OwnerUserNickName: "所有人", // todo: i18n
OwnerUserNickName: "-",
CPUQuota: calcu.MillcoreToCore(sharedResource[0], 3),
CPUWaterLevel: 1,
CPUWaterLevel: 100,
MemQuota: calcu.ByteToGibibyte(sharedResource[1], 3),
MemWaterLevel: 1,
MemWaterLevel: 100,
Nodes: sharedNodes,
})
data.Sum()
Expand All @@ -188,3 +197,9 @@ func ReportTableWithCMP(cmp interface {
t.cmp = cmp
}
}

func ReportTableWithTrans(trans i18n.Translator) ReportTableOption {
return func(t *ReportTable) {
t.trans = trans
}
}
1 change: 1 addition & 0 deletions modules/core-services/services/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,7 @@ func (p *Project) GetNamespacesBelongsTo(ctx context.Context, namespaces map[str
ProjectID: uint(project.ID),
ProjectName: project.Name,
ProjectDisplayName: project.DisplayName,
ProjectDesc: project.Desc,
OwnerUserID: uint(userID),
OwnerUserName: owner.Name,
OwnerUserNickname: owner.Nick,
Expand Down
11 changes: 7 additions & 4 deletions pkg/resourcecalculator/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,14 @@ func CoreToMillcore(v float64) uint64 {
}

func MillcoreToCore(v uint64, accuracy int32) float64 {
value, _ := decimal.NewFromFloat(float64(v) / 1000).Round(accuracy).Float64()
return value
return Accuracy(float64(v)/1000, accuracy)
}

func GibibyteToByte(v float64) uint64 {
return uint64(v * 1024 * 1024 * 1024)
}
func ByteToGibibyte(v uint64, accuracy int32) float64 {
value, _ := decimal.NewFromFloat(float64(v) / (1024 * 1024 * 1024)).Round(accuracy).Float64()
return value
return Accuracy(float64(v)/(1024*1024*1024), accuracy)
}

func priority(workspace Workspace) []string {
Expand Down Expand Up @@ -294,6 +292,11 @@ func ResourceToString(res float64, typ string) string {
}
}

func Accuracy(v float64, accuracy int32) float64 {
v, _ = decimal.NewFromFloat(v).Round(accuracy).Float64()
return v
}

func setPrec(f float64, prec int) float64 {
pow := math.Pow10(prec)
f = float64(int64(f*pow)) / pow
Expand Down

0 comments on commit 6cf5f64

Please sign in to comment.