From d111121e2437d39cc8d2841693c15456e8caa1cb Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 16 Mar 2023 23:09:04 +0400 Subject: [PATCH] #91 -- Use new models --- .../TimeSeriesMarkupController.java | 36 ++++++++------- .../extractor/markup/model/MarkupForm.java | 33 ++++---------- .../markup/model/TimeSeriesForMarkup.java | 44 +++++++++++++++++++ .../markup/service/MarkupService.java | 41 +++++++++++++++-- src/main/resources/templates/markup.html | 12 ++--- 5 files changed, 115 insertions(+), 51 deletions(-) create mode 100644 src/main/java/ru/ulstu/extractor/markup/model/TimeSeriesForMarkup.java diff --git a/src/main/java/ru/ulstu/extractor/markup/controller/TimeSeriesMarkupController.java b/src/main/java/ru/ulstu/extractor/markup/controller/TimeSeriesMarkupController.java index 0672544..94d1ec4 100644 --- a/src/main/java/ru/ulstu/extractor/markup/controller/TimeSeriesMarkupController.java +++ b/src/main/java/ru/ulstu/extractor/markup/controller/TimeSeriesMarkupController.java @@ -8,14 +8,12 @@ import org.springframework.web.bind.annotation.PostMapping; import ru.ulstu.extractor.branch.service.BranchService; import ru.ulstu.extractor.core.Route; import ru.ulstu.extractor.markup.model.MarkupForm; -import ru.ulstu.extractor.markup.model.MarkupRow; +import ru.ulstu.extractor.markup.model.TimeSeriesForMarkup; import ru.ulstu.extractor.markup.service.MarkupService; -import ru.ulstu.extractor.ts.model.TimeSeries; import ru.ulstu.extractor.ts.service.TimeSeriesService; import springfox.documentation.annotations.ApiIgnore; import java.util.List; -import java.util.stream.Collectors; @Controller @ApiIgnore @@ -34,18 +32,21 @@ public class TimeSeriesMarkupController { @GetMapping("time-series-markup") public String markupTs(Model model) { - model.addAttribute("branches", branchService.findAll()); + model.addAttribute("branches", branchService.findAllValid()); model.addAttribute("markupForm", new MarkupForm()); return "markup"; } @PostMapping("time-series-markup") public String filter(Model model, @ModelAttribute MarkupForm markupForm) { - model.addAttribute("branches", branchService.findAll()); + model.addAttribute("branches", branchService.findAllValid()); if (markupForm != null && markupForm.getBranchId() != null) { - List tss = timeSeriesService.getByBranch(markupForm.getBranchId()); - MarkupForm markupFormWithTs = new MarkupForm(tss.stream().map(MarkupRow::new).limit(200).collect(Collectors.toList())); - markupFormWithTs.setBranchId(markupForm.getBranchId()); + List tss = markupService.getTimeSeriesForMarkup( + timeSeriesService.getGroupedTendencies( + timeSeriesService.getByBranch(markupForm.getBranchId()) + ) + ); + MarkupForm markupFormWithTs = new MarkupForm(markupForm.getBranchId(), tss); model.addAttribute("markupForm", markupFormWithTs); } else { model.addAttribute("markupForm", new MarkupForm()); @@ -55,15 +56,18 @@ public class TimeSeriesMarkupController { @PostMapping(Route.ADD_MARKUP) public String addMarkups(Model model, @ModelAttribute MarkupForm markupForm) { - model.addAttribute("branches", branchService.findAll()); - if (markupForm.getBranchId() != null) { - List tss = timeSeriesService.getByBranch(markupForm.getBranchId()); - MarkupForm markupFormWithTs = new MarkupForm(tss.stream().map(MarkupRow::new).limit(200).collect(Collectors.toList())); - markupFormWithTs.setBranchId(markupForm.getBranchId()); + model.addAttribute("branches", branchService.findAllValid()); + if (markupForm != null && markupForm.getBranchId() != null) { + List tss = markupService.getTimeSeriesForMarkup( + timeSeriesService.getGroupedTendencies( + timeSeriesService.getByBranch(markupForm.getBranchId()) + ) + ); + MarkupForm markupFormWithTs = new MarkupForm(markupForm.getBranchId(), tss); model.addAttribute("markupForm", markupFormWithTs); - if (markupForm.getMarkupRows() != null && !markupForm.getMarkupRows().isEmpty()) { - markupService.saveMarkup(markupForm); - } +// if (markupForm.getMarkupRows() != null && !markupForm.getMarkupRows().isEmpty()) { +// markupService.saveMarkup(markupForm); +// } } return "markup"; } diff --git a/src/main/java/ru/ulstu/extractor/markup/model/MarkupForm.java b/src/main/java/ru/ulstu/extractor/markup/model/MarkupForm.java index c07c837..098a379 100644 --- a/src/main/java/ru/ulstu/extractor/markup/model/MarkupForm.java +++ b/src/main/java/ru/ulstu/extractor/markup/model/MarkupForm.java @@ -4,24 +4,23 @@ import java.util.ArrayList; import java.util.List; public class MarkupForm { - private List markupRows = new ArrayList<>(); + private List timeSeriesForMarkupList = new ArrayList<>(); private Integer branchId; - private boolean save; - private boolean filter; public MarkupForm() { } - public MarkupForm(List markupRows) { - this.markupRows = markupRows; + public MarkupForm(Integer branchId, List tss) { + this.timeSeriesForMarkupList = tss; + this.branchId = branchId; } - public List getMarkupRows() { - return markupRows; + public List getTimeSeriesForMarkupList() { + return timeSeriesForMarkupList; } - public void setMarkupRows(List markupRows) { - this.markupRows = markupRows; + public void setTimeSeriesForMarkupList(List timeSeriesForMarkupList) { + this.timeSeriesForMarkupList = timeSeriesForMarkupList; } public Integer getBranchId() { @@ -31,20 +30,4 @@ public class MarkupForm { public void setBranchId(Integer branchId) { this.branchId = branchId; } - - public boolean isSave() { - return save; - } - - public void setSave(boolean save) { - this.save = save; - } - - public boolean isFilter() { - return filter; - } - - public void setFilter(boolean filter) { - this.filter = filter; - } } diff --git a/src/main/java/ru/ulstu/extractor/markup/model/TimeSeriesForMarkup.java b/src/main/java/ru/ulstu/extractor/markup/model/TimeSeriesForMarkup.java new file mode 100644 index 0000000..3bb84ff --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/markup/model/TimeSeriesForMarkup.java @@ -0,0 +1,44 @@ +package ru.ulstu.extractor.markup.model; + +import ru.ulstu.extractor.ts.model.TimeSeries; + +import java.util.Date; +import java.util.Set; +import java.util.TreeSet; + +public class TimeSeriesForMarkup { + private Set timeSeriesSet = new TreeSet<>(); + private Date dateFrom; + private Date dateTo; + + public TimeSeriesForMarkup(TimeSeries ts1, TimeSeries ts2, Date dateFrom, Date dateTo) { + this.dateFrom = dateFrom; + this.dateTo = dateTo; + this.timeSeriesSet.add(ts1); + this.timeSeriesSet.add(ts2); + } + + public Set getTimeSeriesSet() { + return timeSeriesSet; + } + + public void setTimeSeriesSet(Set timeSeriesSet) { + this.timeSeriesSet = timeSeriesSet; + } + + public Date getDateFrom() { + return dateFrom; + } + + public void setDateFrom(Date dateFrom) { + this.dateFrom = dateFrom; + } + + public Date getDateTo() { + return dateTo; + } + + public void setDateTo(Date dateTo) { + this.dateTo = dateTo; + } +} diff --git a/src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java b/src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java index c4d3fd3..2d3096f 100644 --- a/src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java +++ b/src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java @@ -1,13 +1,14 @@ package ru.ulstu.extractor.markup.service; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import ru.ulstu.extractor.markup.model.Markup; -import ru.ulstu.extractor.markup.model.MarkupForm; +import ru.ulstu.extractor.markup.model.TimeSeriesForMarkup; import ru.ulstu.extractor.markup.repository.MarkupRepository; import ru.ulstu.extractor.ts.model.TimeSeries; +import ru.ulstu.extractor.ts.model.TimeSeriesValue; import ru.ulstu.extractor.ts.service.TimeSeriesService; +import java.util.ArrayList; import java.util.List; @Service @@ -21,15 +22,47 @@ public class MarkupService { this.timeSeriesService = timeSeriesService; } - @Transactional + /*@Transactional public void saveMarkup(MarkupForm markupForm) { markupForm.getMarkupRows().forEach(markupRow -> { TimeSeries timeSeries = timeSeriesService.getById(markupRow.getTimeSeries().getId()); markupRepository.save(new Markup(timeSeries, markupRow.getMarkup())); }); - } + }*/ public List findAll() { return markupRepository.findAll(); } + + public List getTimeSeriesForMarkup(List timeSeriesList) { + List tsForMarkup = new ArrayList<>(); + for (int i = 0; i < timeSeriesList.size(); i++) { + TimeSeries ts1 = timeSeriesList.get(i); + for (int j = i + 1; j < timeSeriesList.size(); j++) { + TimeSeries ts2 = timeSeriesList.get(j); + List values1 = ts1.getValues(); + for (int k = 0; k < values1.size() - 1; k++) { + List values2 = ts2.getValues(); + for (int l = 0; l < values2.size() - 1; l++) { + if (values1.get(k).getDate().equals(values2.get(l).getDate()) + && values1.get(k + 1).getDate().equals(values2.get(l + 1).getDate())) { + final int index = k; + TimeSeriesForMarkup found = tsForMarkup + .stream() + .filter(m -> m.getDateFrom().equals(values1.get(index).getDate()) && m.getDateTo().equals(values1.get(index + 1).getDate())) + .findAny() + .orElse(null); + if (found == null) { + tsForMarkup.add(new TimeSeriesForMarkup(ts1, ts2, values1.get(index).getDate(), values1.get(index + 1).getDate())); + } else { + found.getTimeSeriesSet().add(ts1); + found.getTimeSeriesSet().add(ts2); + } + } + } + } + } + } + return tsForMarkup; + } } diff --git a/src/main/resources/templates/markup.html b/src/main/resources/templates/markup.html index c0c98a7..0f085fe 100644 --- a/src/main/resources/templates/markup.html +++ b/src/main/resources/templates/markup.html @@ -36,15 +36,15 @@ - + - - - - + + + + +
Разметка временных рядовОхарактеризуйте периоды вашего проекта