http://jira.mfd.msk:8088/browse/BKI-3199 Added loading error file for monitoring

This commit is contained in:
Mikhail Trofimov 2025-09-11 14:13:44 +03:00
parent 1e8b19d663
commit c2c1eab58b
8 changed files with 187 additions and 2 deletions

View file

@ -4,6 +4,7 @@ import jakarta.xml.bind.JAXBException;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import ru.nbch.credit_tracker.config.settings.CreditTrackerSettings; import ru.nbch.credit_tracker.config.settings.CreditTrackerSettings;
import ru.nbch.credit_tracker.entities.external_response.monitoring_report_file.SgnlReportMonitorigSetupError;
import ru.nbch.credit_tracker.entities.external_response.online_report.SgnlOnlineReport; import ru.nbch.credit_tracker.entities.external_response.online_report.SgnlOnlineReport;
import ru.nbch.credit_tracker.entities.user_response.errors.MonitoringError; import ru.nbch.credit_tracker.entities.user_response.errors.MonitoringError;
import ru.nbch.credit_tracker.entities.user_response.success.MonitoringSetupResponse; import ru.nbch.credit_tracker.entities.user_response.success.MonitoringSetupResponse;
@ -24,6 +25,7 @@ public class XmlConfig {
return new XmlProcessorImplAnyClasses(Charset.forName(settings.getEncoding()), return new XmlProcessorImplAnyClasses(Charset.forName(settings.getEncoding()),
// unmarshalling classes // unmarshalling classes
SgnlOnlineReport.class, SgnlOnlineReport.class,
SgnlReportMonitorigSetupError.class,
// marshalling classes // marshalling classes
MonitoringError.class, MonitoringError.class,
MonitoringSetupResponse.class); MonitoringSetupResponse.class);

View file

@ -0,0 +1,21 @@
package ru.nbch.credit_tracker.entities.external_response.monitoring_report_file;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlType;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"filename"
})
public class ErrorsForNextReport {
@XmlElement(name = "filename", required = true)
protected String filename;
}

View file

@ -0,0 +1,24 @@
package ru.nbch.credit_tracker.entities.external_response.monitoring_report_file;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlType;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"memberCode",
"packCode"
})
public class InputParams {
@XmlElement(name = "memberCode", required = true)
protected String memberCode;
@XmlElement(name = "packCode", required = true)
protected String packCode;
}

View file

@ -0,0 +1,20 @@
package ru.nbch.credit_tracker.entities.external_response.monitoring_report_file;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlType;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"errorsForNextReport"
})
public class OperationReport {
@XmlElement(name = "ErrorsForNextReport")
protected ErrorsForNextReport errorsForNextReport;
}

View file

@ -0,0 +1,22 @@
package ru.nbch.credit_tracker.entities.external_response.monitoring_report_file;
import jakarta.xml.bind.annotation.*;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"inputParams",
"operationReport"
})
@XmlRootElement(name = "SgnlReport")
public class SgnlReportMonitorigSetupError {
@XmlElement(name = "InputParams", required = true)
protected InputParams inputParams;
@XmlElement(name = "OperationReport", required = true)
protected OperationReport operationReport;
}

View file

