Merge remote-tracking branch 'origin/SPM-28'

This commit is contained in:
Johannes Theiner 2019-05-18 12:39:29 +02:00
commit 0f25f4e2d9
12 changed files with 139 additions and 55 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
.project
.vscode
target
*.csv

View File

@ -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";
}
}

View File

@ -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;
}

View File

@ -9,6 +9,7 @@ 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;
@ -21,6 +22,7 @@ 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
@ -48,15 +50,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<DayHour, Integer> map = shoppingTimes.getResult();
Set<String> days = new TreeSet<>();

View File

@ -8,6 +8,7 @@ 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;
@ -17,6 +18,7 @@ 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
@ -43,16 +45,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<String> count = new ArrayList<>();
List<String> revenue = new ArrayList<>();

View File

@ -7,7 +7,11 @@
<!--Script für Materlialize-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<script type="text/javascript" src="js/materialize_components.js"></script>
<script>
M.AutoInit();
</script>
<script src="js/cache.js"></script>
<script src="js/week_overview.js"></script>
<script src="js/shopping_times.js"></script>

View File

@ -23,11 +23,11 @@
<body class="blue-grey lighten-5">
<!--Navbar-->
<div class="navbar-fixed">
<nav class="nav-extended blue-grey lighten-3 white-text row">
<nav class="nav-extended blue-grey darken-3 white-text row">
<a href="#" class="brand-logo hide-on-small-and-down center">BauDas</a>
<!--Navbar Tabs-->
<div class="nav-content col 12 ">
<ul class="tabs tabs-transparent blue-grey lighten-3 white-text">
<ul class="tabs tabs-transparent blue-grey darken-3 white-text">
<li class="tab"><a class="active" href="#analysis"><i class="material-icons left">poll</i>Analyse</a>
</li>
<li class="tab"><a href="#marketing"><i class="material-icons left">public</i>Marketing</a></li>
@ -79,15 +79,9 @@
</li>
<li>
<div class="input-field container">
<label>Datensatzauswahl</label>
<label for="dataset">Datensatzauswahl</label>
<br>
<select>
<option value="0">aktuell</option>
<option value="1">Datensatz 1</option>
<option value="2">Datensatz 2</option>
<option value="3">Datensatz 3</option>
<option value="4">Datensatz 4</option>
<option value="5">Datensatz 5</option>
<select id="dataset" onChange="updateCache()">
</select>
<br>
</div>

View File

@ -34,7 +34,7 @@
</div>
<div class="center">
<div class="input-field col s12">
<select id="day" onchange="updateDays()">
<select id="day" onchange="changeDay()">
<option value="0">Montag</option>
<option value="1">Dienstag</option>
<option value="2">Mittwoch</option>

View File

@ -0,0 +1,74 @@
$(document).ready(function () {
let dataset = $('#dataset');
request('files').then(results => {
dataset.empty();
for (const result of results) {
dataset.append($("<option></option>")
.attr("value", result.uuid)
.text(result.name + " (" + result.date.date.day + "." + result.date.date.month + " "
+ result.date.time.hour + ":" + result.date.time.minute + ")"));
}
dataset.formSelect();
});
updateCache();
});
/**
* update all charts with correct values for dataset
*/
function updateCache() {
let dataset = $('#dataset');
let selected = $('#dataset :selected');
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();
});
}
}
/**
* request json data
* @param url base url
* @param id dataset id
* @returns {Promise<result>}
*/
function request(url, id) {
return new Promise(((resolve) => {
if (typeof id !== 'undefined') {
requestInternal(url + '?id=' + id).then(function (data) {
resolve(data);
});
} else {
requestInternal(url).then(function (data) {
resolve(data);
});
}
}));
}
/**
* only for use inside this file, javascript has no better way
* @param url url
* @returns {Promise<result>}
*/
function requestInternal(url) {
return new Promise(((resolve) => {
$.ajax({
url: url,
dataType: 'json'
}).done(function (results) {
resolve(results);
});
}));
}

View File

@ -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();
});

View File

@ -52,15 +52,19 @@ 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;
changeDay();
});
}else request('shopping_times').then(function(data) {
shopping_result = data;
changeDay();
});
}
function updateDays() {
function changeDay() {
if(typeof shopping_result["Montag"] !== 'undefined') {
let e = document.getElementById("day");
let value = e.options[e.selectedIndex].text;

View File

@ -1,4 +1,6 @@
let week_overview = new Chart(document.getElementById("overview_chart"), {
let week_result;
let week_overview = new Chart($("#overview_chart"), {
type: 'line',
data: {
labels: [0, 0, 0, 0, 0],
@ -52,14 +54,22 @@ 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();
});
}