diff --git a/src/backend/opdata/api-opdata/build.gradle.kts b/src/backend/opdata/api-opdata/build.gradle.kts index beff456627..654e9d98b6 100644 --- a/src/backend/opdata/api-opdata/build.gradle.kts +++ b/src/backend/opdata/api-opdata/build.gradle.kts @@ -32,6 +32,7 @@ dependencies { implementation(project(":common:common-api")) implementation(project(":common:common-operate:operate-annotation")) + api("com.alibaba:easyexcel:3.1.1") compileOnly("org.springframework.cloud:spring-cloud-openfeign-core") testImplementation(project(":common:common-operate:operate-service")) } 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 f79125559c..b183fbce3d 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 @@ -52,6 +52,8 @@ const val OPDATA_PIPELINE_SIZE = "pipelineSize" const val OPDATA_PROJECT_NUM = "ProjectNum" const val OPDATA_CAP_SIZE = "CapSize" +const val OPDATA_DOCKER_CAP_SIZE = "DockerCapSize" +const val OPDATA_GENERIC_CAP_SIZE = "GenericCapSize" const val OPDATA_NODE_NUM = "NodeNum" const val OPDATA_PROJECT = "project" diff --git a/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/ProjectMetrics.kt b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/ProjectMetrics.kt new file mode 100644 index 0000000000..b7e7f9aada --- /dev/null +++ b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/ProjectMetrics.kt @@ -0,0 +1,47 @@ +/* + * 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.opdata.pojo + +import com.alibaba.excel.annotation.ExcelProperty +import com.alibaba.excel.annotation.write.style.ColumnWidth +import java.time.LocalDateTime + +data class ProjectMetrics( + @ColumnWidth(30) + @ExcelProperty(value = ["项目ID"], order = 0) + var projectId: String, + @ColumnWidth(20) + @ExcelProperty(value = ["节点个数"], order = 1) + var nodeNum: Long, + @ColumnWidth(20) + @ExcelProperty(value = ["项目大小(GB)"], order = 2) + var capSize: Long, + @ColumnWidth(20) + @ExcelProperty(value = ["统计时间"], order = 3) + val createdDate: LocalDateTime? = LocalDateTime.now() +) diff --git a/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/ProjectMetricsOption.kt b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/ProjectMetricsOption.kt index 91f8fa91c7..4bfc7c18f0 100644 --- a/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/ProjectMetricsOption.kt +++ b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/ProjectMetricsOption.kt @@ -36,5 +36,5 @@ data class ProjectMetricsOption( var projectId: String? = null, val pageNumber: Int = DEFAULT_PAGE_NUMBER, val pageSize: Int = DEFAULT_PAGE_SIZE, - val createdDate: LocalDateTime = LocalDate.now().atStartOfDay() + val createdDate: LocalDateTime = LocalDate.now().minusDays(1).atStartOfDay() ) diff --git a/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/ProjectMetricsRequest.kt b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/ProjectMetricsRequest.kt new file mode 100644 index 0000000000..514f8701e1 --- /dev/null +++ b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/ProjectMetricsRequest.kt @@ -0,0 +1,35 @@ +/* + * 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.opdata.pojo + +data class ProjectMetricsRequest( + val limitSize: Long = 1024 * 1024 * 1024L, + val minusDay: Long = 1, + val default: Boolean = true, + val type: String? = null +) 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 7b61bace74..25d2571d50 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 @@ -41,5 +41,7 @@ data class RepoMetrics( @ApiModelProperty("size") var size: Long, @ApiModelProperty("num") - val num: Long + val num: Long, + @ApiModelProperty("type") + val type: String? = null ) diff --git a/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/enums/Metrics.kt b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/enums/Metrics.kt index 57faaf21c2..5a1937f554 100644 --- a/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/enums/Metrics.kt +++ b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/enums/Metrics.kt @@ -50,4 +50,8 @@ enum class Metrics { NODECOLLECTION, REPONODENUM, REPONODESIZE, + GENERICREPOSIZEINPROJECT, + DOCKERREPOSIZEINPROJECT, + DOCKERCAPSIZE, + GENERICCAPSIZE, } diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/controller/ProjectController.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/controller/ProjectController.kt index 7ebf8873b3..f36834c416 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/controller/ProjectController.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/controller/ProjectController.kt @@ -33,7 +33,9 @@ import com.tencent.bkrepo.common.security.permission.Principal import com.tencent.bkrepo.common.security.permission.PrincipalType import com.tencent.bkrepo.common.service.util.ResponseBuilder import com.tencent.bkrepo.opdata.model.TProjectMetrics +import com.tencent.bkrepo.opdata.pojo.ProjectMetrics import com.tencent.bkrepo.opdata.pojo.ProjectMetricsOption +import com.tencent.bkrepo.opdata.pojo.ProjectMetricsRequest import com.tencent.bkrepo.opdata.service.ProjectMetricsService import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestMapping @@ -55,4 +57,23 @@ class ProjectController( ): Response> { return ResponseBuilder.success(projectMetricsService.page(option)) } + + /** + * 获取项目的统计数据 + */ + @GetMapping("/list/project/capSize") + fun getProjectCapSizeMetrics( + metricsRequest: ProjectMetricsRequest + ): Response> { + return ResponseBuilder.success(projectMetricsService.list(metricsRequest)) + } + /** + * 获取项目的统计数据 + */ + @GetMapping("/list/project/capSize/download") + fun downloadProjectCapSizeMetrics( + metricsRequest: ProjectMetricsRequest + ) { + projectMetricsService.download(metricsRequest) + } } diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/BaseCapSizeHandler.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/BaseCapSizeHandler.kt new file mode 100644 index 0000000000..fc221591f6 --- /dev/null +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/BaseCapSizeHandler.kt @@ -0,0 +1,71 @@ +/* + * 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.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.model.StatDateModel +import com.tencent.bkrepo.opdata.pojo.Columns +import com.tencent.bkrepo.opdata.pojo.QueryResult +import com.tencent.bkrepo.opdata.pojo.Target +import com.tencent.bkrepo.opdata.pojo.enums.Metrics +import com.tencent.bkrepo.opdata.repository.ProjectMetricsRepository + +open class BaseCapSizeHandler( + private val projectMetricsRepository: ProjectMetricsRepository, + private val statDateModel: StatDateModel +) : QueryHandler { + + override val metric: Metrics get() = Metrics.DEFAULT + + open val repoType: List = emptyList() + + open val columnsText: String = OPDATA_CAP_SIZE + + override fun handle(target: Target, result: MutableList) { + var size = 0L + val projects = projectMetricsRepository.findAllByCreatedDate(statDateModel.getShedLockInfo()) + if (repoType.isEmpty()) { + projects.forEach { + size += it.capSize + } + } else { + projects.forEach { project -> + project.repoMetrics.filter { !it.type.isNullOrEmpty() && it.type in repoType }.forEach { repo -> + size += repo.size + } + } + } + val column = Columns(columnsText, OPDATA_GRAFANA_NUMBER) + 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/CapSizeHandler.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/CapSizeHandler.kt index 76e066dc1e..e04cc75a63 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 @@ -31,13 +31,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 -import com.tencent.bkrepo.opdata.pojo.Target +import com.tencent.bkrepo.opdata.model.StatDateModel import com.tencent.bkrepo.opdata.pojo.enums.Metrics import com.tencent.bkrepo.opdata.repository.ProjectMetricsRepository import org.springframework.stereotype.Component @@ -47,20 +41,9 @@ import org.springframework.stereotype.Component */ @Component class CapSizeHandler( - private val projectMetricsRepository: ProjectMetricsRepository -) : QueryHandler { + projectMetricsRepository: ProjectMetricsRepository, + statDateModel: StatDateModel +) : BaseCapSizeHandler(projectMetricsRepository, statDateModel) { override val metric: Metrics get() = Metrics.CAPSIZE - - override fun handle(target: Target, result: MutableList) { - var size = 0L - val projects = projectMetricsRepository.findAllByCreatedDate() - projects.forEach { - size += it.capSize - } - val column = Columns(OPDATA_CAP_SIZE, OPDATA_GRAFANA_NUMBER) - 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/DockerCapSizeHandler.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/DockerCapSizeHandler.kt new file mode 100644 index 0000000000..6036fe14c4 --- /dev/null +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/DockerCapSizeHandler.kt @@ -0,0 +1,55 @@ +/* + * 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.opdata.handler.impl + +import com.tencent.bkrepo.opdata.constant.OPDATA_DOCKER_CAP_SIZE +import com.tencent.bkrepo.opdata.model.StatDateModel +import com.tencent.bkrepo.opdata.pojo.enums.Metrics +import com.tencent.bkrepo.opdata.repository.ProjectMetricsRepository +import org.springframework.stereotype.Component + +/** + * DOCKER 仓库总容量统计 + */ +@Component +class DockerCapSizeHandler( + projectMetricsRepository: ProjectMetricsRepository, + statDateModel: StatDateModel +) : BaseCapSizeHandler(projectMetricsRepository, statDateModel) { + + override val metric: Metrics get() = Metrics.DOCKERCAPSIZE + + override val columnsText: String = OPDATA_DOCKER_CAP_SIZE + + override val repoType: List + get() = DOCKER_TYPES + + companion object { + private val DOCKER_TYPES = listOf("DOCKER", "OCI") + } +} \ No newline at end of file diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/DockerRepoSizeInProject.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/DockerRepoSizeInProject.kt new file mode 100644 index 0000000000..cd5abb82d9 --- /dev/null +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/DockerRepoSizeInProject.kt @@ -0,0 +1,52 @@ +/* + * 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.opdata.handler.impl + +import com.tencent.bkrepo.opdata.model.StatDateModel +import com.tencent.bkrepo.opdata.pojo.enums.Metrics +import com.tencent.bkrepo.opdata.repository.ProjectMetricsRepository +import org.springframework.stereotype.Component + +/** + * 项目中镜像仓库总大小 + */ +@Component +class DockerRepoSizeInProject( + projectMetricsRepository: ProjectMetricsRepository, + statDateModel: StatDateModel +) : RepoSizeInProject(projectMetricsRepository, statDateModel) { + + override val metric: Metrics + get() = Metrics.DOCKERREPOSIZEINPROJECT + override val repoType: List + get() = DOCKER_TYPES + + companion object { + private val DOCKER_TYPES = listOf("DOCKER", "OCI") + } +} \ No newline at end of file diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/EffectiveProjectNumHandler.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/EffectiveProjectNumHandler.kt index 0eaebd19b7..e2f526ec1a 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/EffectiveProjectNumHandler.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/EffectiveProjectNumHandler.kt @@ -34,6 +34,7 @@ package com.tencent.bkrepo.opdata.handler.impl import com.tencent.bkrepo.opdata.constant.OPDATA_GRAFANA_NUMBER import com.tencent.bkrepo.opdata.constant.OPDATA_PROJECT_NUM import com.tencent.bkrepo.opdata.handler.QueryHandler +import com.tencent.bkrepo.opdata.model.StatDateModel import com.tencent.bkrepo.opdata.pojo.Columns import com.tencent.bkrepo.opdata.pojo.QueryResult import com.tencent.bkrepo.opdata.pojo.Target @@ -46,13 +47,14 @@ import org.springframework.stereotype.Component */ @Component class EffectiveProjectNumHandler( - private val projectMetricsRepository: ProjectMetricsRepository + private val projectMetricsRepository: ProjectMetricsRepository, + private val statDateModel: StatDateModel ) : QueryHandler { override val metric: Metrics get() = Metrics.EFFECTIVEPROJECTNUM override fun handle(target: Target, result: MutableList) { - val projects = projectMetricsRepository.findAllByCreatedDate() + val projects = projectMetricsRepository.findAllByCreatedDate(statDateModel.getShedLockInfo()) val count = projects.filter { it.capSize > 0 }.size.toLong() val columns = Columns(OPDATA_PROJECT_NUM, OPDATA_GRAFANA_NUMBER) val row = listOf(count) diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/GenericCapSizeHandler.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/GenericCapSizeHandler.kt new file mode 100644 index 0000000000..a9956cc107 --- /dev/null +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/GenericCapSizeHandler.kt @@ -0,0 +1,55 @@ +/* + * 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.opdata.handler.impl + +import com.tencent.bkrepo.opdata.constant.OPDATA_GENERIC_CAP_SIZE +import com.tencent.bkrepo.opdata.model.StatDateModel +import com.tencent.bkrepo.opdata.pojo.enums.Metrics +import com.tencent.bkrepo.opdata.repository.ProjectMetricsRepository +import org.springframework.stereotype.Component + +/** + * GENERIC 仓库总容量统计 + */ +@Component +class GenericCapSizeHandler( + projectMetricsRepository: ProjectMetricsRepository, + statDateModel: StatDateModel +) : BaseCapSizeHandler(projectMetricsRepository, statDateModel) { + + override val metric: Metrics get() = Metrics.GENERICCAPSIZE + + override val columnsText: String = OPDATA_GENERIC_CAP_SIZE + + override val repoType: List + get() = listOf(GENERIC_TYPE) + + companion object { + private const val GENERIC_TYPE = "GENERIC" + } +} \ No newline at end of file diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/GenericRepoSizeInProject.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/GenericRepoSizeInProject.kt new file mode 100644 index 0000000000..d40b92fce7 --- /dev/null +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/GenericRepoSizeInProject.kt @@ -0,0 +1,52 @@ +/* + * 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.opdata.handler.impl + +import com.tencent.bkrepo.opdata.model.StatDateModel +import com.tencent.bkrepo.opdata.pojo.enums.Metrics +import com.tencent.bkrepo.opdata.repository.ProjectMetricsRepository +import org.springframework.stereotype.Component + +/** + * 项目中generic仓库总大小 + */ +@Component +class GenericRepoSizeInProject( + projectMetricsRepository: ProjectMetricsRepository, + statDateModel: StatDateModel +) : RepoSizeInProject(projectMetricsRepository, statDateModel) { + + override val metric: Metrics + get() = Metrics.GENERICREPOSIZEINPROJECT + override val repoType: List + get() = listOf(GENERIC_TYPE) + + companion object { + private const val GENERIC_TYPE = "GENERIC" + } +} \ No newline at end of file diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/NodeNumHandler.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/NodeNumHandler.kt index c6876eae36..e04b5dce7b 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/NodeNumHandler.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/NodeNumHandler.kt @@ -34,6 +34,7 @@ package com.tencent.bkrepo.opdata.handler.impl import com.tencent.bkrepo.opdata.constant.OPDATA_GRAFANA_NUMBER import com.tencent.bkrepo.opdata.constant.OPDATA_NODE_NUM import com.tencent.bkrepo.opdata.handler.QueryHandler +import com.tencent.bkrepo.opdata.model.StatDateModel import com.tencent.bkrepo.opdata.pojo.Columns import com.tencent.bkrepo.opdata.pojo.QueryResult import com.tencent.bkrepo.opdata.pojo.Target @@ -46,14 +47,15 @@ import org.springframework.stereotype.Component */ @Component class NodeNumHandler( - private val projectMetricsRepository: ProjectMetricsRepository + private val projectMetricsRepository: ProjectMetricsRepository, + private val statDateModel: StatDateModel ) : QueryHandler { override val metric: Metrics get() = Metrics.NODENUM override fun handle(target: Target, result: MutableList) { var num = 0L - val projects = projectMetricsRepository.findAllByCreatedDate() + val projects = projectMetricsRepository.findAllByCreatedDate(statDateModel.getShedLockInfo()) projects.forEach { num += it.nodeNum } 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 1303937ccc..bf1525b472 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 @@ -41,6 +41,7 @@ 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.StatDateModel import com.tencent.bkrepo.opdata.model.TProjectMetrics import com.tencent.bkrepo.opdata.pojo.Columns import com.tencent.bkrepo.opdata.pojo.QueryResult @@ -54,7 +55,8 @@ import org.springframework.stereotype.Component */ @Component class ProjectListHandler( - private val projectMetricsRepository: ProjectMetricsRepository + private val projectMetricsRepository: ProjectMetricsRepository, + private val statDateModel: StatDateModel ) : QueryHandler { override val metric: Metrics get() = Metrics.PROJECTLIST @@ -62,7 +64,7 @@ class ProjectListHandler( override fun handle(target: Target, result: MutableList) { val rows = mutableListOf>() val columns = mutableListOf() - val info = projectMetricsRepository.findAllByCreatedDate() + val info = projectMetricsRepository.findAllByCreatedDate(statDateModel.getShedLockInfo()) columns.add(Columns(TProjectMetrics::projectId.name, OPDATA_GRAFANA_STRING)) columns.add(Columns(TProjectMetrics::nodeNum.name, OPDATA_GRAFANA_NUMBER)) columns.add(Columns(TProjectMetrics::capSize.name, OPDATA_GRAFANA_NUMBER)) diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/ProjectNodeNumHandler.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/ProjectNodeNumHandler.kt index 85ddb97739..83e528059f 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/ProjectNodeNumHandler.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/ProjectNodeNumHandler.kt @@ -33,6 +33,7 @@ package com.tencent.bkrepo.opdata.handler.impl import com.tencent.bkrepo.opdata.constant.PROJECT_NAME import com.tencent.bkrepo.opdata.handler.QueryHandler +import com.tencent.bkrepo.opdata.model.StatDateModel import com.tencent.bkrepo.opdata.pojo.Target import com.tencent.bkrepo.opdata.pojo.enums.Metrics import com.tencent.bkrepo.opdata.repository.ProjectMetricsRepository @@ -43,13 +44,14 @@ import org.springframework.stereotype.Component */ @Component class ProjectNodeNumHandler( - private val projectMetricsRepository: ProjectMetricsRepository + private val projectMetricsRepository: ProjectMetricsRepository, + private val statDateModel: StatDateModel ) : QueryHandler { override val metric: Metrics get() = Metrics.PROJECTNODENUM override fun handle(target: Target, result: MutableList): List { - val projects = projectMetricsRepository.findAllByCreatedDate() + val projects = projectMetricsRepository.findAllByCreatedDate(statDateModel.getShedLockInfo()) val tmpMap = HashMap() projects.forEach { val projectId = it.projectId 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 b32cf51f13..5df5de0557 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 @@ -33,6 +33,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.model.StatDateModel import com.tencent.bkrepo.opdata.pojo.Target import com.tencent.bkrepo.opdata.pojo.enums.Metrics import com.tencent.bkrepo.opdata.repository.ProjectMetricsRepository @@ -43,13 +44,14 @@ import org.springframework.stereotype.Component */ @Component class ProjectNodeSizeHandler( - private val projectMetricsRepository: ProjectMetricsRepository + private val projectMetricsRepository: ProjectMetricsRepository, + private val statDateModel: StatDateModel ) : QueryHandler { override val metric: Metrics get() = Metrics.PROJECTNODESIZE override fun handle(target: Target, result: MutableList): List { - val projects = projectMetricsRepository.findAllByCreatedDate() + val projects = projectMetricsRepository.findAllByCreatedDate(statDateModel.getShedLockInfo()) val tmpMap = HashMap() projects.forEach { val projectId = it.projectId diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/RepoNodeNumHandler.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/RepoNodeNumHandler.kt index 7895b3730f..329e73f078 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/RepoNodeNumHandler.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/RepoNodeNumHandler.kt @@ -33,6 +33,7 @@ package com.tencent.bkrepo.opdata.handler.impl import com.tencent.bkrepo.opdata.constant.PROJECT_NAME import com.tencent.bkrepo.opdata.handler.QueryHandler +import com.tencent.bkrepo.opdata.model.StatDateModel import com.tencent.bkrepo.opdata.pojo.Target import com.tencent.bkrepo.opdata.pojo.enums.Metrics import com.tencent.bkrepo.opdata.repository.ProjectMetricsRepository @@ -43,13 +44,14 @@ import org.springframework.stereotype.Component */ @Component class RepoNodeNumHandler( - private val projectMetricsRepository: ProjectMetricsRepository + private val projectMetricsRepository: ProjectMetricsRepository, + private val statDateModel: StatDateModel ) : QueryHandler { override val metric: Metrics get() = Metrics.REPONODENUM override fun handle(target: Target, result: MutableList): List { - val projects = projectMetricsRepository.findAllByCreatedDate() + val projects = projectMetricsRepository.findAllByCreatedDate(statDateModel.getShedLockInfo()) val tmpMap = HashMap() projects.forEach { it -> val projectId = it.projectId 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 9cb4fd4004..cd9fa1724a 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 @@ -33,6 +33,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.model.StatDateModel import com.tencent.bkrepo.opdata.pojo.Target import com.tencent.bkrepo.opdata.pojo.enums.Metrics import com.tencent.bkrepo.opdata.repository.ProjectMetricsRepository @@ -43,13 +44,14 @@ import org.springframework.stereotype.Component */ @Component class RepoNodeSizeHandler( - private val projectMetricsRepository: ProjectMetricsRepository + private val projectMetricsRepository: ProjectMetricsRepository, + private val statDateModel: StatDateModel ) : QueryHandler { override val metric: Metrics get() = Metrics.REPONODESIZE override fun handle(target: Target, result: MutableList): List { - val projects = projectMetricsRepository.findAllByCreatedDate() + val projects = projectMetricsRepository.findAllByCreatedDate(statDateModel.getShedLockInfo()) val tmpMap = HashMap() projects.forEach { it -> val projectId = it.projectId diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/RepoSizeInProject.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/RepoSizeInProject.kt new file mode 100644 index 0000000000..e86e101ed4 --- /dev/null +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/RepoSizeInProject.kt @@ -0,0 +1,62 @@ +/* + * 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.opdata.handler.impl + +import com.tencent.bkrepo.opdata.constant.TO_GIGABYTE +import com.tencent.bkrepo.opdata.handler.QueryHandler +import com.tencent.bkrepo.opdata.model.StatDateModel +import com.tencent.bkrepo.opdata.pojo.Target +import com.tencent.bkrepo.opdata.pojo.enums.Metrics +import com.tencent.bkrepo.opdata.repository.ProjectMetricsRepository + +open class RepoSizeInProject( + private val projectMetricsRepository: ProjectMetricsRepository, + private val statDateModel: StatDateModel +) : QueryHandler { + + override val metric: Metrics get() = Metrics.DEFAULT + + open val repoType: List = emptyList() + + override fun handle(target: Target, result: MutableList): Any { + val projects = projectMetricsRepository.findAllByCreatedDate(statDateModel.getShedLockInfo()) + val tmpMap = HashMap() + projects.forEach { tP -> + val projectId = tP.projectId + var repoSize = 0L + tP.repoMetrics.filter { !it.type.isNullOrEmpty()&& it.type in repoType }.forEach {repo -> + repoSize += repo.size + } + val gbSize = repoSize / TO_GIGABYTE + if (gbSize != 0L) { + tmpMap[projectId] = gbSize + } + } + return convToDisplayData(tmpMap, result) + } +} \ No newline at end of file diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/StatDateModel.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/StatDateModel.kt new file mode 100644 index 0000000000..3dbdd1f467 --- /dev/null +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/StatDateModel.kt @@ -0,0 +1,88 @@ +/* + * 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.opdata.model + +import com.tencent.bkrepo.common.job.JobAutoConfiguration.Companion.SHED_LOCK_COLLECTION_NAME +import com.tencent.bkrepo.common.mongo.constant.ID +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.data.mongodb.core.MongoTemplate +import org.springframework.data.mongodb.core.query.Criteria +import org.springframework.data.mongodb.core.query.Query +import org.springframework.stereotype.Service +import java.time.LocalDate +import java.time.LocalDateTime + +@Service +class StatDateModel @Autowired constructor( + private val mongoTemplate: MongoTemplate +) { + + companion object { + private val JOB_NAMES = listOf("ProjectRepoMetricsStatJob") + } + + fun getShedLockInfo(ids: List = JOB_NAMES): LocalDateTime { + return try { + val query = Query(Criteria.where(ID).`in`(ids)) + val result = mongoTemplate.find(query, ShedlockInfo::class.java, SHED_LOCK_COLLECTION_NAME) + getLockedAtDate(result) + } catch (e: Exception) { + LocalDate.now().minusDays(1).atStartOfDay() + } + } + + + private fun getLockedAtDate(lockInfos: List): LocalDateTime { + if (lockInfos.isEmpty()) { + return LocalDate.now().minusDays(1).atStartOfDay() + } + var statDateTime: LocalDateTime? = null + lockInfos.forEach { + val tempStatDate = if (it.lockUntil!!.isBefore(LocalDateTime.now())) { + it.lockedAt!!.toLocalDate().atStartOfDay() + } else { + it.lockedAt!!.toLocalDate().minusDays(1).atStartOfDay() + } + if (statDateTime == null) { + statDateTime = tempStatDate + } else { + if (statDateTime!!.isBefore(tempStatDate)) { + statDateTime = tempStatDate + } + } + } + return statDateTime!! + } + + data class ShedlockInfo( + val id: String, + var lockUntil: LocalDateTime?, + var lockedAt: LocalDateTime?, + var lockedBy: String, + ) +} diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/StorageCredentialsModel.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/StorageCredentialsModel.kt index 1d45207b5b..3d64b8dbfd 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/StorageCredentialsModel.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/StorageCredentialsModel.kt @@ -39,12 +39,13 @@ import org.springframework.stereotype.Service @Service class StorageCredentialsModel @Autowired constructor( - private val projectMetricsRepository: ProjectMetricsRepository + private val projectMetricsRepository: ProjectMetricsRepository, + private val statDateModel: StatDateModel ) { fun getStorageCredentialsStat(metrics: StatMetrics): Map { val result = mutableMapOf() - val projectMetricsList = projectMetricsRepository.findAllByCreatedDate() + val projectMetricsList = projectMetricsRepository.findAllByCreatedDate(statDateModel.getShedLockInfo()) projectMetricsList.forEach { projectMetrics -> projectMetrics.repoMetrics.forEach { val value = if (metrics == StatMetrics.NUM) it.num else it.size diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/repository/ProjectMetricsRepository.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/repository/ProjectMetricsRepository.kt index 8a64dafa20..14c95d8719 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/repository/ProjectMetricsRepository.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/repository/ProjectMetricsRepository.kt @@ -48,6 +48,6 @@ interface ProjectMetricsRepository : MongoRepository{ createdDate: LocalDateTime, pageable: Pageable ): Page fun findAllByCreatedDate( - createdDate: LocalDateTime = LocalDate.now().atStartOfDay() + createdDate: LocalDateTime = LocalDate.now().minusDays(1).atStartOfDay() ): List } 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 88462d97bc..5a59b7b3e8 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 @@ -30,14 +30,20 @@ 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.StatDateModel import com.tencent.bkrepo.opdata.model.TProjectMetrics +import com.tencent.bkrepo.opdata.pojo.ProjectMetrics import com.tencent.bkrepo.opdata.pojo.ProjectMetricsOption +import com.tencent.bkrepo.opdata.pojo.ProjectMetricsRequest import com.tencent.bkrepo.opdata.repository.ProjectMetricsRepository +import com.tencent.bkrepo.opdata.util.EasyExcelUtils import org.springframework.stereotype.Service +import java.time.LocalDate @Service class ProjectMetricsService ( - private val projectMetricsRepository: ProjectMetricsRepository + private val projectMetricsRepository: ProjectMetricsRepository, + private val statDateModel: StatDateModel ){ fun page(option: ProjectMetricsOption): Page { @@ -59,4 +65,60 @@ class ProjectMetricsService ( return Pages.ofResponse(pageRequest, queryResult.totalElements, queryResult.content) } } + + fun list(metricsRequest: ProjectMetricsRequest): List { + return getProjectMetrics(metricsRequest) + } + + fun download(metricsRequest: ProjectMetricsRequest) { + val records = getProjectMetrics(metricsRequest) + // 导出 + val includeColumns = mutableSetOf( + ProjectMetrics::projectId.name, + ProjectMetrics::nodeNum.name, + ProjectMetrics::capSize.name, + ProjectMetrics::createdDate.name + ) + val fileName = "大于${metricsRequest.limitSize/TO_GIGABYTE}GB的项目信息" + EasyExcelUtils.download(records, fileName, ProjectMetrics::class.java, includeColumns) + } + + private fun getProjectMetrics(metricsRequest: ProjectMetricsRequest): List { + val createdDate = if (metricsRequest.default) { + statDateModel.getShedLockInfo() + } else { + LocalDate.now().minusDays(metricsRequest.minusDay).atStartOfDay() + } + val queryResult = projectMetricsRepository.findAllByCreatedDate(createdDate) + val result = mutableListOf() + queryResult.forEach { + val (sizeOfRepoType, nodeNumOfRepoType) = getSizeAndNodeNum(metricsRequest.type, it) + if (sizeOfRepoType >= metricsRequest.limitSize) { + result.add(ProjectMetrics( + projectId = it.projectId, + capSize = sizeOfRepoType / TO_GIGABYTE, + nodeNum = nodeNumOfRepoType, + createdDate = it.createdDate + )) + } + } + return result.sortedByDescending { it.capSize } + } + + + private fun getSizeAndNodeNum(type: String?, projectMetrics: TProjectMetrics): Pair { + return if (type.isNullOrEmpty()) { + Pair(projectMetrics.capSize , projectMetrics.nodeNum) + } else { + var sizeOfRepoType: Long = 0 + var nodeNumOfRepoType: Long = 0 + projectMetrics.repoMetrics.forEach { repo -> + if (repo.type == type) { + sizeOfRepoType += repo.size + nodeNumOfRepoType += repo.num + } + } + Pair(sizeOfRepoType, nodeNumOfRepoType) + } + } } \ No newline at end of file diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/util/EasyExcelUtils.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/util/EasyExcelUtils.kt new file mode 100644 index 0000000000..0fbe82db54 --- /dev/null +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/util/EasyExcelUtils.kt @@ -0,0 +1,83 @@ +/* + * 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.opdata.util + +import com.alibaba.excel.EasyExcel +import com.alibaba.excel.write.metadata.style.WriteCellStyle +import com.alibaba.excel.write.style.HorizontalCellStyleStrategy +import com.tencent.bkrepo.common.service.util.HttpContextHolder +import org.apache.poi.ss.usermodel.HorizontalAlignment +import org.apache.poi.ss.usermodel.VerticalAlignment +import org.slf4j.LoggerFactory +import java.io.IOException +import java.net.URLEncoder +import java.time.LocalDateTime +import java.time.format.DateTimeFormatter +import javax.servlet.http.HttpServletResponse + +object EasyExcelUtils { + + private val logger = LoggerFactory.getLogger(EasyExcelUtils::class.java) + + fun download(data: Collection<*>, name: String, dataClass: Class<*>, includeColumns: Set? = null) { + val response = getDownloadResponse(name) + try { + EasyExcel.write(response.outputStream, dataClass).build().use { excelWriter -> + val date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss")) + val cellStyleStrategy = HorizontalCellStyleStrategy(WriteCellStyle(), WriteCellStyle().apply { + this.horizontalAlignment = HorizontalAlignment.LEFT + this.verticalAlignment = VerticalAlignment.CENTER + this.wrapped = true + }) + val writerSheetBuilder = EasyExcel + .writerSheet(date) + .registerWriteHandler(cellStyleStrategy) + .head(dataClass) + includeColumns?.let { + writerSheetBuilder.includeColumnFieldNames(includeColumns) + } + excelWriter.write(data, writerSheetBuilder.build()) + } + } catch (e: IOException) { + logger.warn("download exception: $e") + } + } + + private fun getDownloadResponse(fileName: String): HttpServletResponse { + val response = HttpContextHolder.getResponse() + response.contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + response.characterEncoding = "utf-8" + val date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss")) + val exportFileName = URLEncoder.encode("$fileName-$date", "UTF-8") + response.setHeader( + "Content-disposition", + "attachment;filename*=utf-8''$exportFileName.xlsx" + ) + return response + } +}