reports-service рефакторинг запуска отчётов
This commit is contained in:
parent
f2ec40d3c4
commit
285f0960df
4 changed files with 82 additions and 84 deletions
|
|
@ -9,20 +9,14 @@ import org.slf4j.LoggerFactory;
|
|||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import ru.spcex.clearing.reports.notifications.REXNotificationBuilder;
|
||||
import ru.spcex.clearing.reports.reports.AbstractReport;
|
||||
import ru.spcex.clearing.reports.reports.bt_12_2.ReportPA_BT2_B;
|
||||
import ru.spcex.clearing.reports.reports.bt_12_2.ReportPA_BT2_IV;
|
||||
import ru.spcex.clearing.reports.services.ReportDataCollector;
|
||||
import ru.spcex.clearing.reports.services.ReportsService;
|
||||
import ru.spcex.clearing.reports.services.ReportsServiceCommand;
|
||||
import ru.spcex.clearing.reports.services.builders.XMLReportBuilder;
|
||||
import ru.spcex.clearing.reports.services.collector.ReportPA_BT2_B_Collector;
|
||||
import ru.spcex.clearing.reports.services.collector.ReportPA_BT2_IV_Collector;
|
||||
import ru.spcex.platform.imdg.api.ImdgProvider;
|
||||
|
||||
import java.io.File;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Map;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ReportsServiceApplication {
|
||||
|
|
@ -33,25 +27,11 @@ public class ReportsServiceApplication {
|
|||
ConfigurableApplicationContext context = builder.run(args);
|
||||
|
||||
test(context);
|
||||
|
||||
//todo это тест. Переписать код:
|
||||
// Logger log = LoggerFactory.getLogger(ReportsServiceApplication.class);
|
||||
// ImdgProvider imdg = context.getBean(ImdgProvider.class);
|
||||
// log.info("Wait IMDG...");
|
||||
// imdg.waitAvailable();
|
||||
// log.info("Run generate report.");
|
||||
// Map<String, ReportDataCollector> allReports = context.getBeansOfType(ReportDataCollector.class);
|
||||
// log.info("Report types: {}", allReports);
|
||||
// for (ReportDataCollector collector : allReports.values()) {
|
||||
// log.info("Make report {}", collector);
|
||||
// File outPath=new File(".");
|
||||
// XMLReportBuilder xmlReportBuilder = new XMLReportBuilder(collector.getReportClass(), outPath);
|
||||
// LocalDate date2 = LocalDate.now();
|
||||
// LocalDate date1 = LocalDate.of(date2.getYear(), date2.getMonth(), 1);//date2.minusMonths(1);
|
||||
// AbstractReport report = collector.collectReportWithPeriod(date1, date2);
|
||||
// xmlReportBuilder.createReport(report);
|
||||
// }
|
||||
// log.info("Shutdown application after work");
|
||||
//todo это тест. Переписать код с учётом параметров запуска - как сервис или разово все из командной строки.
|
||||
ReportsServiceCommand reportCommand = context.getBean(ReportsServiceCommand.class);
|
||||
reportCommand.writeAllXML();
|
||||
Logger log = LoggerFactory.getLogger(ReportsServiceApplication.class);
|
||||
log.info("Shutdown application after work");
|
||||
// context.close(); // todo если будет запущен как сервис, сделать выход опционально, по другому.
|
||||
} catch (Throwable e) {
|
||||
LoggerFactory.getLogger(ReportsServiceApplication.class).error("Report-service start failed: {} -> {}", e.getClass().getSimpleName(), ExceptionUtils.getStackTrace(e));
|
||||
|
|
|
|||
|
|
@ -9,24 +9,17 @@ import ru.spcex.clearing.platform.messaging.domain.BaseRequest;
|
|||
import ru.spcex.clearing.platform.messaging.domain.cud.reports.ReportWithPeriodRequest;
|
||||
import ru.spcex.clearing.platform.messaging.service.QueueConsumer;
|
||||
import ru.spcex.clearing.reports.config.element.ReportsServiceSettings;
|
||||
import ru.spcex.clearing.reports.reports.AbstractReport;
|
||||
import ru.spcex.clearing.reports.reports.ReportId;
|
||||
import ru.spcex.clearing.reports.services.builders.XMLReportBuilder;
|
||||
import ru.spcex.platform.enumeration.Task;
|
||||
import ru.spcex.platform.imdg.api.ImdgProvider;
|
||||
import ru.spcex.platform.utils.enumeration.IEnumKey;
|
||||
import ru.spcex.platform.utils.log.ExceptionUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class QCommandExecutor extends QueueConsumer implements InitializingBean {
|
||||
private static final String QUEUE_RUN_REPORT_COMMAND = "GREP"; // см. Task.createReport
|
||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||
private final Map<ReportId, ReportDataCollector> collectorForReport = new HashMap<>();
|
||||
|
||||
// private final ImdgId idGenerator;
|
||||
// private final KafkaSender kafkaReqProducer;
|
||||
|
|
@ -38,10 +31,6 @@ public class QCommandExecutor extends QueueConsumer implements InitializingBean
|
|||
ReportsServiceCommand reportsServiceCommand,
|
||||
ReportsServiceSettings config) {
|
||||
super(kafkaQueue);
|
||||
for (ReportDataCollector<?> collector : listOfCollectors) {
|
||||
ReportId rId = collector.getReportId();
|
||||
collectorForReport.put(rId, collector);
|
||||
}
|
||||
this.reportsServiceCommand = reportsServiceCommand;
|
||||
this.config = config;
|
||||
}
|
||||
|
|
@ -54,15 +43,6 @@ public class QCommandExecutor extends QueueConsumer implements InitializingBean
|
|||
.forDestination(Task.createReport.topic(), callbacks::put);
|
||||
}
|
||||
|
||||
ReportId parseReportId(String reportStr) {
|
||||
ReportId id = IEnumKey.getEnumByKey(ReportId.class, reportStr);
|
||||
if (id == null) {
|
||||
log.trace("");
|
||||
id = ReportId.valueOf(reportStr);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
private void newReportWithPeriod(BaseRequest<ReportWithPeriodRequest> reportRequest) {
|
||||
log.debug("newReportWithPeriod request received");
|
||||
|
||||
|
|
@ -70,31 +50,26 @@ public class QCommandExecutor extends QueueConsumer implements InitializingBean
|
|||
|
||||
if (StringUtils.isBlank(request.getReportId()))
|
||||
throw new IllegalStateException("ReportID is empty");
|
||||
ReportId reportId = parseReportId(request.getReportId());
|
||||
ReportDataCollector<?> reportDataCollector = collectorForReport.get(reportId);
|
||||
if (reportDataCollector == null)
|
||||
throw new IllegalStateException("Collector for ReportID " + reportId + " not implemented yet");
|
||||
|
||||
if (request.getStartDate() == null || request.getEndDate() == null)
|
||||
throw new IllegalStateException("StartDate or EndDate is null");
|
||||
LocalDate startDate = request.getStartDate();
|
||||
LocalDate endDate = request.getEndDate();
|
||||
if (startDate.isAfter(endDate)) throw new IllegalStateException("StartDate > EndDate");
|
||||
|
||||
File outPath = new File(config.getReportModuleOut());
|
||||
XMLReportBuilder reportBuilder = new XMLReportBuilder(reportDataCollector.getReportClass(), outPath);
|
||||
AbstractReport report = reportDataCollector.collectReportWithPeriod(startDate, endDate);
|
||||
File reportFile = null;
|
||||
try {
|
||||
reportFile = reportBuilder.createReport(report);
|
||||
if (QUEUE_RUN_REPORT_COMMAND.equals(request.getReportId())) {
|
||||
log.info("Generate all report by command {}", request.getReportId());
|
||||
reportsServiceCommand.generateReportAll(startDate, endDate);
|
||||
} else {
|
||||
log.info("Generate single report by command {}", request.getReportId());
|
||||
reportsServiceCommand.generateReportById(request.getReportId(), startDate, endDate);
|
||||
}
|
||||
|
||||
log.debug("successfully processed");
|
||||
} catch (IOException e) {
|
||||
log.error("Error create report {}: {}", request.getReportId(), ExceptionUtils.getStackTrace(e));
|
||||
}
|
||||
|
||||
if (reportFile != null) {
|
||||
// put file in folder
|
||||
}
|
||||
log.debug("successfully processed");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,14 +30,6 @@ public abstract class ReportDataCollector<T extends AbstractReport> {
|
|||
*/
|
||||
abstract public T collectReportWithPeriod(LocalDate startDate, LocalDate endDate);
|
||||
|
||||
// /**
|
||||
// * T extends ReportWithClearingStatus
|
||||
// * @param startDate
|
||||
// * @param endDate
|
||||
// * @return
|
||||
// */
|
||||
// abstract public List<T> collectReportWithClearingStatus(LocalDate startDate, LocalDate endDate, String clearingStatus);
|
||||
|
||||
abstract public Class<T> getReportClass();
|
||||
|
||||
public ReportId getReportId() {
|
||||
|
|
|
|||
|
|
@ -7,16 +7,21 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.stereotype.Service;
|
||||
import ru.spcex.clearing.reports.config.element.ReportsServiceSettings;
|
||||
import ru.spcex.clearing.reports.reports.AbstractReport;
|
||||
import ru.spcex.clearing.reports.reports.ReportId;
|
||||
import ru.spcex.clearing.reports.services.builders.XMLReportBuilder;
|
||||
import ru.spcex.platform.imdg.api.ImdgProvider;
|
||||
import ru.spcex.platform.utils.enumeration.IEnumKey;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Исполняет создание отчётов.
|
||||
*
|
||||
* @see QCommandExecutor
|
||||
*/
|
||||
@Service
|
||||
|
|
@ -25,25 +30,77 @@ public class ReportsServiceCommand implements InitializingBean {
|
|||
|
||||
protected ReportsServiceSettings settings;
|
||||
protected ImdgProvider imdg;
|
||||
protected Map<String, ReportDataCollector> allReports;
|
||||
protected List<ReportDataCollector> allReports; // Spring services
|
||||
private final Map<ReportId, ReportDataCollector> collectorForReportById;
|
||||
|
||||
@Autowired
|
||||
public ReportsServiceCommand(ImdgProvider imdg, Map<String, ReportDataCollector> allReports, ReportsServiceSettings settings) {
|
||||
public ReportsServiceCommand(ImdgProvider imdg, List<ReportDataCollector> allReports, ReportsServiceSettings settings) {
|
||||
this.imdg = imdg;
|
||||
this.allReports = allReports; // allReports = context.getBeansOfType(ReportDataCollector.class);
|
||||
this.settings = settings;
|
||||
|
||||
this.allReports = allReports; // allReports = context.getBeansOfType(ReportDataCollector.class);
|
||||
this.collectorForReportById = new HashMap<>();
|
||||
for (ReportDataCollector<?> collector : allReports) {
|
||||
ReportId rId = collector.getReportId();
|
||||
collectorForReportById.put(rId, collector);
|
||||
String idStr = rId == null ? null : rId.getKey();
|
||||
log.info("Register collector service {} (report id \"{}\")for generate {}", collector, idStr, collector.getReportClass().getSimpleName());
|
||||
}
|
||||
assert allReports.size() == collectorForReportById.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
log.info("Wait IMDG...");
|
||||
imdg.waitAvailable();
|
||||
log.info("Report generator ready.");
|
||||
}
|
||||
|
||||
public void generateReportAll(LocalDate startDate, LocalDate endDate) throws IOException {
|
||||
for (ReportDataCollector<?> reportDataCollector : allReports) {
|
||||
makeSingleReport(reportDataCollector, startDate, endDate);
|
||||
}
|
||||
}
|
||||
|
||||
public void generateReportById(String reportIdStr, LocalDate startDate, LocalDate endDate) throws IOException {
|
||||
ReportId reportId = parseReportId(reportIdStr);
|
||||
ReportDataCollector<?> reportDataCollector = collectorForReportById.get(reportId);
|
||||
if (reportDataCollector == null)
|
||||
throw new IllegalStateException("Collector for ReportID " + reportId + " not implemented yet");
|
||||
|
||||
makeSingleReport(reportDataCollector, startDate, endDate);
|
||||
}
|
||||
|
||||
|
||||
ReportId parseReportId(String reportStr) {
|
||||
ReportId id = IEnumKey.getEnumByKey(ReportId.class, reportStr);
|
||||
if (id == null) {
|
||||
log.trace("Can not find ReportId by name: {}", reportStr);
|
||||
id = ReportId.valueOf(reportStr);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
protected void makeSingleReport(ReportDataCollector<?> reportDataCollector, LocalDate startDate, LocalDate endDate) throws IOException {
|
||||
log.debug("Generate report by collector {} - {}", reportDataCollector.getClass().getSimpleName(), reportDataCollector.getReportClass().getSimpleName());
|
||||
File outPath = new File(settings.getReportModuleOut());
|
||||
XMLReportBuilder reportBuilder = new XMLReportBuilder(reportDataCollector.getReportClass(), outPath);
|
||||
AbstractReport report = reportDataCollector.collectReportWithPeriod(startDate, endDate);
|
||||
File reportFile = null;
|
||||
reportFile = reportBuilder.createReport(report);
|
||||
|
||||
if (reportFile != null) {
|
||||
log.debug("New report file done: \"{}\"", reportFile);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeAllXML() throws IOException {
|
||||
log.info("Report types: {}", allReports);
|
||||
for (ReportDataCollector collector : allReports.values()) {
|
||||
for (ReportDataCollector collector : allReports) {
|
||||
log.info("Make report {}", collector);
|
||||
File outPath = new File(settings.getReportModuleOut());
|
||||
XMLReportBuilder xmlReportBuilder = new XMLReportBuilder(collector.getReportClass(), outPath);
|
||||
LocalDate date2 = LocalDate.now();
|
||||
LocalDate date1 = LocalDate.of(date2.getYear(), date2.getMonth(), 1);//date2.minusMonths(1);
|
||||
AbstractReport report = collector.collectReportWithPeriod(date1, date2);
|
||||
xmlReportBuilder.createReport(report);
|
||||
makeSingleReport(collector, date1, date2);
|
||||
}
|
||||
log.info("Shutdown application after work");
|
||||
}
|
||||
|
|
@ -52,10 +109,4 @@ public class ReportsServiceCommand implements InitializingBean {
|
|||
log.info("TODO write docx"); // todo write docx
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
log.info("Wait IMDG...");
|
||||
imdg.waitAvailable();
|
||||
log.info("Report generator ready. Available reports: {}", allReports.keySet());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue