Skip to content

Commit

Permalink
perf: 详情页的旧数据兼容下架 TencentBlueKing#9522
Browse files Browse the repository at this point in the history
  • Loading branch information
carlyin0801 committed Jun 21, 2024
1 parent 78365d8 commit 1dfcb6d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import com.tencent.devops.common.redis.RedisOperation
import com.tencent.devops.common.service.utils.LogUtils
import com.tencent.devops.common.web.utils.I18nUtil
import com.tencent.devops.common.websocket.enum.RefreshType
import com.tencent.devops.process.constant.ProcessMessageCode
import com.tencent.devops.process.dao.record.BuildRecordModelDao
import com.tencent.devops.process.engine.control.lock.PipelineBuildRecordLock
import com.tencent.devops.process.engine.dao.PipelineBuildDao
Expand All @@ -68,6 +69,7 @@ import com.tencent.devops.process.utils.KEY_PIPELINE_ID
import com.tencent.devops.process.utils.KEY_PROJECT_ID
import org.jooq.DSLContext
import org.slf4j.LoggerFactory
import javax.ws.rs.core.Response

@Suppress("LongParameterList", "MagicNumber", "ReturnCount", "ComplexMethod")
open class BaseBuildRecordService(
Expand Down Expand Up @@ -151,6 +153,51 @@ open class BaseBuildRecordService(
return
}

fun getRecordModel(
projectId: String,
pipelineId: String,
version: Int,
buildId: String,
executeCount: Int? = null,
queryDslContext: DSLContext? = null,
debug: Boolean? = false
): Model? {
val fixedExecuteCount = if (executeCount == null) {
val buildInfo = pipelineBuildDao.getBuildInfo(
dslContext = queryDslContext ?: dslContext, projectId = projectId, buildId = buildId
) ?: throw ErrorCodeException(
statusCode = Response.Status.NOT_FOUND.statusCode,
errorCode = ProcessMessageCode.ERROR_NO_BUILD_EXISTS_BY_ID,
params = arrayOf(buildId)
)
buildInfo.executeCount ?: 1
} else {
executeCount
}
val buildRecordModel = buildRecordModelDao.getRecord(
dslContext = queryDslContext ?: dslContext,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
executeCount = fixedExecuteCount
)
return if (buildRecordModel != null) {
getRecordModel(
projectId = projectId,
pipelineId = pipelineId,
version = version,
buildId = buildId,
fixedExecuteCount = fixedExecuteCount,
buildRecordModel = buildRecordModel,
executeCount = executeCount,
queryDslContext = queryDslContext,
debug = debug
)
} else {
null
}
}

fun getRecordModel(
projectId: String,
pipelineId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@

package com.tencent.devops.process.engine.service.record

import com.tencent.devops.common.api.exception.ErrorCodeException
import com.tencent.devops.common.api.pojo.ErrorInfo
import com.tencent.devops.common.api.util.JsonUtil
import com.tencent.devops.common.api.util.Watcher
import com.tencent.devops.common.api.util.timestampmilli
import com.tencent.devops.common.event.dispatcher.pipeline.PipelineEventDispatcher
Expand All @@ -52,8 +54,10 @@ import com.tencent.devops.common.pipeline.utils.ModelUtils
import com.tencent.devops.common.redis.RedisOperation
import com.tencent.devops.common.service.utils.LogUtils
import com.tencent.devops.common.web.utils.I18nUtil
import com.tencent.devops.process.constant.ProcessMessageCode
import com.tencent.devops.process.constant.ProcessMessageCode.BK_EVENT
import com.tencent.devops.process.constant.ProcessMessageCode.BK_WAREHOUSE_EVENTS
import com.tencent.devops.process.dao.BuildDetailDao
import com.tencent.devops.process.dao.record.BuildRecordContainerDao
import com.tencent.devops.process.dao.record.BuildRecordModelDao
import com.tencent.devops.process.dao.record.BuildRecordStageDao
Expand Down Expand Up @@ -87,6 +91,7 @@ import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import javax.ws.rs.core.Response

@Suppress(
"LongParameterList",
Expand All @@ -97,7 +102,6 @@ import org.springframework.stereotype.Service
)
@Service
class PipelineBuildRecordService @Autowired constructor(
private val pipelineBuildDetailService: PipelineBuildDetailService,
private val pipelineRepositoryService: PipelineRepositoryService,
private val pipelineBuildSummaryDao: PipelineBuildSummaryDao,
private val pipelineTriggerReviewDao: PipelineTriggerReviewDao,
Expand All @@ -106,6 +110,7 @@ class PipelineBuildRecordService @Autowired constructor(
private val recordStageDao: BuildRecordStageDao,
private val recordContainerDao: BuildRecordContainerDao,
private val recordTaskDao: BuildRecordTaskDao,
private val buildDetailDao: BuildDetailDao,
recordModelService: PipelineRecordModelService,
pipelineResourceDao: PipelineResourceDao,
pipelineBuildDao: PipelineBuildDao,
Expand Down Expand Up @@ -206,7 +211,8 @@ class PipelineBuildRecordService @Autowired constructor(
"fixedExecuteCount=$fixedExecuteCount"
)
watcher.start("getDetailModel")
val detail = pipelineBuildDetailService.getBuildModel(projectId, buildId, queryDslContext) ?: return null
val record = buildDetailDao.get(queryDslContext ?: dslContext, projectId, buildId) ?: return null
val detail = JsonUtil.to(record.model, Model::class.java)
fixDetailTimeCost(buildInfo, detail)
detail
}
Expand Down Expand Up @@ -463,12 +469,6 @@ class PipelineBuildRecordService @Autowired constructor(
cancelUser: String,
executeCount: Int
) {
pipelineBuildDetailService.buildCancel(
projectId = projectId,
buildId = buildId,
buildStatus = buildStatus,
cancelUser = cancelUser
)
logger.info("[$buildId]|BUILD_CANCEL|cancelUser=$cancelUser|buildStatus=$buildStatus")
dslContext.transaction { configuration ->
val context = DSL.using(configuration)
Expand Down Expand Up @@ -552,18 +552,26 @@ class PipelineBuildRecordService @Autowired constructor(
errorMsg: String?
): Triple<Model, List<BuildStageStatus>, BuildRecordTimeCost?> {
logger.info("[$buildId]|BUILD_END|buildStatus=$buildStatus")
// var allStageStatus: List<BuildStageStatus> = emptyList()
var timeCost: BuildRecordTimeCost? = null
val recordModel = recordModelDao.getRecord(
dslContext = dslContext,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
executeCount = executeCount
) ?: run {
logger.warn(
"ENGINE|$buildId|buildEnd| get model($buildId) record failed."
)
throw ErrorCodeException(
statusCode = Response.Status.NOT_FOUND.statusCode,
errorCode = ProcessMessageCode.ERROR_NO_BUILD_EXISTS_BY_ID,
params = arrayOf(buildId)
)
}
var allStageStatus: List<BuildStageStatus> = emptyList()
dslContext.transaction { configuration ->
val context = DSL.using(configuration)
val recordModel = recordModelDao.getRecord(
context, projectId, pipelineId, buildId, executeCount
) ?: run {
logger.warn(
"ENGINE|$buildId|buildEnd| get model($buildId) record failed."
)
return@transaction
}
val now = LocalDateTime.now()
val runningStatusSet = enumValues<BuildStatus>().filter { it.isRunning() }.toSet()
// 刷新运行中stage状态,取出所有stage记录还需用于耗时计算
Expand Down Expand Up @@ -602,12 +610,9 @@ class PipelineBuildRecordService @Autowired constructor(
recordTaskDao.batchSave(context, recordTasks)
recordContainerDao.batchSave(context, recordContainers)
recordStageDao.batchSave(context, recordStages)

// allStageStatus = fetchHistoryStageStatus(
// recordStages = recordStages,
// buildStatus = buildStatus,
// errorMsg = errorMsg
// )
allStageStatus = fetchHistoryStageStatus(
recordStages = recordStages, buildStatus = buildStatus, errorMsg = errorMsg
)

val modelVar = mutableMapOf<String, Any>()
timeCost = recordModel.generateBuildTimeCost(recordStages)
Expand All @@ -625,13 +630,18 @@ class PipelineBuildRecordService @Autowired constructor(
executeCount = executeCount
)
}
val detail = pipelineBuildDetailService.buildEnd(
val model = getRecordModel(
projectId = projectId,
pipelineId = pipelineId,
version = recordModel.resourceVersion,
buildId = buildId,
buildStatus = buildStatus,
errorMsg = errorMsg
executeCount = executeCount
) ?: throw ErrorCodeException(
statusCode = Response.Status.NOT_FOUND.statusCode,
errorCode = ProcessMessageCode.ERROR_NO_BUILD_EXISTS_BY_ID,
params = arrayOf(buildId)
)
return Triple(detail.first, detail.second, timeCost)
return Triple(model, allStageStatus, timeCost)
}

fun updateBuildCancelUser(
Expand All @@ -640,11 +650,6 @@ class PipelineBuildRecordService @Autowired constructor(
executeCount: Int,
cancelUserId: String
) {
pipelineBuildDetailService.updateBuildCancelUser(
projectId = projectId,
buildId = buildId,
cancelUserId = cancelUserId
)
recordModelDao.updateBuildCancelUser(
dslContext = dslContext,
projectId = projectId,
Expand Down

0 comments on commit 1dfcb6d

Please sign in to comment.