Skip to content

Commit

Permalink
feat: add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
rchen9 committed Jun 24, 2024
1 parent b744f58 commit f52d1a3
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.arextest.common.model.response.Response;
import com.arextest.common.model.response.ResponseCode;
import com.arextest.common.utils.ResponseUtils;
import com.arextest.web.common.LogUtils;
import com.arextest.web.common.LogUtils.LogTagKeySummary;
import com.arextest.web.core.business.DiffSceneService;
import com.arextest.web.core.business.MsgShowService;
import com.arextest.web.core.business.QueryPlanItemStatisticService;
Expand Down Expand Up @@ -68,6 +70,10 @@
import com.arextest.web.model.contract.contracts.replay.AnalyzeCompareResultsResponseType;
import com.arextest.web.model.contract.contracts.replay.UpdateReportInfoRequestType;
import com.arextest.web.model.contract.contracts.replay.UpdateReportInfoResponseType;
import com.google.common.collect.ImmutableMap;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
Expand All @@ -79,10 +85,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Slf4j
@Controller
@RequestMapping("/api/report/")
Expand Down Expand Up @@ -222,7 +224,8 @@ public Response queryPlanItemStatistics(@PathVariable String planItemId) {

@PostMapping("/deletePlanItemStatistics")
@ResponseBody
public Response deletePlanItemStatistics(@RequestBody DeletePlanItemStatisticsRequestType request) {
public Response deletePlanItemStatistics(
@RequestBody DeletePlanItemStatisticsRequestType request) {
SuccessResponse response = new SuccessResponse();
response.setSuccess(reportService.deletePlanItemStatistic(request.getPlanItemIds()));
return ResponseUtils.successResponse(response);
Expand Down Expand Up @@ -312,19 +315,21 @@ public void downloadReplayMsg(@Valid @RequestBody DownloadReplayMsgRequestType r
@GetMapping("/delete/{planId}")
@ResponseBody
public Response deleteReport(@PathVariable String planId) {
ImmutableMap<String, String> logTag = ImmutableMap.of(
LogTagKeySummary.PLAN_ID, planId);
try {
SuccessResponseType response = new SuccessResponseType();
String stopRes = scheduleService.stopPlan(planId);
if (StringUtils.isEmpty(stopRes)) {
LOGGER.error("stop plan error, planId:{}", planId);
LogUtils.error(LOGGER, logTag, "stop plan error, planId:{}", planId);
return ResponseUtils.errorResponse("stop plan error",
ResponseCode.REQUESTED_HANDLE_EXCEPTION);
}
LOGGER.info("stop plan success, {}", stopRes);
LogUtils.info(LOGGER, logTag, "stop plan success, {}", stopRes);
response.setSuccess(reportService.deleteReport(planId));
return ResponseUtils.successResponse(response);
} catch (Throwable t) {
LOGGER.error("delete plan error", t);
LogUtils.error(LOGGER, logTag, "delete plan error", t);
return ResponseUtils.errorResponse(t.getMessage(), ResponseCode.REQUESTED_HANDLE_EXCEPTION);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.arextest.common.model.response.Response;
import com.arextest.common.model.response.ResponseCode;
import com.arextest.common.utils.ResponseUtils;
import com.arextest.web.common.LogUtils;
import com.arextest.web.core.business.SystemConfigurationService;
import com.arextest.web.model.contract.contracts.config.SaveSystemConfigRequestType;
import com.arextest.web.model.contract.contracts.config.SystemConfigWithProperties;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
Expand Down Expand Up @@ -42,8 +44,10 @@ public Response saveSystemConfig(@RequestBody SaveSystemConfigRequestType reques
@GetMapping("/list")
@ResponseBody
public Response listSystemConfig() {
SystemConfigWithProperties systemConfigWithProperties = systemConfigurationService.listSystemConfig();
LogUtils.info(LOGGER, "listSystemConfig:{}", systemConfigWithProperties);
return ResponseUtils.successResponse(
systemConfigurationService.listSystemConfig()
systemConfigWithProperties
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,19 @@ public static void error(Logger logger, Map<String, String> tags, String s, Thro
CommonMetrics.incErrorCount(AREX_API, NetworkInterfaceManager.INSTANCE.getLocalHostAddress());
clear();
}

public static class LogTagKeySummary {

public static final String PLAN_ID = "planId";

public static final String PLAN_ITEM_ID = "planItemId";

public static final String RECORD_ID = "recordId";

public static final String REPLAY_ID = "replayId";

public static final String PLAN_STATUS = "planStatus";

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public boolean initPlan(ReportInitialRequestType request) {
if (request == null) {
return false;
}

ImmutableMap<String, String> logTag = ImmutableMap.of("planId", request.getPlanId());
LogUtils.info(LOGGER, logTag, "init the plan, request: {}", request);

try {
ReportPlanStatisticDto reportPlanStatisticDto = PlanMapper.INSTANCE.dtoFromContract(request);
reportPlanStatisticDto.setDataChangeCreateTime(System.currentTimeMillis());
Expand All @@ -50,16 +54,15 @@ public boolean initPlan(ReportInitialRequestType request) {
});
}
} catch (Exception e) {
LogUtils.error(LOGGER, "updateReplayBaseInfo", e);
LogUtils.error(LOGGER, logTag, "updateReplayBaseInfo", e);
return false;
}
return true;
}

public boolean updatePlan(UpdateReportInfoRequestType request) {
LogUtils.info(LOGGER, ImmutableMap.of("planId", request.getPlanId()),
"update the info of plan, request: {}",
request);
ImmutableMap<String, String> logTag = ImmutableMap.of("planId", request.getPlanId());
LogUtils.info(LOGGER, logTag, "update the info of plan, request: {}", request);
try {
ReportPlanStatisticDto reportPlanStatisticDto = PlanMapper.INSTANCE.dtoFromContract(request);
reportPlanStatisticRepository.findAndModifyBaseInfo(reportPlanStatisticDto);
Expand All @@ -74,7 +77,7 @@ public boolean updatePlan(UpdateReportInfoRequestType request) {
});
}
} catch (Exception e) {
LogUtils.error(LOGGER, "updatePlan", e);
LogUtils.error(LOGGER, logTag, "updatePlan", e);
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@

import com.arextest.model.mock.MockCategoryType;
import com.arextest.web.common.LogUtils;
import com.arextest.web.core.business.iosummary.SceneReportService;
import com.arextest.web.common.LogUtils.LogTagKeySummary;
import com.arextest.web.core.business.iosummary.SummaryService;
import com.arextest.web.core.business.listener.planfinish.PlanFinishedService;
import com.arextest.web.core.repository.ReplayCompareResultRepository;
import com.arextest.web.core.repository.ReportDiffAggStatisticRepository;
import com.arextest.web.core.repository.ReportPlanItemStatisticRepository;
import com.arextest.web.core.repository.ReportPlanStatisticRepository;
import com.arextest.web.model.contract.contracts.ChangeReplayStatusRequestType;
import com.arextest.web.model.contract.contracts.PushCompareResultsRequestType;
import com.arextest.web.model.contract.contracts.RemoveErrorMsgRequest;
import com.arextest.web.model.contract.contracts.RemoveRecordsAndScenesRequest;
import com.arextest.web.model.contract.contracts.common.CompareResult;
import com.arextest.web.model.contract.contracts.replay.AnalyzeCompareResultsRequestType;
import com.arextest.web.model.dto.CompareResultDto;
import com.arextest.web.model.dto.PlanItemDto;
import com.arextest.web.model.dto.ReportPlanStatisticDto;
import com.arextest.web.model.enums.ReplayStatusType;
import com.arextest.web.model.mapper.CompareResultMapper;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -35,6 +34,7 @@
@Slf4j
@Component
public class ReportService {

@Resource
private ReplayCompareResultRepository replayCompareResultRepository;
@Resource
Expand All @@ -57,6 +57,16 @@ public boolean analyzeCompareResults(AnalyzeCompareResultsRequestType request) {
.map(CompareResultMapper.INSTANCE::dtoFromAnalyzeContract).collect(Collectors.toList());

if (CollectionUtils.isNotEmpty(results)) {
CompareResultDto compareResultDto = results.get(0);
LogUtils.info(LOGGER,
ImmutableMap.of(
LogTagKeySummary.PLAN_ID, compareResultDto.getPlanId(),
LogTagKeySummary.PLAN_ITEM_ID, compareResultDto.getPlanItemId(),
LogTagKeySummary.RECORD_ID, compareResultDto.getRecordId(),
LogTagKeySummary.REPLAY_ID, compareResultDto.getReplayId()
),
"analyze compare results"
);
// save caseSummary to db
summaryService.analysis(results);
statisticService.statisticPlanItems(results);
Expand All @@ -65,13 +75,16 @@ public boolean analyzeCompareResults(AnalyzeCompareResultsRequestType request) {
}

public boolean changeReportStatus(ChangeReplayStatusRequestType request) {
String planId = request.getPlanId();
LogUtils.info(LOGGER, ImmutableMap.of(LogTagKeySummary.PLAN_ID, planId),
"change report status, request: {}",
request);
if (request.getStatus() == ReplayStatusType.FINISHED) {
ReportPlanStatisticDto plan = planStatisticRepository.findByPlanId(request.getPlanId());
int retryTimes = 3;
boolean match = false;
for (int i = 0; i < retryTimes; i++) {
int count = replayCompareResultRepository.queryCompareResultCountByPlanId(
request.getPlanId());
int count = replayCompareResultRepository.queryCompareResultCountByPlanId(planId);
if (!Objects.equals(count, plan.getTotalCaseCount())) {
try {
Thread.sleep(6000);
Expand All @@ -84,7 +97,8 @@ public boolean changeReportStatus(ChangeReplayStatusRequestType request) {
}
}
if (!match) {
LogUtils.error(LOGGER, "The number of received cases does not match the declaration.");
LogUtils.error(LOGGER, ImmutableMap.of(LogTagKeySummary.PLAN_ID, planId),
"The number of received cases does not match the declaration.");
}
}
ReportPlanStatisticDto planDto = planStatisticRepository.changePlanStatus(request.getPlanId(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package com.arextest.web.core.business.listener.planfinish;

import com.arextest.web.common.LogUtils;
import com.arextest.web.common.LogUtils.LogTagKeySummary;
import com.google.common.collect.ImmutableMap;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
* created by xinyuan_wang on 2023/1/12
*/
@Slf4j
@Component
public class PlanFinishedService {

@Autowired
private List<PlanFinishedLinstener> planFinishedLinsteners;

public void onPlanFinishEvent(String appId, String planId, Integer status) {
LogUtils.info(LOGGER,
ImmutableMap.of(
LogTagKeySummary.PLAN_ID, planId,
LogTagKeySummary.PLAN_STATUS, String.valueOf(status)
),
"Plan finished event, appId: {}, planId: {}, status: {}", appId, planId, status);

if (CollectionUtils.isEmpty(this.planFinishedLinsteners)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.util.List;
import javax.validation.constraints.NotBlank;
import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class ChangeReplayStatusRequestType {

@NotBlank(message = "Plan id cannot be empty")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.util.List;
import java.util.Map;
import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class ReportInitialRequestType {

private String planId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.arextest.web.model.contract.contracts.config;

import com.arextest.config.model.dto.system.SystemConfiguration;
import java.util.Set;
import lombok.Data;
import lombok.ToString;

/**
* @author wildeslam.
* @create 2024/2/20 17:45
*/
@Data
@ToString(callSuper = true)
public class SystemConfigWithProperties extends SystemConfiguration {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import java.util.List;
import javax.validation.constraints.NotBlank;
import lombok.Data;
import lombok.ToString;

/**
* Created by rchen9 on 2023/6/2.
*/
@Data
@ToString
public class UpdateReportInfoRequestType {

@NotBlank(message = "planId cannot be empty")
Expand Down

0 comments on commit f52d1a3

Please sign in to comment.