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
+112 -50
View File
@@ -6,6 +6,7 @@ const bodyParser = require('body-parser');
const fs = require('fs');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csv = require('csvtojson');
const Database = require('better-sqlite3');
// Create an Express app
const app = express();
@@ -29,6 +30,23 @@ if (!fs.existsSync(csvDir)) {
if (!fs.existsSync(csvFilePath)) {
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({
path: csvFilePath,
header: [
@@ -50,26 +68,25 @@ app.get('/statistics', (req, res) => {
// Handle form submission
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;
// Ergebnisse in die CSV-Datei einfügen
const records = Object.keys(results).map(category => ({
date,
category,
rating: results[category]
}));
try {
// Prepare the insert statement
const stmt = db.prepare('INSERT INTO feedback_entries (date, time, category, rating) VALUES (?, ?, ?, ?)');
// 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);
res.json({ status: 'success' });
})
.catch(error => {
console.error('Error writing to CSV:', error);
res.status(500).json({ status: 'error', message: 'Error writing to CSV' });
});
console.log('Received feedback:', results);
res.json({ status: 'success' });
} catch (error) {
console.error('Error writing to database:', error);
res.status(500).json({ status: 'error', message: 'Error writing to database' });
}
});
@@ -112,17 +129,31 @@ app.get('/survey', (req, res) => {
// Serve the survey results data
app.get('/results', (req, res) => {
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({
noheader: true,
headers: ['date', 'category', 'rating']
})
.fromFile(csvFilePath)
.then((jsonObj) => {
res.json(jsonObj);
.then((csvResults) => {
// Combine both results
const combinedResults = [...csvResults.slice(1), ...dbResults];
res.json(combinedResults);
})
.catch((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,46 +166,66 @@ app.get('/summary', (req, res) => {
app.get('/weekly-summary', (req, res) => {
const week = req.query.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 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);
weekEnd.setUTCDate(weekStart.getUTCDate() + 4); // Friday of the selected week
weekEnd.setUTCHours(23, 59, 59, 999); // End of Friday
weekEnd.setUTCDate(weekStart.getUTCDate() + 4);
weekEnd.setUTCHours(23, 59, 59, 999);
const csvFilePath = path.join(__dirname, '../../tablet-web-survey-data/data.csv');
// Initialize summary structure
const summary = {
Monday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Tagessalat: [] },
Tuesday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Tagessalat: [] },
Wednesday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Tagessalat: [] },
Thursday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Tagessalat: [] },
Friday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Tagessalat: [] }
};
const ratingValues = {
veryBad: 1,
bad: 2,
neutral: 3,
good: 4,
veryGood: 5
};
// 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(csvFilePath)
.then((jsonObj) => {
const summary = {
Monday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Tagessalat: [] },
Tuesday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Tagessalat: [] },
Wednesday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Tagessalat: [] },
Thursday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Tagessalat: [] },
Friday: { Fleischgericht: [], Vegetarisch: [], Tagesgericht: [], Tagessalat: [] }
};
const ratingValues = {
veryBad: 1,
bad: 2,
neutral: 3,
good: 4,
veryGood: 5
};
jsonObj.slice(1).forEach(row => {
.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 [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));
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 = {};
Object.keys(summary).forEach(day => {
averageSummary[day] = {};
@@ -201,7 +252,18 @@ app.get('/weekly-summary', (req, res) => {
})
.catch((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);
});
});