express: results now has averge times!
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
e0bf0d8609
commit
771e5a188d
1 changed files with 92 additions and 22 deletions
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue