From 9e431757044345632f7c6710d7281619bca1aca1 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Wed, 15 Mar 2023 14:18:23 +0400 Subject: [PATCH] #80 -- Add validation --- .../controller/AssessmentController.java | 2 +- .../assessment/service/AssessmentService.java | 7 ++----- .../branch/service/BranchService.java | 13 +++++++++++- .../ts/service/TimeSeriesService.java | 20 ++++++++++++++----- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java b/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java index 3434377..e5721fa 100644 --- a/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java +++ b/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java @@ -27,7 +27,7 @@ public class AssessmentController { @GetMapping(ASSESSMENTS) public String getAssessments(Model model, @RequestParam Optional branchId) { - model.addAttribute("branches", branchService.findAll()); + model.addAttribute("branches", branchService.findAllValid()); if (branchId.isPresent()) { model.addAttribute("assessments", assessmentService.getAssessmentsByForecastTendencies(branchId.get())); model.addAttribute("filterBranchForm", new FilterBranchForm(branchId.get())); diff --git a/src/main/java/ru/ulstu/extractor/assessment/service/AssessmentService.java b/src/main/java/ru/ulstu/extractor/assessment/service/AssessmentService.java index d221806..e1a2020 100644 --- a/src/main/java/ru/ulstu/extractor/assessment/service/AssessmentService.java +++ b/src/main/java/ru/ulstu/extractor/assessment/service/AssessmentService.java @@ -54,9 +54,7 @@ public class AssessmentService { private List getSingleAssessmentByTimeSeriesTendencies(List dbRules, List timeSeries) throws AssessmentException { Map variableValues = new HashMap<>(); - timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), - timeSeriesService.getLastTimeSeriesTendency(ts) - .orElseThrow(() -> new AssessmentException("")))); + timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService.getLastTimeSeriesTendency(ts))); return fuzzyInferenceService.getFuzzyInference(dbRules, antecedentValueService.getList(), variableValues, @@ -74,8 +72,7 @@ public class AssessmentService { .filter(ts -> ts.getTimeSeriesType() == dbRule.getFirstAntecedent() || ts.getTimeSeriesType() == dbRule.getSecondAntecedent()) .forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService - .getLastTimeSeriesTendency(ts) - .orElse(ts.getValues().get(ts.getValues().size() - 1).getValue()))); + .getLastTimeSeriesTendency(ts))); return fuzzyInferenceService.getFuzzyInference(List.of(dbRule), antecedentValueService.getList(), variableValues, diff --git a/src/main/java/ru/ulstu/extractor/branch/service/BranchService.java b/src/main/java/ru/ulstu/extractor/branch/service/BranchService.java index e123a71..0684ed4 100644 --- a/src/main/java/ru/ulstu/extractor/branch/service/BranchService.java +++ b/src/main/java/ru/ulstu/extractor/branch/service/BranchService.java @@ -16,6 +16,7 @@ import ru.ulstu.extractor.commit.model.Commit; import ru.ulstu.extractor.commit.service.CommitService; import ru.ulstu.extractor.core.BaseEntity; import ru.ulstu.extractor.gitrepository.model.GitRepository; +import ru.ulstu.extractor.ts.service.TimeSeriesService; import java.util.List; import java.util.Optional; @@ -26,11 +27,14 @@ public class BranchService { private final static Logger LOG = LoggerFactory.getLogger(BranchService.class); private final BranchRepository branchRepository; private final CommitService commitService; + private final TimeSeriesService timeSeriesService; public BranchService(BranchRepository branchRepository, - CommitService commitService) { + CommitService commitService, + TimeSeriesService timeSeriesService) { this.branchRepository = branchRepository; this.commitService = commitService; + this.timeSeriesService = timeSeriesService; } @Transactional @@ -87,4 +91,11 @@ public class BranchService { branch.setIndexingStatus(indexingStatus); return branchRepository.save(branch); } + + public List findAllValid() { + return findAll() + .stream() + .filter(timeSeriesService::isBranchContainsAllTimeSeries) + .collect(Collectors.toList()); + } } diff --git a/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java b/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java index d5e12e6..234757f 100644 --- a/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java +++ b/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java @@ -26,10 +26,13 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; +import java.util.stream.Stream; @Service public class TimeSeriesService { private final static Logger LOG = LoggerFactory.getLogger(TimeSeriesService.class); + private final static int MIN_TIME_SERIES_LENGTH = 5; + private final static double DEFAULT_TIME_SERIES_TENDENCY = 0.0; private final TimeSeriesRepository timeSeriesRepository; private final TimeSeriesValueRepository timeSeriesValueRepository; private final TimeSeriesDateMapper.TimeSeriesInterval timeSeriesInterval = TimeSeriesDateMapper.TimeSeriesInterval.HOUR; @@ -124,16 +127,23 @@ public class TimeSeriesService { return timeSeriesRepository.getTimeSeriesByBranchId(branchId); } - public Optional getLastTimeSeriesTendency(TimeSeries ts) { - if (ts != null && ts.getValues().size() > 5) { + public double getLastTimeSeriesTendency(TimeSeries ts) { + if (ts != null && ts.getValues().size() > MIN_TIME_SERIES_LENGTH) { JSONObject response = httpService.post(TIME_SERIES_TENDENCY_URL, new JSONObject(new SmoothingTimeSeries(ts))); LOG.debug("Успешно отправлен на сервис сглаживания"); if (response.has("response") && response.getString("response").equals("empty")) { - return Optional.empty(); + return DEFAULT_TIME_SERIES_TENDENCY; } JSONArray jsonArray = response.getJSONObject("timeSeries").getJSONArray("values"); - return Optional.of(jsonArray.getJSONObject(jsonArray.length() - 1).getDouble("value")); + return jsonArray.getJSONObject(jsonArray.length() - 1).getDouble("value"); } - return Optional.empty(); + return DEFAULT_TIME_SERIES_TENDENCY; + } + + public boolean isBranchContainsAllTimeSeries(Branch b) { + List timeSeries = getByBranch(b.getId()); + return Stream.of(TimeSeriesType.values()).allMatch(type -> timeSeries + .stream() + .anyMatch(ts -> type == ts.getTimeSeriesType())); } } -- 2.34.1