Skip to content

Commit

Permalink
feat:Code 方式创建的流水线,变量面板-输出变量未获取到问题优化 TencentBlueKing#10755
Browse files Browse the repository at this point in the history
  • Loading branch information
carlyin0801 committed Aug 7, 2024
1 parent def9d8d commit 4d6519d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import javax.ws.rs.Consumes
import javax.ws.rs.DELETE
import javax.ws.rs.GET
import javax.ws.rs.HeaderParam
import javax.ws.rs.POST
import javax.ws.rs.PUT
import javax.ws.rs.Path
import javax.ws.rs.PathParam
Expand Down Expand Up @@ -206,4 +207,12 @@ interface UserAtomResource {
@Parameter(description = "卸载插件请求包体", required = true)
unInstallReq: UnInstallReq
): Result<Boolean>

@Operation(summary = "批量获取插件输出信息")
@POST
@Path("/output/info/list")
fun getAtomOutputInfos(
@Parameter(description = "插件信息集合,格式:插件标识@版本号", required = true)
atomInfos: Set<String>
): Result<Map<String, String>?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.tencent.devops.common.api.pojo.Page
import com.tencent.devops.common.api.pojo.Result
import com.tencent.devops.common.web.RestResource
import com.tencent.devops.store.api.atom.UserAtomResource
import com.tencent.devops.store.atom.service.AtomPropService
import com.tencent.devops.store.pojo.atom.AtomBaseInfoUpdateRequest
import com.tencent.devops.store.pojo.atom.AtomResp
import com.tencent.devops.store.pojo.atom.AtomRespItem
Expand All @@ -42,8 +43,10 @@ import com.tencent.devops.store.atom.service.AtomService
import org.springframework.beans.factory.annotation.Autowired

@RestResource
class UserAtomResourceImpl @Autowired constructor(private val atomService: AtomService) :
UserAtomResource {
class UserAtomResourceImpl @Autowired constructor(
private val atomService: AtomService,
private val atomPropService: AtomPropService
) : UserAtomResource {

override fun getPipelineAtom(
projectCode: String,
Expand Down Expand Up @@ -133,4 +136,8 @@ class UserAtomResourceImpl @Autowired constructor(private val atomService: AtomS
): Result<Boolean> {
return atomService.uninstallAtom(userId, projectCode, atomCode, unInstallReq)
}

override fun getAtomOutputInfos(atomInfos: Set<String>): Result<Map<String, String>?> {
return Result(atomPropService.getAtomOutputInfos(atomInfos))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@ interface AtomPropService {
fun getAtomProps(
atomCodes: Set<String>
): Map<String, AtomProp>?

/**
* 获取插件输出信息
* @param atomInfos 插件信息集合,格式:插件标识@版本号
* @return 插件输出信息集合,格式:key-插件标识@版本号, value-插件输出对象
*/
fun getAtomOutputInfos(atomInfos: Set<String>): Map<String, String>?
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,54 @@ package com.tencent.devops.store.atom.service.impl

import com.fasterxml.jackson.core.type.TypeReference
import com.github.benmanes.caffeine.cache.Caffeine
import com.tencent.devops.common.api.constant.CommonMessageCode
import com.tencent.devops.common.api.exception.ErrorCodeException
import com.tencent.devops.common.api.util.JsonUtil
import com.tencent.devops.common.util.RegexUtils
import com.tencent.devops.common.web.utils.I18nUtil
import com.tencent.devops.model.store.tables.TAtom
import com.tencent.devops.store.atom.dao.AtomDao
import com.tencent.devops.store.atom.dao.AtomPropDao
import com.tencent.devops.store.pojo.atom.AtomProp
import com.tencent.devops.store.atom.service.AtomPropService
import com.tencent.devops.store.common.service.StoreI18nMessageService
import com.tencent.devops.store.common.service.action.StoreDecorateFactory
import com.tencent.devops.store.common.utils.StoreUtils
import com.tencent.devops.store.pojo.atom.enums.AtomStatusEnum
import com.tencent.devops.store.pojo.common.ATOM_OUTPUT
import com.tencent.devops.store.pojo.common.enums.StoreTypeEnum
import org.apache.commons.collections4.ListUtils
import org.jooq.DSLContext
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import java.util.concurrent.TimeUnit

@Service
class AtomPropServiceImpl @Autowired constructor(
private val dslContext: DSLContext,
private val atomPropDao: AtomPropDao
private val atomPropDao: AtomPropDao,
private val atomDao: AtomDao,
private val storeI18nMessageService: StoreI18nMessageService
) : AtomPropService {

companion object {
private const val DEFAULT_MAX_QUERY_NUM = 50
}

private val atomPropCache = Caffeine.newBuilder()
.maximumSize(5000)
.expireAfterWrite(6, TimeUnit.HOURS)
.build<String, AtomProp>()

private val atomOutputCache = Caffeine.newBuilder()
.maximumSize(5000)
.expireAfterWrite(6, TimeUnit.HOURS)
.build<String, String>()

@Value("\${store.maxQueryNum:100}")
private val maxQueryNum: Int = DEFAULT_MAX_QUERY_NUM

override fun getAtomProps(atomCodes: Set<String>): Map<String, AtomProp>? {
var atomPropMap: MutableMap<String, AtomProp>? = null
// 从缓存中查找插件属性信息
Expand Down Expand Up @@ -105,4 +129,50 @@ class AtomPropServiceImpl @Autowired constructor(
}
return atomPropMap
}

@Suppress("UNCHECKED_CAST")
override fun getAtomOutputInfos(atomInfos: Set<String>): Map<String, String>? {
// 检查查询的梳理是否超过了系统限制
if (atomInfos.size > maxQueryNum) {
throw ErrorCodeException(
errorCode = CommonMessageCode.ERROR_QUERY_NUM_TOO_BIG, params = arrayOf(maxQueryNum.toString())
)
}
var atomOutputInfoMap: MutableMap<String, String>? = null
atomInfos.forEach { atomInfo ->
// 获取请求用户的语言
val language = I18nUtil.getRequestUserLanguage()
val cacheKey = "$atomInfo@$language"
// 从缓存中获取指定插件输出参数
var outputInfo = atomOutputCache.getIfPresent(cacheKey)
if (atomOutputInfoMap == null) {
atomOutputInfoMap = mutableMapOf()
}
if (outputInfo != null) {
atomOutputInfoMap!![atomInfo] = outputInfo
} else {
// 获取插件标识和版本号
val arrays = atomInfo.split("@")
val atomCode = arrays[0]
val version = arrays[1]
val atomRecord = atomDao.getPipelineAtom(dslContext, atomCode, version) ?: return@forEach
val propMap = JsonUtil.toMap(atomRecord.props)
val outputDataMap = propMap[ATOM_OUTPUT] as? Map<String, Any>
if (outputDataMap.isNullOrEmpty()) {
return@forEach
}
outputInfo = storeI18nMessageService.parseJsonStrI18nInfo(
jsonStr = JsonUtil.toJson(outputDataMap), keyPrefix = StoreUtils.getStoreFieldKeyPrefix(
storeType = StoreTypeEnum.ATOM, storeCode = atomCode, version = atomRecord.version
)
)
atomOutputInfoMap!![atomInfo] = outputInfo
if (AtomStatusEnum.getProcessingStatusList().contains(atomRecord.atomStatus)) {
// 把状态为非流程中状态的插件版本输出信息放入缓存
atomOutputCache.put(cacheKey, outputInfo)
}
}
}
return atomOutputInfoMap
}
}

0 comments on commit 4d6519d

Please sign in to comment.