diff --git a/src/main/java/ru/ulstu/db/DbFileService.java b/src/main/java/ru/ulstu/db/DbFileService.java new file mode 100644 index 0000000..a55e9e7 --- /dev/null +++ b/src/main/java/ru/ulstu/db/DbFileService.java @@ -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 getSets() { + return Arrays.stream(Objects.requireNonNull(new File(timeSeriesDbPath).listFiles(File::isDirectory))) + .map(TimeSeriesSet::new) + .collect(Collectors.toList()); + } + + @Override + public List getTimeSeriesMeta(TimeSeriesSet timeSeriesSet) throws IOException { + validateTimeSeriesSet(timeSeriesSet); + List 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 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); + } +} diff --git a/src/main/java/ru/ulstu/db/DbService.java b/src/main/java/ru/ulstu/db/DbService.java new file mode 100644 index 0000000..4fc121c --- /dev/null +++ b/src/main/java/ru/ulstu/db/DbService.java @@ -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 getSets(); + + List getTimeSeriesMeta(TimeSeriesSet timeSeriesSet) throws IOException; + + TimeSeries getTimeSeries(TimeSeriesSet timeSeriesSet, String timeSeriesKey) throws IOException; + + boolean addNewSet(String key); + + boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries); +} diff --git a/src/main/java/ru/ulstu/db/model/TimeSeriesMeta.java b/src/main/java/ru/ulstu/db/model/TimeSeriesMeta.java new file mode 100644 index 0000000..a885d1f --- /dev/null +++ b/src/main/java/ru/ulstu/db/model/TimeSeriesMeta.java @@ -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; + } +} diff --git a/src/main/java/ru/ulstu/db/model/TimeSeriesSet.java b/src/main/java/ru/ulstu/db/model/TimeSeriesSet.java new file mode 100644 index 0000000..ed014f7 --- /dev/null +++ b/src/main/java/ru/ulstu/db/model/TimeSeriesSet.java @@ -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; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8d0c45f..125dbda 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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 logging.level.tech.athene=DEBUG server.port=8080 spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false joinfaces.primefaces.theme=afterwork -joinfaces.primefaces.font-awesome=true \ No newline at end of file +joinfaces.primefaces.font-awesome=true +time-series.db-path=time-series-db \ No newline at end of file