diff --git a/src/backend/job/biz-job/src/main/kotlin/com/tencent/bkrepo/job/batch/context/ProjectRepoChildContext.kt b/src/backend/job/biz-job/src/main/kotlin/com/tencent/bkrepo/job/batch/context/ProjectRepoChildContext.kt index 60954cb1ec..ab2259d7f3 100644 --- a/src/backend/job/biz-job/src/main/kotlin/com/tencent/bkrepo/job/batch/context/ProjectRepoChildContext.kt +++ b/src/backend/job/biz-job/src/main/kotlin/com/tencent/bkrepo/job/batch/context/ProjectRepoChildContext.kt @@ -65,7 +65,7 @@ class ProjectRepoChildContext( return ProjectRepoStatChildJob.TProjectMetrics( projectId = projectId, nodeNum = nodeNum.toLong(), - capSize = capSize.toLong() / TO_GIGABYTE, + capSize = capSize.toLong(), repoMetrics = repoMetrics ) } @@ -128,7 +128,7 @@ class ProjectRepoChildContext( return ProjectRepoStatChildJob.TRepoMetrics( repoName = repoName, credentialsKey = credentialsKey, - size = size.toLong() / TO_GIGABYTE, + size = size.toLong(), num = num.toLong() ) } diff --git a/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/constant/Constants.kt b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/constant/Constants.kt index 6a8285940d..f79125559c 100644 --- a/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/constant/Constants.kt +++ b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/constant/Constants.kt @@ -59,8 +59,7 @@ const val OPDATA_PROJECT_ID = "projectId" const val OPDATA_REPOSITORY = "repository" const val OPDATA_REPO_NAME = "repoName" const val OPDATA_PATH = "path" - - +const val TO_GIGABYTE = 1024 * 1024 * 1024 const val OPDATA_FILE_EXTENSION_METRICS = "file_extension_metrics" diff --git a/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/RepoMetrics.kt b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/RepoMetrics.kt index c889bf3507..7b61bace74 100644 --- a/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/RepoMetrics.kt +++ b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/RepoMetrics.kt @@ -39,7 +39,7 @@ data class RepoMetrics( @ApiModelProperty("credentialsKey") val credentialsKey: String? = "default", @ApiModelProperty("size") - val size: Long, + var size: Long, @ApiModelProperty("num") val num: Long ) diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/CapSizeHandler.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/CapSizeHandler.kt index 1eaeecbdd1..eaa7373c10 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/CapSizeHandler.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/CapSizeHandler.kt @@ -33,6 +33,7 @@ package com.tencent.bkrepo.opdata.handler.impl import com.tencent.bkrepo.opdata.constant.OPDATA_CAP_SIZE import com.tencent.bkrepo.opdata.constant.OPDATA_GRAFANA_NUMBER +import com.tencent.bkrepo.opdata.constant.TO_GIGABYTE import com.tencent.bkrepo.opdata.handler.QueryHandler import com.tencent.bkrepo.opdata.pojo.Columns import com.tencent.bkrepo.opdata.pojo.QueryResult @@ -58,7 +59,7 @@ class CapSizeHandler( size += it.capSize } val column = Columns(OPDATA_CAP_SIZE, OPDATA_GRAFANA_NUMBER) - val row = listOf(size) + val row = listOf(size / TO_GIGABYTE) val data = QueryResult(listOf(column), listOf(row), target.type) result.add(data) } diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/ProjectListHandler.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/ProjectListHandler.kt index 2faa238d9e..e4cf8ff813 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/ProjectListHandler.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/ProjectListHandler.kt @@ -39,6 +39,7 @@ import com.tencent.bkrepo.opdata.constant.OPDATA_GRAFANA_STRING import com.tencent.bkrepo.opdata.constant.OPDATA_PIPELINE import com.tencent.bkrepo.opdata.constant.OPDATA_PIPELINE_NUM import com.tencent.bkrepo.opdata.constant.OPDATA_PIPELINE_SIZE +import com.tencent.bkrepo.opdata.constant.TO_GIGABYTE import com.tencent.bkrepo.opdata.handler.QueryHandler import com.tencent.bkrepo.opdata.model.TProjectMetrics import com.tencent.bkrepo.opdata.pojo.Columns @@ -86,7 +87,7 @@ class ProjectListHandler( } val row = listOf( repo.projectId, repo.nodeNum, repo.capSize, - customNum, customSize, pipelineNum, pipelineSize + customNum, customSize / TO_GIGABYTE, pipelineNum, pipelineSize / TO_GIGABYTE ) rows.add(row) } diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/ProjectNodeSizeHandler.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/ProjectNodeSizeHandler.kt index 23156dea71..c80a7e337c 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/ProjectNodeSizeHandler.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/ProjectNodeSizeHandler.kt @@ -31,6 +31,7 @@ package com.tencent.bkrepo.opdata.handler.impl +import com.tencent.bkrepo.opdata.constant.TO_GIGABYTE import com.tencent.bkrepo.opdata.handler.QueryHandler import com.tencent.bkrepo.opdata.pojo.Target import com.tencent.bkrepo.opdata.pojo.enums.Metrics @@ -52,8 +53,9 @@ class ProjectNodeSizeHandler( val tmpMap = HashMap() projects.forEach { val projectId = it.projectId - if (it.capSize != 0L) { - tmpMap[projectId] = it.capSize + val gbSize = it.capSize / TO_GIGABYTE + if (gbSize != 0L) { + tmpMap[projectId] = gbSize } } return convToDisplayData(tmpMap, result) diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/RepoNodeSizeHandler.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/RepoNodeSizeHandler.kt index bd8faf3b18..c7d9149d1c 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/RepoNodeSizeHandler.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/RepoNodeSizeHandler.kt @@ -31,6 +31,7 @@ package com.tencent.bkrepo.opdata.handler.impl +import com.tencent.bkrepo.opdata.constant.TO_GIGABYTE import com.tencent.bkrepo.opdata.handler.QueryHandler import com.tencent.bkrepo.opdata.pojo.Target import com.tencent.bkrepo.opdata.pojo.enums.Metrics @@ -54,8 +55,9 @@ class RepoNodeSizeHandler( val projectId = it.projectId it.repoMetrics.forEach { val repoName = it.repoName - if (it.size != 0L) { - tmpMap["$projectId-$repoName"] = it.size + val gbSize = it.size / TO_GIGABYTE + if (gbSize != 0L) { + tmpMap["$projectId-$repoName"] = gbSize } } } diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/ProjectMetricsService.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/ProjectMetricsService.kt index 5c6d37dd7a..38e2d0581c 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/ProjectMetricsService.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/ProjectMetricsService.kt @@ -29,6 +29,7 @@ package com.tencent.bkrepo.opdata.service import com.tencent.bkrepo.common.api.pojo.Page import com.tencent.bkrepo.common.mongo.dao.util.Pages +import com.tencent.bkrepo.opdata.constant.TO_GIGABYTE import com.tencent.bkrepo.opdata.model.TProjectMetrics import com.tencent.bkrepo.opdata.pojo.ProjectMetricsOption import com.tencent.bkrepo.opdata.repository.ProjectMetricsRepository @@ -47,6 +48,12 @@ class ProjectMetricsService ( } else { projectMetricsRepository.findAllByOrderByCreatedDateDesc(pageRequest) } + queryResult.content.forEach { + it.capSize = it.capSize / TO_GIGABYTE + it.repoMetrics.forEach { repo -> + repo.size = repo.size / TO_GIGABYTE + } + } return Pages.ofResponse(pageRequest, queryResult.totalElements, queryResult.content) } } diff --git a/src/backend/replication/biz-replication/src/main/kotlin/com/tencent/bkrepo/replication/manager/LocalDataManager.kt b/src/backend/replication/biz-replication/src/main/kotlin/com/tencent/bkrepo/replication/manager/LocalDataManager.kt index 21e8518098..3e71ca69b3 100644 --- a/src/backend/replication/biz-replication/src/main/kotlin/com/tencent/bkrepo/replication/manager/LocalDataManager.kt +++ b/src/backend/replication/biz-replication/src/main/kotlin/com/tencent/bkrepo/replication/manager/LocalDataManager.kt @@ -253,4 +253,14 @@ class LocalDataManager( val repo = findRepoByName(projectId, repoName) return getBlobDataByRange(sha256, range, repo) } + + + /** + * 从项目仓库统计信息中找出对应仓库的大小 + */ + fun getRepoMetricInfo(projectId: String, repoName: String): Long { + findRepoByName(projectId, repoName) + val projectMetrics = projectClient.getProjectMetrics(projectId).data ?: return 0 + return projectMetrics.repoMetrics.firstOrNull { it.repoName == repoName }?.size ?: 0 + } } diff --git a/src/backend/replication/biz-replication/src/main/kotlin/com/tencent/bkrepo/replication/service/impl/ReplicaTaskServiceImpl.kt b/src/backend/replication/biz-replication/src/main/kotlin/com/tencent/bkrepo/replication/service/impl/ReplicaTaskServiceImpl.kt index 01f651d857..8465821c99 100644 --- a/src/backend/replication/biz-replication/src/main/kotlin/com/tencent/bkrepo/replication/service/impl/ReplicaTaskServiceImpl.kt +++ b/src/backend/replication/biz-replication/src/main/kotlin/com/tencent/bkrepo/replication/service/impl/ReplicaTaskServiceImpl.kt @@ -264,6 +264,7 @@ class ReplicaTaskServiceImpl( return when (replicaObjectType) { ReplicaObjectType.PACKAGE -> computePackageSize(localProjectId, replicaTaskObjects) ReplicaObjectType.PATH -> computeNodeSize(localProjectId, replicaTaskObjects) + ReplicaObjectType.REPOSITORY -> computeRepositorySize(localProjectId, replicaTaskObjects) else -> -1 } } @@ -286,7 +287,7 @@ class ReplicaTaskServiceImpl( val taskObject = replicaTaskObjects.first() return taskObject.pathConstraints!!.sumOf { val node = localDataManager.findNodeDetail(localProjectId, taskObject.localRepoName, it.path!!) - if (node.folder) { + if (node.folder && node.size == 0L) { try { localDataManager.listNode(localProjectId, taskObject.localRepoName, it.path!!).sumOf { n -> n.size } } catch (e: Exception) { @@ -299,6 +300,11 @@ class ReplicaTaskServiceImpl( } } + private fun computeRepositorySize(localProjectId: String, replicaTaskObjects: List): Long { + val taskObject = replicaTaskObjects.first() + return localDataManager.getRepoMetricInfo(localProjectId, taskObject.localRepoName) + } + private fun validateRequest(request: ReplicaTaskCreateRequest) { with(request) { // 验证任务是否存在 diff --git a/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/api/ProjectClient.kt b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/api/ProjectClient.kt index 77dba352a0..60281282c9 100644 --- a/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/api/ProjectClient.kt +++ b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/api/ProjectClient.kt @@ -36,6 +36,7 @@ import com.tencent.bkrepo.common.api.pojo.Page import com.tencent.bkrepo.common.api.pojo.Response import com.tencent.bkrepo.repository.pojo.project.ProjectCreateRequest import com.tencent.bkrepo.repository.pojo.project.ProjectInfo +import com.tencent.bkrepo.repository.pojo.project.ProjectMetricsInfo import com.tencent.bkrepo.repository.pojo.project.ProjectRangeQueryRequest import io.swagger.annotations.Api import io.swagger.annotations.ApiOperation @@ -69,4 +70,8 @@ interface ProjectClient { @ApiOperation("创建项目") @PostMapping("/create") fun createProject(@RequestBody request: ProjectCreateRequest): Response + + @ApiOperation("项目仓库统计信息列表") + @PostMapping("/metrics/{name}") + fun getProjectMetrics(@ApiParam(value = "项目名") @PathVariable name: String): Response } diff --git a/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectMetricsInfo.kt b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectMetricsInfo.kt new file mode 100644 index 0000000000..7b5b01a975 --- /dev/null +++ b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectMetricsInfo.kt @@ -0,0 +1,45 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. + * + * A copy of the MIT License is included in this file. + * + * + * Terms of the MIT License: + * --------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT + * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.tencent.bkrepo.repository.pojo.project + +import com.tencent.bkrepo.repository.pojo.repo.RepoMetricsInfo +import io.swagger.annotations.ApiModelProperty +import java.time.LocalDateTime + +data class ProjectMetricsInfo( + @ApiModelProperty("projectId") + var projectId: String, + @ApiModelProperty("nodeNum") + var nodeNum: Long, + @ApiModelProperty("capSize") + var capSize: Long, + @ApiModelProperty("repoMetrics") + val repoMetrics: List, + @ApiModelProperty("createdDate") + val createdDate: LocalDateTime? = LocalDateTime.now() +) diff --git a/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/repo/RepoMetricsInfo.kt b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/repo/RepoMetricsInfo.kt new file mode 100644 index 0000000000..2cad97113a --- /dev/null +++ b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/repo/RepoMetricsInfo.kt @@ -0,0 +1,41 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. + * + * A copy of the MIT License is included in this file. + * + * + * Terms of the MIT License: + * --------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT + * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.tencent.bkrepo.repository.pojo.repo + +import io.swagger.annotations.ApiModelProperty + +data class RepoMetricsInfo( + @ApiModelProperty("repoName") + val repoName: String, + @ApiModelProperty("credentialsKey") + val credentialsKey: String? = "default", + @ApiModelProperty("size") + var size: Long, + @ApiModelProperty("num") + val num: Long +) diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/controller/service/ProjectController.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/controller/service/ProjectController.kt index b6e6b0f2b5..fba2937ee9 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/controller/service/ProjectController.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/controller/service/ProjectController.kt @@ -37,6 +37,7 @@ import com.tencent.bkrepo.common.service.util.ResponseBuilder import com.tencent.bkrepo.repository.api.ProjectClient import com.tencent.bkrepo.repository.pojo.project.ProjectCreateRequest import com.tencent.bkrepo.repository.pojo.project.ProjectInfo +import com.tencent.bkrepo.repository.pojo.project.ProjectMetricsInfo import com.tencent.bkrepo.repository.pojo.project.ProjectRangeQueryRequest import com.tencent.bkrepo.repository.service.repo.ProjectService import org.springframework.web.bind.annotation.RestController @@ -64,4 +65,8 @@ class ProjectController( override fun createProject(request: ProjectCreateRequest): Response { return ResponseBuilder.success(projectService.createProject(request)) } + + override fun getProjectMetrics(name: String): Response { + return ResponseBuilder.success(projectService.getProjectMetricsInfo(name)) + } } diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/controller/user/UserProjectController.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/controller/user/UserProjectController.kt index 5cf716c130..1268c90856 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/controller/user/UserProjectController.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/controller/user/UserProjectController.kt @@ -37,6 +37,7 @@ import com.tencent.bkrepo.common.service.util.ResponseBuilder import com.tencent.bkrepo.repository.pojo.project.ProjectCreateRequest import com.tencent.bkrepo.repository.pojo.project.ProjectInfo import com.tencent.bkrepo.repository.pojo.project.ProjectListOption +import com.tencent.bkrepo.repository.pojo.project.ProjectMetricsInfo import com.tencent.bkrepo.repository.pojo.project.ProjectSearchOption import com.tencent.bkrepo.repository.pojo.project.ProjectUpdateRequest import com.tencent.bkrepo.repository.pojo.project.UserProjectCreateRequest @@ -141,4 +142,14 @@ class UserProjectController( ): Response { return this.createProject(userId, userProjectRequest) } + + @ApiOperation("项目仓库统计信息列表") + @GetMapping("/metrics/{projectId}") + fun projectMetricsList( + @ApiParam(value = "项目ID", required = true) + @PathVariable projectId: String + ): Response { + permissionManager.checkProjectPermission(PermissionAction.READ, projectId) + return ResponseBuilder.success(projectService.getProjectMetricsInfo(projectId)) + } } diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/dao/repository/ProjectMetricsRepository.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/dao/repository/ProjectMetricsRepository.kt new file mode 100644 index 0000000000..8a4ddae826 --- /dev/null +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/dao/repository/ProjectMetricsRepository.kt @@ -0,0 +1,41 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. + * + * A copy of the MIT License is included in this file. + * + * + * Terms of the MIT License: + * --------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.tencent.bkrepo.repository.dao.repository + +import com.tencent.bkrepo.repository.model.TProjectMetrics +import org.springframework.data.mongodb.repository.MongoRepository +import org.springframework.stereotype.Repository + +@Repository +interface ProjectMetricsRepository : MongoRepository{ + fun findByProjectId(projectId:String): TProjectMetrics? +} diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/model/TProjectMetrics.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/model/TProjectMetrics.kt new file mode 100644 index 0000000000..1c30a143f4 --- /dev/null +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/model/TProjectMetrics.kt @@ -0,0 +1,46 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. + * + * A copy of the MIT License is included in this file. + * + * + * Terms of the MIT License: + * --------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.tencent.bkrepo.repository.model + +import com.tencent.bkrepo.repository.pojo.repo.RepoMetricsInfo +import org.springframework.data.mongodb.core.mapping.Document +import java.time.LocalDateTime + +@Document("project_metrics") +data class TProjectMetrics( + var id: String? = null, + var projectId: String, + var nodeNum: Long, + var capSize: Long, + val repoMetrics: List, + val createdDate: LocalDateTime? = LocalDateTime.now() +) diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/ProjectService.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/ProjectService.kt index 63778a6b5d..f05d0f371c 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/ProjectService.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/ProjectService.kt @@ -31,6 +31,7 @@ import com.tencent.bkrepo.common.api.pojo.Page import com.tencent.bkrepo.repository.pojo.project.ProjectCreateRequest import com.tencent.bkrepo.repository.pojo.project.ProjectInfo import com.tencent.bkrepo.repository.pojo.project.ProjectListOption +import com.tencent.bkrepo.repository.pojo.project.ProjectMetricsInfo import com.tencent.bkrepo.repository.pojo.project.ProjectRangeQueryRequest import com.tencent.bkrepo.repository.pojo.project.ProjectSearchOption import com.tencent.bkrepo.repository.pojo.project.ProjectUpdateRequest @@ -92,4 +93,11 @@ interface ProjectService { * 判断项目信息是否存在 */ fun checkProjectExist(name: String?, displayName: String?): Boolean + + /** + * 查询项目统计信息,包含节点数、大小以及仓库的节点数和大小 + * @param userId 用户id + * @param name 项目列表选项 + */ + fun getProjectMetricsInfo(name: String): ProjectMetricsInfo? } diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/impl/ProjectServiceImpl.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/impl/ProjectServiceImpl.kt index 5f4bc75378..9352c7d953 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/impl/ProjectServiceImpl.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/impl/ProjectServiceImpl.kt @@ -40,11 +40,14 @@ import com.tencent.bkrepo.common.artifact.message.ArtifactMessageCode import com.tencent.bkrepo.common.mongo.dao.util.Pages import com.tencent.bkrepo.common.service.cluster.DefaultCondition import com.tencent.bkrepo.repository.dao.ProjectDao +import com.tencent.bkrepo.repository.dao.repository.ProjectMetricsRepository import com.tencent.bkrepo.repository.listener.ResourcePermissionListener import com.tencent.bkrepo.repository.model.TProject +import com.tencent.bkrepo.repository.model.TProjectMetrics import com.tencent.bkrepo.repository.pojo.project.ProjectCreateRequest import com.tencent.bkrepo.repository.pojo.project.ProjectInfo import com.tencent.bkrepo.repository.pojo.project.ProjectListOption +import com.tencent.bkrepo.repository.pojo.project.ProjectMetricsInfo import com.tencent.bkrepo.repository.pojo.project.ProjectRangeQueryRequest import com.tencent.bkrepo.repository.pojo.project.ProjectSearchOption import com.tencent.bkrepo.repository.pojo.project.ProjectUpdateRequest @@ -75,7 +78,8 @@ import java.util.regex.Pattern @Conditional(DefaultCondition::class) class ProjectServiceImpl( private val projectDao: ProjectDao, - private val servicePermissionClient: ServicePermissionClient + private val servicePermissionClient: ServicePermissionClient, + private val projectMetricsRepository: ProjectMetricsRepository ) : ProjectService { @Autowired @@ -201,6 +205,10 @@ class ProjectServiceImpl( return nameResult || displayNameResult } + override fun getProjectMetricsInfo(name: String): ProjectMetricsInfo? { + return convert(projectMetricsRepository.findByProjectId(name)) + } + override fun updateProject(name: String, request: ProjectUpdateRequest): Boolean { if (!checkExist(name)) { throw ErrorCodeException(ArtifactMessageCode.PROJECT_NOT_FOUND, name) @@ -258,5 +266,17 @@ class ProjectServiceImpl( ) } } + + private fun convert(tProjectMetrics: TProjectMetrics?): ProjectMetricsInfo? { + return tProjectMetrics?.let { + ProjectMetricsInfo( + projectId = it.projectId, + nodeNum = it.nodeNum, + capSize = it.capSize, + repoMetrics = it.repoMetrics, + createdDate = it.createdDate + ) + } + } } } diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/impl/center/CommitEdgeCenterProjectServiceImpl.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/impl/center/CommitEdgeCenterProjectServiceImpl.kt index 6acbec25e7..a756dd0e42 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/impl/center/CommitEdgeCenterProjectServiceImpl.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/impl/center/CommitEdgeCenterProjectServiceImpl.kt @@ -30,6 +30,7 @@ package com.tencent.bkrepo.repository.service.repo.impl.center import com.tencent.bkrepo.auth.api.ServicePermissionClient import com.tencent.bkrepo.common.service.cluster.CommitEdgeCenterCondition import com.tencent.bkrepo.repository.dao.ProjectDao +import com.tencent.bkrepo.repository.dao.repository.ProjectMetricsRepository import com.tencent.bkrepo.repository.service.repo.impl.ProjectServiceImpl import org.springframework.context.annotation.Conditional import org.springframework.stereotype.Service @@ -38,8 +39,10 @@ import org.springframework.stereotype.Service @Conditional(CommitEdgeCenterCondition::class) class CommitEdgeCenterProjectServiceImpl( projectDao: ProjectDao, - servicePermissionClient: ServicePermissionClient + servicePermissionClient: ServicePermissionClient, + projectMetricsRepository: ProjectMetricsRepository ) : ProjectServiceImpl( projectDao, - servicePermissionClient + servicePermissionClient, + projectMetricsRepository ) diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/impl/edge/EdgeProjectServiceImpl.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/impl/edge/EdgeProjectServiceImpl.kt index 07bc070a4c..7cfaee6a10 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/impl/edge/EdgeProjectServiceImpl.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/impl/edge/EdgeProjectServiceImpl.kt @@ -35,6 +35,7 @@ import com.tencent.bkrepo.common.service.exception.RemoteErrorCodeException import com.tencent.bkrepo.common.service.feign.FeignClientFactory import com.tencent.bkrepo.repository.api.cluster.ClusterProjectClient import com.tencent.bkrepo.repository.dao.ProjectDao +import com.tencent.bkrepo.repository.dao.repository.ProjectMetricsRepository import com.tencent.bkrepo.repository.pojo.project.ProjectCreateRequest import com.tencent.bkrepo.repository.pojo.project.ProjectInfo import com.tencent.bkrepo.repository.service.repo.impl.ProjectServiceImpl @@ -46,10 +47,12 @@ import org.springframework.stereotype.Service class EdgeProjectServiceImpl( projectDao: ProjectDao, servicePermissionClient: ServicePermissionClient, - clusterProperties: ClusterProperties + clusterProperties: ClusterProperties, + projectMetricsRepository: ProjectMetricsRepository ) : ProjectServiceImpl( projectDao, - servicePermissionClient + servicePermissionClient, + projectMetricsRepository ) { private val centerProjectClient: ClusterProjectClient by lazy {