|
|
|
@ -27,11 +27,14 @@ import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@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;
|
|
|
|
@ -127,17 +130,24 @@ public class TimeSeriesService {
|
|
|
|
|
return timeSeriesRepository.getTimeSeriesByBranchId(branchId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<Double> 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> timeSeries = getByBranch(b.getId());
|
|
|
|
|
return Stream.of(TimeSeriesType.values()).allMatch(type -> timeSeries
|
|
|
|
|
.stream()
|
|
|
|
|
.anyMatch(ts -> type == ts.getTimeSeriesType()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<TimeSeries> getGroupedTendencies(List<TimeSeries> tsList) {
|
|
|
|
|