From 9a38056a94f5709c46bc4c82968b20b07d455833 Mon Sep 17 00:00:00 2001 From: BarminaA Date: Wed, 8 Jun 2022 15:44:53 +0400 Subject: [PATCH] #58-add AbstractTSExtractor --- .../extractor/model/TimeSeriesValue.java | 3 +- .../repository/TimeSeriesRepository.java | 10 +++ .../repository/TimeSeriesValueRepository.java | 13 ++++ .../extractor/service/TimeSeriesService.java | 75 +++++++++++++++++++ .../extractor/ts/AbstractTSExtractor.java | 28 +++++++ .../java/ru/ulstu/extractor/ts/AuthorTS.java | 34 +++++++++ 6 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ru/ulstu/extractor/repository/TimeSeriesRepository.java create mode 100644 src/main/java/ru/ulstu/extractor/repository/TimeSeriesValueRepository.java create mode 100644 src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java create mode 100644 src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java create mode 100644 src/main/java/ru/ulstu/extractor/ts/AuthorTS.java diff --git a/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java b/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java index 544db71..f4daaf4 100644 --- a/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java +++ b/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java @@ -12,7 +12,8 @@ public class TimeSeriesValue extends BaseEntity { @ManyToOne(fetch = FetchType.LAZY) private TimeSeries timeSeries; - public TimeSeriesValue(Date date, Integer value) { + public TimeSeriesValue(TimeSeries timeSeries, Date date, Integer value) { + this.timeSeries = timeSeries; this.date = date; this.value = value; } diff --git a/src/main/java/ru/ulstu/extractor/repository/TimeSeriesRepository.java b/src/main/java/ru/ulstu/extractor/repository/TimeSeriesRepository.java new file mode 100644 index 0000000..e324774 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/repository/TimeSeriesRepository.java @@ -0,0 +1,10 @@ +package ru.ulstu.extractor.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import ru.ulstu.extractor.model.TimeSeries; + +import java.util.Optional; + +public interface TimeSeriesRepository extends JpaRepository { + Optional findByName(String name); +} diff --git a/src/main/java/ru/ulstu/extractor/repository/TimeSeriesValueRepository.java b/src/main/java/ru/ulstu/extractor/repository/TimeSeriesValueRepository.java new file mode 100644 index 0000000..8289b55 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/repository/TimeSeriesValueRepository.java @@ -0,0 +1,13 @@ +package ru.ulstu.extractor.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import ru.ulstu.extractor.model.TimeSeries; +import ru.ulstu.extractor.model.TimeSeriesValue; + +import java.util.List; + +public interface TimeSeriesValueRepository extends JpaRepository { + TimeSeriesValue findByTimeSeriesAndName(TimeSeries timeSeries, String name); + + List findTimeSeriesValueById(Integer repositoryId); +} diff --git a/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java b/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java new file mode 100644 index 0000000..1054ce9 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + +package ru.ulstu.extractor.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import ru.ulstu.extractor.model.BaseEntity; +import ru.ulstu.extractor.model.TimeSeries; +import ru.ulstu.extractor.model.TimeSeriesValue; +import ru.ulstu.extractor.repository.TimeSeriesRepository; +import ru.ulstu.extractor.repository.TimeSeriesValueRepository; + +import java.util.Date; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +@Service +public class TimeSeriesService { + private final static Logger LOG = LoggerFactory.getLogger(TimeSeriesService.class); + private final TimeSeriesRepository timeSeriesRepository; + private final TimeSeriesValueRepository timeSeriesValueRepository; + + + public TimeSeriesService(TimeSeriesRepository timeSeriesRepository, TimeSeriesValueRepository timeSeriesValueRepository) { + this.timeSeriesRepository = timeSeriesRepository; + this.timeSeriesValueRepository = timeSeriesValueRepository; + } + + public TimeSeries save(TimeSeries timeSeries, List timeSeriesValues) { + LOG.debug("Start save {} time series with {} time series values ", timeSeries.getName(), timeSeriesValues.size()); + List timeSeriesValuesToRemoveIds = timeSeries.getValues().stream().map(BaseEntity::getId).collect(Collectors.toList()); + timeSeries.setValues(timeSeriesValues); + LOG.debug("Save time series {} ", timeSeries.getName()); + timeSeries = timeSeriesRepository.save(timeSeries); + LOG.debug("Clear {} ime series values ", timeSeriesValuesToRemoveIds.size()); + return timeSeries; + } + + public TimeSeries findOrCreate(String timeSeriesName) { + Optional maybeTimeSeries = timeSeriesRepository.findByName(timeSeriesName); + if (maybeTimeSeries.isPresent()) { + LOG.debug("TimeSeries {} exists.", maybeTimeSeries.get().getName()); + return maybeTimeSeries.get(); + } + return timeSeriesRepository.save(new TimeSeries(timeSeriesName)); + } + + public List save(List timeSeriesValues) { + return timeSeriesValues.stream() + .map(timeSeriesValue -> { + timeSeriesValue.setValue((timeSeriesValue.getValue())); + timeSeriesValue.setDate((timeSeriesValue.getDate())); + return timeSeriesValueRepository.save(timeSeriesValue); + }).collect(Collectors.toList()); + } + + public void addTimeSeriesValue(String timeSeriesName, Date date, Integer value) { + LOG.debug("Start add time series values to {} time series values ", timeSeriesName); + TimeSeries timeSeries = findOrCreate(timeSeriesName); + timeSeriesValueRepository.save(new TimeSeriesValue(timeSeries, date, value)); + } + + public TimeSeriesValue findByTimeSeriesAndName(TimeSeries timeSeries, String name) { + return timeSeriesValueRepository.findByTimeSeriesAndName(timeSeries, name); + } + + public List findAll() { + return timeSeriesValueRepository.findAll(); + } +} diff --git a/src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java b/src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java new file mode 100644 index 0000000..e211df7 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java @@ -0,0 +1,28 @@ +package ru.ulstu.extractor.ts; + +public abstract class AbstractTSExtractor { + private String name; + private int value; + + public abstract void getAbstractTSName(); + + public abstract void addPoint(int value); + + public abstract void addTS(String name, int value); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } +} diff --git a/src/main/java/ru/ulstu/extractor/ts/AuthorTS.java b/src/main/java/ru/ulstu/extractor/ts/AuthorTS.java new file mode 100644 index 0000000..ef7014f --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/AuthorTS.java @@ -0,0 +1,34 @@ +package ru.ulstu.extractor.ts; + +import org.springframework.stereotype.Component; +import ru.ulstu.extractor.service.TimeSeriesService; + +@Component +public class AuthorTS extends AbstractTSExtractor { + private static String name = "AuthorTimeSeries"; + private static TimeSeriesService timeSeriesService; + + public static void setTimeSeriesService(TimeSeriesService timeSeriesService) { + AuthorTS.timeSeriesService = timeSeriesService; + } + + @Override + public void getAbstractTSName() { + timeSeriesService.findOrCreate(name); + } + + @Override + public void addPoint(int value) { + + } + + @Override + public void addTS(String name, int value) { + + } + + @Override + public String getName() { + return name; + } +}