All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arextest.schedule.service.ReplayReportService Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package com.arextest.schedule.service;

import com.arextest.common.model.response.GenericResponseType;
import com.arextest.common.model.response.Response;
import com.arextest.common.model.response.ResponseStatusType;
import com.arextest.diff.model.enumeration.DiffResultCode;
import com.arextest.diff.sdk.CompareSDK;
import com.arextest.model.mock.MockCategoryType;
import com.arextest.schedule.client.HttpWepServiceApiClient;
import com.arextest.schedule.comparer.ComparisonWriter;
import com.arextest.schedule.dao.mongodb.ReplayCompareResultRepositoryImpl;
import com.arextest.schedule.model.ReplayActionCaseItem;
import com.arextest.schedule.model.ReplayActionItem;
import com.arextest.schedule.model.ReplayCompareResult;
import com.arextest.schedule.model.ReplayPlan;
import com.arextest.schedule.model.ReplayStatusType;
import com.arextest.schedule.model.converter.ReplayCompareResultConverter;
import com.arextest.web.model.contract.contracts.ChangeReplayStatusRequestType;
import com.arextest.web.model.contract.contracts.DeletePlanItemStatisticsRequestType;
import com.arextest.web.model.contract.contracts.QueryPlanStatisticRequestType;
import com.arextest.web.model.contract.contracts.QueryPlanStatisticResponseType;
import com.arextest.web.model.contract.contracts.RemoveErrorMsgRequest;
import com.arextest.web.model.contract.contracts.RemoveRecordsAndScenesRequest;
import com.arextest.web.model.contract.contracts.ReportInitialRequestType;
import com.arextest.web.model.contract.contracts.common.PlanStatistic;
import com.arextest.web.model.contract.contracts.replay.AnalyzeCompareResultsRequestType;
import com.arextest.web.model.contract.contracts.replay.UpdateReportInfoRequestType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Created by wang_yc on 2021/10/19
 */
@Slf4j
@Component
public final class ReplayReportService implements ComparisonWriter {

  private static final String CASE_COUNT_LIMIT_NAME = "caseCountLimit";
  @Resource
  private ReplayCompareResultRepositoryImpl replayCompareResultRepository;
  @Resource
  private ReplayCompareResultConverter converter;
  @Resource
  private HttpWepServiceApiClient httpWepServiceApiClient;
  @Value("${arex.api.init.url}")
  private String reportInitUrl;
  @Value("${arex.api.push.compareResult.url}")
  private String pushReplayCompareResultUrl;
  @Value("${arex.api.push.replayStatus.url}")
  private String pushReplayStatusUrl;
  @Value("${arex.api.update.report.info.url}")
  private String updateReportInfoUrl;
  @Value("${arex.api.remove.records.url}")
  private String removeRecordsUrl;
  @Value("${arex.api.remove.errorMsg.url}")
  private String removeErrorMsgUrl;
  @Value("${arex.api.queryPlanStatistic.url}")
  private String queryPlanStatisticUrl;
  @Value("${arex.api.delete.planItemStatistics.url}")
  private String deletePlanItemStatisticsUrl;

