diff --git a/express/server.js b/express/server.js index 78c7065..236ebba 100644 --- a/express/server.js +++ b/express/server.js @@ -19,10 +19,17 @@ const db = new sqlite3.Database("db/results.db", (err) => { session_id TEXT, animal_name TEXT, button_name TEXT, - timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, + timestamp DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, time_difference INTEGER -- Add this column for time difference ) `); + db.run(` + CREATE TABLE IF NOT EXISTS animals ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL UNIQUE, + timestamp DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL + ) + `); } }); @@ -43,6 +50,22 @@ try { } var jsondata = JSON.parse(data); animals = jsondata.animals; + for (const animal of animals) { + db.run( + ` + INSERT INTO animals(name) + SELECT '${animal}' + WHERE NOT EXISTS(SELECT 1 FROM animals WHERE name = '${animal}'); + `, + (err) => { + if (err) { + console.error(`Error inserting animal ${animal}: `, err.message); + } else { + console.log(`Success inserting animal ${animal}`); + } + }, + ); + } }); } catch (error) { console.error("Error loading animals:", error); @@ -104,30 +127,77 @@ app.post("/recordButtonClick", (req, res) => { // Route to show the current results from SQLite app.get("/results", async (req, res) => { try { - const results = { count: {} }; + const results = { + count: {}, + avgTimes: {}, + }; const getCount = new Promise((resolve, reject) => { - db.all( - "SELECT animal_name, button_name, COUNT(*) as count FROM button_clicks GROUP BY button_name, animal_name", - (err, rows) => { - if (err) { - reject("getCount: "+ err.message); - } else { - rows.forEach((row) => { - if (typeof results.count[row.animal_name] == "undefined") { - results.count[row.animal_name] = {}; - } - results.count[row.animal_name][row.button_name] = row.count; - }); - resolve(); - } - }, - ); + db.all( + ` + SELECT animal_name, button_name, COUNT(*) AS count + FROM button_clicks + GROUP BY button_name, animal_name + `, + (err, rows) => { + if (err) { + reject("getCount: " + err.message); + } else { + rows.forEach((row) => { + if (typeof results.count[row.animal_name] == "undefined") { + results.count[row.animal_name] = {}; + } + results.count[row.animal_name][row.button_name] = row.count; + }); + resolve(); + } + }, + ); }); const getAvgTime = new Promise((resolve, reject) => { - //... - resolve(); - }) - await Promise.all([getCount, getAvgTime]); + db.all( + ` + SELECT animal_name, button_name, AVG(time_difference) AS time + FROM button_clicks + GROUP BY animal_name, button_name; + `, + (err, rows) => { + if (err) { + reject("getAvgTime: " + err.message); + } else { + rows.forEach((row) => { + if (typeof results.avgTimes[row.animal_name] == "undefined") { + results.avgTimes[row.animal_name] = {}; + } + results.avgTimes[row.animal_name][row.button_name] = row.time; + }); + resolve(); + } + }, + ); + }); + const getTotalAvgTime = new Promise((resolve, reject) => { + db.all( + ` + SELECT animal_name, AVG(time_difference) AS time + FROM button_clicks + GROUP BY animal_name; + `, + (err, rows) => { + if (err) { + reject("getTotalAvgTime: " + err.message); + } else { + rows.forEach((row) => { + if (typeof results.avgTimes[row.animal_name] == "undefined") { + results.avgTimes[row.animal_name] = {}; + } + results.avgTimes[row.animal_name]["total"] = row.time; + }); + resolve(); + } + }, + ); + }); + await Promise.all([getCount, getTotalAvgTime, getAvgTime]); res.json(results); } catch (error) { console.error("Error fetching results:", error);