~ fixed a few problems that sonarqube found

#SPM32: add work development 2h
This commit is contained in:
Johannes Theiner 2019-05-29 18:59:56 +02:00
parent c19bb535f4
commit 89468525ab
26 changed files with 139 additions and 54 deletions

View File

@ -2,8 +2,8 @@
#Kundenkarten Analyse
[![Build Status](https://teamcity.joethei.xyz/app/rest/builds/buildType:(id:Studium_Programmierung_Softwareprojektmanagement_SpmProd)/statusIcon)](https://tcstatus.joethei.space/dashboard/spm)
[![Quality Gate](https://sonarqube.joethei.xyz/api/project_badges/measure?project=Studium_Programmierung_Softwareprojektmanagement&metric=alert_status)
![Code Coverage](https://sonarqube.joethei.xyz/api/project_badges/measure?project=Studium_Programmierung_Softwareprojektmanagement&metric=coverage)](https://sonarqube.joethei.xyz/dashboard?id=Studium_Programmierung_Softwareprojektmanagement)
[![Quality Gate Status](https://sonarqube.joethei.xyz/api/project_badges/measure?project=de.hs-el.spm%3AbauDas-kundenkarten&metric=alert_status)](https://sonarqube.joethei.xyz/dashboard?id=de.hs-el.spm%3AbauDas-kundenkarten)
## minimal Requirements

View File

@ -6,6 +6,7 @@ import weka.core.converters.CSVLoader;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;
/**
* base interface for analysis.
@ -31,7 +32,7 @@ public interface Analysis<T> {
loader.setSource(file);
return loader.getDataSet();
} catch (IOException e) {
e.printStackTrace();
Logger.getLogger("BauDas").throwing(this.getClass().getName(), "load", e);
}
return null;
}

View File

@ -37,5 +37,7 @@ class Attribute {
static final int GARDENING_TOOLS = 25;
private Attribute() {
}
}

View File

@ -9,6 +9,7 @@ import weka.filters.unsupervised.attribute.Remove;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
/**
* Cluster Analysis.
@ -46,7 +47,7 @@ public class Cluster implements Analysis<Map<Integer, Map<String, String>>> {
remove.setInputFormat(instances);
instances = Filter.useFilter(instances, remove);
} catch (Exception e) {
e.printStackTrace();
Logger.getLogger("BauDas").throwing(this.getClass().getName(), "getResult", e);
}
//creating a single cluster to get average, weka has no way to get that from a bigger one.
@ -59,7 +60,7 @@ public class Cluster implements Analysis<Map<Integer, Map<String, String>>> {
averageCentroids = averageMeans.getClusterCentroids();
} catch (Exception ex) {
ex.printStackTrace();
Logger.getLogger("BauDas").throwing(this.getClass().getName(), "getResult", ex);
}
assert averageCentroids != null;
@ -77,7 +78,7 @@ public class Cluster implements Analysis<Map<Integer, Map<String, String>>> {
putIntoMap(centroids, count);
} catch (Exception ex) {
ex.printStackTrace();
Logger.getLogger("BauDas").throwing(this.getClass().getName(), "getResult", ex);
}
return result;
}

View File

@ -13,6 +13,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
/**
* The shopping-cart analysis.
@ -45,7 +46,7 @@ public class ShoppingCart implements Analysis<Map<List<String>, List<String>>> {
nc.setInputFormat(instances);
instances = Filter.useFilter(instances, nc);
} catch (Exception e) {
e.printStackTrace();
Logger.getLogger("BauDas").throwing(this.getClass().getName(), "getResult", e);
}
@ -55,7 +56,7 @@ public class ShoppingCart implements Analysis<Map<List<String>, List<String>>> {
num2nom.setInputFormat(instances);
instances = Filter.useFilter(instances, num2nom);
} catch (Exception e) {
e.printStackTrace();
Logger.getLogger("BauDas").throwing(this.getClass().getName(), "getResult", e);
}
Apriori model = new Apriori();
@ -64,7 +65,7 @@ public class ShoppingCart implements Analysis<Map<List<String>, List<String>>> {
try {
model.buildAssociations(instances);
} catch (Exception e) {
e.printStackTrace();
Logger.getLogger("BauDas").throwing(this.getClass().getName(), "getResult", e);
}
for (int i = 0; i < model.getAssociationRules().getRules().size(); i++) {

View File

@ -21,7 +21,6 @@ public class WeekOverview implements Analysis<Map<String, Map.Entry<Double, Inte
private Map<String, Map.Entry<Double, Integer>> result;
public WeekOverview(File file) {
instances = load(file);
}

View File

@ -32,10 +32,9 @@ public class AuthorizationFilter implements Filter {
* initialize this filter.
*
* @param filterConfig configuration
* @throws ServletException failed to initialize filter
*/
@Override
public void init(@NotNull FilterConfig filterConfig) throws ServletException {
public void init(@NotNull FilterConfig filterConfig) {
this.context = filterConfig.getServletContext();
this.context.log("AuthorizationFilter initialized");
}

View File

@ -16,6 +16,8 @@ import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Logger;
import java.util.stream.Stream;
/**
* manages data about .csv files.
@ -45,8 +47,10 @@ public class DatasetManagement {
if (files.get(session).size() >= 5) {
SavedFile file = files.get(session).iterator().next();
if (!get(file.getUuid()).delete()) {
System.err.println("failed to delete file " + file);
try {
Files.delete(get(file.getUuid()).toPath());
} catch (IOException e) {
Logger.getLogger("BauDas").throwing(DatasetManagement.class.getName(), "add", e);
}
files.get(session).remove(file);
}
@ -63,16 +67,19 @@ public class DatasetManagement {
if (!files.containsKey(session)) return;
for (SavedFile file : files.get(session)) {
if (!get(file.getUuid()).delete())
System.err.println("failed to delete file " + file);
try {
Files.delete(get(file.getUuid()).toPath());
} catch (IOException e) {
Logger.getLogger("BauDas").throwing(DatasetManagement.class.getName(), "delete", e);
}
}
files.remove(session);
//deleting all files older than x days.
int days = 30;
try {
Files.list(Paths.get("")).forEach(path -> {
try (Stream<Path> stream = Files.list(Paths.get(""))) {
stream.forEach(path -> {
if (path.toFile().getName().endsWith("*.csv")) {
long diff = new Date().getTime() - path.toFile().lastModified();
@ -80,13 +87,13 @@ public class DatasetManagement {
try {
Files.delete(path);
} catch (IOException e) {
e.printStackTrace();
Logger.getLogger("BauDas").throwing(DatasetManagement.class.getName(), "delete", e);
}
}
}
});
} catch (IOException ex) {
ex.printStackTrace();
Logger.getLogger("BauDas").throwing(DatasetManagement.class.getName(), "delete", ex);
}
}
@ -113,4 +120,7 @@ public class DatasetManagement {
private static String getFileName(@NotNull UUID uuid) {
return uuid + ".csv";
}
private DatasetManagement() {
}
}

View File

@ -27,7 +27,6 @@ public enum ErrorCode {
*/
public static boolean exists(String message) {
for (ErrorCode code : values()) {
System.out.println(message + " == " + code.name());
if (code.name().equals(message))
return true;
}

View File

@ -32,6 +32,7 @@ public class LoginServlet extends HttpServlet {
* @param response response object
* @throws IOException failed to initialize print writer
*/
@Override
protected void doPost(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response) throws ServletException, IOException {
String password = request.getParameter("password");
if (this.password.equals(password)) {

View File

@ -27,6 +27,7 @@ public class LogoutServlet extends HttpServlet {
* @param response response object
* @throws IOException failed to initialize print writer
*/
@Override
protected void doGet(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
if (session != null) {

View File

@ -17,6 +17,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.logging.Logger;
/**
* saves uploaded files.
@ -62,45 +63,59 @@ public class UploadServlet extends HttpServlet {
Part filePart = req.getPart("file");
if (filePart == null) {
resp.sendRedirect("error.jsp?code=" + ErrorCode.FILE_NOT_FOUND);
error(resp, ErrorCode.FILE_NOT_FOUND);
return;
}
if (filePart.getSubmittedFileName() == null) {
resp.sendRedirect("error.jsp?code=" + ErrorCode.FILE_NOT_FOUND);
error(resp, ErrorCode.FILE_NOT_FOUND);
return;
}
String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
if (fileName == null) {
resp.sendRedirect("error.jsp?code=" + ErrorCode.FILE_NOT_FOUND);
error(resp, ErrorCode.FILE_NOT_FOUND);
return;
}
InputStream inputStream = filePart.getInputStream();
if (inputStream == null) {
resp.sendRedirect("error.jsp?code=" + ErrorCode.FILE_NOT_FOUND);
error(resp, ErrorCode.FILE_NOT_FOUND);
return;
}
if (inputStream.available() == 0) {
resp.sendRedirect("error.jsp?code=" + ErrorCode.EMPTY_FILE);
error(resp, ErrorCode.EMPTY_FILE);
return;
}
if (!checkFormat(inputStream)) {
resp.sendRedirect("error.jsp?code=" + ErrorCode.INVALID_FORMAT);
error(resp, ErrorCode.INVALID_FORMAT);
return;
} else {
inputStream = filePart.getInputStream();
}
Path path = DatasetManagement.add(fileName, req.getSession().getId());
if (!Files.exists(path)) {
if (!path.toFile().exists()) {
Files.createFile(path);
}
Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
}
/**
* redirect to error page.
*
* @param response response object
* @param code error code
*/
private void error(@NotNull HttpServletResponse response, @NotNull ErrorCode code) {
try {
response.sendRedirect("error.jsp?code=" + code);
} catch (IOException e) {
Logger.getLogger("BauDas").throwing(getClass().getName(), "error", e);
}
}
}

View File

@ -26,7 +26,7 @@ import java.util.UUID;
@WebServlet("/clusters")
public class ClusterTable extends HttpServlet implements Depiction {
private static Map<UUID, Map> cache;
private static Map<UUID, Map> cache = new HashMap<>();
/**
* returns formatted analysis result to HTTP GET request from supplied id.
@ -37,15 +37,14 @@ public class ClusterTable extends HttpServlet implements Depiction {
*/
@Override
protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) throws IOException {
UUID uuid = setup(req, resp, cache);
UUID uuid = init(req, resp, cache);
if (uuid == null) return;
Map<Integer, Map<String, String>> result = new HashMap<>();
File file = DatasetManagement.get(uuid);
Cluster cluster = new Cluster(file);
result = cluster.getResult();
Map<Integer, Map<String, String>> result = cluster.getResult();
PrintWriter out = resp.getWriter();
Gson gson = new Gson();

View File

@ -11,6 +11,7 @@ import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Logger;
import java.util.regex.Pattern;
/**
@ -31,7 +32,7 @@ interface Depiction {
* @return validated uuid
*/
@Nullable
default UUID setup(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Map<UUID, Map> cache) {
default UUID init(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Map<UUID, Map> cache) {
try {
request.setCharacterEncoding(StandardCharsets.UTF_8.name());
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
@ -50,7 +51,7 @@ interface Depiction {
}
return uuid;
} catch (IOException e) {
e.printStackTrace();
Logger.getLogger("BauDas").throwing(this.getClass().getName(), "init", e);
}
return null;
}

View File

@ -42,7 +42,7 @@ public class SalesNumberChart extends HttpServlet implements Depiction {
*/
@Override
protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) throws IOException {
UUID uuid = setup(req, resp, cache);
UUID uuid = init(req, resp, cache);
if (uuid == null) return;
File file = DatasetManagement.get(uuid);

View File

@ -31,7 +31,7 @@ public class ShoppingCartTable extends HttpServlet implements Depiction {
private static final long serialVersionUID = 5026732432605473505L;
private static Map<UUID, Map> cache;
private static Map<UUID, Map> cache = new HashMap<>();
/**
* returns formatted analysis result to HTTP GET request from supplied id.
@ -40,8 +40,9 @@ public class ShoppingCartTable extends HttpServlet implements Depiction {
* @param resp response object
* @throws IOException failed to initialize print writer
*/
@Override
protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) throws IOException {
UUID uuid = setup(req, resp, cache);
UUID uuid = init(req, resp, cache);
if (uuid == null) return;

View File

@ -47,7 +47,7 @@ public class ShoppingTimesChart extends HttpServlet implements Depiction {
*/
@Override
protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) throws IOException {
UUID uuid = setup(req, resp, cache);
UUID uuid = init(req, resp, cache);
if (uuid == null) return;

View File

@ -44,7 +44,7 @@ public class WeekOverviewChart extends HttpServlet implements Depiction {
@Override
protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) throws IOException {
UUID uuid = setup(req, resp, cache);
UUID uuid = init(req, resp, cache);
if (uuid == null) return;

View File

@ -1,30 +1,38 @@
/**
* renders cluster analysis results as a table.
*
* @author Julian Hinxlage
* @version 0.1
* @since 1.0
*/
let cluster_result;
function updateClusters(id) {
if(typeof id !== 'undefined') {
if (typeof id !== 'undefined') {
request('clusters', id).then(function (data) {
cluster_result = data;
drawClusterTable();
});
}else request('clusters').then(function(data) {
} else request('clusters').then(function (data) {
cluster_result = data;
drawClusterTable();
});
}
function drawClusterTable(){
function drawClusterTable() {
let table = document.getElementById("cluster_table");
while(table.hasChildNodes()){
while (table.hasChildNodes()) {
table.removeChild(table.firstChild);
}
let row = $("<thead>");
row.append("<tr>");
row.append("<td></td>");
for (x in cluster_result){
if(x == 0){
for (let x in cluster_result) {
if (x == 0) {
row.append("<td>" + "Durchschnitt" + "</td>");
}else{
} else {
row.append("<td>" + x + "</td>");
}
}
@ -44,12 +52,12 @@ function drawClusterTable(){
$("#cluster_table").append(row);
}
function appendClusterTable(row, attrib){
function appendClusterTable(row, attrib) {
row.append("</tbody>");
row.append("<tr>");
row.append("<td>" + attrib + "</td>");
for (x in cluster_result){
row.append($("<td>" + cluster_result[x][attrib] +"</td>"));
for (let x in cluster_result) {
row.append($("<td>" + cluster_result[x][attrib] + "</td>"));
}
row.append("</tr>");
}

View File

@ -1,3 +1,11 @@
/**
* Dataset switching and listing.
*
* @author Johannes Theiner
* @version 0.1
* @since 1.0
*/
Chart.defaults.global.defaultFontColor = "black";

View File

@ -1,3 +1,11 @@
/**
* renders sales numbers as chart.
*
* @author Johannes Theiner
* @version 0.1
* @since 1.0
*/
let top_flop_result;
@ -13,8 +21,7 @@ let sales = new Chart($("#sales_chart"), {
options: {
responsive: true,
title: {
display: false,
text: 'Top Flop Artikel'
display: false
},
legend: {
display: false

View File

@ -1,3 +1,11 @@
/**
* renders shopping cart analysis results as a table.
*
* @author Karsten Eden
* @version 0.1
* @since 1.0
*/
let shopping_cart_result;
function updateShoppingCartTable(id) {
@ -25,7 +33,7 @@ function drawShoppingCartTable(){
row.append("</tr>");
row.append("</thead>");
row.append("<tbody>");
for (x in shopping_cart_result){
for (let x in shopping_cart_result){
row.append("<tr>");
row.append("<td>" + shopping_cart_result[x].key + "</td>");
row.append("<td>" + shopping_cart_result[x].value + "</td>");

View File

@ -1,3 +1,11 @@
/**
* renders shopping times analysis result as chart with day selection.
*
* @author Johannes Theiner
* @version 0.1
* @since 1.0
*/
let shopping_result;
let shopping_chart = new Chart(document.getElementById("shopping_times_chart"), {

View File

@ -1,3 +1,11 @@
/**
* upload files and show loading modal
*
* @author Johannes Theiner
* @version 0.1
* @since 1.0
*/
let form = document.getElementById("upload");
form.onsubmit = function (event) {
@ -12,7 +20,7 @@ form.onsubmit = function (event) {
loading.removeClass("hide");
$('.sidenav').sidenav('close');
loading.modal('open');
}
};
xhr.upload.addEventListener("progress", function (event) {
if (event.lengthComputable) {

View File

@ -1,3 +1,11 @@
/**
* render week overview analysis result as chart.
*
* @author Johannes Theiner
* @version 0.1
* @since 1.0
*/
let week_result;
let week_overview = new Chart($("#overview_chart"), {

View File

@ -5,7 +5,7 @@
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="js/upload.js"></script>
<script src="js/cache.js"></script>
<script src="js/datasets.js"></script>
<script src="js/week_overview.js"></script>
<script src="js/shopping_times.js"></script>
<script src="js/sales.js"></script>