  public boolean initReportInfo(ReplayPlan replayPlan) {
    ReportInitialRequestType requestType = new ReportInitialRequestType();
    requestType.setPlanId(replayPlan.getId());
    requestType.setPlanName(replayPlan.getPlanName());
    requestType.setCreator(replayPlan.getOperator());
    requestType.setTotalCaseCount(replayPlan.getCaseTotalCount());
    Map customTags = new HashMap<>();
    customTags.put(CASE_COUNT_LIMIT_NAME, replayPlan.getCaseCountLimit());
    requestType.setCustomTags(customTags);
    // for case env and tag
    ReportInitialRequestType.CaseSourceEnvironment caseSourceEnv;
    caseSourceEnv = new ReportInitialRequestType.CaseSourceEnvironment();
    caseSourceEnv.setCaseStartTime(replayPlan.getCaseSourceFrom().getTime());
    caseSourceEnv.setCaseEndTime(replayPlan.getCaseSourceTo().getTime());
    caseSourceEnv.setCaseSourceType(replayPlan.getCaseSourceType());
    caseSourceEnv.setCaseTags(replayPlan.getCaseTags());
    requestType.setCaseSourceEnv(caseSourceEnv);
    // for app
    ReportInitialRequestType.Application application = new ReportInitialRequestType.Application();
    application.setAppId(replayPlan.getAppId());
    application.setAppName(replayPlan.getAppName());
    requestType.setApplication(application);
    // for host env
    ReportInitialRequestType.HostEnvironment hostEnv = new ReportInitialRequestType.HostEnvironment();
    hostEnv.setSourceEnv(replayPlan.getSourceEnv());
    hostEnv.setSourceHost(replayPlan.getSourceHost());
    hostEnv.setTargetEnv(replayPlan.getTargetEnv());
    hostEnv.setTargetHost(replayPlan.getTargetHost());
    requestType.setHostEnv(hostEnv);
    // for version
    ReportInitialRequestType.Version version = new ReportInitialRequestType.Version();
    version.setCaseRecordVersion(replayPlan.getCaseRecordVersion());
    version.setCoreVersion(replayPlan.getArexCordVersion());
    version.setExtVersion(replayPlan.getArexExtVersion());
    requestType.setVersion(version);
    // for image
    ReportInitialRequestType.TargetImage targetImage = new ReportInitialRequestType.TargetImage();
    targetImage.setTargetImageId(replayPlan.getTargetImageId());
    targetImage.setTargetImageName(replayPlan.getTargetImageName());
    requestType.setTargetImage(targetImage);
    // for plan actions
    List actionItemList = replayPlan.getReplayActionItemList();
    List reportItemList = new ArrayList<>(
        actionItemList.size());
    if (replayPlan.isInitReportItem()) {
      ReportInitialRequestType.ReportItem reportItem;
      for (ReplayActionItem actionItem : actionItemList) {
        if (actionItem.getReplayCaseCount() == 0) {
          continue;
        }
        reportItem = new ReportInitialRequestType.ReportItem();
        reportItem.setOperationId(actionItem.getOperationId());
        reportItem.setOperationName(actionItem.getOperationName());
        reportItem.setServiceName(actionItem.getServiceKey());
        reportItem.setPlanItemId(actionItem.getId());
        reportItem.setTotalCaseCount(actionItem.getReplayCaseCount());
        reportItemList.add(reportItem);
      }
    }
    requestType.setReportItemList(reportItemList);
    LOGGER.info("initReport request:{}", requestType);
    Response response = httpWepServiceApiClient.jsonPost(reportInitUrl, requestType,
        GenericResponseType.class);
    LOGGER.info("initReport request:{}, response:{}", requestType, response);
    return response != null && !response.getResponseStatusType().hasError();
  }

  public void pushPlanStatus(String planId, ReplayStatusType statusType, String errorMessage,
      boolean rerun) {
    ChangeReplayStatusRequestType requestType = new ChangeReplayStatusRequestType();
    requestType.setPlanId(planId);
    requestType.setStatus(statusType.getValue());
    requestType.setErrorMessage(errorMessage);
    requestType.setRerun(rerun);
    Object response = httpWepServiceApiClient.jsonPost(pushReplayStatusUrl, requestType,
        GenericResponseType.class);
    LOGGER.info("push plan status planId: {},status: {}, result:{}", planId, statusType, response);
  }

  @Override
  public boolean write(List comparedResult) {
    if (CollectionUtils.isEmpty(comparedResult)) {
      LOGGER.info("not write comparedResult");
      return true;
    }
    int comparedSize = comparedResult.size();

    AnalyzeCompareResultsRequestType request = new AnalyzeCompareResultsRequestType();
    List reqItems = new ArrayList<>(
        comparedSize);

    ReplayCompareResult firstResult = comparedResult.get(0);
    this.replayCompareResultRepository.deleteByRecord(firstResult.getRecordId(),
        firstResult.getPlanItemId());
    this.replayCompareResultRepository.save(comparedResult);

    for (ReplayCompareResult sourceResult : comparedResult) {
      reqItems.add(converter.reportContractFromBo(sourceResult));
    }
    request.setAnalyzeCompareInfos(reqItems);

    Response response = httpWepServiceApiClient.jsonPost(pushReplayCompareResultUrl, request,
        GenericResponseType.class);
    if (response == null || response.getResponseStatusType().hasError()) {
      LOGGER.error("push replay compared result to report size: {}, result:{}", comparedSize,
          response);
    }
    return true;
  }

  @Override
  public boolean writeIncomparable(ReplayActionCaseItem caseItem, String remark) {
    com.arextest.diff.model.CompareResult sdkResult =
        CompareSDK.fromException(caseItem.requestMessage(), null, remark);
    ReplayCompareResult replayCompareResult = ReplayCompareResult.createFrom(caseItem, sdkResult);
    replayCompareResult.setDiffResultCode(sdkResult.getCode());
    return this.write(Collections.singletonList(replayCompareResult));
  }

  public boolean writeQmqCompareResult(ReplayActionCaseItem caseItem) {
    if (caseItem == null) {
      return true;
    }
    AnalyzeCompareResultsRequestType request = new AnalyzeCompareResultsRequestType();
    ReplayCompareResult compareResult = toQMQCompareResult(caseItem);

    this.replayCompareResultRepository.deleteByRecord(compareResult.getRecordId(),
        compareResult.getPlanItemId());
    this.replayCompareResultRepository.save(compareResult);

    request.setAnalyzeCompareInfos(
        Collections.singletonList(converter.reportContractFromBo(compareResult)));

    Response response = httpWepServiceApiClient.jsonPost(pushReplayCompareResultUrl, request,
        GenericResponseType.class);
    if (response == null || response.getResponseStatusType().hasError()) {
      LOGGER.error("writeQmqCompareResult to report result:{}", response);
    }
    return true;
  }

