add timestamp and new weekly reseults

This commit is contained in:
Mateo Körber Belmonte
2025-01-17 13:44:50 +01:00
parent 45041b060f
commit 4bd9a6ca42
+57 -18
View File
@@ -34,7 +34,7 @@ app.get('/', (req, res) => {
// Handle form submission // Handle form submission
app.post('/submit', (req, res) => { app.post('/submit', (req, res) => {
const date = new Date().toISOString().split('T')[0]; // Aktuelles Datum const date = new Date().toISOString().replace('T', ' ').split('.')[0]; // Aktuelles Datum und Uhrzeit
const results = req.body; const results = req.body;
// Ergebnisse in die CSV-Datei einfügen // Ergebnisse in die CSV-Datei einfügen
@@ -55,7 +55,7 @@ app.post('/submit', (req, res) => {
res.status(500).json({ status: 'error', message: 'Error writing to CSV' }); res.status(500).json({ status: 'error', message: 'Error writing to CSV' });
}); });
} }
}); });
// Serve the survey results page // Serve the survey results page
@@ -123,7 +123,11 @@ app.get('/summary', (req, res) => {
<link rel="stylesheet" type="text/css" href="style.css"> <link rel="stylesheet" type="text/css" href="style.css">
</head> </head>
<body> <body>
<h1>Weekly Summary</h1> <h1 id="week-title"></h1>
<div>
<button id="prev-week">&lt;</button>
<button id="next-week">&gt;</button>
</div>
<table id="summary-table"> <table id="summary-table">
<thead> <thead>
<tr> <tr>
@@ -139,10 +143,23 @@ app.get('/summary', (req, res) => {
</tbody> </tbody>
</table> </table>
<script> <script>
fetch('/weekly-summary') let currentWeek = new Date();
currentWeek.setDate(currentWeek.getDate() - currentWeek.getDay() + 1); // Set to Monday of the current week
function updateWeekTitle() {
const startOfWeek = new Date(currentWeek);
const endOfWeek = new Date(currentWeek);
endOfWeek.setDate(startOfWeek.getDate() + 4); // Friday of the current week
const weekNumber = Math.ceil(((startOfWeek - new Date(startOfWeek.getFullYear(), 0, 1)) / 86400000 + startOfWeek.getDay() + 1) / 7);
document.getElementById('week-title').textContent = \`KW\${weekNumber} \${startOfWeek.toLocaleDateString()} - \${endOfWeek.toLocaleDateString()}\`;
}
function fetchSummary() {
fetch(\`/weekly-summary?week=\${currentWeek.toISOString()}\`)
.then(response => response.json()) .then(response => response.json())
.then(data => { .then(data => {
const tableBody = document.getElementById('summary-table').getElementsByTagName('tbody')[0]; const tableBody = document.getElementById('summary-table').getElementsByTagName('tbody')[0];
tableBody.innerHTML = '';
const categories = ['Fleischgericht', 'Vegetarisch', 'Tagesgericht', 'Service']; const categories = ['Fleischgericht', 'Vegetarisch', 'Tagesgericht', 'Service'];
categories.forEach(category => { categories.forEach(category => {
const tr = document.createElement('tr'); const tr = document.createElement('tr');
@@ -160,6 +177,22 @@ app.get('/summary', (req, res) => {
.catch(error => { .catch(error => {
console.error('Error fetching summary:', error); console.error('Error fetching summary:', error);
}); });
}
document.getElementById('prev-week').addEventListener('click', () => {
currentWeek.setDate(currentWeek.getDate() - 7);
updateWeekTitle();
fetchSummary();
});
document.getElementById('next-week').addEventListener('click', () => {
currentWeek.setDate(currentWeek.getDate() + 7);
updateWeekTitle();
fetchSummary();
});
updateWeekTitle();
fetchSummary();
</script> </script>
</body> </body>
</html> </html>
@@ -168,6 +201,10 @@ app.get('/summary', (req, res) => {
// Serve the weekly summary data // Serve the weekly summary data
app.get('/weekly-summary', (req, res) => { app.get('/weekly-summary', (req, res) => {
const weekStart = new Date(req.query.week);
const weekEnd = new Date(weekStart);
weekEnd.setDate(weekStart.getDate() + 4); // Friday of the selected week
const csvFilePath = 'survey_results.csv'; const csvFilePath = 'survey_results.csv';
csv({ csv({
noheader: true, noheader: true,
@@ -176,11 +213,11 @@ app.get('/weekly-summary', (req, res) => {
.fromFile(csvFilePath) .fromFile(csvFilePath)
.then((jsonObj) => { .then((jsonObj) => {
const summary = { const summary = {
Monday: { Fleischgericht: { total: 0, count: 0 }, Vegetarisch: { total: 0, count: 0 }, Tagesgericht: { total: 0, count: 0 }, Service: { total: 0, count: 0 } }, Monday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Service: [] },
Tuesday: { Fleischgericht: { total: 0, count: 0 }, Vegetarisch: { total: 0, count: 0 }, Tagesgericht: { total: 0, count: 0 }, Service: { total: 0, count: 0 } }, Tuesday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Service: [] },
Wednesday: { Fleischgericht: { total: 0, count: 0 }, Vegetarisch: { total: 0, count: 0 }, Tagesgericht: { total: 0, count: 0 }, Service: { total: 0, count: 0 } }, Wednesday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Service: [] },
Thursday: { Fleischgericht: { total: 0, count: 0 }, Vegetarisch: { total: 0, count: 0 }, Tagesgericht: { total: 0, count: 0 }, Service: { total: 0, count: 0 } }, Thursday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Service: [] },
Friday: { Fleischgericht: { total: 0, count: 0 }, Vegetarisch: { total: 0, count: 0 }, Tagesgericht: { total: 0, count: 0 }, Service: { total: 0, count: 0 } } Friday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Service: [] }
}; };
const ratingValues = { const ratingValues = {
@@ -199,28 +236,30 @@ app.get('/weekly-summary', (req, res) => {
jsonObj.forEach(row => { jsonObj.forEach(row => {
const date = new Date(row.date); const date = new Date(row.date);
if (date >= weekStart && date <= weekEnd) {
const day = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()]; const day = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()];
if (summary[day]) { if (summary[day]) {
if (row.category === 'Service') { if (row.category === 'Service') {
summary[day][row.category].total += serviceRatingValues[row.rating]; summary[day][row.category].push(serviceRatingValues[row.rating]);
} else { } else {
summary[day][row.category].total += ratingValues[row.rating]; summary[day][row.category].push(ratingValues[row.rating]);
}
} }
summary[day][row.category].count += 1;
} }
}); });
// Calculate average ratings
const averageSummary = {};
Object.keys(summary).forEach(day => { Object.keys(summary).forEach(day => {
averageSummary[day] = {};
Object.keys(summary[day]).forEach(category => { Object.keys(summary[day]).forEach(category => {
if (summary[day][category].count > 0) { const ratings = summary[day][category];
summary[day][category] = (summary[day][category].total / summary[day][category].count).toFixed(2); const average = ratings.length ? (ratings.reduce((a, b) => a + b, 0) / ratings.length).toFixed(2) : 'N/A';
} else { averageSummary[day][category] = average;
summary[day][category] = 'N/A';
}
}); });
}); });
res.json(summary); res.json(averageSummary);
}) })
.catch((error) => { .catch((error) => {
console.error('Error reading CSV:', error); console.error('Error reading CSV:', error);