2016-05-10 09:10:19 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
|
|
|
|
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 (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/vmware/harbor/dao"
|
|
|
|
"github.com/vmware/harbor/utils/log"
|
|
|
|
)
|
|
|
|
|
2016-09-02 05:24:06 +02:00
|
|
|
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"
|
|
|
|
// TPC : total count of projects
|
|
|
|
TPC = "total_project_count"
|
|
|
|
// TRC : total count of repositories
|
|
|
|
TRC = "total_repo_count"
|
|
|
|
)
|
|
|
|
|
2016-05-11 09:54:26 +02:00
|
|
|
// StatisticAPI handles request to /api/statistics/
|
2016-05-10 09:10:19 +02:00
|
|
|
type StatisticAPI struct {
|
|
|
|
BaseAPI
|
2016-05-16 11:31:38 +02:00
|
|
|
userID int
|
2016-05-10 09:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Prepare validates the URL and the user
|
|
|
|
func (s *StatisticAPI) Prepare() {
|
2016-05-16 11:31:38 +02:00
|
|
|
s.userID = s.ValidateUser()
|
2016-05-10 09:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get total projects and repos of the user
|
|
|
|
func (s *StatisticAPI) Get() {
|
2016-09-02 05:24:06 +02:00
|
|
|
statistic := map[string]int64{}
|
|
|
|
|
|
|
|
n, err := dao.GetTotalOfProjects("", 1)
|
2016-05-10 09:10:19 +02:00
|
|
|
if err != nil {
|
2016-09-02 05:24:06 +02:00
|
|
|
log.Errorf("failed to get total of public projects: %v", err)
|
|
|
|
s.CustomAbort(http.StatusInternalServerError, "")
|
2016-05-16 11:31:38 +02:00
|
|
|
}
|
2016-09-02 05:24:06 +02:00
|
|
|
statistic[PPC] = n
|
|
|
|
|
|
|
|
n, err = dao.GetTotalOfPublicRepositories("")
|
2016-05-13 09:28:26 +02:00
|
|
|
if err != nil {
|
2016-09-02 05:24:06 +02:00
|
|
|
log.Errorf("failed to get total of public repositories: %v", err)
|
|
|
|
s.CustomAbort(http.StatusInternalServerError, "")
|
2016-05-11 09:54:26 +02:00
|
|
|
}
|
2016-09-02 05:24:06 +02:00
|
|
|
statistic[PRC] = n
|
|
|
|
|
|
|
|
isAdmin, err := dao.IsAdminRole(s.userID)
|
2016-05-18 08:36:20 +02:00
|
|
|
if err != nil {
|
2016-09-02 05:24:06 +02:00
|
|
|
log.Errorf("Error occured in check admin, error: %v", err)
|
2016-05-18 08:36:20 +02:00
|
|
|
s.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
|
|
|
}
|
2016-09-02 05:24:06 +02:00
|
|
|
|
2016-05-11 09:54:26 +02:00
|
|
|
if isAdmin {
|
2016-09-02 05:24:06 +02:00
|
|
|
n, err := dao.GetTotalOfProjects("")
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to get total of projects: %v", err)
|
|
|
|
s.CustomAbort(http.StatusInternalServerError, "")
|
2016-05-16 11:31:38 +02:00
|
|
|
}
|
2016-09-02 05:24:06 +02:00
|
|
|
statistic[MPC] = n
|
|
|
|
statistic[TPC] = n
|
2016-05-10 09:10:19 +02:00
|
|
|
|
2016-09-02 05:24:06 +02:00
|
|
|
n, err = dao.GetTotalOfRepositories("")
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to get total of repositories: %v", err)
|
|
|
|
s.CustomAbort(http.StatusInternalServerError, "")
|
2016-05-11 09:54:26 +02:00
|
|
|
}
|
2016-09-02 05:24:06 +02:00
|
|
|
statistic[MRC] = n
|
|
|
|
statistic[TRC] = n
|
|
|
|
} else {
|
|
|
|
n, err := dao.GetTotalOfUserRelevantProjects(s.userID, "")
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to get total of projects for user %d: %v", s.userID, err)
|
|
|
|
s.CustomAbort(http.StatusInternalServerError, "")
|
|
|
|
}
|
|
|
|
statistic[MPC] = n
|
2016-05-13 16:45:30 +02:00
|
|
|
|
2016-09-02 05:24:06 +02:00
|
|
|
n, err = dao.GetTotalOfUserRelevantRepositories(s.userID, "")
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to get total of repositories for user %d: %v", s.userID, err)
|
|
|
|
s.CustomAbort(http.StatusInternalServerError, "")
|
|
|
|
}
|
|
|
|
statistic[MRC] = n
|
2016-05-13 16:45:30 +02:00
|
|
|
}
|
|
|
|
|
2016-09-02 05:24:06 +02:00
|
|
|
s.Data["json"] = statistic
|
|
|
|
s.ServeJSON()
|
2016-05-13 16:45:30 +02:00
|
|
|
}
|