@ -1,6 +1,7 @@
package ru.nbch.credit_tracker.facade; package ru.nbch.credit_tracker.facade;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.FileUrlResource; import org.springframework.core.io.FileUrlResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@ -225,6 +226,7 @@ public class SignalFacade {
if (signalRestService.setupMonitoring(packageId)) { if (signalRestService.setupMonitoring(packageId)) {
log.info("Package was successfully registered in signal service. PackageId: {} PackageName: {}", packageId, packageName); log.info("Package was successfully registered in signal service. PackageId: {} PackageName: {}", packageId, packageName);
signalService.updatePackageStatus(packageId, Status.ON_MONITORING); signalService.updatePackageStatus(packageId, Status.ON_MONITORING);
findErrors(packageId);
} else { } else {
log.info("Package registration was rejected by signal service. PackageId: {} PackageName: {}", packageId, packageName); log.info("Package registration was rejected by signal service. PackageId: {} PackageName: {}", packageId, packageName);
signalService.updatePackageStatus(packageId, Status.ERROR); signalService.updatePackageStatus(packageId, Status.ERROR);
@ -237,6 +239,21 @@ public class SignalFacade {
}); });
} }
private void findErrors(Integer packageId) {
UserPackage userPackage = signalService.findPackage(packageId);
if (userPackage != null) {
String packCode = userPackage.getUserId().substring(0, 6) + "_" + userPackage.getPackageName();
log.info("Проверка на ошибки при постановке на мониторинг. packCode={}, packageId={}", packCode, packageId);
String fileName = signalRestService.getErrorFileName(packageId, packCode);
if (StringUtils.isNotBlank(fileName)) {
String filePath = signalRestService.loadErrorFile(packageId, fileName);
if (StringUtils.isNotBlank(filePath)) {
signalService.updateSubjectsErrors(filePath);
}
}
}
}
private boolean isZipFile(MultipartFile file) { private boolean isZipFile(MultipartFile file) {
return file.getContentType() != null && return file.getContentType() != null &&
(file.getContentType().equals("application/zip") || file.getContentType().equals("application/x-zip-compressed")) || (file.getContentType().equals("application/zip") || file.getContentType().equals("application/x-zip-compressed")) ||

View file

@ -277,6 +277,10 @@ public class SignalService {
return persons; return persons;
} }
public void updateSubjectsErrors(String filePath) {
}
public List<UserPackage> findActivePackages(String userId) { public List<UserPackage> findActivePackages(String userId) {
return userPackageService.findUserPackages(userId, Status.ON_MONITORING, Status.PROCESSING); return userPackageService.findUserPackages(userId, Status.ON_MONITORING, Status.PROCESSING);
} }
@ -317,6 +321,10 @@ public class SignalService {
return phoneFidMapService.hasNonNullfids(packageId); return phoneFidMapService.hasNonNullfids(packageId);
} }
public UserPackage findPackage(Integer packageId) {
return userPackageService.findPackage(packageId);
}
@Getter @Getter
@AllArgsConstructor @AllArgsConstructor
public static class UpdateResult { public static class UpdateResult {

View file

@ -12,6 +12,7 @@ import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import ru.nbch.credit_tracker.config.settings.CreditTrackerSettings; import ru.nbch.credit_tracker.config.settings.CreditTrackerSettings;
import ru.nbch.credit_tracker.entities.external_response.errors.SgnlReport; import ru.nbch.credit_tracker.entities.external_response.errors.SgnlReport;
import ru.nbch.credit_tracker.entities.external_response.monitoring_report_file.SgnlReportMonitorigSetupError;
import ru.nbch.credit_tracker.entities.external_response.online_report.SgnlOnlineReport; import ru.nbch.credit_tracker.entities.external_response.online_report.SgnlOnlineReport;
import ru.nbch.credit_tracker.service.SignalService; import ru.nbch.credit_tracker.service.SignalService;
import ru.nbch.credit_tracker.service.xml.jaxb.IXmlProcessor; import ru.nbch.credit_tracker.service.xml.jaxb.IXmlProcessor;
@ -34,7 +35,9 @@ public class SignalRestService {
private static String UPDATE_URI = "update"; private static String UPDATE_URI = "update";
private static String DELETE_URI = "delete"; private static String DELETE_URI = "delete";
private static String GET_REPORTS_URI = "onlinereport"; private static String GET_REPORTS_URI = "onlinereport";
private static String DOWNLOAD_REPORT_URI = "onlinedownload"; private static String GET_REPORT_URI = "report";
private static String DOWNLOAD_REPORT_URI = "download";
private static String DOWNLOAD_ONLINE_REPORT_URI = "onlinedownload";
private final RestTemplate restTemplate; private final RestTemplate restTemplate;
private final IXmlProcessor xmlProcessor; private final IXmlProcessor xmlProcessor;
@ -98,6 +101,70 @@ public class SignalRestService {
return false; return false;
} }
/**
* @return имя файла с ошибками по поставленному на мониторинг отчету
*/
public String getErrorFileName(Integer packageId, String packCode) {
String url = GET_REPORT_URI + "?packCode=" + packCode;
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(authToken);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(
url,
HttpMethod.GET,
requestEntity,
String.class
);
String answer = response.getBody();
SgnlReport error = staxProcessor.read(answer, SgnlReport.class);
if (error.getError().getDescription() != null) {
log.error("Ошибка при получении имени файла ошибки из сервиса НБКИ. packCode={}. Описание: {}", packCode, error.getError().getDescription());
return null;
}
try {
SgnlReportMonitorigSetupError report = xmlProcessor.read(answer, SgnlReportMonitorigSetupError.class);
if (report.getOperationReport().getErrorsForNextReport() != null) {
String fileName = report.getOperationReport().getErrorsForNextReport().getFilename();
log.error("Найдены ошибки по пакету. packCode={}, packageId={}, fileName = {}", packCode, packageId, fileName);
return fileName;
} else {
log.info("Пакет был поставлен на мониторинг без ошибок. packCode={}, packageId={}", packCode, packageId);
}
} catch (Exception e) {
log.error("Ошибка при получении имени файла ошибки из сервиса НБКИ. Код ошибки: {} packCode={}", response.getStatusCode().value(), packCode);
}
return null;
}
/**
* Загружает .gz отчет ошибок из официального сервиса сигналов, разархивирует в tmp_folder
*
* @return путь к разархивированному файлу. Пример: <loadDir>/tmp_folder/<package_id>/0001XX_MY_BATCH_223112.1.errors.xml.gz
*/
public String loadErrorFile(Integer packageId, String fileName) {
String url = DOWNLOAD_REPORT_URI + "?fNameRequest=" + fileName;
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(authToken);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(headers);
ResponseEntity<byte[]> response = restTemplate.exchange(
url,
HttpMethod.GET,
requestEntity,
byte[].class
);
return unzipReport(response, packageId, fileName);
}
/** /**
* Удаляет пакет с мониторинга * Удаляет пакет с мониторинга
*/ */
@ -162,7 +229,7 @@ public class SignalRestService {
* @return путь к разархивированному файлу. Пример: <loadDir>/tmp_folder/<package_id>/0001XX_MY_BATCH_204.1.online.2025-07-08T20-00-46.xml.gz.xml * @return путь к разархивированному файлу. Пример: <loadDir>/tmp_folder/<package_id>/0001XX_MY_BATCH_204.1.online.2025-07-08T20-00-46.xml.gz.xml
*/ */
public String downloadReport(Integer packageId, String reportName) { public String downloadReport(Integer packageId, String reportName) {
String url = DOWNLOAD_REPORT_URI + "?fNameReport=" + reportName; String url = DOWNLOAD_ONLINE_REPORT_URI + "?fNameReport=" + reportName;
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(authToken); headers.setBasicAuth(authToken);
@ -176,6 +243,10 @@ public class SignalRestService {
byte[].class byte[].class
); );
return unzipReport(response,packageId, reportName);
}
private String unzipReport(ResponseEntity<byte[]> response, Integer packageId, String reportName) {
boolean isGzipped = response.getHeaders().getContentType() != null boolean isGzipped = response.getHeaders().getContentType() != null
&& response.getHeaders().getContentType().toString().equals("application/x-gzip"); && response.getHeaders().getContentType().toString().equals("application/x-gzip");