  private ReplayCompareResult toQMQCompareResult(ReplayActionCaseItem caseItem) {
    ReplayCompareResult compareResult = new ReplayCompareResult();
    ReplayActionItem parent = caseItem.getParent();
    compareResult.setPlanId(parent.getPlanId());
    compareResult.setOperationId(parent.getOperationId());
    compareResult.setServiceName(parent.getServiceName());
    compareResult.setCategoryName(MockCategoryType.Q_MESSAGE_CONSUMER.getName());
    compareResult.setDiffResultCode(DiffResultCode.COMPARED_WITHOUT_DIFFERENCE);
    compareResult.setOperationName(parent.getOperationName());
    compareResult.setReplayId(caseItem.getTargetResultId());
    compareResult.setRecordId(caseItem.getRecordId());
    compareResult.setPlanItemId(caseItem.getPlanItemId());
    compareResult.setCaseId(caseItem.getId());
    return compareResult;
  }

  public void removeRecordsAndScenes(Map> actionIdAndRecordIdsMap) {
    RemoveRecordsAndScenesRequest requestType = new RemoveRecordsAndScenesRequest();
    requestType.setActionIdAndRecordIdsMap(actionIdAndRecordIdsMap);
    Response response = httpWepServiceApiClient.jsonPost(removeRecordsUrl, requestType,
        GenericResponseType.class);
    LOGGER.info("removeRecordsAndScenes request:{}, response:{}", requestType, response);
  }

  public void removeErrorMsg(String planId, List planItemIdList) {
    RemoveErrorMsgRequest request = new RemoveErrorMsgRequest();
    request.setPlanId(planId);
    request.setPlanItemIdList(planItemIdList);
    Response response = httpWepServiceApiClient.jsonPost(removeErrorMsgUrl, request,
        GenericResponseType.class);
    LOGGER.info("removeErrorMsg request:{}, response:{}", request, response);
  }

  public PlanStatistic queryPlanStatistic(String planId, String appId) {
    QueryPlanStatisticRequestType request = new QueryPlanStatisticRequestType();
    request.setPlanId(planId);
    request.setAppId(appId);
      QueryPlanStatisticResponseWrapper response = httpWepServiceApiClient.jsonPost(
          queryPlanStatisticUrl, request, QueryPlanStatisticResponseWrapper.class);
      LOGGER.info("queryPlanStatistic request:{}, response:{}", request, response);
      if (response == null || response.getBody() == null) {
        return null;
      }
      return response.getBody().getPlanStatistic();
  }

  public void updateReportInfo(ReplayPlan replayPlan) {
    UpdateReportInfoRequestType requestType = new UpdateReportInfoRequestType();
    requestType.setPlanId(replayPlan.getId());
    requestType.setTotalCaseCount(replayPlan.getCaseTotalCount());
    List actionItemList = replayPlan.getReplayActionItemList();
    if (CollectionUtils.isNotEmpty(actionItemList)) {
      List updateReportInfoList = new ArrayList<>(actionItemList.size());
      UpdateReportInfoRequestType.UpdateReportItem reportItem;
      for (ReplayActionItem actionItem : actionItemList) {
        reportItem = new UpdateReportInfoRequestType.UpdateReportItem();
        reportItem.setPlanItemId(actionItem.getId());
        reportItem.setTotalCaseCount(actionItem.getReplayCaseCount());
        updateReportInfoList.add(reportItem);
      }
      requestType.setUpdateReportItems(updateReportInfoList);
    }
    Response response = httpWepServiceApiClient.jsonPost(updateReportInfoUrl, requestType,
        GenericResponseType.class);
    LOGGER.info("updateReportCaseCount request:{}, response:{}", requestType, response);
  }

  public void deletePlanItemStatistics(String planId, List planItemIds) {
    if (CollectionUtils.isEmpty(planItemIds)) {
      return;
    }
    DeletePlanItemStatisticsRequestType request = new DeletePlanItemStatisticsRequestType();
    request.setPlanId(planId);
    request.setPlanItemIds(planItemIds);
    Response response = httpWepServiceApiClient.jsonPost(deletePlanItemStatisticsUrl, request,
        GenericResponseType.class);
    LOGGER.info("deletePlanItemStatistics request:{}, response:{}", request, response);
  }

  @Data
  static class QueryPlanStatisticResponseWrapper implements Response {
    private QueryPlanStatisticResponseType body;
    private ResponseStatusType responseStatusType;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy