From 6e9ec3f22c11b8dbb77a88052f0f844b8236c9a9 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Mon, 26 Jun 2017 14:15:01 +0800 Subject: [PATCH] refactor statistics API --- docs/swagger.yaml | 8 ++--- src/ui/api/statistic.go | 35 ++++++++++--------- src/ui/api/statistic_test.go | 10 +++--- .../statistics-panel.component.html | 4 +-- .../src/app/shared/statictics/statistics.ts | 4 +-- tests/apitests/apilib/statistic_map.go | 12 +++---- 6 files changed, 38 insertions(+), 35 deletions(-) diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 462d9242f..899400b3b 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -2005,14 +2005,14 @@ definitions: StatisticMap: type: object properties: - my_project_count: + private_project_count: type: integer format: int32 - description: The count of the projects which the user is a member of. - my_repo_count: + description: The count of the private projects which the user is a member of. + private_repo_count: type: integer format: int32 - description: The count of the repositories belonging to the projects which the user is a member of. + description: The count of the private repositories belonging to the projects which the user is a member of. public_project_count: type: integer format: int32 diff --git a/src/ui/api/statistic.go b/src/ui/api/statistic.go index b0e6cc16d..c9eccd9c0 100644 --- a/src/ui/api/statistic.go +++ b/src/ui/api/statistic.go @@ -24,14 +24,14 @@ import ( ) const ( - // MPC : count of my projects - MPC = "my_project_count" - // MRC : count of my repositories - MRC = "my_repo_count" - // PPC : count of public projects - PPC = "public_project_count" - // PRC : count of public repositories - PRC = "public_repo_count" + // PriPC : count of private projects + PriPC = "private_project_count" + // PriRC : count of private repositories + PriRC = "private_repo_count" + // PubPC : count of public projects + PubPC = "public_project_count" + // PubRC : count of public repositories + PubRC = "public_repo_count" // TPC : total count of projects TPC = "total_project_count" // TRC : total count of repositories @@ -57,17 +57,17 @@ func (s *StatisticAPI) Prepare() { // Get total projects and repos of the user func (s *StatisticAPI) Get() { statistic := map[string]int64{} - projects, err := s.ProjectMgr.GetPublic() + pubProjs, err := s.ProjectMgr.GetPublic() if err != nil { s.HandleInternalServerError(fmt.Sprintf( "failed to get public projects: %v", err)) return } - statistic[PPC] = (int64)(len(projects)) + statistic[PubPC] = (int64)(len(pubProjs)) ids := []int64{} - for _, p := range projects { + for _, p := range pubProjs { ids = append(ids, p.ProjectID) } n, err := dao.GetTotalOfRepositoriesByProject(ids, "") @@ -75,7 +75,7 @@ func (s *StatisticAPI) Get() { log.Errorf("failed to get total of public repositories: %v", err) s.CustomAbort(http.StatusInternalServerError, "") } - statistic[PRC] = n + statistic[PubRC] = n if s.SecurityCtx.IsSysAdmin() { n, err := dao.GetTotalOfProjects(nil) @@ -83,18 +83,20 @@ func (s *StatisticAPI) Get() { log.Errorf("failed to get total of projects: %v", err) s.CustomAbort(http.StatusInternalServerError, "") } - statistic[MPC] = n statistic[TPC] = n + statistic[PriPC] = n - statistic[PubPC] n, err = dao.GetTotalOfRepositories("") if err != nil { log.Errorf("failed to get total of repositories: %v", err) s.CustomAbort(http.StatusInternalServerError, "") } - statistic[MRC] = n statistic[TRC] = n + statistic[PriRC] = n - statistic[PubRC] } else { + value := false projects, err := s.ProjectMgr.GetAll(&models.ProjectQueryParam{ + Public: &value, Member: &models.MemberQuery{ Name: s.username, }, @@ -104,7 +106,8 @@ func (s *StatisticAPI) Get() { "failed to get projects of user %s: %v", s.username, err)) return } - statistic[MPC] = (int64)(len(projects)) + + statistic[PriPC] = (int64)(len(projects)) ids := []int64{} for _, p := range projects { @@ -118,7 +121,7 @@ func (s *StatisticAPI) Get() { s.username, err)) return } - statistic[MRC] = n + statistic[PriRC] = n } s.Data["json"] = statistic diff --git a/src/ui/api/statistic_test.go b/src/ui/api/statistic_test.go index 5ec5d1f21..708cf691d 100644 --- a/src/ui/api/statistic_test.go +++ b/src/ui/api/statistic_test.go @@ -30,7 +30,7 @@ func TestStatisticGet(t *testing.T) { //prepare for test - var priMyProjectCount, priMyRepoCount int32 + var privateProjectCount, privateRepoCount int32 var priPublicProjectCount, priPublicRepoCount int32 var priTotalProjectCount, priTotalRepoCount int32 @@ -53,8 +53,8 @@ func TestStatisticGet(t *testing.T) { } else { assert.Equal(httpStatusCode, int(200), "Case 2: Get status info with admin login. (200)") //fmt.Println("pri status data %+v", result) - priMyProjectCount = result.MyProjectCount - priMyRepoCount = result.MyRepoCount + privateProjectCount = result.PrivateProjectCount + privateRepoCount = result.PrivateRepoCount priPublicProjectCount = result.PublicProjectCount priPublicRepoCount = result.PublicRepoCount priTotalProjectCount = result.TotalProjectCount @@ -74,8 +74,8 @@ func TestStatisticGet(t *testing.T) { t.Error("Error while get statistic information", err.Error()) t.Log(err) } else { - assert.Equal(priMyProjectCount+1, result.MyProjectCount, "MyProjectCount should be +1") - assert.Equal(priMyRepoCount+1, result.MyRepoCount, "MyRepoCount should be +1") + assert.Equal(privateProjectCount+1, result.PrivateProjectCount, "PrivateProjectCount should be +1") + assert.Equal(privateRepoCount, result.PrivateRepoCount) assert.Equal(priPublicProjectCount, result.PublicProjectCount, "PublicProjectCount should be equal") assert.Equal(priPublicRepoCount+1, result.PublicRepoCount, "PublicRepoCount should be +1") assert.Equal(priTotalProjectCount+1, result.TotalProjectCount, "TotalProCount should be +1") diff --git a/src/ui_ng/src/app/shared/statictics/statistics-panel.component.html b/src/ui_ng/src/app/shared/statictics/statistics-panel.component.html index 425d80741..3be348dd3 100644 --- a/src/ui_ng/src/app/shared/statictics/statistics-panel.component.html +++ b/src/ui_ng/src/app/shared/statictics/statistics-panel.component.html @@ -12,10 +12,10 @@
- +
- +
diff --git a/src/ui_ng/src/app/shared/statictics/statistics.ts b/src/ui_ng/src/app/shared/statictics/statistics.ts index 290934af7..d1137b631 100644 --- a/src/ui_ng/src/app/shared/statictics/statistics.ts +++ b/src/ui_ng/src/app/shared/statictics/statistics.ts @@ -14,8 +14,8 @@ export class Statistics { constructor() {} - my_project_count: number; - my_repo_count: number; + private_project_count: number; + private_repo_count: number; public_project_count: number; public_repo_count: number; total_project_count: number; diff --git a/tests/apitests/apilib/statistic_map.go b/tests/apitests/apilib/statistic_map.go index ec89e79a1..1cf96c6f3 100644 --- a/tests/apitests/apilib/statistic_map.go +++ b/tests/apitests/apilib/statistic_map.go @@ -1,10 +1,10 @@ -/* +/* * Harbor API * * These APIs provide services for manipulating Harbor project. * * OpenAPI spec version: 0.3.0 - * + * * Generated by: https://github.com/swagger-api/swagger-codegen.git * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,11 +24,11 @@ package apilib type StatisticMap struct { - // The count of the projects which the user is a member of. - MyProjectCount int32 `json:"my_project_count,omitempty"` + // The count of the private projects which the user is a member of. + PrivateProjectCount int32 `json:"private_project_count,omitempty"` - // The count of the repositories belonging to the projects which the user is a member of. - MyRepoCount int32 `json:"my_repo_count,omitempty"` + // The count of the private repositories belonging to the projects which the user is a member of. + PrivateRepoCount int32 `json:"private_repo_count,omitempty"` // The count of the public projects. PublicProjectCount int32 `json:"public_project_count,omitempty"`