#5 -- add time series db service
This commit is contained in:
parent
24d974fa70
commit
88249612ec
111
src/main/java/ru/ulstu/db/DbFileService.java
Normal file
111
src/main/java/ru/ulstu/db/DbFileService.java
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
/*
|
||||||
|
* 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.db;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import ru.ulstu.datamodel.ts.TimeSeries;
|
||||||
|
import ru.ulstu.db.model.TimeSeriesMeta;
|
||||||
|
import ru.ulstu.db.model.TimeSeriesSet;
|
||||||
|
import ru.ulstu.service.UtilService;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class DbFileService implements DbService {
|
||||||
|
@Value("${time-series.db-path}")
|
||||||
|
private String timeSeriesDbPath;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<TimeSeriesSet> getSets() {
|
||||||
|
return Arrays.stream(Objects.requireNonNull(new File(timeSeriesDbPath).listFiles(File::isDirectory)))
|
||||||
|
.map(TimeSeriesSet::new)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<TimeSeriesMeta> getTimeSeriesMeta(TimeSeriesSet timeSeriesSet) throws IOException {
|
||||||
|
validateTimeSeriesSet(timeSeriesSet);
|
||||||
|
List<TimeSeriesMeta> list = new ArrayList<>();
|
||||||
|
for (File file : getTimeSeriesMetaFiles(timeSeriesSet)) {
|
||||||
|
list.add(new ObjectMapper().readValue(Paths.get(timeSeriesDbPath, file.getName()).toFile(), TimeSeriesMeta.class));
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TimeSeries getTimeSeries(TimeSeriesSet timeSeriesSet, String timeSeriesKey) throws IOException {
|
||||||
|
validateTimeSeriesSet(timeSeriesSet);
|
||||||
|
validateTimeSeries(timeSeriesSet, timeSeriesKey);
|
||||||
|
File timeSeriesFile = getTimeSeriesFile(timeSeriesSet, timeSeriesKey);
|
||||||
|
BufferedReader csvReader = new BufferedReader(new FileReader(timeSeriesFile));
|
||||||
|
String row;
|
||||||
|
TimeSeries result = new TimeSeries(timeSeriesKey);
|
||||||
|
while ((row = csvReader.readLine()) != null) {
|
||||||
|
TimeSeries partOfTimeSeries = new UtilService().getTimeSeriesFromString(row);
|
||||||
|
if (partOfTimeSeries.getLength() > 0) {
|
||||||
|
result.getValues().addAll(partOfTimeSeries.getValues());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
csvReader.close();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addNewSet(String key) {
|
||||||
|
TimeSeriesSet timeSeriesSet = new TimeSeriesSet(key);
|
||||||
|
if (isTimeSeriesSetExists(timeSeriesSet)) {
|
||||||
|
throw new RuntimeException(String.format("Time series set %s already exists", timeSeriesSet.getKey()));
|
||||||
|
} else {
|
||||||
|
return Paths.get(timeSeriesDbPath, timeSeriesSet.getKey()).toFile().mkdirs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) {
|
||||||
|
validateTimeSeriesSet(timeSeriesSet);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validateTimeSeriesSet(TimeSeriesSet timeSeriesSet) {
|
||||||
|
if (!isTimeSeriesSetExists(timeSeriesSet)) {
|
||||||
|
throw new RuntimeException(String.format("Time series set %s not exists", timeSeriesSet.getKey()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validateTimeSeries(TimeSeriesSet timeSeriesSet, String timeSeriesKey) {
|
||||||
|
if (Arrays.stream(Objects.requireNonNull(Paths.get(timeSeriesDbPath, timeSeriesSet.getKey()).toFile().listFiles(f -> f.getName().equals(timeSeriesKey)))).findAny().isEmpty()) {
|
||||||
|
throw new RuntimeException(String.format("Time series %s not exists", timeSeriesKey));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isTimeSeriesSetExists(TimeSeriesSet timeSeriesSet) {
|
||||||
|
return Arrays.stream(Objects.requireNonNull(new File(timeSeriesDbPath).listFiles(File::isDirectory)))
|
||||||
|
.anyMatch(d -> d.getName().equals(timeSeriesSet.getKey()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<File> getTimeSeriesMetaFiles(TimeSeriesSet timeSeriesSet) {
|
||||||
|
return Arrays.asList(Objects.requireNonNull(Paths.get(timeSeriesDbPath, timeSeriesSet.getKey()).toFile().listFiles(f -> f.getName().endsWith(".meta"))));
|
||||||
|
}
|
||||||
|
|
||||||
|
private File getTimeSeriesFile(TimeSeriesSet timeSeriesSet, String timeSeriesKey) {
|
||||||
|
return Arrays.stream(Objects.requireNonNull(Paths.get(timeSeriesDbPath, timeSeriesSet.getKey()).toFile().listFiles(f -> f.getName().equals(timeSeriesKey))))
|
||||||
|
.findAny()
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
}
|
26
src/main/java/ru/ulstu/db/DbService.java
Normal file
26
src/main/java/ru/ulstu/db/DbService.java
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* 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.db;
|
||||||
|
|
||||||
|
import ru.ulstu.datamodel.ts.TimeSeries;
|
||||||
|
import ru.ulstu.db.model.TimeSeriesMeta;
|
||||||
|
import ru.ulstu.db.model.TimeSeriesSet;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface DbService {
|
||||||
|
List<TimeSeriesSet> getSets();
|
||||||
|
|
||||||
|
List<TimeSeriesMeta> getTimeSeriesMeta(TimeSeriesSet timeSeriesSet) throws IOException;
|
||||||
|
|
||||||
|
TimeSeries getTimeSeries(TimeSeriesSet timeSeriesSet, String timeSeriesKey) throws IOException;
|
||||||
|
|
||||||
|
boolean addNewSet(String key);
|
||||||
|
|
||||||
|
boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries);
|
||||||
|
}
|
25
src/main/java/ru/ulstu/db/model/TimeSeriesMeta.java
Normal file
25
src/main/java/ru/ulstu/db/model/TimeSeriesMeta.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* 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.db.model;
|
||||||
|
|
||||||
|
public class TimeSeriesMeta {
|
||||||
|
private String key;
|
||||||
|
private int size;
|
||||||
|
private boolean hasDateTime;
|
||||||
|
|
||||||
|
public String getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSize() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isHasDateTime() {
|
||||||
|
return hasDateTime;
|
||||||
|
}
|
||||||
|
}
|
25
src/main/java/ru/ulstu/db/model/TimeSeriesSet.java
Normal file
25
src/main/java/ru/ulstu/db/model/TimeSeriesSet.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* 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.db.model;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class TimeSeriesSet {
|
||||||
|
private String key;
|
||||||
|
|
||||||
|
public TimeSeriesSet(File dir) {
|
||||||
|
this.key = dir.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TimeSeriesSet(String key) {
|
||||||
|
this.key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,13 @@
|
|||||||
|
#
|
||||||
|
# Copyright (C) 2021 Anton Romanov - All Rights Reserved
|
||||||
|
# You may use, distribute and modify this code, please write to: romanov73@gmail.com.
|
||||||
|
#
|
||||||
|
#
|
||||||
|
|
||||||
spring.main.banner-mode=off
|
spring.main.banner-mode=off
|
||||||
logging.level.tech.athene=DEBUG
|
logging.level.tech.athene=DEBUG
|
||||||
server.port=8080
|
server.port=8080
|
||||||
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
|
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
|
||||||
joinfaces.primefaces.theme=afterwork
|
joinfaces.primefaces.theme=afterwork
|
||||||
joinfaces.primefaces.font-awesome=true
|
joinfaces.primefaces.font-awesome=true
|
||||||
|
time-series.db-path=time-series-db
|
Loading…
Reference in New Issue
Block a user