*: standard
js lint
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
380ec821b1
commit
bde4828074
6 changed files with 202 additions and 201 deletions
226
app.js
226
app.js
|
@ -1,23 +1,23 @@
|
|||
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');
|
||||
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')
|
||||
|
||||
const config = require('./config');
|
||||
const config = require('./config')
|
||||
|
||||
const log = bole('app');
|
||||
const app = express();
|
||||
const log = bole('app')
|
||||
const app = express()
|
||||
|
||||
// Create an SQLite database and initialize tables
|
||||
const db = new sqlite3.Database(config.db_path, (err) => {
|
||||
if (err) {
|
||||
log.error("Error opening SQLite database:", err.message);
|
||||
log.error('Error opening SQLite database:', err.message)
|
||||
} else {
|
||||
log.info("Connected to SQLite database", config.db_path);
|
||||
log.info('Connected to SQLite database', config.db_path)
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS button_clicks (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
@ -27,133 +27,133 @@ const db = new sqlite3.Database(config.db_path, (err) => {
|
|||
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
|
||||
)
|
||||
`);
|
||||
`)
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
var accessLogStream = fs.createWriteStream(
|
||||
path.join(__dirname, "log", "access.log"),
|
||||
{ flags: "a" },
|
||||
);
|
||||
const accessLogStream = fs.createWriteStream(
|
||||
path.join(__dirname, 'log', 'access.log'),
|
||||
{ flags: 'a' }
|
||||
)
|
||||
|
||||
app.use(bodyParser.json());
|
||||
app.use(morgan("combined", { stream: accessLogStream }));
|
||||
app.use(bodyParser.json())
|
||||
app.use(morgan('combined', { stream: accessLogStream }))
|
||||
app.use(session({
|
||||
resave: false,
|
||||
saveUninitialized: false,
|
||||
secret: config.session_token,
|
||||
}));
|
||||
secret: config.session_token
|
||||
}))
|
||||
|
||||
var animals;
|
||||
let animals
|
||||
// check and load animals into redis
|
||||
try {
|
||||
fs.readFile("./animals.json", function (err, data) {
|
||||
fs.readFile('./animals.json', function (err, data) {
|
||||
if (err) {
|
||||
throw err;
|
||||
throw err
|
||||
}
|
||||
var jsondata = JSON.parse(data);
|
||||
animals = jsondata.animals;
|
||||
const jsondata = JSON.parse(data)
|
||||
animals = jsondata.animals
|
||||
for (const animal of animals) {
|
||||
db.run(`
|
||||
INSERT INTO animals(name)
|
||||
SELECT ?
|
||||
WHERE NOT EXISTS(SELECT 1 FROM animals WHERE name = ?);
|
||||
`,
|
||||
[animal, animal],
|
||||
(err) => {
|
||||
if (err) {
|
||||
log.error(`Error inserting animal ${animal}: `, err.message);
|
||||
} else {
|
||||
log.info(`Success inserting animal ${animal}`);
|
||||
}
|
||||
},
|
||||
);
|
||||
[animal, animal],
|
||||
(err) => {
|
||||
if (err) {
|
||||
log.error(`Error inserting animal ${animal}: `, err.message)
|
||||
} else {
|
||||
log.info(`Success inserting animal ${animal}`)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
});
|
||||
})
|
||||
} catch (error) {
|
||||
log.error("Error loading animals:", error);
|
||||
animals = ["Dog", "Cat", "Elephant", "Lion", "Giraffe"];
|
||||
log.error('Error loading animals:', error)
|
||||
animals = ['Dog', 'Cat', 'Elephant', 'Lion', 'Giraffe']
|
||||
}
|
||||
|
||||
// Serve the HTML file
|
||||
app.get("/", (req, res) => {
|
||||
if (typeof req.session.cookie.expires == "undefined") {
|
||||
var hour = 3600000;
|
||||
req.session.cookie.expires = new Date(Date.now() + hour);
|
||||
req.session.cookie.maxAge = hour;
|
||||
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
|
||||
}
|
||||
|
||||
res.sendFile(__dirname + "/index.html");
|
||||
});
|
||||
app.get("/asset/frontend.js", (req, res) => {
|
||||
res.sendFile(__dirname + "/asset/frontend.js");
|
||||
});
|
||||
res.sendFile(path.join(__dirname, 'index.html'))
|
||||
})
|
||||
app.get('/asset/frontend.js', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, 'asset/frontend.js'))
|
||||
})
|
||||
|
||||
app.get("/newSession", (req, res) => {
|
||||
app.get('/newSession', (req, res) => {
|
||||
// XXX
|
||||
log.info(req.session);
|
||||
req.session.regenerate();
|
||||
log.info(req.session);
|
||||
});
|
||||
log.info(req.session)
|
||||
req.session.regenerate()
|
||||
log.info(req.session)
|
||||
})
|
||||
|
||||
// Route to get a random animal name
|
||||
app.get("/getNextAnimal", async (req, res) => {
|
||||
app.get('/getNextAnimal', async (req, res) => {
|
||||
try {
|
||||
// TODO this is currently random, and should have a bit of reasoning behind the next choice
|
||||
const randomIndex = Math.floor(Math.random() * animals.length);
|
||||
const randomAnimal = animals[randomIndex];
|
||||
res.json({ animalName: randomAnimal });
|
||||
const randomIndex = Math.floor(Math.random() * animals.length)
|
||||
const randomAnimal = animals[randomIndex]
|
||||
res.json({ animalName: randomAnimal })
|
||||
} catch (error) {
|
||||
log.error("Error fetching random animal:", error);
|
||||
res.status(500).json({ error: "Internal server error" });
|
||||
log.error('Error fetching random animal:', error)
|
||||
res.status(500).json({ error: 'Internal server error' })
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
// Route to record button clicks along with session IDs in SQLite
|
||||
app.post("/recordButtonClick", (req, res) => {
|
||||
app.post('/recordButtonClick', (req, res) => {
|
||||
try {
|
||||
//const { buttonName, sessionId } = req.body;
|
||||
const result = req.body;
|
||||
log.error(result);
|
||||
// const { buttonName, sessionId } = req.body;
|
||||
const result = req.body
|
||||
log.error(result)
|
||||
|
||||
db.run(
|
||||
"INSERT INTO button_clicks (session_id, animal_name, button_name, timestamp, time_difference) VALUES (?, ?, ?, ?, ?)",
|
||||
'INSERT INTO button_clicks (session_id, animal_name, button_name, timestamp, time_difference) VALUES (?, ?, ?, ?, ?)',
|
||||
[
|
||||
result.session,
|
||||
result.animal,
|
||||
result.button,
|
||||
result.time,
|
||||
result.difference,
|
||||
result.difference
|
||||
],
|
||||
(err) => {
|
||||
if (err) {
|
||||
log.error("Error recording button click:", err.message);
|
||||
res.status(500).json({ error: "Internal server error" });
|
||||
log.error('Error recording button click:', err.message)
|
||||
res.status(500).json({ error: 'Internal server error' })
|
||||
} else {
|
||||
res.sendStatus(200);
|
||||
res.sendStatus(200)
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
)
|
||||
} catch (error) {
|
||||
log.error("Error recording button click:", error);
|
||||
res.status(500).json({ error: "Internal server error" });
|
||||
log.error('Error recording button click:', error)
|
||||
res.status(500).json({ error: 'Internal server error' })
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
// Route to show the current results from SQLite
|
||||
app.get("/results", async (req, res) => {
|
||||
app.get('/results', async (req, res) => {
|
||||
try {
|
||||
const results = {
|
||||
count: {},
|
||||
avgTimes: {},
|
||||
};
|
||||
avgTimes: {}
|
||||
}
|
||||
const getCount = new Promise((resolve, reject) => {
|
||||
db.all(
|
||||
`
|
||||
|
@ -163,19 +163,19 @@ app.get("/results", async (req, res) => {
|
|||
`,
|
||||
(err, rows) => {
|
||||
if (err) {
|
||||
reject("getCount: " + err.message);
|
||||
reject(new Error('getCount: ' + err.message))
|
||||
} else {
|
||||
rows.forEach((row) => {
|
||||
if (typeof results.count[row.animal_name] == "undefined") {
|
||||
results.count[row.animal_name] = {};
|
||||
if (typeof results.count[row.animal_name] === 'undefined') {
|
||||
results.count[row.animal_name] = {}
|
||||
}
|
||||
results.count[row.animal_name][row.button_name] = row.count;
|
||||
});
|
||||
resolve();
|
||||
results.count[row.animal_name][row.button_name] = row.count
|
||||
})
|
||||
resolve()
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
)
|
||||
})
|
||||
const getAvgTime = new Promise((resolve, reject) => {
|
||||
db.all(
|
||||
`
|
||||
|
@ -185,19 +185,19 @@ app.get("/results", async (req, res) => {
|
|||
`,
|
||||
(err, rows) => {
|
||||
if (err) {
|
||||
reject("getAvgTime: " + err.message);
|
||||
reject(new Error('getAvgTime: ' + err.message))
|
||||
} else {
|
||||
rows.forEach((row) => {
|
||||
if (typeof results.avgTimes[row.animal_name] == "undefined") {
|
||||
results.avgTimes[row.animal_name] = {};
|
||||
if (typeof results.avgTimes[row.animal_name] === 'undefined') {
|
||||
results.avgTimes[row.animal_name] = {}
|
||||
}
|
||||
results.avgTimes[row.animal_name][row.button_name] = row.time;
|
||||
});
|
||||
resolve();
|
||||
results.avgTimes[row.animal_name][row.button_name] = row.time
|
||||
})
|
||||
resolve()
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
)
|
||||
})
|
||||
const getTotalAvgTime = new Promise((resolve, reject) => {
|
||||
db.all(
|
||||
`
|
||||
|
@ -207,27 +207,27 @@ app.get("/results", async (req, res) => {
|
|||
`,
|
||||
(err, rows) => {
|
||||
if (err) {
|
||||
reject("getTotalAvgTime: " + err.message);
|
||||
reject(new Error('getTotalAvgTime: ' + err.message))
|
||||
} else {
|
||||
rows.forEach((row) => {
|
||||
if (typeof results.avgTimes[row.animal_name] == "undefined") {
|
||||
results.avgTimes[row.animal_name] = {};
|
||||
if (typeof results.avgTimes[row.animal_name] === 'undefined') {
|
||||
results.avgTimes[row.animal_name] = {}
|
||||
}
|
||||
results.avgTimes[row.animal_name]["total"] = row.time;
|
||||
});
|
||||
resolve();
|
||||
results.avgTimes[row.animal_name].total = row.time
|
||||
})
|
||||
resolve()
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
await Promise.all([getCount, getTotalAvgTime, getAvgTime]);
|
||||
res.json(results);
|
||||
}
|
||||
)
|
||||
})
|
||||
await Promise.all([getCount, getTotalAvgTime, getAvgTime])
|
||||
res.json(results)
|
||||
} catch (error) {
|
||||
log.error("Error fetching results:", error);
|
||||
res.status(500).json({ error: "Internal server error" });
|
||||
log.error('Error fetching results:', error)
|
||||
res.status(500).json({ error: 'Internal server error' })
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
module.exports = app;
|
||||
module.exports = app
|
||||
|
||||
// vim:set sts=2 sw=2 et:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue