| | |
| | | import com.zy.acs.framework.common.R; |
| | | import com.zy.acs.framework.common.SnowflakeIdWorker; |
| | | import com.zy.acs.manager.common.interceptor.IntegrationOpenApiInterceptor.IntegrationRequestContext; |
| | | import com.zy.acs.manager.common.interceptor.IntegrationOpenApiInterceptor.RequestPayload; |
| | | import com.zy.acs.manager.common.utils.IpTools; |
| | | import com.zy.acs.manager.manager.entity.IntegrationRecord; |
| | | import com.zy.acs.manager.manager.enums.IntegrationDirectionType; |
| | | import com.zy.acs.manager.manager.enums.StatusType; |
| | | import com.zy.acs.manager.manager.service.IntegrationRecordService; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.jetbrains.annotations.NotNull; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | if (context == null) { |
| | | return body; |
| | | } |
| | | IntegrationRecord record = null; |
| | | try { |
| | | IntegrationRecord record = buildRecord(body, request, context); |
| | | integrationRecordService.syncRecord(record); |
| | | record = buildRecord(body, request, context, null); |
| | | } catch (Exception e) { |
| | | log.error("Failed to persist integration log for {}", context.getHandler(), e); |
| | | log.error("Failed to build integration log for {}", context.getHandler(), e); |
| | | record = buildRecord(null, request, context, e); |
| | | } finally { |
| | | if (record != null) { |
| | | try { |
| | | integrationRecordService.syncRecord(record); |
| | | } catch (Exception persistEx) { |
| | | log.error("Failed to persist integration log for {}", context.getHandler(), persistEx); |
| | | } |
| | | } |
| | | } |
| | | return body; |
| | | } |
| | | |
| | | private IntegrationRecord buildRecord(Object responseBody, |
| | | HttpServletRequest request, |
| | | IntegrationRequestContext context) { |
| | | IntegrationRequestContext context, |
| | | Exception failure) { |
| | | Date now = new Date(); |
| | | ResultView resultView = resolveResult(responseBody); |
| | | RequestPayload payload = context.getPayload(); |
| | | |
| | | IntegrationRecord record = new IntegrationRecord(); |
| | | record.setUuid(nextUuid()); |
| | | record.setNamespace(context.getNamespace()); |
| | | record.setUrl(context.getUri()); |
| | | record.setNamespace(context.getNamespaceType().name()); |
| | | record.setUrl(payload.getUri()); |
| | | record.setAppkey(request.getHeader(HEADER_APP_KEY)); |
| | | record.setCaller(resolveCaller(request)); |
| | | record.setDirection(IntegrationDirectionType.INBOUND.value); |
| | | record.setTimestamp(String.valueOf(context.getStartAt())); |
| | | record.setClientIp(IpTools.gainRealIp(request)); |
| | | record.setRequest(safeToJson(context.getRequestSnapshot())); |
| | | record.setRequest(safeToJson(payload)); |
| | | record.setResponse(safeToJson(responseBody)); |
| | | record.setErr(resultView.getError()); |
| | | record.setResult(resultView.getResult()); |
| | | applyResult(record, responseBody, failure); |
| | | record.setCostMs(cost(context.getStartAt())); |
| | | record.setStatus(StatusType.ENABLE.val); |
| | | record.setCreateTime(now); |
| | |
| | | return record; |
| | | } |
| | | |
| | | private ResultView resolveResult(Object body) { |
| | | private void applyResult(IntegrationRecord record, Object body, Exception failure) { |
| | | if (failure != null) { |
| | | record.setResult(0); |
| | | record.setErr(failure.getMessage()); |
| | | return; |
| | | } |
| | | if (!(body instanceof R)) { |
| | | return ResultView.unknown(); |
| | | record.setResult(null); |
| | | record.setErr(null); |
| | | return; |
| | | } |
| | | R response = (R) body; |
| | | Integer code = parseInteger(response.get("code")); |
| | | if (code == null) { |
| | | return ResultView.unknown(); |
| | | record.setResult(null); |
| | | record.setErr(null); |
| | | return; |
| | | } |
| | | boolean success = code == 200; |
| | | String error = success ? null : safeToString(response.get("msg")); |
| | | return new ResultView(success ? 1 : 0, error); |
| | | record.setResult(success ? 1 : 0); |
| | | record.setErr(success ? null : safeToString(response.get("msg"))); |
| | | } |
| | | |
| | | private Integer parseInteger(Object codeObj) { |
| | |
| | | |
| | | private String safeToString(Object value) { |
| | | return value == null ? null : String.valueOf(value); |
| | | } |
| | | |
| | | @Getter |
| | | @AllArgsConstructor |
| | | private static class ResultView { |
| | | private final Integer result; |
| | | private final String error; |
| | | |
| | | private static ResultView unknown() { |
| | | return new ResultView(null, null); |
| | | } |
| | | } |
| | | } |