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"
|
2016-05-11 09:54:26 +02:00
|
|
|
"strings"
|
2016-05-10 09:10:19 +02:00
|
|
|
|
|
|
|
"github.com/vmware/harbor/dao"
|
|
|
|
"github.com/vmware/harbor/models"
|
|
|
|
svc_utils "github.com/vmware/harbor/service/utils"
|
|
|
|
"github.com/vmware/harbor/utils/log"
|
|
|
|
)
|
|
|
|
|
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-05-16 11:31:38 +02:00
|
|
|
isAdmin, err := dao.IsAdminRole(s.userID)
|
2016-05-10 09:10:19 +02:00
|
|
|
if err != nil {
|
2016-05-16 11:31:38 +02:00
|
|
|
log.Errorf("Error occured in check admin, error: %v", err)
|
2016-05-10 09:10:19 +02:00
|
|
|
s.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
|
|
|
}
|
2016-05-16 11:31:38 +02:00
|
|
|
var projectList []models.Project
|
|
|
|
if isAdmin {
|
2016-05-17 11:03:40 +02:00
|
|
|
projectList, err = dao.GetAllProjects("")
|
2016-05-16 11:31:38 +02:00
|
|
|
} else {
|
2016-05-17 11:03:40 +02:00
|
|
|
projectList, err = dao.GetUserRelevantProjects(s.userID, "")
|
2016-05-16 11:31:38 +02:00
|
|
|
}
|
2016-05-13 09:28:26 +02:00
|
|
|
if err != nil {
|
2016-05-16 11:31:38 +02:00
|
|
|
log.Errorf("Error occured in QueryProject, error: %v", err)
|
2016-05-11 09:54:26 +02:00
|
|
|
s.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
|
|
|
}
|
2016-05-13 10:39:22 +02:00
|
|
|
proMap := map[string]int{}
|
|
|
|
proMap["my_project_count"] = 0
|
|
|
|
proMap["my_repo_count"] = 0
|
|
|
|
proMap["public_project_count"] = 0
|
|
|
|
proMap["public_repo_count"] = 0
|
2016-05-11 09:54:26 +02:00
|
|
|
if isAdmin {
|
2016-05-13 16:45:30 +02:00
|
|
|
proMap["total_project_count"] = len(projectList)
|
|
|
|
proMap["total_repo_count"] = getTotalRepoCount()
|
2016-05-11 09:54:26 +02:00
|
|
|
}
|
2016-05-10 09:10:19 +02:00
|
|
|
for i := 0; i < len(projectList); i++ {
|
2016-05-16 11:31:38 +02:00
|
|
|
if isAdmin {
|
|
|
|
projectList[i].Role = models.PROJECTADMIN
|
|
|
|
}
|
2016-05-13 09:28:26 +02:00
|
|
|
if projectList[i].Role == models.PROJECTADMIN || projectList[i].Role == models.DEVELOPER ||
|
|
|
|
projectList[i].Role == models.GUEST {
|
2016-05-13 16:45:30 +02:00
|
|
|
proMap["my_project_count"]++
|
|
|
|
proMap["my_repo_count"] += getRepoCountByProject(projectList[i].Name)
|
2016-05-10 09:10:19 +02:00
|
|
|
}
|
|
|
|
if projectList[i].Public == 1 {
|
2016-05-13 16:45:30 +02:00
|
|
|
proMap["public_project_count"]++
|
|
|
|
proMap["public_repo_count"] += getRepoCountByProject(projectList[i].Name)
|
2016-05-10 09:10:19 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-11 09:54:26 +02:00
|
|
|
s.Data["json"] = proMap
|
2016-05-10 09:10:19 +02:00
|
|
|
s.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2016-05-13 16:45:30 +02:00
|
|
|
//getReposByProject returns repo numbers of specified project
|
|
|
|
func getRepoCountByProject(projectName string) int {
|
2016-05-10 09:10:19 +02:00
|
|
|
repoList, err := svc_utils.GetRepoFromCache()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to get repo from cache, error: %v", err)
|
2016-05-13 16:45:30 +02:00
|
|
|
return 0
|
2016-05-11 09:54:26 +02:00
|
|
|
}
|
2016-05-13 09:28:26 +02:00
|
|
|
var resp int
|
2016-05-11 09:54:26 +02:00
|
|
|
if len(projectName) > 0 {
|
|
|
|
for _, r := range repoList {
|
|
|
|
if strings.Contains(r, "/") && r[0:strings.LastIndex(r, "/")] == projectName {
|
2016-05-13 10:18:48 +02:00
|
|
|
resp++
|
2016-05-11 09:54:26 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-13 09:28:26 +02:00
|
|
|
return resp
|
2016-05-11 09:54:26 +02:00
|
|
|
}
|
|
|
|
return 0
|
2016-05-10 09:10:19 +02:00
|
|
|
}
|
2016-05-13 16:45:30 +02:00
|
|
|
|
|
|
|
//getTotalRepoCount returns total repo count
|
|
|
|
func getTotalRepoCount() int {
|
|
|
|
repoList, err := svc_utils.GetRepoFromCache()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to get repo from cache, error: %v", err)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return len(repoList)
|
|
|
|
|
|
|
|
}
|