2023-09-29 01:31:05 +00:00
|
|
|
const express = require('express')
|
|
|
|
const session = require('express-session')
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const morgan = require('morgan')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const sqlite3 = require('sqlite3').verbose()
|
|
|
|
const bole = require('bole')
|
2023-09-22 20:39:37 +00:00
|
|
|
|
2023-09-29 01:31:05 +00:00
|
|
|
const config = require('./config')
|
2023-09-29 01:19:58 +00:00
|
|
|
|
2023-09-29 01:31:05 +00:00
|
|
|
const log = bole('app')
|
|
|
|
const app = express()
|
2023-09-22 20:39:37 +00:00
|
|
|
|
|
|
|
// Create an SQLite database and initialize tables
|
2023-09-28 18:13:59 +00:00
|
|
|
const db = new sqlite3.Database(config.db_path, (err) => {
|
2023-09-22 20:39:37 +00:00
|
|
|
if (err) {
|
2023-09-29 01:31:05 +00:00
|
|
|
log.error('Error opening SQLite database:', err.message)
|
2023-09-22 20:39:37 +00:00
|
|
|
} else {
|
2023-09-29 01:31:05 +00:00
|
|
|
log.info('Connected to SQLite database', config.db_path)
|
2023-09-22 20:39:37 +00:00
|
|
|
db.run(`
|
|
|
|
CREATE TABLE IF NOT EXISTS button_clicks (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
session_id TEXT,
|
|
|
|
animal_name TEXT,
|
|
|
|
button_name TEXT,
|
|
|
|
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
|
|
|
time_difference INTEGER -- Add this column for time difference
|
|
|
|
)
|
2023-09-29 01:31:05 +00:00
|
|
|
`)
|
2023-09-22 20:39:37 +00:00
|
|
|
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
|
|
|
|
)
|
2023-09-29 01:31:05 +00:00
|
|
|
`)
|
2023-09-22 20:39:37 +00:00
|
|
|
}
|
2023-09-29 01:31:05 +00:00
|
|
|
})
|
2023-09-22 20:39:37 +00:00
|
|
|
|
2023-09-29 01:31:05 +00:00
|
|
|
const accessLogStream = fs.createWriteStream(
|
|
|
|
path.join(__dirname, 'log', 'access.log'),
|
|
|
|
{ flags: 'a' }
|
|
|
|
)
|
2023-09-22 20:39:37 +00:00
|
|
|
|
2023-09-29 01:31:05 +00:00
|
|
|
app.use(bodyParser.json())
|
|
|
|
app.use(morgan('combined', { stream: accessLogStream }))
|
2023-09-29 01:19:58 +00:00
|
|
|
app.use(session({
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: false,
|
2023-09-29 01:31:05 +00:00
|
|
|
secret: config.session_token
|
|
|
|
}))
|
2023-09-22 20:39:37 +00:00
|
|
|
|
2023-09-29 01:31:05 +00:00
|
|
|
let animals
|
2023-09-22 20:39:37 +00:00
|
|
|
// check and load animals into redis
|
|
|
|
try {
|
2023-09-29 01:31:05 +00:00
|
|
|
fs.readFile('./animals.json', function (err, data) {
|
2023-09-22 20:39:37 +00:00
|
|
|
if (err) {
|
2023-09-29 01:31:05 +00:00
|
|
|
throw err
|
2023-09-22 20:39:37 +00:00
|
|
|
}
|
2023-09-29 01:31:05 +00:00
|
|
|
const jsondata = JSON.parse(data)
|
|
|
|
animals = jsondata.animals
|
2023-09-22 20:39:37 +00:00
|
|
|
for (const animal of animals) {
|
|
|
|
db.run(`
|
|
|
|
INSERT INTO animals(name)
|
|
|
|
SELECT ?
|
|
|
|
WHERE NOT EXISTS(SELECT 1 FROM animals WHERE name = ?);
|
|
|
|
`,
|
2023-09-29 01:31:05 +00:00
|
|
|
[animal, animal],
|
|
|
|
(err) => {
|
|
|
|
if (err) {
|
|
|
|
log.error(`Error inserting animal ${animal}: `, err.message)
|
|
|
|
} else {
|
|
|
|
log.info(`Success inserting animal ${animal}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2023-09-22 20:39:37 +00:00
|
|
|
}
|
2023-09-29 01:31:05 +00:00
|
|
|
})
|
2023-09-22 20:39:37 +00:00
|
|
|
} catch (error) {
|
2023-09-29 01:31:05 +00:00
|
|
|
log.error('Error loading animals:', error)
|
|
|
|
animals = ['Dog', 'Cat', 'Elephant', 'Lion', 'Giraffe']
|
2023-09-22 20:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serve the HTML file
|
2023-09-29 01:31:05 +00:00
|
|
|
app.get('/', (req, res) => {
|
|
|
|
if (typeof req.session.cookie.expires === 'undefined') {
|
|
|
|
const hour = 3600000
|
|
|
|
req.session.cookie.expires = new Date(Date.now() + hour)
|
|
|
|
req.session.cookie.maxAge = hour
|
2023-09-29 01:19:58 +00:00
|
|
|
}
|
|
|
|
|
2023-09-29 01:31:05 +00:00
|
|
|
res.sendFile(path.join(__dirname, 'index.html'))
|
|
|
|
})
|
|
|
|
app.get('/asset/frontend.js', (req, res) => {
|
|
|
|
res.sendFile(path.join(__dirname, 'asset/frontend.js'))
|
|
|
|
})
|
2023-09-22 20:39:37 +00:00
|
|
|
|
2023-09-29 01:31:05 +00:00
|
|
|
app.get('/newSession', (req, res) => {
|
2023-09-29 01:19:58 +00:00
|
|
|
// XXX
|
2023-09-29 01:31:05 +00:00
|
|
|
log.info(req.session)
|
|
|
|
req.session.regenerate()
|
|
|
|
log.info(req.session)
|
|
|
|
})
|
2023-09-29 01:19:58 +00:00
|
|
|
|
2023-09-22 20:39:37 +00:00
|
|
|
// Route to get a random animal name
|
2023-09-29 01:31:05 +00:00
|
|
|
app.get('/getNextAnimal', async (req, res) => {
|
2023-09-22 20:39:37 +00:00
|
|
|
try {
|
|
|
|
// TODO this is currently random, and should have a bit of reasoning behind the next choice
|
2023-09-29 01:31:05 +00:00
|
|
|
const randomIndex = Math.floor(Math.random() * animals.length)
|
|
|
|
const randomAnimal = animals[randomIndex]
|
|
|
|
res.json({ animalName: randomAnimal })
|
2023-09-22 20:39:37 +00:00
|
|
|
} catch (error) {
|
2023-09-29 01:31:05 +00:00
|
|
|
log.error('Error fetching random animal:', error)
|
|
|
|
res.status(500).json({ error: 'Internal server error' })
|
2023-09-22 20:39:37 +00:00
|
|
|
}
|
2023-09-29 01:31:05 +00:00
|
|
|
})
|
2023-09-22 20:39:37 +00:00
|
|
|
|
|
|
|
// Route to record button clicks along with session IDs in SQLite
|
2023-09-29 01:31:05 +00:00
|
|
|
app.post('/recordButtonClick', (req, res) => {
|
2023-09-22 20:39:37 +00:00
|
|
|
try {
|
2023-09-29 01:31:05 +00:00
|
|
|
// const { buttonName, sessionId } = req.body;
|
|
|
|
const result = req.body
|
|
|
|
log.error(result)
|
2023-09-22 20:39:37 +00:00
|
|
|
|
|
|
|
db.run(
|
2023-09-29 01:31:05 +00:00
|
|
|
'INSERT INTO button_clicks (session_id, animal_name, button_name, timestamp, time_difference) VALUES (?, ?, ?, ?, ?)',
|
2023-09-22 20:39:37 +00:00
|
|
|
[
|
|
|
|
result.session,
|
|
|
|
result.animal,
|
|
|
|
result.button,
|
|
|
|
result.time,
|
2023-09-29 01:31:05 +00:00
|
|
|
result.difference
|
2023-09-22 20:39:37 +00:00
|
|
|
],
|
|
|
|
(err) => {
|
|
|
|
if (err) {
|
2023-09-29 01:31:05 +00:00
|
|
|
log.error('Error recording button click:', err.message)
|
|
|
|
res.status(500).json({ error: 'Internal server error' })
|
2023-09-22 20:39:37 +00:00
|
|
|
} else {
|
2023-09-29 01:31:05 +00:00
|
|
|
res.sendStatus(200)
|
2023-09-22 20:39:37 +00:00
|
|
|
}
|
2023-09-29 01:31:05 +00:00
|
|
|
}
|
|
|
|
)
|
2023-09-22 20:39:37 +00:00
|
|
|
} catch (error) {
|
2023-09-29 01:31:05 +00:00
|
|
|
log.error('Error recording button click:', error)
|
|
|
|
res.status(500).json({ error: 'Internal server error' })
|
2023-09-22 20:39:37 +00:00
|
|
|
}
|
2023-09-29 01:31:05 +00:00
|
|
|
})
|
2023-09-22 20:39:37 +00:00
|
|
|
|
|
|
|
// Route to show the current results from SQLite
|
2023-09-29 01:31:05 +00:00
|
|
|
app.get('/results', async (req, res) => {
|
2023-09-22 20:39:37 +00:00
|
|
|
try {
|
|
|
|
const results = {
|
|
|
|
count: {},
|
2023-09-29 01:31:05 +00:00
|
|
|
avgTimes: {}
|
|
|
|
}
|
2023-09-22 20:39:37 +00:00
|
|
|
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) {
|
2023-09-29 01:31:05 +00:00
|
|
|
reject(new Error('getCount: ' + err.message))
|
2023-09-22 20:39:37 +00:00
|
|
|
} else {
|
|
|
|
rows.forEach((row) => {
|
2023-09-29 01:31:05 +00:00
|
|
|
if (typeof results.count[row.animal_name] === 'undefined') {
|
|
|
|
results.count[row.animal_name] = {}
|
2023-09-22 20:39:37 +00:00
|
|
|
}
|
2023-09-29 01:31:05 +00:00
|
|
|
results.count[row.animal_name][row.button_name] = row.count
|
|
|
|
})
|
|
|
|
resolve()
|
2023-09-22 20:39:37 +00:00
|
|
|
}
|
2023-09-29 01:31:05 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2023-09-22 20:39:37 +00:00
|
|
|
const getAvgTime = new Promise((resolve, reject) => {
|
|
|
|
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) {
|
2023-09-29 01:31:05 +00:00
|
|
|
reject(new Error('getAvgTime: ' + err.message))
|
2023-09-22 20:39:37 +00:00
|
|
|
} else {
|
|
|
|
rows.forEach((row) => {
|
2023-09-29 01:31:05 +00:00
|
|
|
if (typeof results.avgTimes[row.animal_name] === 'undefined') {
|
|
|
|
results.avgTimes[row.animal_name] = {}
|
2023-09-22 20:39:37 +00:00
|
|
|
}
|
2023-09-29 01:31:05 +00:00
|
|
|
results.avgTimes[row.animal_name][row.button_name] = row.time
|
|
|
|
})
|
|
|
|
resolve()
|
2023-09-22 20:39:37 +00:00
|
|
|
}
|
2023-09-29 01:31:05 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2023-09-22 20:39:37 +00:00
|
|
|
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) {
|
2023-09-29 01:31:05 +00:00
|
|
|
reject(new Error('getTotalAvgTime: ' + err.message))
|
2023-09-22 20:39:37 +00:00
|
|
|
} else {
|
|
|
|
rows.forEach((row) => {
|
2023-09-29 01:31:05 +00:00
|
|
|
if (typeof results.avgTimes[row.animal_name] === 'undefined') {
|
|
|
|
results.avgTimes[row.animal_name] = {}
|
2023-09-22 20:39:37 +00:00
|
|
|
}
|
2023-09-29 01:31:05 +00:00
|
|
|
results.avgTimes[row.animal_name].total = row.time
|
|
|
|
})
|
|
|
|
resolve()
|
2023-09-22 20:39:37 +00:00
|
|
|
}
|
2023-09-29 01:31:05 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
await Promise.all([getCount, getTotalAvgTime, getAvgTime])
|
|
|
|
res.json(results)
|
2023-09-22 20:39:37 +00:00
|
|
|
} catch (error) {
|
2023-09-29 01:31:05 +00:00
|
|
|
log.error('Error fetching results:', error)
|
|
|
|
res.status(500).json({ error: 'Internal server error' })
|
2023-09-22 20:39:37 +00:00
|
|
|
}
|
2023-09-29 01:31:05 +00:00
|
|
|
})
|
2023-09-22 20:39:37 +00:00
|
|
|
|
2023-09-29 01:31:05 +00:00
|
|
|
module.exports = app
|
2023-09-22 20:39:37 +00:00
|
|
|
|
|
|
|
// vim:set sts=2 sw=2 et:
|