From 8a66068b98657cb512e1dabeef6885fde8d2be07 Mon Sep 17 00:00:00 2001 From: BIG2EYEZ Date: Tue, 9 Jan 2024 17:34:16 +0800 Subject: [PATCH] update with dashboard data --- Sean/modules/mysql.js | 11 ++- Sean/server.js | 54 +++++++++------ Sean/views/home.ejs | 154 +++++++++++++++++++++++++++++++++++++++--- Sean/views/home.js | 107 +++++++++++++++++++++++++++++ 4 files changed, 295 insertions(+), 31 deletions(-) create mode 100644 Sean/views/home.js diff --git a/Sean/modules/mysql.js b/Sean/modules/mysql.js index 7dbb840..4ae5252 100644 --- a/Sean/modules/mysql.js +++ b/Sean/modules/mysql.js @@ -40,7 +40,14 @@ const connection = mysql.createConnection({ timezone: "Z", // Set the timezone to UTC }); - -module.exports = { connection }; + const connection2 = mysql.createConnection({ + host: process.env.host, + user: process.env.DB_USER, + password: process.env.DB_PASS, + database: "eco_saver", + timezone: "Z", // Set the timezone to UTC + }); +module.exports = { connection,connection2 }; + diff --git a/Sean/server.js b/Sean/server.js index cb5bc0b..f3c3222 100644 --- a/Sean/server.js +++ b/Sean/server.js @@ -11,7 +11,7 @@ const validator = require('validator'); const { transporter } = require("./modules/nodeMailer"); const { connection } = require("./modules/mysql"); - +const { connection2 } = require("./modules/mysql"); const app = express(); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); @@ -207,28 +207,40 @@ const logActivity = async (username, success, message) => { -// Update your /home route to retrieve the overall last 10 logins for all users -app.get("/home", isAuthenticated, (req, res) => { - // Retrieve the overall last 10 logins for all users - const loginsQuery = - "SELECT username, lastLogin FROM users ORDER BY lastLogin DESC LIMIT 10"; - connection.query(loginsQuery, (error, loginResults) => { - if (error) { - console.error("Error executing login logs query:", error); - res.status(500).send("Internal Server Error"); - return; - } - - // Log the results on the server side - console.log("Login Logs on Server:", loginResults); - - // Render the home page with login logs data - res.render("home", { - username: req.session.username, - loginLogs: loginResults, - }); + app.get("/home", isAuthenticated, (req, res) => { + + // Retrieve the last 10 sensor data records from the sensordata table + const sensorDataQuery = + "SELECT locationid, measurement, createdAt FROM sensordata ORDER BY createdAt DESC LIMIT 10"; + + connection2.query(sensorDataQuery, (error, sensorDataResults) => { + if (error) { + console.error("Error executing sensor data query:", error); + res.status(500).send("Internal Server Error"); + return; + } + + // Log the results on the server side + + + // Render the home page with sensor data + res.render("home", { + username: req.session.username, + sensorData: sensorDataResults, + }); }); + }); + app.get('/api/locations', (req, res) => { + connection2.query('SELECT `id`, `name` FROM `locations`', (error, results) => { + if (error) { + console.error('Error fetching locations:', error); + res.status(500).json({ error: 'Internal Server Error' }); + return; + } + res.json(results); + }); }); + app.get("/inusers", isAuthenticated, (req, res) => { // Fetch all user data from the database const allUsersQuery = "SELECT * FROM users"; diff --git a/Sean/views/home.ejs b/Sean/views/home.ejs index a2e2d81..50f852b 100644 --- a/Sean/views/home.ejs +++ b/Sean/views/home.ejs @@ -8,7 +8,15 @@ Home +