adding dashbord
This commit is contained in:
@@ -15,4 +15,8 @@ public class NotFoundException extends RuntimeException {
|
||||
public NotFoundException(final Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public NotFoundException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.lucares.pdbui.dashboard;
|
||||
|
||||
public abstract class BaseDashboardWidget {
|
||||
|
||||
private DashboardWidgetType type;
|
||||
|
||||
private DashboardWidgetSize size;
|
||||
|
||||
public BaseDashboardWidget() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BaseDashboardWidget(final DashboardWidgetType type, final DashboardWidgetSize size) {
|
||||
this.type = type;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public DashboardWidgetType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(final DashboardWidgetType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public DashboardWidgetSize getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(final DashboardWidgetSize size) {
|
||||
this.size = size;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package org.lucares.pdbui.dashboard;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Dashboard {
|
||||
|
||||
public static class ByNameComparator implements Comparator<Dashboard> {
|
||||
@Override
|
||||
public int compare(final Dashboard o1, final Dashboard o2) {
|
||||
return o1.getName().compareToIgnoreCase(o2.getName());
|
||||
}
|
||||
}
|
||||
|
||||
public static final Comparator<Dashboard> BY_NAME = new ByNameComparator().thenComparing(Dashboard::getId);
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
private String description;
|
||||
|
||||
private List<TextWidget> texts = new ArrayList<>();
|
||||
|
||||
public Dashboard() {
|
||||
super();
|
||||
|
||||
}
|
||||
|
||||
public static Dashboard creatNew(final String name, final String description) {
|
||||
|
||||
final Dashboard dashboard = new Dashboard();
|
||||
dashboard.setId(UUID.randomUUID().toString());
|
||||
dashboard.setName(name);
|
||||
dashboard.setDescription(description);
|
||||
return dashboard;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(final String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public List<TextWidget> getTexts() {
|
||||
return texts;
|
||||
}
|
||||
|
||||
public void setTexts(final List<TextWidget> texts) {
|
||||
this.texts = texts;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package org.lucares.pdbui.dashboard;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.lucares.pdbui.BadRequest;
|
||||
import org.lucares.pdbui.NotFoundException;
|
||||
import org.lucares.utils.file.FileUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||
|
||||
@Controller
|
||||
@EnableAutoConfiguration
|
||||
@RequestMapping(path = "/api/dashboards")
|
||||
@CrossOrigin(origins = { "http://localhost:4200", "http://127.0.0.1:4200" })
|
||||
public class DashboardController {
|
||||
|
||||
private final Path baseDir;
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private final ObjectWriter objectWriter = objectMapper.writerWithDefaultPrettyPrinter();
|
||||
|
||||
public DashboardController(@Value("${db.base}") final String dbBaseDir) throws IOException {
|
||||
baseDir = Path.of(dbBaseDir).resolve("dashboards");
|
||||
Files.createDirectories(baseDir);
|
||||
objectMapper.setConfig(objectMapper.getSerializationConfig().with());
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/", //
|
||||
method = RequestMethod.POST, //
|
||||
consumes = MediaType.APPLICATION_JSON_VALUE, //
|
||||
produces = MediaType.APPLICATION_JSON_VALUE //
|
||||
)
|
||||
@ResponseBody
|
||||
@ResponseStatus(code = HttpStatus.CREATED)
|
||||
public Dashboard createDashboard(@RequestBody final DashboardCreationData creationData) throws IOException {
|
||||
|
||||
final Dashboard dashboard = Dashboard.creatNew(creationData.getName(), creationData.getDescription());
|
||||
return saveDashboard(dashboard);
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/", method = RequestMethod.GET, //
|
||||
produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
@ResponseBody
|
||||
public DashboardList getDashboards() throws IOException {
|
||||
|
||||
final List<Dashboard> dashboards = new ArrayList<>(Files.list(baseDir)//
|
||||
.filter(p -> p.getFileName().toString().endsWith(".json"))//
|
||||
.map(FileUtils::readSilent)//
|
||||
.map(this::deserializeDashboard)//
|
||||
.toList());
|
||||
|
||||
Collections.sort(dashboards, Dashboard.BY_NAME);
|
||||
|
||||
return new DashboardList(dashboards);
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/{dashboardId}", method = RequestMethod.GET, //
|
||||
produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
@ResponseBody
|
||||
public Dashboard getDashboard(@PathVariable("dashboardId") final String dashboardId) throws IOException {
|
||||
|
||||
final Path file = baseDir.resolve(dashboardId + ".json");
|
||||
if (!Files.isReadable(file) || !Files.isRegularFile(file)) {
|
||||
throw new NotFoundException("Not Found");
|
||||
}
|
||||
|
||||
final String json = Files.readString(file, StandardCharsets.UTF_8);
|
||||
return deserializeDashboard(json);
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/{dashboardId}", method = RequestMethod.PUT, //
|
||||
consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
@ResponseBody
|
||||
public Dashboard updateDashboard(@PathVariable("dashboardId") final String dashboardId,
|
||||
@RequestBody final Dashboard dashboard) throws IOException {
|
||||
|
||||
final Path file = baseDir.resolve(dashboardId + ".json");
|
||||
if (!Files.isReadable(file) || !Files.isRegularFile(file)) {
|
||||
throw new NotFoundException("Not Found");
|
||||
}
|
||||
if (!dashboardId.equals(dashboard.getId())) {
|
||||
throw new BadRequest("Inconsistent dashboard id in URL and payload");
|
||||
}
|
||||
|
||||
return saveDashboard(dashboard);
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/{dashboardId}", method = RequestMethod.DELETE, //
|
||||
produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
@ResponseBody
|
||||
public void deleteDashboard(@PathVariable("dashboardId") final String dashboardId) throws IOException {
|
||||
final Path file = baseDir.resolve(dashboardId + ".json");
|
||||
Files.deleteIfExists(file);
|
||||
}
|
||||
|
||||
private Dashboard deserializeDashboard(final String json) {
|
||||
try {
|
||||
return objectMapper.readValue(json, Dashboard.class);
|
||||
} catch (final JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Dashboard saveDashboard(final Dashboard dashboard) throws JsonProcessingException, IOException {
|
||||
final Path file = baseDir.resolve(dashboard.getId() + ".json");
|
||||
final String dashboardJson = objectWriter.writeValueAsString(dashboard);
|
||||
Files.writeString(file, dashboardJson, StandardCharsets.UTF_8);
|
||||
return dashboard;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.lucares.pdbui.dashboard;
|
||||
|
||||
public class DashboardCreationData {
|
||||
private String name;
|
||||
private String description;
|
||||
|
||||
public DashboardCreationData() {
|
||||
super();
|
||||
}
|
||||
|
||||
public DashboardCreationData(final String name, final String description) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(final String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.lucares.pdbui.dashboard;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DashboardList {
|
||||
|
||||
private List<Dashboard> dashboards;
|
||||
|
||||
public DashboardList() {
|
||||
super();
|
||||
}
|
||||
|
||||
public DashboardList(final List<Dashboard> dashboards) {
|
||||
this.dashboards = dashboards;
|
||||
}
|
||||
|
||||
public void setDashboards(final List<Dashboard> dashboards) {
|
||||
this.dashboards = dashboards;
|
||||
}
|
||||
|
||||
public List<Dashboard> getDashboards() {
|
||||
return dashboards;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.lucares.pdbui.dashboard;
|
||||
|
||||
public enum DashboardWidgetSize {
|
||||
SMALL, MEDIUM, LARGE
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.lucares.pdbui.dashboard;
|
||||
|
||||
public enum DashboardWidgetType {
|
||||
TEXT, PLOT
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.lucares.pdbui.dashboard;
|
||||
|
||||
public class PlotWidget extends BaseDashboardWidget {
|
||||
|
||||
public PlotWidget() {
|
||||
super();
|
||||
}
|
||||
|
||||
public PlotWidget(final DashboardWidgetSize size) {
|
||||
super(DashboardWidgetType.PLOT, size);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.lucares.pdbui.dashboard;
|
||||
|
||||
public class Position {
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
public Position() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Position(final int x, final int y) {
|
||||
super();
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(final int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(final int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return x + "," + y;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.lucares.pdbui.dashboard;
|
||||
|
||||
public class TextWidget extends BaseDashboardWidget {
|
||||
|
||||
private String text;
|
||||
|
||||
public TextWidget() {
|
||||
super();
|
||||
}
|
||||
|
||||
public TextWidget(final DashboardWidgetSize size, final String text) {
|
||||
super(DashboardWidgetType.TEXT, size);
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(final String text) {
|
||||
this.text = text;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user