Skip to content

Commit

Permalink
perf: job-analysis部分代码优化 TencentBlueKing#3322
Browse files Browse the repository at this point in the history
1. 重构部分代码,增加部分注释。
  • Loading branch information
jsonwan committed Dec 12, 2024
1 parent 49546de commit 1acef85
Show file tree
Hide file tree
Showing 33 changed files with 416 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ allprojects {
}

repositories {
mavenCentral()
mavenLocal()

// 用户通过命令行方式指定的 maven repo
Expand All @@ -198,7 +199,6 @@ allprojects {
maven { url repoUrl }
}
}
mavenCentral()
maven {
url "https://repo.spring.io/snapshot"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class AIAnalyzeErrorReq {
private Long taskInstanceId;

@ApiModelProperty("步骤执行类型:1-脚本,2-文件")
// @NotNull(message = "{validation.constraints.AIAnalyzeError_stepExecuteTypeEmpty.message}")
private Integer stepExecuteType;

@ApiModelProperty(value = "步骤ID")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import com.tencent.bk.job.analysis.model.web.req.AIAnalyzeErrorReq;
import com.tencent.bk.job.analysis.model.web.resp.AIChatRecord;

/**
* AI分析报错信息服务
*/
public interface AIAnalyzeErrorService {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

import java.util.List;

/**
* AI聊天记录服务
*/
public interface AIChatHistoryService {
/**
* 构建AI聊天记录
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

import com.tencent.bk.job.analysis.model.web.resp.AIChatRecord;

/**
* AI脚本检查服务
*/
public interface AICheckScriptService {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

/**
* AI核心能力服务
*/
public interface AIService {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

import java.util.List;

/**
* AI聊天服务
*/
public interface ChatService {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

import com.tencent.bk.job.analysis.model.dto.AIPromptDTO;

/**
* 检查脚本的AI提示符服务
*/
public interface CheckScriptAIPromptService {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import com.tencent.bk.job.analysis.model.dto.AIPromptDTO;
import com.tencent.bk.job.analysis.service.ai.context.model.FileTaskContext;

/**
* 文件分发任务报错信息AI提示符服务
*/
public interface FileTransferTaskErrorAIPromptService {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import com.tencent.bk.job.analysis.model.dto.AIPromptDTO;
import com.tencent.bk.job.analysis.service.ai.context.model.ScriptTaskContext;

/**
* 脚本执行任务报错信息的AI提示符服务
*/
public interface ScriptExecuteTaskErrorAIPromptService {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import com.tencent.bk.job.analysis.service.ai.context.model.TaskContext;
import com.tencent.bk.job.analysis.service.ai.context.model.TaskContextQuery;

/**
* 任务上下文服务接口
*/
public interface TaskContextService {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ public FileTaskContextService(ServiceLogResource logResource,
this.fileTaskFailLogAnalyzer = fileTaskFailLogAnalyzer;
}

/**
* 根据步骤实例与上下文查询条件获取对应的任务上下文
*
* @param stepInstance 步骤实例
* @param contextQuery 上下文查询条件
* @return 任务上下文
*/
public TaskContext getTaskContext(ServiceStepInstanceDTO stepInstance, TaskContextQuery contextQuery) {
String jobCreateDate = LogFieldUtil.buildJobCreateDate(stepInstance.getCreateTime());
// 上传日志
Expand Down Expand Up @@ -106,6 +113,14 @@ public TaskContext getTaskContext(ServiceStepInstanceDTO stepInstance, TaskConte
return buildContextForFileTask(stepInstance, uploadExecuteObjectLogList, downloadExecuteObjectLog);
}

/**
* 构建文件任务上下文
*
* @param stepInstance 步骤实例
* @param uploadExecuteObjectLogList 上传执行对象日志
* @param downloadExecuteObjectLog 下载执行对象日志
* @return 任务上下文
*/
private TaskContext buildContextForFileTask(ServiceStepInstanceDTO stepInstance,
List<ServiceExecuteObjectLogDTO> uploadExecuteObjectLogList,
ServiceExecuteObjectLogDTO downloadExecuteObjectLog) {
Expand Down Expand Up @@ -139,13 +154,25 @@ private TaskContext buildContextForFileTask(ServiceStepInstanceDTO stepInstance,
return new TaskContext(stepInstance.getExecuteType(), stepInstance.getStatus(), null, fileTaskContext);
}

/**
* 判断是否为上传失败日志
*
* @param fileTaskLog 文件任务日志
* @return 是否为上传失败日志
*/
private boolean isUploadFailLog(ServiceFileTaskLogDTO fileTaskLog) {
Integer mode = fileTaskLog.getMode();
Integer status = fileTaskLog.getStatus();
return FileTaskModeEnum.UPLOAD.getValue().equals(mode)
&& FileDistStatusEnum.FAILED.getValue().equals(status);
}

/**
* 判断是否为下载失败日志
*
* @param fileTaskLog 文件任务日志
* @return 是否为下载失败日志
*/
private boolean isDownloadFailLog(ServiceFileTaskLogDTO fileTaskLog) {
Integer mode = fileTaskLog.getMode();
Integer status = fileTaskLog.getStatus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public TaskContextServiceImpl(ServiceStepInstanceResource serviceStepInstanceRes
this.fileTaskContextService = fileTaskContextService;
}

/**
* 根据用户名与上下文查询条件获取对应的任务上下文
*
* @param username 用户名
* @param contextQuery 上下文查询条件
* @return 任务上下文
*/
@Override
public TaskContext getTaskContext(String username, TaskContextQuery contextQuery) {
InternalResponse<ServiceStepInstanceDTO> resp = serviceStepInstanceResource.getStepInstance(
Expand All @@ -81,6 +88,12 @@ public TaskContext getTaskContext(String username, TaskContextQuery contextQuery
throw new ServiceException(resp.getErrorMsg(), ErrorType.valOf(resp.getErrorType()), resp.getCode());
}

/**
* 构建脚本任务上下文
*
* @param stepInstance 步骤实例
* @return 脚本任务上下文
*/
private TaskContext buildContextForScriptTask(ServiceStepInstanceDTO stepInstance) {
ServiceScriptStepInstanceDTO scriptStepInstance = stepInstance.getScriptStepInstance();
ScriptTaskContext scriptTaskContext = new ScriptTaskContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@

import java.util.function.Consumer;

/**
* 互相绑定的异步任务消费者与流式响应体对
*/
@AllArgsConstructor
@Data
public class AsyncConsumerAndProducerPair {
public class AsyncConsumerAndStreamingResponseBodyPair {
/**
* 消费者
* 消费者,消费其他数据源产生的数据,将其写入消息队列
*/
Consumer<String> consumer;
/**
* 生产者
* 流式响应体,读取消息队列中的数据并将其写入输出流中
*/
StreamingResponseBody producer;
StreamingResponseBody streamingResponseBody;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
import java.util.List;
import java.util.Map;

/**
* 文件任务上下文
*/
@AllArgsConstructor
@Data
public class FileTaskContext {
Expand All @@ -55,7 +58,9 @@ public class FileTaskContext {
* 文件分发任务错误根源分析结果
*/
FileTaskErrorSourceResult errorSourceResult;

/**
* 执行对象表,Key为执行对象ID,Value为执行对象
*/
Map<String, ServiceExecuteObject> executeObjectMap;

public FileTaskContext(String name,
Expand All @@ -66,6 +71,9 @@ public FileTaskContext(String name,
this.errorSourceResult = errorSourceResult;
}

/**
* 根据文件步骤实例信息构建执行对象表
*/
private void buildExecuteObjectMapIfNeeded() {
if (executeObjectMap != null) {
return;
Expand Down Expand Up @@ -100,10 +108,20 @@ private void buildExecuteObjectMapIfNeeded() {
}
}

/**
* 获取文件任务错误根源的国际化Key
*
* @return 国际化Key
*/
public String getFileTaskErrorSourceI18nKey() {
return errorSourceResult.getErrorSource().getI18nKey();
}

/**
* 获取上传文件错误数据
*
* @return 错误数据
*/
@SuppressWarnings("DuplicatedCode")
public String getUploadFileErrorData() {
buildExecuteObjectMapIfNeeded();
Expand Down Expand Up @@ -137,6 +155,11 @@ public String getUploadFileErrorData() {
return JsonUtils.toJson(uploadFileErrorInfoList);
}

/**
* 获取下载文件错误数据
*
* @return 错误数据
*/
@SuppressWarnings("DuplicatedCode")
public String getDownloadFileErrorData() {
buildExecuteObjectMapIfNeeded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public class ScriptTaskContext {
*/
private boolean secureParam;

/**
* 获取脱敏后的脚本参数
*
* @return 脱敏后的脚本参数
*/
public String getInsensitiveScriptParamsStr() {
if (!secureParam) {
return scriptParams == null ? "" : scriptParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import lombok.AllArgsConstructor;
import lombok.Data;

/**
* 任务上下文,用作动态数据填充模板,进而构建向AI提问的Prompt
*/
@AllArgsConstructor
@Data
public class TaskContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* 通过AI分析任务报错信息的服务实现类
*/
@Slf4j
@Service
public class AIAnalyzeErrorServiceImpl extends AIBaseService implements AIAnalyzeErrorService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package com.tencent.bk.job.analysis.service.ai.impl;

import com.tencent.bk.job.analysis.model.web.resp.AIAnswer;
import com.tencent.bk.job.analysis.service.ai.context.model.AsyncConsumerAndProducerPair;
import com.tencent.bk.job.analysis.service.ai.context.model.AsyncConsumerAndStreamingResponseBodyPair;
import com.tencent.bk.job.analysis.service.ai.context.model.MessagePartEvent;
import com.tencent.bk.job.analysis.util.ai.AIAnswerUtil;
import com.tencent.bk.job.common.constant.ErrorCode;
Expand All @@ -43,7 +43,7 @@

/**
* AI回答流式响应同步器
* 使用阻塞队列构建异步消费者与生产者组合,消费者读取到数据后立即提供给生产者进行输出
* 使用阻塞队列构建互相绑定的异步消费者与流式响应体组合,消费者从流式数据源读取到数据后将其写入到阻塞队列中并提供给流式响应体进行读取输出
*/
@Slf4j
public class AIAnswerStreamSynchronizer {
Expand All @@ -61,11 +61,11 @@ public AIAnswerStreamSynchronizer(int capacity) {
}

/**
* 构建异步消费者与生产者组合
* 构建异步消费者与流式响应体组合
*
* @return 异步消费者与生产者组合
* @return 异步消费者与流式响应体组合
*/
public AsyncConsumerAndProducerPair buildAsyncConsumerAndProducerPair() {
public AsyncConsumerAndStreamingResponseBodyPair buildAsyncConsumerAndStreamingResponseBodyPair() {
StreamingResponseBody streamingResponseBody = outputStream -> {
while (!isFinished.get()) {
try {
Expand Down Expand Up @@ -111,11 +111,11 @@ public AsyncConsumerAndProducerPair buildAsyncConsumerAndProducerPair() {
outputStream.close();
};
Consumer<String> partialRespConsumer = new AIMessagePartConsumer(messageQueue);
return new AsyncConsumerAndProducerPair(partialRespConsumer, streamingResponseBody);
return new AsyncConsumerAndStreamingResponseBodyPair(partialRespConsumer, streamingResponseBody);
}

/**
* 触发结束事件,消费者读取完数据后,触发生产者停止生产并清理
* 触发结束事件,消费者读取完数据后,通知流式响应体做出相应的停止输出动作并清理
*
* @param throwable 异常
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import com.tencent.bk.job.common.util.JobContextUtil;
import lombok.extern.slf4j.Slf4j;

/**
* 基础AI提示符服务
*/
@Slf4j
public class AIBasePromptService {

Expand All @@ -40,6 +43,12 @@ public AIBasePromptService(AIPromptTemplateDAO aiPromptTemplateDAO) {
this.aiPromptTemplateDAO = aiPromptTemplateDAO;
}

/**
* 根据模板代码获取提示符模板
*
* @param templateCode 模板代码
* @return AI提示符模板
*/
protected AIPromptTemplateDTO getPromptTemplate(String templateCode) {
String userLang = JobContextUtil.getUserLang();
AIPromptTemplateDTO promptTemplate = aiPromptTemplateDAO.getAIPromptTemplate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* AI基础服务
*/
@Slf4j
@Service
public class AIBaseService {
Expand Down
Loading

0 comments on commit 1acef85

Please sign in to comment.