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 11b6c69..ed40db8 100644 --- a/src/main/java/de/hsel/spm/baudas/web/ShoppingTimesDiagram.java +++ b/src/main/java/de/hsel/spm/baudas/web/ShoppingTimesDiagram.java @@ -23,6 +23,7 @@ import java.util.Map; import java.util.Set; import java.util.TreeSet; import java.util.UUID; +import java.util.regex.Pattern; /** * changes data from shopping times diagram into a readable format for chart.js @@ -34,6 +35,8 @@ import java.util.UUID; @WebServlet("/shopping_times") public class ShoppingTimesDiagram extends HttpServlet { + private static Map cache = new HashMap<>(); + private static final long serialVersionUID = 6567531484L; @Override @@ -50,13 +53,21 @@ public class ShoppingTimesDiagram extends HttpServlet { Gson gson = new Gson(); - - if (req.getParameter("id") == null) { - result.put("labels", definedOrder); - out.print(gson.toJson(result)); + String id = req.getParameter("id"); + boolean match = Pattern.matches("[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}", id); + if (id == null || !match) { return; } - UUID uuid = UUID.fromString(req.getParameter("id")); + + UUID uuid = UUID.fromString(id); + + + if(cache.containsKey(uuid)) { + out.print(gson.toJson(cache.get(uuid))); + return; + } + + File file = Data.get(uuid); ShoppingTimes shoppingTimes = new ShoppingTimes(file); @@ -81,7 +92,7 @@ public class ShoppingTimesDiagram extends HttpServlet { } } - + cache.put(uuid, result); out.print(gson.toJson(result)); } diff --git a/src/main/java/de/hsel/spm/baudas/web/TopFlopArticleDiagram.java b/src/main/java/de/hsel/spm/baudas/web/TopFlopArticleDiagram.java index 0e0abe3..77d9a17 100644 --- a/src/main/java/de/hsel/spm/baudas/web/TopFlopArticleDiagram.java +++ b/src/main/java/de/hsel/spm/baudas/web/TopFlopArticleDiagram.java @@ -17,6 +17,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.*; +import java.util.regex.Pattern; /** * top/flop articles list diagram. @@ -29,6 +31,8 @@ import java.util.UUID; @WebServlet("/top_flop") public class TopFlopArticleDiagram extends HttpServlet { + private static Map cache = new HashMap<>(); + private static final long serialVersionUID = 6567531464214L; @Override @@ -40,7 +44,19 @@ public class TopFlopArticleDiagram extends HttpServlet { Gson gson = new Gson(); - UUID uuid = UUID.fromString(req.getParameter("id")); + String id = req.getParameter("id"); + boolean match = Pattern.matches("[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}", id); + if(id == null || !match) { + return; + } + + UUID uuid = UUID.fromString(id); + + if(cache.containsKey(uuid)) { + out.println(gson.toJson(cache.get(uuid))); + return; + } + File file = Data.get(uuid); TopFlopArticle articles = new TopFlopArticle(file); @@ -59,6 +75,8 @@ public class TopFlopArticleDiagram extends HttpServlet { result.put("labels", labels); result.put("data", data); + + cache.put(uuid, result); out.print(gson.toJson(result)); } } \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/web/Upload.java b/src/main/java/de/hsel/spm/baudas/web/Upload.java index 18f61ec..6383e98 100644 --- a/src/main/java/de/hsel/spm/baudas/web/Upload.java +++ b/src/main/java/de/hsel/spm/baudas/web/Upload.java @@ -95,8 +95,5 @@ public class Upload extends HttpServlet { } Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING); - - - resp.sendRedirect(""); } } \ No newline at end of file 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 9230839..6f8534f 100644 --- a/src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java +++ b/src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java @@ -19,6 +19,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.regex.Pattern; /** * changes data from week overview into readable format for chart.js @@ -30,6 +31,8 @@ import java.util.UUID; @WebServlet("/week_overview") public class WeekOverviewDiagram extends HttpServlet { + private static Map cache = new HashMap<>(); + private static final long serialVersionUID = 8484151844118L; @@ -45,16 +48,20 @@ public class WeekOverviewDiagram extends HttpServlet { Gson gson = new Gson(); + String id = req.getParameter("id"); + boolean match = Pattern.matches("[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}", id); - if (req.getParameter("id") == null) { - result.put("labels", definedOrder); - result.put("count", new ArrayList<>()); - result.put("revenue", new ArrayList<>()); - out.print(gson.toJson(result)); + + if (id == null || !match) { + return; + } + + UUID uuid = UUID.fromString(id); + if(cache.containsKey(uuid)) { + out.println(gson.toJson(cache.get(uuid))); return; } - UUID uuid = UUID.fromString(req.getParameter("id")); File file = Data.get(uuid); WeekOverview overview = new WeekOverview(file); @@ -79,6 +86,7 @@ public class WeekOverviewDiagram extends HttpServlet { result.put("count", count); result.put("revenue", revenue); + cache.put(uuid, result); out.print(gson.toJson(result)); } } \ No newline at end of file diff --git a/src/main/webapp/footer.jsp b/src/main/webapp/footer.jsp index c6fa151..1e71394 100644 --- a/src/main/webapp/footer.jsp +++ b/src/main/webapp/footer.jsp @@ -7,6 +7,7 @@ <%@include file="theming.jsp"%> + diff --git a/src/main/webapp/js/cache.js b/src/main/webapp/js/cache.js index a7acfb1..5768c22 100644 --- a/src/main/webapp/js/cache.js +++ b/src/main/webapp/js/cache.js @@ -1,6 +1,36 @@ + Chart.defaults.global.defaultFontColor = "black"; +let loading = $('#loading'); + +Chart.plugins.register({ + afterDraw: function(chart) { + if (chart.data.datasets[0].data.length === 0) { + // No data is present + var ctx = chart.chart.ctx; + var width = chart.chart.width; + var height = chart.chart.height; + chart.clear(); + + ctx.save(); + ctx.textAlign = 'center'; + ctx.textBaseline = 'middle'; + ctx.font = "16px normal 'Helvetica Nueue'"; + ctx.fillText('Kein Datenset ausgewählt', width / 2, height / 2); + ctx.restore(); + } + } +}); + $(document).ready(function () { + updateDatasets(); +}); + +function clearSelected(){ + $('#dataset').prop('selectedIndex', -1); +} + +function updateDatasets() { let dataset = $('#dataset'); request('files').then(results => { dataset.empty(); @@ -13,7 +43,7 @@ $(document).ready(function () { dataset.formSelect(); }); updateCache(); -}); +} /** * update all charts with correct values for dataset diff --git a/src/main/webapp/js/shopping_times.js b/src/main/webapp/js/shopping_times.js index c5d85dc..1f29752 100644 --- a/src/main/webapp/js/shopping_times.js +++ b/src/main/webapp/js/shopping_times.js @@ -3,10 +3,10 @@ let shopping_result; let shopping_chart = new Chart(document.getElementById("shopping_times_chart"), { type: 'bar', data: { - labels: [1, 2, 3, 4, 5], + labels: [], datasets: [{ label: 'Anzahl verkaufter Artikel', - data: [0, 0, 0, 0, 0], + data: [], backgroundColor: [ 'rgba(143, 170, 220, 0.9)', 'rgba(143, 170, 220, 0.9)', diff --git a/src/main/webapp/js/upload.js b/src/main/webapp/js/upload.js new file mode 100644 index 0000000..11d344d --- /dev/null +++ b/src/main/webapp/js/upload.js @@ -0,0 +1,38 @@ +let form = document.getElementById("upload"); + +form.onsubmit = function (event) { + event.preventDefault(); + let formData = new FormData(form); + + formData.append('file', document.getElementById("file")); + + let xhr = new XMLHttpRequest(); + + xhr.onloadstart = function(e) { + loading.removeClass("hide"); + $('.sidenav').sidenav('close'); + loading.modal('open'); + } + + xhr.upload.addEventListener("progress", function (event) { + if (event.lengthComputable) { + self.progress = Math.round(100 / event.total * event.loaded); + } else if (this.explicitTotal) { + self.progress = Math.min(1, event.loaded / self.explicitTotal); + } else { + self.progress = 0; + } + + }, false); + + xhr.onloadend = function (e) { + clearSelected(); + updateDatasets(); + form.reset(); + }; + + xhr.open('POST', form.getAttribute('action'), true); + xhr.send(formData); + + return false; +}; diff --git a/src/main/webapp/js/week_overview.js b/src/main/webapp/js/week_overview.js index 522a3b1..c20f5b7 100644 --- a/src/main/webapp/js/week_overview.js +++ b/src/main/webapp/js/week_overview.js @@ -3,18 +3,18 @@ let week_result; let week_overview = new Chart($("#overview_chart"), { type: 'line', data: { - labels: [0, 0, 0, 0, 0], + labels: [], datasets: [{ label: "Kundenanzahl", yAxisID: 'y-axis-1', - data: [0, 0, 0, 0, 0], + data: [], fill: true, backgroundColor: 'rgba(104, 216, 154, 0.5)', lineTension: 0 }, { label: "Einnahmen in €", yAxisID: 'y-axis-2', - data: [0, 0, 0, 0, 0], + data: [], fill: true, backgroundColor: 'rgba(113, 114, 231, 1)', lineTension: 0 @@ -89,5 +89,5 @@ function updateWeekOverview() { week_overview.data.datasets[0].data = week_result.count; week_overview.data.datasets[1].data = week_result.revenue; week_overview.update(); - + loading.modal('close'); } \ No newline at end of file diff --git a/src/main/webapp/menu.jsp b/src/main/webapp/menu.jsp index c028fc5..33b82d3 100644 --- a/src/main/webapp/menu.jsp +++ b/src/main/webapp/menu.jsp @@ -15,7 +15,7 @@