#5 -- add controller
This commit is contained in:
parent
5b325a122a
commit
5674f5f27f
69
src/main/java/ru/ulstu/controller/DbController.java
Normal file
69
src/main/java/ru/ulstu/controller/DbController.java
Normal file
@ -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<TimeSeriesSet> getTimeSeriesSets() {
|
||||||
|
return dbService.getSets();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("get-time-series-meta")
|
||||||
|
public List<TimeSeriesMeta> 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));
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +7,7 @@
|
|||||||
package ru.ulstu.datamodel.ts;
|
package ru.ulstu.datamodel.ts;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -66,6 +67,7 @@ public class TimeSeries {
|
|||||||
values.add(new TimeSeriesValue(basedOnValue.getDate(), value));
|
values.add(new TimeSeriesValue(basedOnValue.getDate(), value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JsonIgnore
|
||||||
public TimeSeriesValue getLastValue() {
|
public TimeSeriesValue getLastValue() {
|
||||||
return values.get(values.size() - 1);
|
return values.get(values.size() - 1);
|
||||||
}
|
}
|
||||||
@ -74,6 +76,7 @@ public class TimeSeries {
|
|||||||
return values.size();
|
return values.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JsonIgnore
|
||||||
public TimeSeriesValue getFirstValue() {
|
public TimeSeriesValue getFirstValue() {
|
||||||
if ((values.size() > 0)) {
|
if ((values.size() > 0)) {
|
||||||
return values.get(0);
|
return values.get(0);
|
||||||
|
@ -17,9 +17,11 @@ import ru.ulstu.service.UtilService;
|
|||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileFilter;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -44,15 +46,16 @@ public class DbFileService implements DbService {
|
|||||||
validateTimeSeriesSet(timeSeriesSet);
|
validateTimeSeriesSet(timeSeriesSet);
|
||||||
List<TimeSeriesMeta> list = new ArrayList<>();
|
List<TimeSeriesMeta> list = new ArrayList<>();
|
||||||
for (File file : getTimeSeriesMetaFiles(timeSeriesSet)) {
|
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;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TimeSeries getTimeSeries(TimeSeriesSet timeSeriesSet, String timeSeriesKey) throws IOException {
|
public TimeSeries getTimeSeries(TimeSeriesSet timeSeriesSet, String timeSeriesKey) throws IOException {
|
||||||
validateTimeSeriesSet(timeSeriesSet);
|
validateDb(timeSeriesSet, timeSeriesKey);
|
||||||
validateTimeSeries(timeSeriesSet, timeSeriesKey);
|
|
||||||
File timeSeriesFile = getTimeSeriesFile(timeSeriesSet, timeSeriesKey);
|
File timeSeriesFile = getTimeSeriesFile(timeSeriesSet, timeSeriesKey);
|
||||||
BufferedReader csvReader = new BufferedReader(new FileReader(timeSeriesFile));
|
BufferedReader csvReader = new BufferedReader(new FileReader(timeSeriesFile));
|
||||||
String row;
|
String row;
|
||||||
@ -68,7 +71,7 @@ public class DbFileService implements DbService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean addNewSet(String key) {
|
public boolean addSet(String key) {
|
||||||
TimeSeriesSet timeSeriesSet = new TimeSeriesSet(key);
|
TimeSeriesSet timeSeriesSet = new TimeSeriesSet(key);
|
||||||
if (isTimeSeriesSetExists(timeSeriesSet)) {
|
if (isTimeSeriesSetExists(timeSeriesSet)) {
|
||||||
throw new RuntimeException(String.format("Time series set %s already exists", timeSeriesSet.getKey()));
|
throw new RuntimeException(String.format("Time series set %s already exists", timeSeriesSet.getKey()));
|
||||||
@ -80,12 +83,37 @@ public class DbFileService implements DbService {
|
|||||||
@Override
|
@Override
|
||||||
public boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) throws IOException {
|
public boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) throws IOException {
|
||||||
validateTimeSeriesSet(timeSeriesSet);
|
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.write(new UtilService().getTimeSeriesToDateValueString(timeSeries));
|
||||||
writer.close();
|
writer.close();
|
||||||
return true;
|
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) {
|
private void validateTimeSeriesSet(TimeSeriesSet timeSeriesSet) {
|
||||||
if (!isTimeSeriesSetExists(timeSeriesSet)) {
|
if (!isTimeSeriesSetExists(timeSeriesSet)) {
|
||||||
throw new RuntimeException(String.format("Time series set %s not exists", timeSeriesSet.getKey()));
|
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) {
|
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));
|
throw new RuntimeException(String.format("Time series %s not exists", timeSeriesKey));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -104,12 +132,36 @@ public class DbFileService implements DbService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private List<File> getTimeSeriesMetaFiles(TimeSeriesSet timeSeriesSet) {
|
private List<File> 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) {
|
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()
|
.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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,11 @@ public interface DbService {
|
|||||||
|
|
||||||
TimeSeries getTimeSeries(TimeSeriesSet timeSeriesSet, String timeSeriesKey) throws IOException;
|
TimeSeries getTimeSeries(TimeSeriesSet timeSeriesSet, String timeSeriesKey) throws IOException;
|
||||||
|
|
||||||
boolean addNewSet(String key);
|
boolean addSet(String key);
|
||||||
|
|
||||||
boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) throws IOException;
|
boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) throws IOException;
|
||||||
|
|
||||||
|
void deleteTimeSeries(TimeSeriesSet set, String timeSeriesKey) throws IOException;
|
||||||
|
|
||||||
|
void deleteTimeSeriesSet(TimeSeriesSet timeSeriesSet) throws IOException;
|
||||||
}
|
}
|
||||||
|
@ -10,4 +10,4 @@ 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
|
time-series.db-path=d:/tmp/time-series-db
|
Loading…
Reference in New Issue
Block a user