From 5674f5f27fae0b2be63ace53f23ab9e0f2054fdd Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 26 Jun 2021 13:07:05 +0400 Subject: [PATCH] #5 -- add controller --- .../ru/ulstu/controller/DbController.java | 69 ++++++++++++++++++ .../ru/ulstu/datamodel/ts/TimeSeries.java | 3 + src/main/java/ru/ulstu/db/DbFileService.java | 70 ++++++++++++++++--- src/main/java/ru/ulstu/db/DbService.java | 6 +- src/main/resources/application.properties | 2 +- 5 files changed, 139 insertions(+), 11 deletions(-) create mode 100644 src/main/java/ru/ulstu/controller/DbController.java diff --git a/src/main/java/ru/ulstu/controller/DbController.java b/src/main/java/ru/ulstu/controller/DbController.java new file mode 100644 index 0000000..13c6ca5 --- /dev/null +++ b/src/main/java/ru/ulstu/controller/DbController.java @@ -0,0 +1,69 @@ +/* + * 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.controller; + +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import ru.ulstu.configuration.ApiConfiguration; +import ru.ulstu.datamodel.ts.TimeSeries; +import ru.ulstu.db.DbService; +import ru.ulstu.db.model.TimeSeriesMeta; +import ru.ulstu.db.model.TimeSeriesSet; + +import java.io.IOException; +import java.util.List; + +@RestController +@RequestMapping(ApiConfiguration.API_1_0) +public class DbController { + + private final DbService dbService; + + public DbController(DbService dbService) { + this.dbService = dbService; + } + + @GetMapping("get-time-series-sets") + public List getTimeSeriesSets() { + return dbService.getSets(); + } + + @GetMapping("get-time-series-meta") + public List getTimeSeriesSets(@RequestParam("setKey") String setKey) throws IOException { + return dbService.getTimeSeriesMeta(new TimeSeriesSet(setKey)); + } + + @GetMapping("get-time-series") + public TimeSeries getTimeSeries(@RequestParam("setKey") String setKey, @RequestParam("timeSeriesKey") String timeSeriesKey) throws IOException { + return dbService.getTimeSeries(new TimeSeriesSet(setKey), timeSeriesKey); + } + + @GetMapping("add-time-series-set") + public void addTimeSeriesSet(@RequestParam("setKey") String setKey) { + dbService.addSet(setKey); + } + + @PostMapping("add-time-series") + public void addTimeSeries(@RequestParam("setKey") String setKey, @RequestBody TimeSeries timeSeries) throws IOException { + dbService.addTimeSeries(new TimeSeriesSet(setKey), timeSeries); + } + + @DeleteMapping("delete-time-series") + public void deleteTimeSeries(@RequestParam("setKey") String setKey, @RequestParam("timeSeriesKey") String timeSeriesKey) throws IOException { + dbService.deleteTimeSeries(new TimeSeriesSet(setKey), timeSeriesKey); + } + + @DeleteMapping("delete-time-series-set") + public void deleteTimeSeriesSet(@RequestParam("setKey") String setKey) throws IOException { + dbService.deleteTimeSeriesSet(new TimeSeriesSet(setKey)); + } +} diff --git a/src/main/java/ru/ulstu/datamodel/ts/TimeSeries.java b/src/main/java/ru/ulstu/datamodel/ts/TimeSeries.java index 1e5ee54..237be7b 100644 --- a/src/main/java/ru/ulstu/datamodel/ts/TimeSeries.java +++ b/src/main/java/ru/ulstu/datamodel/ts/TimeSeries.java @@ -7,6 +7,7 @@ package ru.ulstu.datamodel.ts; import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import java.util.ArrayList; @@ -66,6 +67,7 @@ public class TimeSeries { values.add(new TimeSeriesValue(basedOnValue.getDate(), value)); } + @JsonIgnore public TimeSeriesValue getLastValue() { return values.get(values.size() - 1); } @@ -74,6 +76,7 @@ public class TimeSeries { return values.size(); } + @JsonIgnore public TimeSeriesValue getFirstValue() { if ((values.size() > 0)) { return values.get(0); diff --git a/src/main/java/ru/ulstu/db/DbFileService.java b/src/main/java/ru/ulstu/db/DbFileService.java index f5ef631..14b7be7 100644 --- a/src/main/java/ru/ulstu/db/DbFileService.java +++ b/src/main/java/ru/ulstu/db/DbFileService.java @@ -17,9 +17,11 @@ import ru.ulstu.service.UtilService; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; +import java.io.FileFilter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; @@ -44,15 +46,16 @@ public class DbFileService implements DbService { validateTimeSeriesSet(timeSeriesSet); List list = new ArrayList<>(); for (File file : getTimeSeriesMetaFiles(timeSeriesSet)) { - list.add(new ObjectMapper().readValue(Paths.get(timeSeriesDbPath, file.getName()).toFile(), TimeSeriesMeta.class)); + list.add(new ObjectMapper() + .readValue(Paths.get(getSetPath(timeSeriesSet).getAbsolutePath(), file.getName()) + .toFile(), TimeSeriesMeta.class)); } return list; } @Override public TimeSeries getTimeSeries(TimeSeriesSet timeSeriesSet, String timeSeriesKey) throws IOException { - validateTimeSeriesSet(timeSeriesSet); - validateTimeSeries(timeSeriesSet, timeSeriesKey); + validateDb(timeSeriesSet, timeSeriesKey); File timeSeriesFile = getTimeSeriesFile(timeSeriesSet, timeSeriesKey); BufferedReader csvReader = new BufferedReader(new FileReader(timeSeriesFile)); String row; @@ -68,7 +71,7 @@ public class DbFileService implements DbService { } @Override - public boolean addNewSet(String key) { + public boolean addSet(String key) { TimeSeriesSet timeSeriesSet = new TimeSeriesSet(key); if (isTimeSeriesSetExists(timeSeriesSet)) { throw new RuntimeException(String.format("Time series set %s already exists", timeSeriesSet.getKey())); @@ -80,12 +83,37 @@ public class DbFileService implements DbService { @Override public boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) throws IOException { validateTimeSeriesSet(timeSeriesSet); - BufferedWriter writer = new BufferedWriter(new FileWriter(Paths.get(timeSeriesDbPath, timeSeriesSet.getKey()).toFile())); + BufferedWriter writer = new BufferedWriter(new FileWriter(Paths.get(getSetPath(timeSeriesSet).getAbsolutePath(), timeSeries.getName() + ".csv").toFile())); writer.write(new UtilService().getTimeSeriesToDateValueString(timeSeries)); writer.close(); return true; } + @Override + public void deleteTimeSeries(TimeSeriesSet set, String timeSeriesKey) throws IOException { + validateDb(set, timeSeriesKey); + Files.delete(getTimeSeriesFile(set, timeSeriesKey).toPath()); + } + + @Override + public void deleteTimeSeriesSet(TimeSeriesSet timeSeriesSet) throws IOException { + validateDb(timeSeriesSet); + deleteDirectory(getSetPath(timeSeriesSet)); + } + + private void validateDb(TimeSeriesSet timeSeriesSet, String timeSeriesKey) { + validateDb(timeSeriesSet); + validateTimeSeries(timeSeriesSet, timeSeriesKey); + } + + private void validateDb(TimeSeriesSet timeSeriesSet) { + File dbPath = new File(timeSeriesDbPath); + if (!dbPath.exists()) { + throw new RuntimeException(String.format("Time series db path not exists: %s", dbPath.getAbsolutePath())); + } + validateTimeSeriesSet(timeSeriesSet); + } + private void validateTimeSeriesSet(TimeSeriesSet timeSeriesSet) { if (!isTimeSeriesSetExists(timeSeriesSet)) { throw new RuntimeException(String.format("Time series set %s not exists", timeSeriesSet.getKey())); @@ -93,7 +121,7 @@ public class DbFileService implements DbService { } 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()) { + if (Arrays.stream(getSetPath(timeSeriesSet).listFiles(getFileFilter(timeSeriesKey, ".csv"))).findAny().isEmpty()) { throw new RuntimeException(String.format("Time series %s not exists", timeSeriesKey)); } } @@ -104,12 +132,36 @@ public class DbFileService implements DbService { } private List getTimeSeriesMetaFiles(TimeSeriesSet timeSeriesSet) { - return Arrays.asList(Objects.requireNonNull(Paths.get(timeSeriesDbPath, timeSeriesSet.getKey()).toFile().listFiles(f -> f.getName().endsWith(".meta")))); + return Arrays.asList(getSetPath(timeSeriesSet).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)))) + return Arrays.stream(getSetPath(timeSeriesSet).listFiles(getFileFilter(timeSeriesKey, ".csv"))) .findAny() - .orElse(null); + .orElseThrow(() -> new RuntimeException(String.format("Time series file not found: %s", timeSeriesKey))); + } + + private File getTimeSeriesMetaFile(TimeSeriesSet timeSeriesSet, String timeSeriesKey) { + return Arrays.stream(getSetPath(timeSeriesSet).listFiles(getFileFilter(timeSeriesKey, ".csv.meta"))) + .findAny() + .orElseThrow(() -> new RuntimeException(String.format("Time series meta file not found: %s", timeSeriesKey))); + } + + private File getSetPath(TimeSeriesSet timeSeriesSet) { + return Paths.get(timeSeriesDbPath, timeSeriesSet.getKey()).toFile(); + } + + private FileFilter getFileFilter(String timeSeriesKey, String extension) { + return f -> f.getName().equals(timeSeriesKey + extension); + } + + private boolean deleteDirectory(File directoryToBeDeleted) { + File[] allContents = directoryToBeDeleted.listFiles(); + if (allContents != null) { + for (File file : allContents) { + deleteDirectory(file); + } + } + return directoryToBeDeleted.delete(); } } diff --git a/src/main/java/ru/ulstu/db/DbService.java b/src/main/java/ru/ulstu/db/DbService.java index b5b58de..d88af85 100644 --- a/src/main/java/ru/ulstu/db/DbService.java +++ b/src/main/java/ru/ulstu/db/DbService.java @@ -20,7 +20,11 @@ public interface DbService { TimeSeries getTimeSeries(TimeSeriesSet timeSeriesSet, String timeSeriesKey) throws IOException; - boolean addNewSet(String key); + boolean addSet(String key); boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) throws IOException; + + void deleteTimeSeries(TimeSeriesSet set, String timeSeriesKey) throws IOException; + + void deleteTimeSeriesSet(TimeSeriesSet timeSeriesSet) throws IOException; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 125dbda..ee0c2c7 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -10,4 +10,4 @@ server.port=8080 spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false joinfaces.primefaces.theme=afterwork joinfaces.primefaces.font-awesome=true -time-series.db-path=time-series-db \ No newline at end of file +time-series.db-path=d:/tmp/time-series-db \ No newline at end of file