Skip to content

Commit

Permalink
Merge pull request TencentBlueKing#10635 from mingshewhe/bug_10634
Browse files Browse the repository at this point in the history
bug: 禁用项目不应该统计用户数 TencentBlueKing#10634
  • Loading branch information
mingshewhe authored Jul 10, 2024
2 parents 247c89a + 9e8a213 commit cbb3676
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ package com.tencent.devops.metrics.service
import com.github.benmanes.caffeine.cache.Caffeine
import com.tencent.devops.common.client.Client
import com.tencent.devops.project.api.service.ServiceProjectResource
import com.tencent.devops.project.pojo.ProjectVO
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.concurrent.TimeUnit
Expand All @@ -39,16 +40,16 @@ import java.util.concurrent.TimeUnit
class CacheProjectInfoService @Autowired constructor(
private val client: Client
) {
private val productIdCache = Caffeine.newBuilder()
private val projectVOCache = Caffeine.newBuilder()
.maximumSize(10000)
.expireAfterWrite(1, TimeUnit.DAYS)
.build<String, Int>()
.expireAfterWrite(1, TimeUnit.HOURS)
.build<String, ProjectVO>()

fun getProjectId(projectId: String): Int {
return productIdCache.getIfPresent(projectId) ?: run {
val productId = client.get(ServiceProjectResource::class).get(projectId).data?.productId ?: 0
productIdCache.put(projectId, productId)
productId
fun getProject(projectId: String): ProjectVO? {
return projectVOCache.getIfPresent(projectId) ?: run {
val projectVO = client.get(ServiceProjectResource::class).get(projectId).data ?: return null
projectVOCache.put(projectId, projectVO)
projectVO
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.tencent.devops.metrics.service.CacheProjectInfoService
import com.tencent.devops.metrics.service.ProjectBuildSummaryService
import org.jooq.DSLContext
import org.jooq.impl.DSL
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.time.LocalDate
Expand All @@ -50,6 +51,7 @@ class ProjectBuildSummaryServiceImpl @Autowired constructor(
) : ProjectBuildSummaryService {

companion object {
private val logger = LoggerFactory.getLogger(ProjectBuildSummaryServiceImpl::class.java)
private fun projectBuildKey(key: String) = "ProjectBuild:$key"
}

Expand All @@ -63,11 +65,15 @@ class ProjectBuildSummaryServiceImpl @Autowired constructor(
val lock = RedisLock(redisOperation, projectBuildKey(projectId), 120)
lock.use {
lock.lock()
val productId = cacheProjectInfoService.getProjectId(projectId)
val projectVO = cacheProjectInfoService.getProject(projectId)
if (projectVO?.enabled == false) {
logger.info("Project [${projectVO.englishName}] has disabled, skip build count")
return
}
projectBuildSummaryDao.saveBuildCount(
dslContext = dslContext,
projectId = projectId,
productId = productId,
productId = projectVO?.productId ?: 0,
trigger = trigger
)
}
Expand All @@ -81,7 +87,11 @@ class ProjectBuildSummaryServiceImpl @Autowired constructor(
val lock = RedisLock(redisOperation, projectBuildKey(projectId), 120)
lock.use {
lock.lock()
val productId = cacheProjectInfoService.getProjectId(projectId)
val projectVO = cacheProjectInfoService.getProject(projectId)
if (projectVO?.enabled == false) {
logger.info("Project [${projectVO.englishName}] has disabled, skip user count")
return
}
dslContext.transaction { configuration ->
val transactionContext = DSL.using(configuration)
val insert = projectBuildSummaryDao.saveProjectUser(
Expand All @@ -93,7 +103,7 @@ class ProjectBuildSummaryServiceImpl @Autowired constructor(
projectBuildSummaryDao.saveUserCount(
dslContext = dslContext,
projectId = projectId,
productId = productId,
productId = projectVO?.productId ?: 0,
theDate = theDate
)
}
Expand Down

0 comments on commit cbb3676

Please sign in to comment.