Временные ряды #59

Merged
romanov73 merged 10 commits from influx into master 2022-10-06 16:09:37 +04:00
6 changed files with 162 additions and 1 deletions
Showing only changes of commit 9a38056a94 - Show all commits

View File

@ -12,7 +12,8 @@ public class TimeSeriesValue extends BaseEntity {
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
private TimeSeries timeSeries; private TimeSeries timeSeries;
public TimeSeriesValue(Date date, Integer value) { public TimeSeriesValue(TimeSeries timeSeries, Date date, Integer value) {
this.timeSeries = timeSeries;
this.date = date; this.date = date;
this.value = value; this.value = value;
} }

View File

@ -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<TimeSeries, Integer> {
Optional<TimeSeries> findByName(String name);
}

View File

@ -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, Integer> {
TimeSeriesValue findByTimeSeriesAndName(TimeSeries timeSeries, String name);
List<TimeSeriesValue> findTimeSeriesValueById(Integer repositoryId);
}

View File

@ -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<TimeSeriesValue> timeSeriesValues) {
LOG.debug("Start save {} time series with {} time series values ", timeSeries.getName(), timeSeriesValues.size());
List<Integer> 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<TimeSeries> 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<TimeSeriesValue> save(List<TimeSeriesValue> 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<TimeSeriesValue> findAll() {
return timeSeriesValueRepository.findAll();
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}