data is now written into sqlite db, csv data still there

new data is written into db and can be fetched from there. old data remains in csv until migration, also can be fetched
This commit is contained in:
Mateo Körber Belmonte
2025-03-02 14:47:52 +01:00
parent d70815be5e
commit 58ad3436ab
+98 -36
View File
@@ -6,6 +6,7 @@ const bodyParser = require('body-parser');
const fs = require('fs'); const fs = require('fs');
const createCsvWriter = require('csv-writer').createObjectCsvWriter; const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csv = require('csvtojson'); const csv = require('csvtojson');
const Database = require('better-sqlite3');
// Create an Express app // Create an Express app
const app = express(); const app = express();
@@ -29,6 +30,23 @@ if (!fs.existsSync(csvDir)) {
if (!fs.existsSync(csvFilePath)) { if (!fs.existsSync(csvFilePath)) {
fs.writeFileSync(csvFilePath, 'DATE,CATEGORY,RATING\n'); fs.writeFileSync(csvFilePath, 'DATE,CATEGORY,RATING\n');
} }
const dataDir = path.join(__dirname, '../../tablet-web-survey-data');
db = new Database(path.join(dataDir, 'data.db'));
db.exec(`
CREATE TABLE IF NOT EXISTS feedback_entries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date TEXT,
time TEXT,
category VARCHAR(50),
rating VARCHAR(20),
CHECK (rating IN ('veryBad', 'bad', 'neutral', 'good', 'veryGood'))
)
`);
const csvWriter = createCsvWriter({ const csvWriter = createCsvWriter({
path: csvFilePath, path: csvFilePath,
header: [ header: [
@@ -50,26 +68,25 @@ app.get('/statistics', (req, res) => {
// Handle form submission // Handle form submission
app.post('/submit', (req, res) => { app.post('/submit', (req, res) => {
let date = new Date().toLocaleString('de-DE', { timeZone: 'Europe/Berlin' }).replace('"', '').replace(',', ''); const now = new Date();
const date = now.toLocaleDateString('de-DE', { timeZone: 'Europe/Berlin' });
const time = now.toLocaleTimeString('de-DE', { timeZone: 'Europe/Berlin' });
const results = req.body; const results = req.body;
// Ergebnisse in die CSV-Datei einfügen try {
const records = Object.keys(results).map(category => ({ // Prepare the insert statement
date, const stmt = db.prepare('INSERT INTO feedback_entries (date, time, category, rating) VALUES (?, ?, ?, ?)');
category,
rating: results[category] // Insert each category rating
})); Object.keys(results).forEach(category => {
stmt.run(date, time, category, results[category]);
});
if (records.length > 0) {
csvWriter.writeRecords(records)
.then(() => {
console.log('Received feedback:', results); console.log('Received feedback:', results);
res.json({ status: 'success' }); res.json({ status: 'success' });
}) } catch (error) {
.catch(error => { console.error('Error writing to database:', error);
console.error('Error writing to CSV:', error); res.status(500).json({ status: 'error', message: 'Error writing to database' });
res.status(500).json({ status: 'error', message: 'Error writing to CSV' });
});
} }
}); });
@@ -112,17 +129,31 @@ app.get('/survey', (req, res) => {
// Serve the survey results data // Serve the survey results data
app.get('/results', (req, res) => { app.get('/results', (req, res) => {
const csvFilePath = path.join(__dirname, '../../tablet-web-survey-data/data.csv'); const csvFilePath = path.join(__dirname, '../../tablet-web-survey-data/data.csv');
// Get data from DB
const dbResults = db.prepare(`
SELECT
date || ' ' || time as date,
category,
rating
FROM feedback_entries
`).all();
// Get data from CSV
csv({ csv({
noheader: true, noheader: true,
headers: ['date', 'category', 'rating'] headers: ['date', 'category', 'rating']
}) })
.fromFile(csvFilePath) .fromFile(csvFilePath)
.then((jsonObj) => { .then((csvResults) => {
res.json(jsonObj); // Combine both results
const combinedResults = [...csvResults.slice(1), ...dbResults];
res.json(combinedResults);
}) })
.catch((error) => { .catch((error) => {
console.error('Error reading CSV:', error); console.error('Error reading CSV:', error);
res.status(500).json({ status: 'error', message: 'Error reading CSV' }); // If CSV fails, return at least DB results
res.json(dbResults);
}); });
}); });
@@ -135,26 +166,16 @@ app.get('/summary', (req, res) => {
app.get('/weekly-summary', (req, res) => { app.get('/weekly-summary', (req, res) => {
const week = req.query.week; const week = req.query.week;
if (!week) { if (!week) {
return res.status(400).json({ status: 'error', message: 'Week query parameter is required! Example: domain.com/weekly-summary?week=2025-02-03' }); return res.status(400).json({ status: 'error', message: 'Week query parameter is required!' });
} }
const [year, month, day] = week.split('-').map(Number); const [year, month, day] = week.split('-').map(Number);
const weekStart = new Date(Date.UTC(year, month - 1, day)); const weekStart = new Date(Date.UTC(year, month - 1, day));
if (isNaN(weekStart.getTime())) {
return res.status(400).json({ status: 'error', message: 'Invalid date format' });
}
const weekEnd = new Date(weekStart); const weekEnd = new Date(weekStart);
weekEnd.setUTCDate(weekStart.getUTCDate() + 4); // Friday of the selected week weekEnd.setUTCDate(weekStart.getUTCDate() + 4);
weekEnd.setUTCHours(23, 59, 59, 999); // End of Friday weekEnd.setUTCHours(23, 59, 59, 999);
const csvFilePath = path.join(__dirname, '../../tablet-web-survey-data/data.csv'); // Initialize summary structure
csv({
noheader: true,
headers: ['date', 'category', 'rating']
})
.fromFile(csvFilePath)
.then((jsonObj) => {
const summary = { const summary = {
Monday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Tagessalat: [] }, Monday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Tagessalat: [] },
Tuesday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Tagessalat: [] }, Tuesday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Tagessalat: [] },
@@ -171,10 +192,40 @@ app.get('/weekly-summary', (req, res) => {
veryGood: 5 veryGood: 5
}; };
jsonObj.slice(1).forEach(row => { // Get DB data
const dbResults = db.prepare(`
SELECT date, time, category, rating
FROM feedback_entries
WHERE date BETWEEN ? AND ?
`).all(
weekStart.toLocaleDateString('de-DE'),
weekEnd.toLocaleDateString('de-DE')
);
// Process DB results
dbResults.forEach(row => {
const [day, month, year] = row.date.split('.').map(Number);
const [hours, minutes, seconds] = row.time.split(':').map(Number);
const date = new Date(Date.UTC(year, month - 1, day, hours, minutes, seconds));
const dayName = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getUTCDay()];
if (summary[dayName]) {
summary[dayName][row.category].push(ratingValues[row.rating]);
}
});
// Get CSV data
csv({
noheader: true,
headers: ['date', 'category', 'rating']
})
.fromFile(path.join(__dirname, '../../tablet-web-survey-data/data.csv'))
.then((csvResults) => {
// Process CSV results
csvResults.slice(1).forEach(row => {
const [datePart, timePart] = row.date.split(' '); const [datePart, timePart] = row.date.split(' ');
const [day, month, year] = datePart.split('.').map(Number); const [day, month, year] = datePart.split('.').map(Number);
const [hours, minutes, seconds] = timePart.split(':').map(Number); const [hours, minutes, seconds] = timePart ? timePart.split(':').map(Number) : [0, 0, 0];
const date = new Date(Date.UTC(year, month - 1, day, hours, minutes, seconds)); const date = new Date(Date.UTC(year, month - 1, day, hours, minutes, seconds));
if (date >= weekStart && date <= weekEnd) { if (date >= weekStart && date <= weekEnd) {
@@ -185,7 +236,7 @@ app.get('/weekly-summary', (req, res) => {
} }
}); });
// Calculate average ratings and count votes // Calculate final averages combining both sources
const averageSummary = {}; const averageSummary = {};
Object.keys(summary).forEach(day => { Object.keys(summary).forEach(day => {
averageSummary[day] = {}; averageSummary[day] = {};
@@ -201,7 +252,18 @@ app.get('/weekly-summary', (req, res) => {
}) })
.catch((error) => { .catch((error) => {
console.error('Error reading CSV:', error); console.error('Error reading CSV:', error);
res.status(500).json({ status: 'error', message: 'Error reading CSV' }); // If CSV fails, calculate averages from DB data only
const averageSummary = {};
Object.keys(summary).forEach(day => {
averageSummary[day] = {};
Object.keys(summary[day]).forEach(category => {
const ratings = summary[day][category];
const average = ratings.length ? (ratings.reduce((a, b) => a + b, 0) / ratings.length).toFixed(2) : 'N/A';
const count = ratings.length;
averageSummary[day][category] = { average, count };
});
});
res.json(averageSummary);
}); });
}); });