#91 -- Use new models

This commit is contained in:
Anton Romanov 2023-03-16 23:09:04 +04:00
parent ddedfc9751
commit d111121e24
5 changed files with 115 additions and 51 deletions

View File

@ -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<TimeSeries> tss = timeSeriesService.getByBranch(markupForm.getBranchId());
MarkupForm markupFormWithTs = new MarkupForm(tss.stream().map(MarkupRow::new).limit(200).collect(Collectors.toList()));
markupFormWithTs.setBranchId(markupForm.getBranchId());
List<TimeSeriesForMarkup> 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<TimeSeries> 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<TimeSeriesForMarkup> 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";
}

View File

@ -4,24 +4,23 @@ import java.util.ArrayList;
import java.util.List;
public class MarkupForm {
private List<MarkupRow> markupRows = new ArrayList<>();
private List<TimeSeriesForMarkup> timeSeriesForMarkupList = new ArrayList<>();
private Integer branchId;
private boolean save;
private boolean filter;
public MarkupForm() {
}
public MarkupForm(List<MarkupRow> markupRows) {
this.markupRows = markupRows;
public MarkupForm(Integer branchId, List<TimeSeriesForMarkup> tss) {
this.timeSeriesForMarkupList = tss;
this.branchId = branchId;
}
public List<MarkupRow> getMarkupRows() {
return markupRows;
public List<TimeSeriesForMarkup> getTimeSeriesForMarkupList() {
return timeSeriesForMarkupList;
}
public void setMarkupRows(List<MarkupRow> markupRows) {
this.markupRows = markupRows;
public void setTimeSeriesForMarkupList(List<TimeSeriesForMarkup> 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;
}
}

View File

@ -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<TimeSeries> 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<TimeSeries> getTimeSeriesSet() {
return timeSeriesSet;
}
public void setTimeSeriesSet(Set<TimeSeries> 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;
}
}

View File

@ -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<Markup> findAll() {
return markupRepository.findAll();
}
public List<TimeSeriesForMarkup> getTimeSeriesForMarkup(List<TimeSeries> timeSeriesList) {
List<TimeSeriesForMarkup> 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<TimeSeriesValue> values1 = ts1.getValues();
for (int k = 0; k < values1.size() - 1; k++) {
List<TimeSeriesValue> 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;
}
}

View File

@ -36,15 +36,15 @@
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col" colspan="3">Разметка временных рядов</th>
<th scope="col" colspan="3">Охарактеризуйте периоды вашего проекта</th>
</tr>
</thead>
<tbody>
<tr th:each="m, itemStat: *{markupRows}">
<td><input type="hidden" th:field="*{markupRows[__${itemStat.index}__].timeSeries.id}"></span></td>
<td><span class="badge badge-success"
th:text="*{markupRows[__${itemStat.index}__].timeSeries.name}"></span></td>
<td><input type="text" th:field="*{markupRows[__${itemStat.index}__].markup}"></input></td>
<tr th:each="m, itemStat: *{timeSeriesForMarkupList}">
<!-- <td><input type="hidden" th:field="*{markupRows[__${itemStat.index}__].timeSeries.id}"></span></td>-->
<td><span class="badge badge-success" th:text="${m.dateFrom}"></span></td>
<td><span class="badge badge-success" th:text="${m.dateTo}"></span></td>
<td><input type="text"></input></td>
</tr>
</tbody>
</table>