From 6f5eb4b24f384bfe503293d1c898c1cbb2b444db Mon Sep 17 00:00:00 2001 From: joethei Date: Thu, 16 May 2019 09:32:58 +0200 Subject: [PATCH] + add upload date to file selection + select latest file if no selection is provided #SPM-28: add work 1h 20m development --- .gitignore | 3 ++- .../java/de/hsel/spm/baudas/web/Data.java | 9 ++++---- .../de/hsel/spm/baudas/web/SavedFile.java | 2 ++ .../spm/baudas/web/ShoppingTimesDiagram.java | 11 ++++++++- .../spm/baudas/web/WeekOverviewDiagram.java | 8 ++++++- src/main/webapp/js/cache.js | 23 ++++++++++++++----- 6 files changed, 43 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 50af0c3..1fe301e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .idea .project target -.vscode \ No newline at end of file +.vscode +*.csv \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/web/Data.java b/src/main/java/de/hsel/spm/baudas/web/Data.java index d5f47e2..6c8d159 100644 --- a/src/main/java/de/hsel/spm/baudas/web/Data.java +++ b/src/main/java/de/hsel/spm/baudas/web/Data.java @@ -9,6 +9,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.time.LocalDateTime; import java.util.UUID; import java.util.concurrent.ConcurrentLinkedQueue; @@ -36,7 +37,7 @@ public class Data { //cleanup old files if (files.isEmpty()) { try { - Files.list(Paths.get("target/")).forEach(path -> { + Files.list(Paths.get("")).forEach(path -> { if (path.toFile().getName().endsWith(".csv")) { try { Files.delete(path); @@ -60,11 +61,11 @@ public class Data { System.out.println("failed to delete file..."); } path = Paths.get(getFileName(uuid)); - files.offer(new SavedFile(uuid, name)); + files.offer(new SavedFile(uuid, name, LocalDateTime.now())); } else { - files.add(new SavedFile(uuid, name)); + files.add(new SavedFile(uuid, name, LocalDateTime.now())); path = Paths.get(getFileName(uuid)); } return path; @@ -91,6 +92,6 @@ public class Data { @Contract(pure = true) @NotNull private static String getFileName(@NotNull UUID uuid) { - return "target/" + uuid + ".csv"; + return uuid + ".csv"; } } \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/web/SavedFile.java b/src/main/java/de/hsel/spm/baudas/web/SavedFile.java index f568faf..c0ae3fa 100644 --- a/src/main/java/de/hsel/spm/baudas/web/SavedFile.java +++ b/src/main/java/de/hsel/spm/baudas/web/SavedFile.java @@ -4,6 +4,7 @@ import lombok.AllArgsConstructor; import lombok.Getter; import lombok.ToString; +import java.time.LocalDateTime; import java.util.UUID; /** @@ -20,4 +21,5 @@ class SavedFile { private UUID uuid; private String name; + private LocalDateTime date; } \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/web/ShoppingTimesDiagram.java b/src/main/java/de/hsel/spm/baudas/web/ShoppingTimesDiagram.java index db4f44a..11b6c69 100644 --- a/src/main/java/de/hsel/spm/baudas/web/ShoppingTimesDiagram.java +++ b/src/main/java/de/hsel/spm/baudas/web/ShoppingTimesDiagram.java @@ -13,7 +13,16 @@ import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; +import java.util.UUID; /** * changes data from shopping times diagram into a readable format for chart.js diff --git a/src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java b/src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java index a9321a7..9230839 100644 --- a/src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java +++ b/src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java @@ -12,7 +12,13 @@ import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; /** * changes data from week overview into readable format for chart.js diff --git a/src/main/webapp/js/cache.js b/src/main/webapp/js/cache.js index 6f5f803..a50e666 100644 --- a/src/main/webapp/js/cache.js +++ b/src/main/webapp/js/cache.js @@ -5,22 +5,33 @@ $(document).ready(function () { for (const result of results) { dataset.append($("") .attr("value", result.uuid) - .text(result.name)); + .text(result.name + " (" + result.date.date.day + "." + result.date.date.month + " " + + result.date.time.hour + ":" + result.date.time.minute + ")")); } dataset.formSelect(); }); - updateWeekOverviewChart(); - updateShoppingTimesChart(); + updateCache(); }); /** * update all charts with correct values for dataset */ function updateCache() { + let dataset = $('#dataset'); let selected = $('#dataset :selected'); - if (selected.index() !== -1) { + if (typeof selected !== 'undefined' && selected.index() !== -1) { updateWeekOverviewChart(selected.val()); updateShoppingTimesChart(selected.val()); + } else { + //requesting files, getting values from html does not work... + //if no selection is provided, select newest file + request('files').then(results => { + let uuid = results[results.length - 1].uuid; + dataset.val(uuid); + updateWeekOverviewChart(uuid); + updateShoppingTimesChart(uuid); + dataset.formSelect(); + }); } } @@ -32,7 +43,7 @@ function updateCache() { * @returns {Promise} */ function request(url, id) { - return new Promise(((resolve, reject) => { + return new Promise(((resolve) => { if (typeof id !== 'undefined') { requestInternal(url + '?id=' + id).then(function (data) { resolve(data); @@ -51,7 +62,7 @@ function request(url, id) { * @returns {Promise} */ function requestInternal(url) { - return new Promise(((resolve, reject) => { + return new Promise(((resolve) => { $.ajax({ url: url, dataType: 'json'