-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistic.go
138 lines (125 loc) · 3.52 KB
/
statistic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2018 Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"fmt"
"net/http"
"github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/common/utils/log"
)
const (
// 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
TRC = "total_repo_count"
)
// StatisticAPI handles request to /api/statistics/
type StatisticAPI struct {
BaseController
username string
}
// Prepare validates the URL and the user
func (s *StatisticAPI) Prepare() {
s.BaseController.Prepare()
if !s.SecurityCtx.IsAuthenticated() {
s.HandleUnauthorized()
return
}
s.username = s.SecurityCtx.GetUsername()
}
// Get total projects and repos of the user
func (s *StatisticAPI) Get() {
statistic := map[string]int64{}
pubProjs, err := s.ProjectMgr.GetPublic()
if err != nil {
s.ParseAndHandleError("failed to get public projects", err)
return
}
statistic[PubPC] = (int64)(len(pubProjs))
if len(pubProjs) == 0 {
statistic[PubRC] = 0
} else {
ids := []int64{}
for _, p := range pubProjs {
ids = append(ids, p.ProjectID)
}
n, err := dao.GetTotalOfRepositories(&models.RepositoryQuery{
ProjectIDs: ids,
})
if err != nil {
log.Errorf("failed to get total of public repositories: %v", err)
s.CustomAbort(http.StatusInternalServerError, "")
}
statistic[PubRC] = n
}
if s.SecurityCtx.IsSysAdmin() {
result, err := s.ProjectMgr.List(nil)
if err != nil {
log.Errorf("failed to get total of projects: %v", err)
s.CustomAbort(http.StatusInternalServerError, "")
}
statistic[TPC] = result.Total
statistic[PriPC] = result.Total - statistic[PubPC]
n, err := dao.GetTotalOfRepositories()
if err != nil {
log.Errorf("failed to get total of repositories: %v", err)
s.CustomAbort(http.StatusInternalServerError, "")
}
statistic[TRC] = n
statistic[PriRC] = n - statistic[PubRC]
} else {
value := false
result, err := s.ProjectMgr.List(&models.ProjectQueryParam{
Public: &value,
Member: &models.MemberQuery{
Name: s.username,
},
})
if err != nil {
s.ParseAndHandleError(fmt.Sprintf(
"failed to get projects of user %s", s.username), err)
return
}
statistic[PriPC] = result.Total
if result.Total == 0 {
statistic[PriRC] = 0
} else {
ids := []int64{}
for _, p := range result.Projects {
ids = append(ids, p.ProjectID)
}
n, err := dao.GetTotalOfRepositories(&models.RepositoryQuery{
ProjectIDs: ids,
})
if err != nil {
s.HandleInternalServerError(fmt.Sprintf(
"failed to get total of repositories for user %s: %v",
s.username, err))
return
}
statistic[PriRC] = n
}
}
s.Data["json"] = statistic
s.ServeJSON()
}