express: time and session ID
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
c811d35fa5
commit
44167de812
6 changed files with 1196 additions and 165 deletions
|
@ -1,71 +1,82 @@
|
|||
const express = require('express');
|
||||
const fs = require('fs');
|
||||
const bodyParser = require('body-parser');
|
||||
const Redis = require('ioredis');
|
||||
const sqlite3 = require('sqlite3').verbose();
|
||||
const app = express();
|
||||
const port = 3000;
|
||||
|
||||
// Create a Redis Client
|
||||
const redis = new Redis();
|
||||
// Create an SQLite database and initialize tables
|
||||
const db = new sqlite3.Database('mydb.db', (err) => {
|
||||
if (err) {
|
||||
console.error('Error opening SQLite database:', err.message);
|
||||
} else {
|
||||
console.log('Connected to SQLite database');
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS button_clicks (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
session_id TEXT,
|
||||
button_name TEXT,
|
||||
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
time_difference INTEGER -- Add this column for time difference
|
||||
)
|
||||
|
||||
`);
|
||||
}
|
||||
});
|
||||
|
||||
app.use(bodyParser.json());
|
||||
|
||||
var animals;
|
||||
// check and load animals into redis
|
||||
fs.readFile("./animals.json", function (err, data) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
var jsondata = JSON.parse(data);
|
||||
animals = jsondata.animals;
|
||||
//await redis.set('animals', jsondata.animals, 'EX', 3000);
|
||||
});
|
||||
|
||||
|
||||
// Serve the HTML file
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(__dirname + '/index.html');
|
||||
});
|
||||
|
||||
// Route to get a random animal name
|
||||
app.get('/getRandomAnimal', (req, res) => {
|
||||
const randomAnimal = animals[Math.floor(Math.random() * animals.length)];
|
||||
res.json({ animalName: randomAnimal });
|
||||
});
|
||||
|
||||
// Route to record button clicks
|
||||
app.post('/recordButtonClick', (req, res) => {
|
||||
try {
|
||||
const { buttonName, sessionId } = req.body;
|
||||
|
||||
// Use a Redis hash to store button clicks associated with session IDs
|
||||
await redis.hincrby('buttonClicks', `${sessionId}:${buttonName}`, 1);
|
||||
|
||||
res.sendStatus(200);
|
||||
|
||||
if (buttonClicks.hasOwnProperty(buttonName)) {
|
||||
buttonClicks[buttonName]++;
|
||||
res.sendStatus(200);
|
||||
} else {
|
||||
res.status(400).json({ error: 'Invalid button name' });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error recording button click:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// Route to show the current results
|
||||
app.get('/results', async (req, res) => {
|
||||
app.get('/getRandomAnimal', async (req, res) => {
|
||||
try {
|
||||
// Get the current random animal from Redis
|
||||
const randomAnimal = await redis.get('randomAnimal');
|
||||
const animals = ['Dog', 'Cat', 'Elephant', 'Lion', 'Giraffe'];
|
||||
const randomIndex = Math.floor(Math.random() * animals.length);
|
||||
const randomAnimal = animals[randomIndex];
|
||||
res.json({ animalName: randomAnimal });
|
||||
} catch (error) {
|
||||
console.error('Error fetching random animal:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Get the button click counts from the Redis hash
|
||||
const buttonClicks = await redis.hgetall('buttonClicks');
|
||||
// Route to record button clicks along with session IDs in SQLite
|
||||
app.post('/recordButtonClick', (req, res) => {
|
||||
try {
|
||||
const { buttonName, sessionId } = req.body;
|
||||
|
||||
res.json({ randomAnimal, buttonClicks });
|
||||
db.run('INSERT INTO button_clicks (session_id, button_name) VALUES (?, ?)', [sessionId, buttonName], (err) => {
|
||||
if (err) {
|
||||
console.error('Error recording button click:', err.message);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
} else {
|
||||
res.sendStatus(200);
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.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', (req, res) => {
|
||||
try {
|
||||
db.all('SELECT button_name, COUNT(*) as count FROM button_clicks GROUP BY button_name', (err, rows) => {
|
||||
if (err) {
|
||||
console.error('Error fetching results:', err.message);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
} else {
|
||||
const results = {};
|
||||
rows.forEach((row) => {
|
||||
results[row.button_name] = row.count;
|
||||
});
|
||||
res.json(results);
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching results:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue