From fcbda0bf9e8200074f7aae80700d3c4f3cee07e2 Mon Sep 17 00:00:00 2001 From: Donny Date: Fri, 30 Aug 2024 10:37:18 -0600 Subject: [PATCH 1/2] Trying to add time data --- app.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/app.js b/app.js index 21abde2..ea956ba 100644 --- a/app.js +++ b/app.js @@ -393,6 +393,56 @@ const addProgram = async (programName, machineName) => { } }; +async function recordTimeSeriesData() { + // Get the current timestamp and timestamp for 5 minutes ago + const now = new Date(); + const localPollrate = new Date(now.getTime() - pollrate); + + // Format dates for MySQL + const nowFormatted = now.toISOString().slice(0, 19).replace('T', ' '); + const fiveMinutesAgoFormatted = localPollrate.toISOString().slice(0, 19).replace('T', ' '); + + // Query to count records within the last 5 minutes + const countQuery = ` + SELECT program, COUNT(*) as count + FROM usedLicences + WHERE date >= ? + GROUP BY program + `; + + // Query to insert data into time_data + const insertQuery = ` + INSERT INTO time_data (name, count, date) + VALUES (?, ?, ?) + `; + + connection.connect(); + + connection.query(countQuery, [fiveMinutesAgoFormatted], (err, results) => { + if (err) { + console.error('Error querying count:', err); + connection.end(); + return; + } + + // Insert aggregated data into time_data + results.forEach(row => { + connection.query(insertQuery, [row.program, row.count, nowFormatted], (err) => { + if (err) { + console.error('Error inserting data:', err); + } + }); + }); + + console.log('Time series data recorded successfully.'); + connection.end(); + }); +} + +setTimeout(() => { + recordTimeSeriesData(); +}, pollrate); + setTimeout(() => { testDatabaseConnection(); }, 1000); From b3858567c615da9f30ecc4e64579492ae012be1d Mon Sep 17 00:00:00 2001 From: Donny Date: Fri, 30 Aug 2024 10:50:20 -0600 Subject: [PATCH 2/2] Updated app.js --- app.js | 50 -------------------------------------------------- 1 file changed, 50 deletions(-) diff --git a/app.js b/app.js index ea956ba..21abde2 100644 --- a/app.js +++ b/app.js @@ -393,56 +393,6 @@ const addProgram = async (programName, machineName) => { } }; -async function recordTimeSeriesData() { - // Get the current timestamp and timestamp for 5 minutes ago - const now = new Date(); - const localPollrate = new Date(now.getTime() - pollrate); - - // Format dates for MySQL - const nowFormatted = now.toISOString().slice(0, 19).replace('T', ' '); - const fiveMinutesAgoFormatted = localPollrate.toISOString().slice(0, 19).replace('T', ' '); - - // Query to count records within the last 5 minutes - const countQuery = ` - SELECT program, COUNT(*) as count - FROM usedLicences - WHERE date >= ? - GROUP BY program - `; - - // Query to insert data into time_data - const insertQuery = ` - INSERT INTO time_data (name, count, date) - VALUES (?, ?, ?) - `; - - connection.connect(); - - connection.query(countQuery, [fiveMinutesAgoFormatted], (err, results) => { - if (err) { - console.error('Error querying count:', err); - connection.end(); - return; - } - - // Insert aggregated data into time_data - results.forEach(row => { - connection.query(insertQuery, [row.program, row.count, nowFormatted], (err) => { - if (err) { - console.error('Error inserting data:', err); - } - }); - }); - - console.log('Time series data recorded successfully.'); - connection.end(); - }); -} - -setTimeout(() => { - recordTimeSeriesData(); -}, pollrate); - setTimeout(() => { testDatabaseConnection(); }, 1000);