#96 -- Add time series generation

This commit is contained in:
Anton Romanov 2024-05-10 23:12:22 +04:00
parent e5d5d7312b
commit eb37dd0c8c
5 changed files with 69 additions and 16 deletions

View File

@ -5,6 +5,8 @@ public class GenerationForm {
private int min;
private int max;
private int base;
private int baseTendency;
private int tendencyDynamic;
public int getTsLength() {
return tsLength;
@ -38,13 +40,19 @@ public class GenerationForm {
this.base = base;
}
@Override
public String toString() {
return "GenerationForm{" +
"tsLength=" + tsLength +
", min=" + min +
", max=" + max +
", base=" + base +
'}';
public int getBaseTendency() {
return baseTendency;
}
public void setBaseTendency(int baseTendency) {
this.baseTendency = baseTendency;
}
public int getTendencyDynamic() {
return tendencyDynamic;
}
public void setTendencyDynamic(int tendencyDynamic) {
this.tendencyDynamic = tendencyDynamic;
}
}

View File

@ -1,18 +1,40 @@
package ru.ulstu.extractor.generation.service;
import org.springframework.stereotype.Service;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.branch.service.BranchService;
import ru.ulstu.extractor.generation.model.GenerationForm;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.ts.service.TimeSeriesService;
import ru.ulstu.extractor.ts.util.DateUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@Service
public class GenerationService {
private final TimeSeriesService timeSeriesService;
private final BranchService branchService;
public GenerationService(TimeSeriesService timeSeriesService) {
public GenerationService(TimeSeriesService timeSeriesService,
BranchService branchService) {
this.timeSeriesService = timeSeriesService;
this.branchService = branchService;
}
public void generate(GenerationForm generationForm) {
System.out.print(generationForm);
Branch branch = branchService.findByBranchId(Branch.GENERATED_BRANCH_ID).orElseThrow();
Date startDate = DateUtils.addDays(-generationForm.getTsLength());
Arrays.stream(TimeSeriesType.values()).forEach(tsType -> {
List<TimeSeriesValue> tsValues = new ArrayList<>();
for (int i = 0; i < generationForm.getTsLength(); i++) {
tsValues.add(new TimeSeriesValue(DateUtils.addDays(startDate, i), (double) i));
}
final String tsName = "Генерированный " + tsType.getDescription();
timeSeriesService.save(tsName, branch, tsType, tsValues);
});
}
}

View File

@ -5,9 +5,16 @@ import java.util.Date;
import java.util.GregorianCalendar;
public class DateUtils {
public static Date addMonths(int amount) {
public static Date addDays(int amount) {
Calendar c = GregorianCalendar.getInstance();
c.add(Calendar.DAY_OF_MONTH, amount);
return c.getTime();
}
public static Date addDays(Date startDate, int amount) {
Calendar c = GregorianCalendar.getInstance();
c.setTime(startDate);
c.add(Calendar.DAY_OF_MONTH, amount);
return c.getTime();
}
}

View File

@ -6,10 +6,10 @@ import java.util.List;
public class Dummy {
public static List<TimeSeriesValue> getDefaultTimeSeries (){
return List.of(new TimeSeriesValue(DateUtils.addMonths(-5), 1.0),
new TimeSeriesValue(DateUtils.addMonths(-4), 2.0),
new TimeSeriesValue(DateUtils.addMonths(-3), 3.0),
new TimeSeriesValue(DateUtils.addMonths(-2), 4.0),
new TimeSeriesValue(DateUtils.addMonths(-1), 5.0));
return List.of(new TimeSeriesValue(DateUtils.addDays(-5), 1.0),
new TimeSeriesValue(DateUtils.addDays(-4), 2.0),
new TimeSeriesValue(DateUtils.addDays(-3), 3.0),
new TimeSeriesValue(DateUtils.addDays(-2), 4.0),
new TimeSeriesValue(DateUtils.addDays(-1), 5.0));
}
}

View File

@ -40,6 +40,22 @@
<input type="text" class="form-control m-1" th:field="*{base}">
</div>
</div>
<div class="row">
<div class="col-md-4 col-sm-12">
Основная тенденция
</div>
<div class="col-md-6 col-sm-12">
<input type="text" class="form-control m-1" th:field="*{baseTendency}">
</div>
</div>
<div class="row">
<div class="col-md-4 col-sm-12">
Динамика тенденции
</div>
<div class="col-md-6 col-sm-12">
<input type="text" class="form-control m-1" th:field="*{tendencyDynamic}">
</div>
</div>
<div class="row">
<div class="col-md-4 col-sm-12">
<input type="submit" class="btn btn-outline-success form-control" value="Сгенерировать временные ряды"/>