From d237b78ba1358607ffc5ba50241d491e46fe0a8b Mon Sep 17 00:00:00 2001 From: joethei Date: Wed, 15 May 2019 15:14:54 +0200 Subject: [PATCH 1/4] + working version #SPM-28: add work 2h development --- .../spm/baudas/web/ShoppingTimesDiagram.java | 20 +++---- .../spm/baudas/web/WeekOverviewDiagram.java | 19 ++++--- src/main/webapp/footer.jsp | 6 ++- src/main/webapp/header.jsp | 10 +--- src/main/webapp/js/cache.js | 53 +++++++++++++++++++ src/main/webapp/js/materialize_components.js | 14 ----- src/main/webapp/js/shopping_times.js | 19 ++++--- src/main/webapp/js/week_overview.js | 27 +++++++--- 8 files changed, 107 insertions(+), 61 deletions(-) create mode 100644 src/main/webapp/js/cache.js delete mode 100644 src/main/webapp/js/materialize_components.js 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 41a491e..db4f44a 100644 --- a/src/main/java/de/hsel/spm/baudas/web/ShoppingTimesDiagram.java +++ b/src/main/java/de/hsel/spm/baudas/web/ShoppingTimesDiagram.java @@ -9,18 +9,11 @@ import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; -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.*; /** * changes data from shopping times diagram into a readable format for chart.js @@ -48,15 +41,16 @@ public class ShoppingTimesDiagram extends HttpServlet { Gson gson = new Gson(); - SavedFile file = Data.getFiles().peek(); - if (file == null) { + + if (req.getParameter("id") == null) { result.put("labels", definedOrder); out.print(gson.toJson(result)); return; } + UUID uuid = UUID.fromString(req.getParameter("id")); + File file = Data.get(uuid); - assert file.getUuid() != null; - ShoppingTimes shoppingTimes = new ShoppingTimes(Data.get(file.getUuid())); + ShoppingTimes shoppingTimes = new ShoppingTimes(file); Map map = shoppingTimes.getResult(); Set days = new TreeSet<>(); 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 905c8a5..a9321a7 100644 --- a/src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java +++ b/src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java @@ -8,15 +8,11 @@ import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; -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.*; /** * changes data from week overview into readable format for chart.js @@ -43,16 +39,19 @@ public class WeekOverviewDiagram extends HttpServlet { Gson gson = new Gson(); - SavedFile file = Data.getFiles().peek(); - if (file == null) { + + if (req.getParameter("id") == null) { result.put("labels", definedOrder); result.put("count", new ArrayList<>()); result.put("revenue", new ArrayList<>()); out.print(gson.toJson(result)); return; } - assert file.getUuid() != null; - WeekOverview overview = new WeekOverview(Data.get(file.getUuid())); + + UUID uuid = UUID.fromString(req.getParameter("id")); + File file = Data.get(uuid); + + WeekOverview overview = new WeekOverview(file); List count = new ArrayList<>(); List revenue = new ArrayList<>(); diff --git a/src/main/webapp/footer.jsp b/src/main/webapp/footer.jsp index e74138a..e6dd7bc 100644 --- a/src/main/webapp/footer.jsp +++ b/src/main/webapp/footer.jsp @@ -7,7 +7,11 @@ - + + + diff --git a/src/main/webapp/header.jsp b/src/main/webapp/header.jsp index c7bd797..3594b19 100644 --- a/src/main/webapp/header.jsp +++ b/src/main/webapp/header.jsp @@ -79,15 +79,9 @@
  • - +
    -
    diff --git a/src/main/webapp/js/cache.js b/src/main/webapp/js/cache.js new file mode 100644 index 0000000..23d1187 --- /dev/null +++ b/src/main/webapp/js/cache.js @@ -0,0 +1,53 @@ +$(document).ready(function () { + let dataset = $('#dataset'); + $.ajax({ + url: 'files', + dataType: 'json' + }).done(function (results) { + dataset.empty(); + for (const result of results) { + dataset.append($("") + .attr("value", result.uuid) + .text(result.name)); + } + dataset.formSelect(); + }); + updateWeekOverviewChart(); + updateShoppingTimesChart(); +}); + +function updateCache() { + let selected = $('#dataset :selected'); + + if (selected.index() !== -1) { + updateWeekOverviewChart(selected.val()); + updateShoppingTimesChart(selected.val()); + } +} + +function request(url, id) { + return new Promise(((resolve, reject) => { + if (typeof id !== 'undefined') { + requestInternal(url + '?id=' + id).then(function (data) { + resolve(data); + }); + } else { + requestInternal(url).then(function (data) { + resolve(data); + }); + } + + })); +} + +function requestInternal(url) { + return new Promise(((resolve, reject) => { + $.ajax({ + url: url, + dataType: 'json' + }).done(function (results) { + resolve(results); + }); + })); + +} \ No newline at end of file diff --git a/src/main/webapp/js/materialize_components.js b/src/main/webapp/js/materialize_components.js deleted file mode 100644 index c788aff..0000000 --- a/src/main/webapp/js/materialize_components.js +++ /dev/null @@ -1,14 +0,0 @@ -$(".dropdown-trigger").dropdown(); -$('.tabs').tabs(); -$(document).ready(function(){ - $('.sidenav').sidenav(); -}); -$(document).ready(function(){ - $('select').formSelect(); -}); -$(document).ready(function(){ - $('.collapsible').collapsible(); -}); -$(document).ready(function() { - M.updateTextFields(); -}); \ No newline at end of file diff --git a/src/main/webapp/js/shopping_times.js b/src/main/webapp/js/shopping_times.js index 6a745b1..02f8047 100644 --- a/src/main/webapp/js/shopping_times.js +++ b/src/main/webapp/js/shopping_times.js @@ -52,13 +52,18 @@ let shopping_chart = new Chart(document.getElementById("shopping_times_chart"), } }); -$.ajax({ - url: 'shopping_times', - dataType: 'json' -}).done(function (results) { - shopping_result = results; - updateDays(); -}); +function updateShoppingTimesChart(id) { + if(typeof id !== 'undefined') { + request('shopping_times', id).then(function (data) { + shopping_result = data; + updateDays(); + }); + }else request('shopping_times').then(function(data) { + shopping_result = data; + updateDays(); + }); +} + function updateDays() { if(typeof shopping_result["Montag"] !== 'undefined') { diff --git a/src/main/webapp/js/week_overview.js b/src/main/webapp/js/week_overview.js index 9d44521..dc5bc92 100644 --- a/src/main/webapp/js/week_overview.js +++ b/src/main/webapp/js/week_overview.js @@ -1,3 +1,5 @@ +let week_result; + let week_overview = new Chart(document.getElementById("overview_chart"), { type: 'line', data: { @@ -52,14 +54,23 @@ let week_overview = new Chart(document.getElementById("overview_chart"), { } }); -$.ajax({ - url: 'week_overview', - dataType: 'json' -}).done(function (results) { - week_overview.data.labels = results.labels; - week_overview.data.datasets[0].data = results.count; - week_overview.data.datasets[1].data = results.revenue; +function updateWeekOverviewChart(id) { + if(typeof id !== 'undefined') { + request('week_overview', id).then(function (data) { + week_result = data; + updateWeekOverview(); + }); + }else request('week_overview').then(function(data) { + week_result = data; + updateWeekOverview(); + }); +} +function updateWeekOverview() { + + week_overview.data.labels = week_result.labels; + week_overview.data.datasets[0].data = week_result.count; + week_overview.data.datasets[1].data = week_result.revenue; week_overview.update(); -}); \ No newline at end of file +} \ No newline at end of file From e67c30532292d2d8cfe171a4e3575b47ef0908d8 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 15 May 2019 20:41:09 +0200 Subject: [PATCH 2/4] + documentation Signed-off-by: Johannes Theiner #SPM-28: add work 1h documentation --- src/main/webapp/index.jsp | 2 +- src/main/webapp/js/cache.js | 22 ++++++++++++++++------ src/main/webapp/js/shopping_times.js | 7 +++---- src/main/webapp/js/week_overview.js | 9 ++++----- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index c0ccd3c..8cf6ee5 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -34,7 +34,7 @@
    - diff --git a/src/main/webapp/js/cache.js b/src/main/webapp/js/cache.js index 23d1187..6f5f803 100644 --- a/src/main/webapp/js/cache.js +++ b/src/main/webapp/js/cache.js @@ -1,9 +1,6 @@ $(document).ready(function () { let dataset = $('#dataset'); - $.ajax({ - url: 'files', - dataType: 'json' - }).done(function (results) { + request('files').then(results => { dataset.empty(); for (const result of results) { dataset.append($("") @@ -16,15 +13,24 @@ $(document).ready(function () { updateShoppingTimesChart(); }); +/** + * update all charts with correct values for dataset + */ function updateCache() { let selected = $('#dataset :selected'); - if (selected.index() !== -1) { updateWeekOverviewChart(selected.val()); updateShoppingTimesChart(selected.val()); } + } +/** + * request json data + * @param url base url + * @param id dataset id + * @returns {Promise} + */ function request(url, id) { return new Promise(((resolve, reject) => { if (typeof id !== 'undefined') { @@ -36,10 +42,14 @@ function request(url, id) { resolve(data); }); } - })); } +/** + * only for use inside this file, javascript has no better way + * @param url url + * @returns {Promise} + */ function requestInternal(url) { return new Promise(((resolve, reject) => { $.ajax({ diff --git a/src/main/webapp/js/shopping_times.js b/src/main/webapp/js/shopping_times.js index 02f8047..c5d85dc 100644 --- a/src/main/webapp/js/shopping_times.js +++ b/src/main/webapp/js/shopping_times.js @@ -56,16 +56,15 @@ function updateShoppingTimesChart(id) { if(typeof id !== 'undefined') { request('shopping_times', id).then(function (data) { shopping_result = data; - updateDays(); + changeDay(); }); }else request('shopping_times').then(function(data) { shopping_result = data; - updateDays(); + changeDay(); }); } - -function updateDays() { +function changeDay() { if(typeof shopping_result["Montag"] !== 'undefined') { let e = document.getElementById("day"); let value = e.options[e.selectedIndex].text; diff --git a/src/main/webapp/js/week_overview.js b/src/main/webapp/js/week_overview.js index dc5bc92..ee3d77c 100644 --- a/src/main/webapp/js/week_overview.js +++ b/src/main/webapp/js/week_overview.js @@ -1,6 +1,6 @@ let week_result; -let week_overview = new Chart(document.getElementById("overview_chart"), { +let week_overview = new Chart($("#overview_chart"), { type: 'line', data: { labels: [0, 0, 0, 0, 0], @@ -55,19 +55,18 @@ let week_overview = new Chart(document.getElementById("overview_chart"), { }); function updateWeekOverviewChart(id) { - - if(typeof id !== 'undefined') { + if (typeof id !== 'undefined') { request('week_overview', id).then(function (data) { week_result = data; updateWeekOverview(); }); - }else request('week_overview').then(function(data) { + } else request('week_overview').then(function (data) { week_result = data; updateWeekOverview(); }); } -function updateWeekOverview() { +function updateWeekOverview() { week_overview.data.labels = week_result.labels; week_overview.data.datasets[0].data = week_result.count; week_overview.data.datasets[1].data = week_result.revenue; From 6f5eb4b24f384bfe503293d1c898c1cbb2b444db Mon Sep 17 00:00:00 2001 From: joethei Date: Thu, 16 May 2019 09:32:58 +0200 Subject: [PATCH 3/4] + 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' From d58de232f24a47009790eed79f94961806ae24c4 Mon Sep 17 00:00:00 2001 From: joethei Date: Thu, 16 May 2019 14:54:11 +0200 Subject: [PATCH 4/4] ~ colors #SPM-28: add work 10m development --- src/main/webapp/header.jsp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/webapp/header.jsp b/src/main/webapp/header.jsp index 3594b19..77aceae 100644 --- a/src/main/webapp/header.jsp +++ b/src/main/webapp/header.jsp @@ -23,11 +23,11 @@