Added getting list of reports by package from monitoring; Filter lastReport
This commit is contained in:
parent
29dd358dc1
commit
7f6e56a835
11 changed files with 240 additions and 0 deletions
|
|
@ -0,0 +1,24 @@
|
||||||
|
package ru.nbch.credit_tracker.entities.response.online_report;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
package ru.nbch.credit_tracker.entities.response.online_report;
|
||||||
|
|
||||||
|
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 = {
|
||||||
|
"reports"
|
||||||
|
})
|
||||||
|
public class OperationReport {
|
||||||
|
|
||||||
|
@XmlElement(name = "Reports", required = true)
|
||||||
|
protected Reports reports;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
package ru.nbch.credit_tracker.entities.response.online_report;
|
||||||
|
|
||||||
|
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 = {
|
||||||
|
"datetime",
|
||||||
|
"length",
|
||||||
|
"filename"
|
||||||
|
})
|
||||||
|
public class Report {
|
||||||
|
|
||||||
|
@XmlElement(name = "datetime", required = true)
|
||||||
|
protected String datetime;
|
||||||
|
@XmlElement(name = "length", required = true)
|
||||||
|
protected Integer length;
|
||||||
|
@XmlElement(name = "filename", required = true)
|
||||||
|
protected String filename;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
package ru.nbch.credit_tracker.entities.response.online_report;
|
||||||
|
|
||||||
|
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 java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
|
@XmlType(name = "", propOrder = {
|
||||||
|
"reports"
|
||||||
|
})
|
||||||
|
public class Reports {
|
||||||
|
|
||||||
|
@XmlElement(name = "Report", required = true)
|
||||||
|
protected List<Report> reports;
|
||||||
|
|
||||||
|
public List<Report> getReports() {
|
||||||
|
if (reports == null) {
|
||||||
|
reports = new ArrayList<>();
|
||||||
|
}
|
||||||
|
return this.reports;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
package ru.nbch.credit_tracker.entities.response.online_report;
|
||||||
|
|
||||||
|
import jakarta.xml.bind.annotation.*;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Setter
|
||||||
|
@Getter
|
||||||
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
|
@XmlType(name = "", propOrder = {
|
||||||
|
"inputParams",
|
||||||
|
"operationReport"
|
||||||
|
})
|
||||||
|
@XmlRootElement(name = "SgnlOnlineReport")
|
||||||
|
public class SgnlOnlineReport {
|
||||||
|
|
||||||
|
@XmlElement(name = "InputParams", required = true)
|
||||||
|
protected InputParams inputParams;
|
||||||
|
|
||||||
|
@XmlElement(name = "OperationReport", required = true)
|
||||||
|
protected OperationReport operationReport;
|
||||||
|
}
|
||||||
|
|
@ -33,4 +33,6 @@ public interface SignalMapper {
|
||||||
SignalService.PackageData findPackageData(@Param("id") Integer id);
|
SignalService.PackageData findPackageData(@Param("id") Integer id);
|
||||||
|
|
||||||
List<SignalService.PackageData> findPackageDataList(@Param("userId") String userId);
|
List<SignalService.PackageData> findPackageDataList(@Param("userId") String userId);
|
||||||
|
|
||||||
|
List<SignalService.PackageData> findPackagesByStatus(@Param("status") String status);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
package ru.nbch.credit_tracker.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import ru.nbch.credit_tracker.entities.response.online_report.Report;
|
||||||
|
import ru.nbch.credit_tracker.entities.response.online_report.SgnlOnlineReport;
|
||||||
|
import ru.nbch.credit_tracker.service.signal.SignalRestService;
|
||||||
|
import ru.nbch.credit_tracker.utils.CTUtil;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class MonitoringService {
|
||||||
|
|
||||||
|
private final SignalRestService signalRestService;
|
||||||
|
private final SignalService signalService;
|
||||||
|
|
||||||
|
public MonitoringService(SignalRestService signalRestService, SignalService signalService) {
|
||||||
|
this.signalRestService = signalRestService;
|
||||||
|
this.signalService = signalService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void monitor() {
|
||||||
|
List<SignalService.PackageData> packages = signalService.findPackagesOnMonitoring();
|
||||||
|
for (SignalService.PackageData packageData : packages) {
|
||||||
|
SgnlOnlineReport onlineReports = signalRestService.getReports(packageData.getUserId().substring(0, 6) + "_" + packageData.getPackageName());
|
||||||
|
|
||||||
|
List<String> fileNames = onlineReports.getOperationReport().getReports().getReports().stream().map(Report::getFilename)
|
||||||
|
.filter(fileName -> CTUtil.parseDateFromReportFileName(fileName) != null).toList(); // TODO оптимизировать
|
||||||
|
|
||||||
|
Optional<String> newReport = Optional.of(fileNames)
|
||||||
|
.orElseGet(Collections::emptyList)
|
||||||
|
.stream().max(Comparator.comparing(CTUtil::parseDateFromReportFileName)); // отфильтровали только те имена, которые парсятся в дату, так что null тут не придет
|
||||||
|
|
||||||
|
LocalDateTime lastDownloadedReportDate = CTUtil.parseDateFromReportFileName(packageData.getLastDownloadedReport());
|
||||||
|
|
||||||
|
if (packageData.getLastDownloadedReport() == null && newReport.isPresent()) {
|
||||||
|
loadReport(newReport.get()); // по этому пакету еще не было загруженных отчетов, продолжаем работу с полученным
|
||||||
|
} else if (lastDownloadedReportDate != null && newReport.isPresent()) {
|
||||||
|
LocalDateTime newReportDate = CTUtil.parseDateFromReportFileName(newReport.get());
|
||||||
|
if (newReportDate.isAfter(lastDownloadedReportDate)) {
|
||||||
|
loadReport(newReport.get()); // полученный отчет новее записанного в базу
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadReport(String report) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -196,12 +196,19 @@ public class SignalService {
|
||||||
return deleteResponse;
|
return deleteResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<PackageData> findPackagesOnMonitoring() {
|
||||||
|
return mapper.findPackagesByStatus(Status.ON_MONITORING.name());
|
||||||
|
}
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public static class PackageData {
|
public static class PackageData {
|
||||||
|
private Integer id;
|
||||||
private String userId;
|
private String userId;
|
||||||
private String packageName;
|
private String packageName;
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
|
private String status;
|
||||||
|
private String lastDownloadedReport;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
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.response.online_report.SgnlOnlineReport;
|
||||||
import ru.nbch.credit_tracker.entities.signal_package.Signals;
|
import ru.nbch.credit_tracker.entities.signal_package.Signals;
|
||||||
import ru.nbch.credit_tracker.service.xml.jaxb.IXmlProcessor;
|
import ru.nbch.credit_tracker.service.xml.jaxb.IXmlProcessor;
|
||||||
import ru.nbch.credit_tracker.service.xml.jaxb.XmlUtilService;
|
import ru.nbch.credit_tracker.service.xml.jaxb.XmlUtilService;
|
||||||
|
|
@ -30,6 +31,7 @@ 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 final RestTemplate restTemplate;
|
private final RestTemplate restTemplate;
|
||||||
private final XmlUtilService xmlUtilService;
|
private final XmlUtilService xmlUtilService;
|
||||||
|
|
@ -116,6 +118,26 @@ public class SignalRestService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SgnlOnlineReport getReports(String packCode) {
|
||||||
|
String url = GET_REPORTS_URI + "?packCode=" + packCode;
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setBasicAuth(authToken);
|
||||||
|
|
||||||
|
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(headers);
|
||||||
|
|
||||||
|
ResponseEntity<SgnlOnlineReport> response = restTemplate.exchange(
|
||||||
|
url,
|
||||||
|
HttpMethod.GET,
|
||||||
|
requestEntity,
|
||||||
|
SgnlOnlineReport.class
|
||||||
|
);
|
||||||
|
|
||||||
|
// TODO обработка ошибок
|
||||||
|
|
||||||
|
return response.getBody();
|
||||||
|
}
|
||||||
|
|
||||||
private String buildToken(String signalsToken, String signalsuser) throws Exception {
|
private String buildToken(String signalsToken, String signalsuser) throws Exception {
|
||||||
try {
|
try {
|
||||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||||
|
|
|
||||||
27
src/main/java/ru/nbch/credit_tracker/utils/CTUtil.java
Normal file
27
src/main/java/ru/nbch/credit_tracker/utils/CTUtil.java
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
package ru.nbch.credit_tracker.utils;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class CTUtil {
|
||||||
|
|
||||||
|
private static DateTimeFormatter onlineReportDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH-mm-ss");
|
||||||
|
|
||||||
|
|
||||||
|
public static LocalDateTime parseDateFromReportFileName(String reportFileName) {
|
||||||
|
if (StringUtils.isBlank(reportFileName)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Pattern pattern = Pattern.compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}-\\d{2}-\\d{2}");
|
||||||
|
Matcher matcher = pattern.matcher(reportFileName);
|
||||||
|
if (matcher.find()) {
|
||||||
|
String dateTimeStr = matcher.group();
|
||||||
|
return LocalDateTime.parse(dateTimeStr, onlineReportDateTimeFormatter);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -12,9 +12,12 @@
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
<resultMap id="packageDataResultMap" type="ru.nbch.credit_tracker.service.SignalService$PackageData">
|
<resultMap id="packageDataResultMap" type="ru.nbch.credit_tracker.service.SignalService$PackageData">
|
||||||
|
<result property="id" column="id" />
|
||||||
<result property="userId" column="membercode" />
|
<result property="userId" column="membercode" />
|
||||||
<result property="packageName" column="package_name" />
|
<result property="packageName" column="package_name" />
|
||||||
<result property="createdAt" column="created_at"/>
|
<result property="createdAt" column="created_at"/>
|
||||||
|
<result property="status" column="status"/>
|
||||||
|
<result property="last_downloaded_report" column="lastDownloadedReport" />
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
<select id="isPackageNotUnique" resultType="boolean">
|
<select id="isPackageNotUnique" resultType="boolean">
|
||||||
|
|
@ -84,4 +87,10 @@
|
||||||
WHERE membercode IS NOT NULL AND LEFT(membercode, 6) = #{userId}
|
WHERE membercode IS NOT NULL AND LEFT(membercode, 6) = #{userId}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<select id="findPackagesByStatus" resultMap="packageDataResultMap" resultType="ru.nbch.credit_tracker.service.SignalService$PackageData">
|
||||||
|
SELECT id, membercode, package_name, created_at, last_downloaded_report
|
||||||
|
FROM signals_user_packages
|
||||||
|
WHERE status = #{status}
|
||||||
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
Loading…
Add table
Reference in a new issue