add tons of (mostly temporary) stats

This commit is contained in:
Mateo Körber Belmonte
2025-02-24 22:53:54 +01:00
parent 194cf32747
commit 38ae3bd98c
10 changed files with 463 additions and 36 deletions
+1 -1
View File
@@ -45,7 +45,7 @@ app.get('/', (req, res) => {
});
app.get('/statistics', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'statistics.html'));
res.sendFile(path.join(__dirname, '../frontend/public', 'statistics.html'));
});
// Handle form submission
View File
+26
View File
@@ -0,0 +1,26 @@
document.addEventListener('DOMContentLoaded', () => {
// Get last week's Monday date
const today = new Date();
const lastWeek = new Date(today);
lastWeek.setDate(today.getDate() - today.getDay() - 6); // Go back to last Monday
const weekString = lastWeek.toISOString().split('T')[0];
// Fetch data from weekly-summary API
fetch(`/weekly-summary?week=${weekString}`)
.then(response => response.json())
.then(data => {
// Sum up all votes across all categories and days
const totalVotes = Object.values(data).reduce((daySum, day) => {
return daySum + Object.values(day).reduce((categorySum, category) => {
return categorySum + (category.count || 0);
}, 0);
}, 0);
// Update the display
document.querySelector('.feedbackValue-countAllLastWeek').textContent = totalVotes;
})
.catch(error => {
console.error('Error fetching last week\'s count:', error);
document.querySelector('.feedbackValue-countAllLastWeek').textContent = 'Fehler';
});
});
+47
View File
@@ -0,0 +1,47 @@
document.addEventListener('DOMContentLoaded', () => {
fetch('/results')
.then(response => response.json())
.then(data => {
// Gruppiere Daten nach Datum
const dateGroups = {};
data.forEach(row => {
const date = row.date.split(' ')[0];
if (!dateGroups[date]) {
dateGroups[date] = { good: 0, bad: 0 };
}
if (['good', 'veryGood'].includes(row.rating)) {
dateGroups[date].good++;
}
if (['bad', 'veryBad'].includes(row.rating)) {
dateGroups[date].bad++;
}
});
const ctx = document.getElementById('feedbackChart-line').getContext('2d');
new Chart(ctx, {
type: 'line',
data: {
labels: Object.keys(dateGroups),
datasets: [{
label: 'Positive Bewertungen',
data: Object.values(dateGroups).map(d => d.good),
borderColor: 'rgba(75, 192, 192, 1)',
tension: 0.1
},
{
label: 'Negative Bewertungen',
data: Object.values(dateGroups).map(d => d.bad),
borderColor: 'rgba(255, 99, 132, 1)',
tension: 0.1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
});
});
+38
View File
@@ -0,0 +1,38 @@
document.addEventListener('DOMContentLoaded', () => {
fetch('/results')
.then(response => response.json())
.then(data => {
const categories = ['Fleischgericht', 'Vegetarisch', 'Tagesgericht', 'Tagessalat'];
const ratings = {
veryBad: 0,
bad: 0,
neutral: 0,
good: 0,
veryGood: 0
};
data.forEach(row => {
if (categories.includes(row.category)) {
ratings[row.rating]++;
}
});
const ctx = document.getElementById('feedbackChart-pie').getContext('2d');
new Chart(ctx, {
type: 'doughnut',
data: {
labels: ['Sehr schlecht', 'Schlecht', 'Neutral', 'Gut', 'Sehr gut'],
datasets: [{
data: [ratings.veryBad, ratings.bad, ratings.neutral, ratings.good, ratings.veryGood],
backgroundColor: [
'rgba(255, 99, 132, 0.8)',
'rgba(255, 159, 64, 0.8)',
'rgba(255, 205, 86, 0.8)',
'rgba(75, 192, 192, 0.8)',
'rgba(54, 162, 235, 0.8)'
]
}]
}
});
});
});
+43
View File
@@ -0,0 +1,43 @@
document.addEventListener('DOMContentLoaded', () => {
fetch('/results')
.then(response => response.json())
.then(data => {
const categories = ['Fleischgericht', 'Vegetarisch', 'Tagesgericht', 'Tagessalat'];
const categoryAverages = {};
// Berechne Durchschnittsbewertungen
categories.forEach(category => {
const ratings = data
.filter(row => row.category === category)
.map(row => {
const scores = {veryBad: 1, bad: 2, neutral: 3, good: 4, veryGood: 5};
return scores[row.rating];
});
categoryAverages[category] = ratings.length ?
ratings.reduce((a, b) => a + b, 0) / ratings.length : 0;
});
const ctx = document.getElementById('feedbackChart-radar').getContext('2d');
new Chart(ctx, {
type: 'radar',
data: {
labels: categories,
datasets: [{
label: 'Durchschnittliche Bewertung',
data: categories.map(c => categoryAverages[c]),
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(75, 192, 192, 1)',
pointBackgroundColor: 'rgba(75, 192, 192, 1)'
}]
},
options: {
scales: {
r: {
beginAtZero: true,
max: 5
}
}
}
});
});
});
+66
View File
@@ -0,0 +1,66 @@
document.addEventListener('DOMContentLoaded', () => {
// Get current date in YYYY-MM-DD format
const today = new Date();
const currentWeek = new Date(today);
currentWeek.setDate(today.getDate() - today.getDay() + 1); // Set to Monday of current week
// Format date for API request
const weekString = currentWeek.toISOString().split('T')[0];
// Get current day name
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const currentDay = days[today.getDay()];
// Fetch data from weekly-summary API
fetch(`/weekly-summary?week=${weekString}`)
.then(response => response.json())
.then(data => {
// Only process if we have data for today (Mon-Fri)
if (currentDay in data) {
// Update mini-stats boxes with today's averages
document.getElementById('todaysAverageMeatFish').textContent =
data[currentDay]['Fleischgericht'].average !== 'N/A'
? `${data[currentDay]['Fleischgericht'].average} ⭐`
: 'Keine Daten';
document.getElementById('todaysAverageVeggi').textContent =
data[currentDay]['Vegetarisch'].average !== 'N/A'
? `${data[currentDay]['Vegetarisch'].average} ⭐`
: 'Keine Daten';
document.getElementById('todaysAverageDailyDish').textContent =
data[currentDay]['Tagesgericht'].average !== 'N/A'
? `${data[currentDay]['Tagesgericht'].average} ⭐`
: 'Keine Daten';
document.getElementById('todaysAverageDailySalad').textContent =
data[currentDay]['Tagessalat'].average !== 'N/A'
? `${data[currentDay]['Tagessalat'].average} ⭐`
: 'Keine Daten';
} else {
// If it's weekend or no data available
const elements = [
'todaysAverageMeatFish',
'todaysAverageVeggi',
'todaysAverageDailyDish',
'todaysAverageDailySalad'
];
elements.forEach(id => {
document.getElementById(id).textContent = 'Keine Daten';
});
}
})
.catch(error => {
console.error('Error fetching today\'s averages:', error);
// Show error state in mini-stats boxes
const elements = [
'todaysAverageMeatFish',
'todaysAverageVeggi',
'todaysAverageDailyDish',
'todaysAverageDailySalad'
];
elements.forEach(id => {
document.getElementById(id).textContent = 'Fehler';
});
});
});
+150
View File
@@ -0,0 +1,150 @@
document.addEventListener('DOMContentLoaded', () => {
const weeks = 52; // Number of weeks to fetch
const allData = {};
let completedRequests = 0;
// Get dates for each week
for (let i = 0; i < weeks; i++) {
const date = new Date();
date.setDate(date.getDate() - (i * 7)); // Go back i weeks
const monday = new Date(date);
monday.setDate(monday.getDate() - monday.getDay() + 1); // Adjust to Monday
const weekString = monday.toISOString().split('T')[0];
// Fetch data for each week
fetch(`/weekly-summary?week=${weekString}`)
.then(response => response.json())
.then(weekData => {
if (Object.keys(weekData).length > 0) { // Only store if we have data
allData[weekString] = weekData;
}
completedRequests++;
// Once all requests are complete, create the chart
if (completedRequests === weeks) {
createChart(allData);
}
})
.catch(error => {
console.error(`Error fetching week ${weekString}:`, error);
completedRequests++;
if (completedRequests === weeks) {
createChart(allData);
}
});
}
});
function getWeekNumber(date) {
// Create a copy of the date to avoid modifying the original
const target = new Date(date.valueOf());
// Set to nearest Thursday: current date + 4 - current day number
// Make Sunday's day number 7
const dayNr = (date.getDay() + 6) % 7;
target.setDate(target.getDate() - dayNr + 3);
// Get first Thursday in January
const firstThursday = new Date(target.getFullYear(), 0, 1);
if (firstThursday.getDay() !== 4) {
firstThursday.setMonth(0, 1 + ((4 - firstThursday.getDay()) + 7) % 7);
}
// Calculate week number: Number of weeks between target and first Thursday
const weekNr = 1 + Math.ceil((target - firstThursday) / (7 * 24 * 60 * 60 * 1000));
return weekNr;
}
function createChart(data) {
const categories = ['Fleischgericht', 'Vegetarisch', 'Tagesgericht', 'Tagessalat'];
const days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
// Sort weeks chronologically
const sortedWeeks = Object.keys(data).sort();
// Generate labels
const labels = sortedWeeks.map(weekStart => {
const date = new Date(weekStart);
return `KW ${getWeekNumber(date)}`;
});
// Prepare datasets
const datasets = categories.map(category => ({
label: category,
data: sortedWeeks.map(week => {
const weekData = data[week];
if (!weekData) return null;
// Calculate weekly average
const validRatings = days
.map(day => {
const rating = weekData[day]?.[category]?.average;
return rating === 'N/A' ? null : Number(rating);
})
.filter(rating => rating !== null && !isNaN(rating));
if (validRatings.length === 0) return null;
return parseFloat((validRatings.reduce((a, b) => a + b) / validRatings.length).toFixed(2));
}),
fill: false,
tension: 0.3,
borderWidth: 2,
spanGaps: true // Connect lines across null values
}));
// Debug output
console.log('Chart Data:', {
weeks: sortedWeeks,
labels: labels,
datasets: datasets.map(ds => ({
label: ds.label,
data: ds.data
}))
});
// Set colors
datasets[0].borderColor = '#FF6384'; // Meat/Fish - Red
datasets[1].borderColor = '#36A2EB'; // Vegetarian - Blue
datasets[2].borderColor = '#FFCE56'; // Daily Dish - Yellow
datasets[3].borderColor = '#4BC0C0'; // Daily Salad - Green
// Create chart
const ctx = document.getElementById('weeklyAverageChart').getContext('2d');
new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: datasets
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true,
max: 5,
ticks: {
stepSize: 1
}
},
x: {
ticks: {
maxRotation: 45,
minRotation: 45
}
}
},
plugins: {
title: {
display: true,
color: 'black',
font: {
size: 16
}
},
legend: {
labels: {
color: 'black'
}
}
}
}
});
}
+38 -22
View File
@@ -17,25 +17,47 @@
<div class="value-wrapper">
<h2>Anzahl aller Bewertungen</h2>
<p class="feedbackValue feedbackValue-countAllEver">0</p>
<script src="countAllEver.js"></script>
<script src="src/countAllEver.js"></script>
</div>
<div class="chart-wrapper">
<div class="value-wrapper">
<h2>Anzahl Bewertungen letzter Woche</h2>
<canvas id="feedbackChart-totalCategoryCounts3" width="450" height="250"></canvas>
<script></script>
<p class="feedbackValue feedbackValue-countAllLastWeek"></p>
<script src="src/countAllLastWeek.js"></script>
</div>
<div class="chart-wrapper">
<h2>Anzahl Bewertungen letzten Monat</h2>
<canvas id="feedbackChart-totalCategoryCounts4" width="450" height="250"></canvas>
<script src=""></script>
<div class="m2x2-wrapper">
<div class="m2x2-grid">
<div class="m2x2-element">
<h3>Fleisch / Fisch heute</h3>
<p class="mini-value" id="todaysAverageMeatFish"></p>
</div>
<div class="m2x2-element">
<h3>Vegetarisch heute</h3>
<p class="mini-value" id="todaysAverageVeggi"></p>
</div>
<div class="m2x2-element">
<h3>Tagesgesempfehlung heute</h3>
<p class="mini-value" id="todaysAverageDailyDish"></p>
</div>
<div class="m2x2-element">
<h3>Tagessalat heute</h3>
<p class="mini-value" id="todaysAverageDailySalad"></p>
</div>
</div>
<script src="src/todaysAverage.js"></script>
</div>
<div class="chart-wrapper">
<h2>Verteilung aller Bewertungen (N / %)</h2>
<canvas id="feedbackChart-distributionOfAllRatings" width="450" height="250"></canvas>
<script src="distributionOfAllRatings.js"></script>
<canvas id="feedbackChart-distributionOfAllRatings" width="450" height="200"></canvas>
<script src="src/distributionOfAllRatings.js"></script>
</div>
<div class="chart-wrapper-double">
<h2>Wöchentliche Durchschnitte</h2>
<canvas id="weeklyAverageChart" width="420" height="150" style="margin-top:-50px;"></canvas>
<script src="src/weeklyAverage.js"></script>
</div>
<div class="chart-wrapper">
@@ -46,26 +68,20 @@
<div class="chart-wrapper">
<h2>Vert. Bewertungen letzten Monat (N / %)</h2>
<canvas id="feedbackChart-totalCategoryCounts6" width="450" height="250"></canvas>
<script src="totalCategoryCounts.js"></script>
<canvas id="feedbackChart-pie" width="450" height="250"></canvas>
<script src="src/pie.js"></script>
</div>
<div class="chart-wrapper">
<h2>Alle Bewertungen</h2>
<canvas id="feedbackChart-totalCategoryCounts7" width="450" height="250"></canvas>
<script src="totalCategoryCounts.js"></script>
<canvas id="feedbackChart-line" width="450" height="250"></canvas>
<script src="src/line.js"></script>
</div>
<div class="chart-wrapper">
<h2>Alle Bewertungen</h2>
<canvas id="feedbackChart-totalCategoryCounts8" width="450" height="250"></canvas>
<script src="totalCategoryCounts.js"></script>
</div>
<div class="chart-wrapper">
<h2>Alle Bewertungen</h2>
<canvas id="feedbackChart-totalCategoryCounts8" width="450" height="250"></canvas>
<script src="totalCategoryCounts.js"></script>
<canvas id="feedbackChart-radar" width="450" height="250"></canvas>
<script src="src/radar.js"></script>
</div>
</div>
+54 -13
View File
@@ -121,12 +121,19 @@ th {
}
.chart-wrapper {
padding: 0px 20px 20px 20px;
background: rgb(224, 224, 224);
background: rgb(190, 190, 190);
border-radius: 20px;
color: black;
flex: 1 1 450px; /* Flex-grow, flex-shrink, flex-basis */
max-width: 450px;
width: 450px;
height: 350px;
}
.chart-wrapper-double {
background: rgb(190, 190, 190);
border-radius: 20px;
color: black;
width: 920px;
height: 350px;
}
.value-wrapper {
@@ -134,25 +141,59 @@ display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 20px;
background: rgb(224, 224, 224);
background: rgb(190, 190, 190);
border-radius: 20px;
color: black;
flex: 1 1 450px; /* Flex-grow, flex-shrink, flex-basis */
max-width: 450px;
width: 450px;
height: 350px;
text-align: center;
position: relative;
}
.value-wrapper h2 {
position: absolute;
top: 20px;
left: 105px;
position: relative;
top: 60px;
margin: 0;
}
.m2x2-wrapper {
border-radius: 20px;
color: black;
width: 450px; /* Feste Breite */
height: 350px; /* Feste Höhe */
}
.feedbackValue {
font-size: 6rem;
font-size: 8rem;
font-weight: bold;
margin-top: 100px; /* Abstand zur Überschrift */
margin-top: 125px; /* Abstand zur Überschrift */
}
.m2x2-grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
gap: 20px;
height: 100%;
}
.m2x2-element {
background: rgb(190, 190, 190);
border-radius: 20px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.m2x2-element h3 {
margin: 0;
font-size: 1rem;
margin-bottom: 10px;
}
.mini-value {
font-size: 2rem;
font-weight: bold;
margin: 0;
}