Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: job-analysis部分代码优化 #3322 #3329

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class StatisticsDTO implements Cloneable {
*/
private String date;
/**
* 统计值
* 序列化的统计数据
*/
private String value;
/**
Expand All @@ -74,6 +74,10 @@ public class StatisticsDTO implements Cloneable {
*/
private Long lastModifyTime;

public StatisticsDTO(String value) {
this.value = value;
}

@Override
public StatisticsDTO clone() {
StatisticsDTO statisticsDTO = new StatisticsDTO();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public class CommonStatisticWithRateVO {
private Float momRate;

@ApiModelProperty("同比趋势:1:上升,0:不变,-1:下降")
private Long yoyTrend;
private Integer yoyTrend;

@ApiModelProperty("环比趋势:1:上升,0:不变,-1:下降")
private Long momTrend;
private Integer momTrend;

}
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
@@ -0,0 +1,52 @@
/*
* Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-JOB蓝鲸智云作业平台 is licensed under the MIT License.
*
* License for BK-JOB蓝鲸智云作业平台:
* --------------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.analysis.consts;

import lombok.Getter;

/**
* 数据趋势
*/
@Getter
public enum DataTrendEnum {
/**
* 上升
*/
UP(1),
/**
* 不变
*/
NOT_CHANGE(0),
/**
* 下降
*/
DOWN(-1);

private final int value;

DataTrendEnum(int value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public AppStatisticService(StatisticsDAO statisticsDAO, StatisticConfig statisti
public CommonStatisticWithRateVO calcAppMomYoyStatistic(StatisticsDTO statisticsDTO,
StatisticsDTO momStatisticsDTO,
StatisticsDTO yoyStatisticsDTO) {
return new AppMomYoyCalculator(statisticsDTO, momStatisticsDTO, yoyStatisticsDTO).getResult();
return new AppMomYoyCalculator(statisticsDTO, momStatisticsDTO, yoyStatisticsDTO).calc();
}

public CommonStatisticWithRateVO getAppTotalStatistics(String username, List<Long> appIdList, String date) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public List<Long> getJoinedAppIdList(String date) {
*/
public CommonStatisticWithRateVO calcMomYoyStatistic(StatisticsDTO statisticsDTO, StatisticsDTO momStatisticsDTO,
StatisticsDTO yoyStatisticsDTO) {
return new SimpleMomYoyCalculator(statisticsDTO, momStatisticsDTO, yoyStatisticsDTO).getResult();
return new SimpleMomYoyCalculator(statisticsDTO, momStatisticsDTO, yoyStatisticsDTO).calc();
}

public CommonStatisticWithRateVO getCommonTotalStatistics(TotalMetricEnum metric, List<Long> appIdList,
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
Loading
Loading