From 0b36985e6c5f7de63b84824e4345670c267a4569 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 8 May 2019 14:12:44 +0200 Subject: [PATCH] + Working WeekOverview + Cleanup + Documentation Signed-off-by: Johannes Theiner #SPM-11: add work 2h development #SPM-11: add work 30m documentation --- checkstyle.xml | 9 +- .../hsel/spm/baudas/analysis/Attribute.java | 4 +- .../de/hsel/spm/baudas/analysis/Cluster.java | 81 ++++++++--------- .../de/hsel/spm/baudas/analysis/DayHour.java | 2 + .../spm/baudas/analysis/ShoppingTimes.java | 20 +++-- .../spm/baudas/analysis/TopFlopArticle.java | 35 +++++--- .../spm/baudas/analysis/WeekOverview.java | 23 +++-- .../java/de/hsel/spm/baudas/web/Overview.java | 59 ------------ .../java/de/hsel/spm/baudas/web/Result.java | 19 ---- .../java/de/hsel/spm/baudas/web/Upload.java | 2 +- .../spm/baudas/web/WeekOverviewDiagram.java | 89 +++++++++++++++++++ src/main/webapp/header.jsp | 5 +- src/main/webapp/js/overview.js | 4 +- src/main/webapp/js/top_articles.js | 2 +- .../spm/baudas/analysis/WeekOverviewTest.java | 2 +- 15 files changed, 195 insertions(+), 161 deletions(-) delete mode 100644 src/main/java/de/hsel/spm/baudas/web/Overview.java delete mode 100644 src/main/java/de/hsel/spm/baudas/web/Result.java create mode 100644 src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java diff --git a/checkstyle.xml b/checkstyle.xml index 1cec088..0c17f24 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -44,7 +44,7 @@ - + @@ -55,13 +55,14 @@ - + + + + value="LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, LITERAL_DO, LITERAL_IF, LITERAL_ELSE"/> diff --git a/src/main/java/de/hsel/spm/baudas/analysis/Attribute.java b/src/main/java/de/hsel/spm/baudas/analysis/Attribute.java index 375714b..e6d9883 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/Attribute.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/Attribute.java @@ -1,11 +1,13 @@ package de.hsel.spm.baudas.analysis; /** + * Indexes for all attributes. + * * @author Johannes Theiner * @version 0.1 * @since 0.1 **/ -public class Attribute { +class Attribute { static final int SEX = 0; static final int AGE = 1; diff --git a/src/main/java/de/hsel/spm/baudas/analysis/Cluster.java b/src/main/java/de/hsel/spm/baudas/analysis/Cluster.java index 10e25b1..f98989c 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/Cluster.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/Cluster.java @@ -1,5 +1,6 @@ package de.hsel.spm.baudas.analysis; +import org.jetbrains.annotations.NotNull; import weka.clusterers.SimpleKMeans; import weka.core.Instances; import weka.filters.Filter; @@ -31,51 +32,51 @@ public class Cluster implements Analysis>> { */ @Override public Map> getResult() { - if(result == null) { - result = new HashMap<>(); - //TODO: anpassen wenn #SPM-17 gemerged ist. - int[] keepIndexes = new int[]{0, 1, 3, 5, 6, 7}; - Remove remove = new Remove(); + if (result != null) { + return result; + } + result = new HashMap<>(); + int[] keepIndexes = new int[]{Attribute.SEX, Attribute.AGE, Attribute.MARITAL_STATUS, Attribute.SHOPPING_DAY, Attribute.SHOPPING_HOUR, Attribute.RESIDENCE}; + Remove remove = new Remove(); - try { - remove.setAttributeIndicesArray(keepIndexes); - remove.setInvertSelection(true); - remove.setInputFormat(instances); - instances = Filter.useFilter(instances, remove); - } catch (Exception e) { - e.printStackTrace(); - } + try { + remove.setAttributeIndicesArray(keepIndexes); + remove.setInvertSelection(true); + remove.setInputFormat(instances); + instances = Filter.useFilter(instances, remove); + } catch (Exception e) { + e.printStackTrace(); + } - //creating a single cluster to get average, weka has no way to get that from a bigger one. - SimpleKMeans fullMeans = new SimpleKMeans(); - Instances fullCentroids = null; - try { - fullMeans.setNumClusters(1); - fullMeans.setPreserveInstancesOrder(true); - fullMeans.buildClusterer(instances); - fullCentroids = fullMeans.getClusterCentroids(); + //creating a single cluster to get average, weka has no way to get that from a bigger one. + SimpleKMeans averageMeans = new SimpleKMeans(); + Instances averageCentroids = null; + try { + averageMeans.setNumClusters(1); + averageMeans.setPreserveInstancesOrder(true); + averageMeans.buildClusterer(instances); + averageCentroids = averageMeans.getClusterCentroids(); - } catch (Exception ex) { - ex.printStackTrace(); - } - assert fullCentroids != null; + } catch (Exception ex) { + ex.printStackTrace(); + } + assert averageCentroids != null; - //creating real cluster - SimpleKMeans kMeans = new SimpleKMeans(); - try { - kMeans.setNumClusters(5); - kMeans.setPreserveInstancesOrder(true); - kMeans.buildClusterer(instances); + //creating real cluster + SimpleKMeans fullMeans = new SimpleKMeans(); + try { + fullMeans.setNumClusters(5); + fullMeans.setPreserveInstancesOrder(true); + fullMeans.buildClusterer(instances); - int count = 0; - count = putIntoMap(fullCentroids, count); + int count = 0; + count = putIntoMap(averageCentroids, count); - Instances centroids = kMeans.getClusterCentroids(); - putIntoMap(centroids, count); + Instances centroids = fullMeans.getClusterCentroids(); + putIntoMap(centroids, count); - } catch (Exception ex) { - ex.printStackTrace(); - } + } catch (Exception ex) { + ex.printStackTrace(); } return result; } @@ -84,10 +85,10 @@ public class Cluster implements Analysis>> { * puts data into map. * * @param centroids cluster analysis result - * @param count current insert count + * @param count current insert count * @return count increment */ - private int putIntoMap(Instances centroids, int count) { + private int putIntoMap(@NotNull Instances centroids, int count) { for (int i = 0; i < centroids.numInstances(); i++) { Map map = new HashMap<>(); for (int j = 0; j < centroids.numAttributes(); j++) { diff --git a/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java b/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java index 52f7793..f2a223d 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java @@ -5,6 +5,8 @@ import lombok.Data; import lombok.EqualsAndHashCode; /** + * object to better handle day, hour combinations. + * * @author Johannes Theiner * @version 0.1 * @since 0.1 diff --git a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java index 865517f..4c4a697 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java @@ -31,17 +31,19 @@ public class ShoppingTimes implements Analysis> { */ @Override public Map getResult() { - if (result == null) { - result = new HashMap<>(); + if (result != null) { + return result; + } - for(Instance instance : instances) { - DayHour dayHour = new DayHour(instance.stringValue(Attribute.SHOPPING_DAY), instance.stringValue(Attribute.SHOPPING_HOUR)); - int tmp = 1; - if(result.containsKey(dayHour)) - tmp = result.get(dayHour) + 1; + result = new HashMap<>(); - result.put(dayHour, tmp); - } + for (Instance instance : instances) { + DayHour dayHour = new DayHour(instance.stringValue(Attribute.SHOPPING_DAY), instance.stringValue(Attribute.SHOPPING_HOUR)); + int tmp = 1; + if (result.containsKey(dayHour)) + tmp = result.get(dayHour) + 1; + + result.put(dayHour, tmp); } return result; } diff --git a/src/main/java/de/hsel/spm/baudas/analysis/TopFlopArticle.java b/src/main/java/de/hsel/spm/baudas/analysis/TopFlopArticle.java index b8a24dc..995379c 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/TopFlopArticle.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/TopFlopArticle.java @@ -1,16 +1,18 @@ package de.hsel.spm.baudas.analysis; -import java.io.*; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.HashMap; -import java.util.stream.Collectors; -import java.util.stream.Stream; - import weka.core.Instance; import weka.core.Instances; +import java.io.File; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + /** + * get sum for every article and sort them ascending. + * * @author Karsten Eden * @version 0.1 * @since 0.1 @@ -22,29 +24,34 @@ public class TopFlopArticle implements Analysis> { private Instances instances; private Map results; - public TopFlopArticle(File file){ - results = new HashMap<>(); + public TopFlopArticle(File file) { + instances = load(file); } /** - * get sum for every article and sort them asending + * get result of analysis. + * * @return Map of Name of Products and Sum of Values */ public Map getResult() { + if (results != null) { + return results; + } + results = new HashMap<>(); String name; - Double sum = 0.0; - for(int i=11; i<=25; i++) { + double sum = 0.0; + for (int i = 11; i <= 25; i++) { for (Instance instance : instances) { sum += instance.value(i); } name = instances.attribute(i).name(); - results.put(name, sum.intValue()); + results.put(name, (int) sum); sum = 0.0; } //Sort Map in ascending order Stream> sorted = results.entrySet().stream().sorted(Map.Entry.comparingByValue()); - results = sorted.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, LinkedHashMap::new)); + results = sorted.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new)); return results; } diff --git a/src/main/java/de/hsel/spm/baudas/analysis/WeekOverview.java b/src/main/java/de/hsel/spm/baudas/analysis/WeekOverview.java index 033365a..7c54043 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/WeekOverview.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/WeekOverview.java @@ -9,7 +9,7 @@ import java.util.HashMap; import java.util.Map; /** - * Week Overview Analysis. + * Week WeekOverviewDiagram Analysis. * * @author Julian Hinxlage * @version 0.1 @@ -21,19 +21,26 @@ public class WeekOverview implements Analysis> result; public WeekOverview(File file) { - result = new HashMap<>(); + instances = load(file); } + /** + * get results for analysis. + * + * @return day, amount, count + */ @Override public Map> getResult() { + if (result != null) { + return result; + } + result = new HashMap<>(); + int dayIndex = Attribute.SHOPPING_DAY; + int amountIndex = Attribute.PURCHASE_AMOUNT; - //TODO: replace values when #SPM-17 is merged - int dayIndex = 5; - int amountIndex = 10; - - int startArticles = 11; - int endArticles = 26; + int startArticles = Attribute.POWER_TOOLS; + int endArticles = Attribute.GARDENING_TOOLS + 1; for (int i = 0; i < instances.numInstances(); i++) { Instance instance = instances.get(i); diff --git a/src/main/java/de/hsel/spm/baudas/web/Overview.java b/src/main/java/de/hsel/spm/baudas/web/Overview.java deleted file mode 100644 index b96bd0f..0000000 --- a/src/main/java/de/hsel/spm/baudas/web/Overview.java +++ /dev/null @@ -1,59 +0,0 @@ -package de.hsel.spm.baudas.web; - -import com.google.gson.Gson; -import de.hsel.spm.baudas.analysis.WeekOverview; - -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.PrintWriter; -import java.nio.charset.StandardCharsets; -import java.util.*; - -/** - * @author Johannes Theiner - * @version 0.1 - * @since 0.1 - **/ -@WebServlet("/week_overview") -public class Overview extends HttpServlet { - - @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { - req.setCharacterEncoding(StandardCharsets.UTF_8.name()); - resp.setCharacterEncoding(StandardCharsets.UTF_8.name()); - resp.setContentType("application/json"); - PrintWriter out = resp.getWriter(); - - Gson gson = new Gson(); - - WeekOverview overview = new WeekOverview(Data.get(Data.getFiles().peek().getUuid())); - - List labels = new ArrayList<>(); - - List data1 = new ArrayList<>(); - List data2 = new ArrayList<>(); - - for(Map.Entry> entry : overview.getResult().entrySet()) { - labels.add(entry.getKey()); - data1.add(entry.getValue().getKey().toString()); - data2.add(entry.getValue().getValue().toString()); - } - - List definedOrder = // define your custom order - Arrays.asList("Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"); - - Comparator comparator = Comparator.comparingInt(definedOrder::indexOf); - labels.sort(comparator); - - List> data = new ArrayList<>(); - data.add(data1); - data.add(data2); - - Result result = new Result(labels, data); - - out.print(gson.toJson(result)); - } -} \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/web/Result.java b/src/main/java/de/hsel/spm/baudas/web/Result.java deleted file mode 100644 index d2ddfd9..0000000 --- a/src/main/java/de/hsel/spm/baudas/web/Result.java +++ /dev/null @@ -1,19 +0,0 @@ -package de.hsel.spm.baudas.web; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -import java.util.List; - -/** - * @author Johannes Theiner - * @version 0.1 - * @since 0.1 - **/ -@Getter -@AllArgsConstructor -public class Result { - - private List labels; - private List> data; -} \ 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 bc63ffe..f969b45 100644 --- a/src/main/java/de/hsel/spm/baudas/web/Upload.java +++ b/src/main/java/de/hsel/spm/baudas/web/Upload.java @@ -56,6 +56,6 @@ public class Upload extends HttpServlet { Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING); - resp.sendRedirect("index.jsp"); + 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 new file mode 100644 index 0000000..21855c4 --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java @@ -0,0 +1,89 @@ +package de.hsel.spm.baudas.web; + +import com.google.gson.Gson; +import de.hsel.spm.baudas.analysis.WeekOverview; +import org.jetbrains.annotations.NotNull; + +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +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; + +/** + * changes data from week overview into readable format for chart.js + * + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + **/ +@WebServlet("/week_overview") +public class WeekOverviewDiagram extends HttpServlet { + + /** + * do get. + * + * @param req request + * @param resp response + * @throws IOException writer could not be retrieved + */ + @Override + protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) throws IOException { + req.setCharacterEncoding(StandardCharsets.UTF_8.name()); + resp.setCharacterEncoding(StandardCharsets.UTF_8.name()); + resp.setContentType("application/json"); + PrintWriter out = resp.getWriter(); + + List definedOrder = Arrays.asList("Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"); + Map> result = new HashMap<>(); + + Gson gson = new Gson(); + + SavedFile file = Data.getFiles().peek(); + if (file == 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())); + + List labels = new ArrayList<>(); + + List count = new ArrayList<>(); + List revenue = new ArrayList<>(); + + for (Map.Entry> entry : overview.getResult().entrySet()) { + labels.add(entry.getKey()); + count.add(entry.getValue().getKey().toString()); + revenue.add(entry.getValue().getValue().toString()); + } + + + Comparator comparator = Comparator.comparingInt(definedOrder::indexOf); + labels.sort(comparator); + + for (String label : labels) { + Map.Entry entry = overview.getResult().get(label); + count.add(entry.getKey().toString()); + revenue.add(entry.getValue().toString()); + } + + + result.put("labels", labels); + result.put("count", count); + result.put("revenue", revenue); + + out.print(gson.toJson(result)); + } +} \ No newline at end of file diff --git a/src/main/webapp/header.jsp b/src/main/webapp/header.jsp index fd79f39..f0aa07f 100644 --- a/src/main/webapp/header.jsp +++ b/src/main/webapp/header.jsp @@ -1,7 +1,7 @@ <% response.setCharacterEncoding(StandardCharsets.UTF_8.name()); %> <% request.setCharacterEncoding(StandardCharsets.UTF_8.name()); %> - + BauDas @@ -10,6 +10,7 @@ + @@ -31,7 +32,7 @@