express: results now has averge times!

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2023-09-15 12:12:49 -04:00
parent e0bf0d8609
commit 771e5a188d
Signed by: vbatts
GPG key ID: E30EFAA812C6E5ED

View file

@ -19,10 +19,17 @@ const db = new sqlite3.Database("db/results.db", (err) => {
session_id TEXT, session_id TEXT,
animal_name TEXT, animal_name TEXT,
button_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 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); var jsondata = JSON.parse(data);
animals = jsondata.animals; 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) { } catch (error) {
console.error("Error loading animals:", error); console.error("Error loading animals:", error);
@ -104,30 +127,77 @@ app.post("/recordButtonClick", (req, res) => {
// Route to show the current results from SQLite // Route to show the current results from SQLite
app.get("/results", async (req, res) => { app.get("/results", async (req, res) => {
try { try {
const results = { count: {} }; const results = {
count: {},
avgTimes: {},
};
const getCount = new Promise((resolve, reject) => { const getCount = new Promise((resolve, reject) => {
db.all( db.all(
"SELECT animal_name, button_name, COUNT(*) as count FROM button_clicks GROUP BY button_name, animal_name", `
(err, rows) => { SELECT animal_name, button_name, COUNT(*) AS count
if (err) { FROM button_clicks
reject("getCount: "+ err.message); GROUP BY button_name, animal_name
} else { `,
rows.forEach((row) => { (err, rows) => {
if (typeof results.count[row.animal_name] == "undefined") { if (err) {
results.count[row.animal_name] = {}; reject("getCount: " + err.message);
} } else {
results.count[row.animal_name][row.button_name] = row.count; rows.forEach((row) => {
}); if (typeof results.count[row.animal_name] == "undefined") {
resolve(); results.count[row.animal_name] = {};
} }
}, results.count[row.animal_name][row.button_name] = row.count;
); });
resolve();
}
},
);
}); });
const getAvgTime = new Promise((resolve, reject) => { const getAvgTime = new Promise((resolve, reject) => {
//... db.all(
resolve(); `
}) SELECT animal_name, button_name, AVG(time_difference) AS time
await Promise.all([getCount, getAvgTime]); 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); res.json(results);
} catch (error) { } catch (error) {
console.error("Error fetching results:", error); console.error("Error fetching results:", error);