Skip to content

Commit

Permalink
【PAC】feat:支持从代码库维度查看对应的代码库事件 #8122
Browse files Browse the repository at this point in the history
  • Loading branch information
hejieehe committed Nov 1, 2023
1 parent 9081f97 commit 80786fc
Showing 1 changed file with 167 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import com.tencent.devops.process.pojo.trigger.RepoTriggerEventVo
import org.jooq.Condition
import org.jooq.DSLContext
import org.jooq.Result
import org.jooq.impl.DSL
import org.jooq.impl.DSL.count
import org.jooq.impl.DSL.`when`
import org.springframework.stereotype.Repository
Expand Down Expand Up @@ -261,41 +260,64 @@ class PipelineTriggerEventDao {
): List<RepoTriggerEventVo> {
val t1 = TPipelineTriggerEvent.T_PIPELINE_TRIGGER_EVENT.`as`("t1")
val t2 = TPipelineTriggerDetail.T_PIPELINE_TRIGGER_DETAIL.`as`("t2")
val conditions = buildConditions(
t1 = t1,
t2 = t2,
val buildDetailCondition = buildDetailCondition(
t2,
projectId = projectId,
pipelineName = pipelineName,
pipelineId = pipelineId,
eventId = eventId
)
val buildEventCondition = buildEventCondition(
t1,
projectId = projectId,
eventSource = eventSource,
eventId = eventId,
pipelineName = pipelineName,
eventType = eventType,
triggerUser = triggerUser,
triggerType = triggerType,
pipelineId = pipelineId,
startTime = startTime,
endTime = endTime
)
// 总数
val totalCondition = if (pipelineName.isNullOrBlank()) {
count()
} else {
count(`when`(t2.PIPELINE_NAME.like("%$pipelineName%"), 1))
}.`as`("total")
// 事件信息
val eventInfo = with(t1) {
dslContext.select(
PROJECT_ID,
EVENT_ID,
EVENT_SOURCE,
EVENT_DESC,
CREATE_TIME
)
.from(this)
.where(buildEventCondition)
.orderBy(CREATE_TIME.desc())
.limit(limit)
.offset(offset)
}
// 详情信息
val detailInfo = with(t2) {
dslContext.select(
EVENT_ID,
PROJECT_ID,
count().`as`("total"),
count(`when`(STATUS.eq(PipelineTriggerStatus.SUCCEED.name), 1)).`as`("success")
)
.from(t2)
.where(buildDetailCondition)
.groupBy(PROJECT_ID, EVENT_ID)
}
return dslContext.select(
t1.PROJECT_ID,
t1.EVENT_ID,
t1.EVENT_SOURCE,
t1.EVENT_DESC,
t1.CREATE_TIME,
totalCondition,
count(`when`(t2.STATUS.eq(PipelineTriggerStatus.SUCCEED.name), 1)).`as`("success ")
).from(t1).leftJoin(t2)
detailInfo.field("total", Int::class.java),
detailInfo.field("success", Int::class.java)
)
.from(eventInfo)
.leftJoin(detailInfo)
.on(t1.EVENT_ID.eq(t2.EVENT_ID)).and(t1.PROJECT_ID.eq(t2.PROJECT_ID))
.where(conditions)
.groupBy(t1.PROJECT_ID, t1.EVENT_ID, t1.EVENT_SOURCE, t1.EVENT_DESC, t1.CREATE_TIME)
.orderBy(t1.CREATE_TIME.desc())
.limit(limit)
.offset(offset)
.where(detailInfo.field("total", Int::class.java)?.gt(0))
.fetch().map {
RepoTriggerEventVo(
projectId = it.value1(),
Expand Down Expand Up @@ -324,25 +346,53 @@ class PipelineTriggerEventDao {
): Long {
val t1 = TPipelineTriggerEvent.T_PIPELINE_TRIGGER_EVENT.`as`("t1")
val t2 = TPipelineTriggerDetail.T_PIPELINE_TRIGGER_DETAIL.`as`("t2")
val conditions = buildConditions(
t1 = t1,
t2 = t2,
val buildDetailCondition = buildDetailCondition(
t2,
projectId = projectId,
pipelineName = pipelineName,
pipelineId = pipelineId,
eventId = eventId
)
val buildEventCondition = buildEventCondition(
t1,
projectId = projectId,
eventSource = eventSource,
eventId = eventId,
pipelineName = pipelineName,
eventType = eventType,
triggerUser = triggerUser,
triggerType = triggerType,
pipelineId = pipelineId,
startTime = startTime,
endTime = endTime
)
return dslContext.select(DSL.countDistinct(t1.PROJECT_ID, t1.EVENT_ID))
.from(t1)
.leftJoin(t2)
// 事件信息
val eventInfo = with(t1) {
dslContext.select(
PROJECT_ID,
EVENT_ID,
EVENT_SOURCE,
EVENT_DESC,
CREATE_TIME
)
.from(this)
.where(buildEventCondition)
.orderBy(CREATE_TIME.desc())
}
// 详情信息
val detailInfo = with(t2) {
dslContext.select(
EVENT_ID,
PROJECT_ID,
count().`as`("total")
)
.from(t2)
.where(buildDetailCondition)
.groupBy(PROJECT_ID, EVENT_ID)
}
return dslContext.selectCount()
.from(eventInfo)
.leftJoin(detailInfo)
.on(t1.EVENT_ID.eq(t2.EVENT_ID)).and(t1.PROJECT_ID.eq(t2.PROJECT_ID))
.where(conditions)
.where(detailInfo.field("total", Int::class.java)?.gt(0))
.fetchOne(0, Long::class.java)!!
}

Expand Down Expand Up @@ -390,6 +440,72 @@ class PipelineTriggerEventDao {
return record?.let { convertDetail(it) }
}

private fun buildDetailCondition(
t2: TPipelineTriggerDetail,
eventId: Long? = null,
pipelineName: String? = null,
projectId: String,
pipelineId: String? = null
): List<Condition> {
val conditions = mutableListOf<Condition>()
with(t2) {
if (eventId != null) {
conditions.add(EVENT_ID.eq(eventId))
}
if (!pipelineName.isNullOrBlank()) {
conditions.add(PIPELINE_NAME.like("%$pipelineName%"))
}
if (projectId.isNotBlank()) {
conditions.add(PROJECT_ID.eq(projectId))
}
if (!pipelineId.isNullOrBlank()) {
conditions.add(PIPELINE_ID.eq(pipelineId))
}
}
return conditions
}

private fun buildEventCondition(
t1: TPipelineTriggerEvent,
eventId: Long? = null,
eventSource: String? = null,
projectId: String,
eventType: String?,
triggerUser: String? = null,
triggerType: String? = null,
startTime: Long? = null,
endTime: Long? = null
): List<Condition> {
val conditions = mutableListOf<Condition>()
with(t1) {
if (eventId != null) {
conditions.add(EVENT_ID.eq(eventId))
}
if (!eventSource.isNullOrBlank()) {
conditions.add(EVENT_SOURCE.eq(eventSource))
}
if (projectId.isNotBlank()) {
conditions.add(PROJECT_ID.eq(projectId))
}
if (!eventType.isNullOrBlank()) {
conditions.add(EVENT_TYPE.eq(eventType))
}
if (!triggerUser.isNullOrBlank()) {
conditions.add(TRIGGER_USER.eq(triggerUser))
}
if (!triggerType.isNullOrBlank()) {
conditions.add(TRIGGER_TYPE.eq(triggerType))
}
if (startTime != null) {
conditions.add(CREATE_TIME.ge(Timestamp(startTime).toLocalDateTime()))
}
if (endTime != null) {
conditions.add(CREATE_TIME.le(Timestamp(endTime).toLocalDateTime()))
}
}
return conditions
}

private fun buildConditions(
t1: TPipelineTriggerEvent,
t2: TPipelineTriggerDetail,
Expand All @@ -405,34 +521,28 @@ class PipelineTriggerEventDao {
endTime: Long? = null
): List<Condition> {
val conditions = mutableListOf<Condition>()
conditions.add(t1.PROJECT_ID.eq(projectId))
if (!eventSource.isNullOrBlank()) {
conditions.add(t1.EVENT_SOURCE.eq(eventSource))
}
if (eventId != null) {
conditions.add(t1.EVENT_ID.eq(eventId))
}
if (!eventType.isNullOrBlank()) {
conditions.add(t1.EVENT_TYPE.eq(eventType))
}
if (!triggerUser.isNullOrBlank()) {
conditions.add(t1.TRIGGER_USER.eq(triggerUser))
}
if (!triggerType.isNullOrBlank()) {
conditions.add(t1.TRIGGER_TYPE.eq(triggerType))
}
if (!pipelineId.isNullOrBlank()) {
conditions.add(t2.PIPELINE_ID.eq(pipelineId))
}
if (startTime != null && startTime > 0) {
conditions.add(t1.CREATE_TIME.ge(Timestamp(startTime).toLocalDateTime()))
}
if (endTime != null && endTime > 0) {
conditions.add(t1.CREATE_TIME.le(Timestamp(endTime).toLocalDateTime()))
}
if (!pipelineName.isNullOrBlank()) {
conditions.add(t2.PIPELINE_NAME.like("%$pipelineName%"))
}
conditions.addAll(
buildEventCondition(
t1,
eventId = eventId,
eventSource = eventSource,
projectId = projectId,
eventType = eventType,
triggerType = triggerType,
triggerUser = triggerUser,
startTime = startTime,
endTime = endTime
)
)
conditions.addAll(
buildDetailCondition(
t2,
eventId = eventId,
projectId = projectId,
pipelineName = pipelineName,
pipelineId = pipelineId
)
)
return conditions
}

Expand Down

0 comments on commit 80786fc

Please sign in to comment.