From b5e2820bc7edfcb24603169cc164b82dfcf8c3e9 Mon Sep 17 00:00:00 2001 From: newtbot Date: Thu, 11 Jan 2024 04:14:20 +0800 Subject: [PATCH] WIP --- Documentation/mqtt_broker_setup.txt | 89 +++- Vivian/functions/validateData.js | 33 ++ Web-Server/functions/APIDatabase.js | 736 +++------------------------- Web-Server/routes/SensorData.js | 78 +-- api.MD | 2 - test.js | 120 +++++ 6 files changed, 334 insertions(+), 724 deletions(-) create mode 100644 Vivian/functions/validateData.js create mode 100644 test.js diff --git a/Documentation/mqtt_broker_setup.txt b/Documentation/mqtt_broker_setup.txt index 9508179..f72881d 100644 --- a/Documentation/mqtt_broker_setup.txt +++ b/Documentation/mqtt_broker_setup.txt @@ -23,4 +23,91 @@ log_type all log_facility 5 log_dest file /var/log/mosquitto/mosquitto.log -https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-the-mosquitto-mqtt-messaging-broker-on-ubuntu-18-04 \ No newline at end of file +https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-the-mosquitto-mqtt-messaging-broker-on-ubuntu-18-04 + + +let ormQuery = {}; +let whereClause = {}; +//let whereNest = {}; +//let whereDate = {}; + +buildFuncs = { + month: function(query){ + const validMonths = [ + "1", + "2", + "3", + "4", + "5", + "6", + "7", + "8", + "9", + "10", + "11", + "12", + ]; + if (validMonths.includes(query.month)) { + whereClause = { + where: sequelize.where( + sequelize.fn("MONTH", sequelize.col("createdAt")), + query.month + ), + }; + } else { + query.month = getMonthFromString(query.month); + whereClause = { + where: sequelize.where( + sequelize.fn("MONTH", sequelize.col("createdAt")), + query.month + ), + }; + } + }, + week: function(query){ + whereClause.week = sequelize.where( + sequelize.fn("WEEK", sequelize.col("createdAt")), + query.week + ); + }, + sensorid: function (query){ + whereClause.sensorid = sequelize.where( + sequelize.col("sensorid"), + query.sensorid + ); + } +} + +function getData(queryString) + for(let query in queryString){ + if(buildFuncs[query]); buildFuncs[query](queryString) + + if (!whereClause) { + return await sensorDataModel.findAll(ormQuery); + } else if (whereClause) { + console.log(whereClause); + //console.log(whereNest); + //console.log(whereDate); + console.log(query); + console.log(ormQuery); + + return await sensorDataModel.findAll({ + //limit default value if query.limit is undefined or not provided + limit: query.limit, + //The operators Op.and, Op.or and Op.not can be used to create arbitrarily complex nested logical comparisons. + //https://sequelize.org/docs/v6/core-concepts/model-querying-basics/#examples-with-opand-and-opor + where: { + //[Op.and]: [whereNest, whereClause], + [Op.and]: [whereClause], + createdAt: { + //https://stackoverflow.com/questions/43115151/sequelize-query-to-find-all-records-that-falls-in-between-date-range + [Op.between]: [whereClause.startdate, whereClause.enddate], + }, + }, + + //only use where clause to lookup based on condition that i put into whereClause + ...ormQuery, + }); + } + } +} \ No newline at end of file diff --git a/Vivian/functions/validateData.js b/Vivian/functions/validateData.js new file mode 100644 index 0000000..1fb5b0f --- /dev/null +++ b/Vivian/functions/validateData.js @@ -0,0 +1,33 @@ +var validator = require("validator"); + +function isAlphaNumericwithSpaces(value) { + return validator.isAlphanumeric(value, ["en-US"], { ignore: " " }); +} + +//allow alphanumeric and spaces and - +function isAlphaNumericWithSpacesAndDash(value) { + const alphanumeric = /^[a-zA-Z0-9]+$/; + const valid = value + .split("") + .every((char) => alphanumeric.test(char) || char === " " || char === "-"); + return valid; +} + +function isJson(value) { + //check if its object + if(typeof value === "object"){ + console.log("its an object") + return true + } + +} + +module.exports = { + validateData, + isValidDateString, + isAlphaNumericwithSpaces, + isAlphaNumericWithSpacesAndDash, + isMacAddress, + isJson, +}; + diff --git a/Web-Server/functions/APIDatabase.js b/Web-Server/functions/APIDatabase.js index 5a8a3d8..9ff4d68 100644 --- a/Web-Server/functions/APIDatabase.js +++ b/Web-Server/functions/APIDatabase.js @@ -2,7 +2,7 @@ const { sequelize } = require("../../Database/mySql.js"); const { locationModel } = require("../../Database/model/locationModel.js"); const { sensorModel } = require("../../Database/model/sensorModel.js"); const { sensorDataModel } = require("../../Database/model/sensorDataModel.js"); -const { Op, where, or } = require("sequelize"); +const { Op } = require("sequelize"); //helper function to convert month name to month number //https://stackoverflow.com/questions/13566552/easiest-way-to-convert-month-name-to-month-number-in-js-jan-01 @@ -170,712 +170,133 @@ async function getSensorDataById(id) { }); return sensorData; } - -async function getData(query) { - let ormQuery = {}; - let whereClause = {}; - //let whereNest = {}; - //let whereDate = {}; - //limit default value if query.limit is undefined - if (query.limit === undefined) { - query.limit = 10; - } - if (query.limit !== undefined && query.order !== undefined) - ormQuery = { - limit: parseInt(query.limit), - //console.log(sentence.toUpperCase()); - order: [["createdAt", query.order.toUpperCase()]], - ...ormQuery, - }; - } - - /* - //handle year and month and week and day and hour and minute and sensorid and locationid through optional chaining - else if ( - query.limit || - query.order || - query.year || - query.month || - query.week || - query.day || - query.hour || - query.minute || - query.sensorid || - query.locationid || - query.startdate || - query.enddate - ) { - if (query.limit !== undefined && query.order !== undefined) +var ormQuery = {}; +var whereClause = {}; +const validMonths = [ + "1", + "2", + "3", + "4", + "5", + "6", + "7", + "8", + "9", + "10", + "11", + "12", +]; +//handle buildfunc for query +buildQuery = { + limit: async function (queryString) { + if (queryString.limit !== undefined) { + ormQuery.limit = parseInt(queryString.limit); + } + }, + order: async function (queryString) { + if (queryString.order !== undefined) { ormQuery = { - limit: parseInt(query.limit), - order: [["createdAt", query.order.toUpperCase()]], ...ormQuery, + order: [["createdAt", queryString.order.toUpperCase()]], }; - - if (query.year) { + } + }, + year: async function (queryString) { + if (queryString.year !== undefined) { + //whereclause assign a value whereClause.year = sequelize.where( sequelize.fn("YEAR", sequelize.col("createdAt")), - query.year + queryString.year ); } - - if (query.month) { - const validMonths = [ - "1", - "2", - "3", - "4", - "5", - "6", - "7", - "8", - "9", - "10", - "11", - "12", - ]; - if (validMonths.includes(query.month)) { - whereClause = { - where: sequelize.where( - sequelize.fn("MONTH", sequelize.col("createdAt")), - query.month - ), - }; + }, + month: async function (queryString) { + console.log("queryString month:", queryString.month, queryString); + if (queryString.month !== undefined) { + console.log("queryString month:", "i should not be here"); + if (validMonths.includes(queryString.month)) { + whereClause.month = sequelize.where( + sequelize.fn("MONTH", sequelize.col("createdAt")), + queryString.month + ); } else { - query.month = getMonthFromString(query.month); - whereClause = { - where: sequelize.where( - sequelize.fn("MONTH", sequelize.col("createdAt")), - query.month - ), - }; + queryString.month = getMonthFromString(queryString.month); + whereClause.month = sequelize.where( + sequelize.fn("MONTH", sequelize.col("createdAt")), + queryString.month + ); } } - if (query.week) { + }, + week: async function (queryString) { + if (queryString.week !== undefined) { whereClause.week = sequelize.where( sequelize.fn("WEEK", sequelize.col("createdAt")), - query.week + queryString.week ); } - if (query.day) { + }, + day: async function (queryString) { + if (queryString.day !== undefined) { whereClause.day = sequelize.where( sequelize.fn("DAY", sequelize.col("createdAt")), - query.day + queryString.day ); } - if (query.hour) { + }, + hour: async function (queryString) { + if (queryString.hour !== undefined) { whereClause.hour = sequelize.where( sequelize.fn("HOUR", sequelize.col("createdAt")), - query.hour + queryString.hour ); } - if (query.minute) { + }, + minute: async function (queryString) { + if (queryString.minute !== undefined) { whereClause.minute = sequelize.where( sequelize.fn("MINUTE", sequelize.col("createdAt")), - query.minute + queryString.minute ); } - if (query.startdate) { - let startdate = new Date(query.startdate); - whereClause.startdate = startdate; - } - if (query.enddate) { - let enddate = new Date(query.enddate); - whereClause.enddate = enddate; - } + }, +}; - //WHERE NEST PREVIOUSLY - if (query.sensorid) { - whereClause.sensorid = sequelize.where( - sequelize.col("sensorid"), - query.sensorid - ); - } - if (query.locationid) { - whereClause.locationid = sequelize.where( - sequelize.col("locationid"), - query.locationid - ); - } - //highest and lowest - if (query.psi !== undefined && query.psi === "highest") { - ormQuery = { - attributes: [ - "id", - "sensorid", - "locationid", - [sequelize.literal("JSON_EXTRACT(measurement, '$.psi')"), "max_psi"], - "createdAt", - ], - //group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("max_psi"), "DESC"]], - limit: parseInt(query.limit), - }; - } - if (query.psi !== undefined && query.psi === "lowest") { - //https://gist.github.com/WunGCQ/c4df1929c5975c2a79133bc8300fcd6e +async function getData(queryString) { + // reset keys... + ormQuery = {}; + whereClause = {}; + for (let query in queryString) { + console.log(queryString); + console.log(query); + console.log(whereClause); + //console.log(ormQuery); - ormQuery = { - attributes: [ - [sequelize.literal("JSON_EXTRACT(measurement, '$.psi')"), "min_psi"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("min_psi"), "ASC"]], - limit: parseInt(query.limit), - }; + if (buildQuery[query]) { + await buildQuery[query](queryString); } - if (query.co !== undefined && query.co === "highest") { - ormQuery = { - attributes: [ - [sequelize.literal("JSON_EXTRACT(measurement, '$.co')"), "max_co"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("max_co"), "DESC"]], - limit: parseInt(query.limit), - }; - } - if (query.co !== undefined && query.co === "lowest") { - ormQuery = { - attributes: [ - [sequelize.literal("JSON_EXTRACT(measurement, '$.co')"), "min_co"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("min_co"), "ASC"]], - limit: parseInt(query.limit), - }; - } - if (query.o3 !== undefined && query.o3 === "highest") { - ormQuery = { - attributes: [ - [sequelize.literal("JSON_EXTRACT(measurement, '$.o3')"), "max_o3"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("max_o3"), "DESC"]], - limit: parseInt(query.limit), - }; - } - if (query.o3 !== undefined && query.o3 === "lowest") { - ormQuery = { - attributes: [ - [sequelize.literal("JSON_EXTRACT(measurement, '$.o3')"), "min_o3"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("min_o3"), "ASC"]], - limit: parseInt(query.limit), - }; - } - if (query.no2 !== undefined && query.no2 === "highest") { - ormQuery = { - attributes: [ - [sequelize.literal("JSON_EXTRACT(measurement, '$.no2')"), "max_no2"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("max_no2"), "DESC"]], - limit: parseInt(query.limit), - }; - } - if (query.no2 !== undefined && query.no2 === "lowest") { - ormQuery = { - attributes: [ - [sequelize.literal("JSON_EXTRACT(measurement, '$.no2')"), "min_no2"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("min_no2"), "ASC"]], - limit: parseInt(query.limit), - }; - } - if (query.so2 !== undefined && query.so2 === "highest") { - ormQuery = { - attributes: [ - [sequelize.literal("JSON_EXTRACT(measurement, '$.so2')"), "max_so2"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("max_so2"), "DESC"]], - limit: parseInt(query.limit), - }; - } - if (query.so2 !== undefined && query.so2 === "lowest") { - ormQuery = { - attributes: [ - [sequelize.literal("JSON_EXTRACT(measurement, '$.so2')"), "min_so2"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("min_so2"), "ASC"]], - limit: parseInt(query.limit), - }; - } - if (query.humidity !== undefined && query.humidity === "highest") { - ormQuery = { - attributes: [ - [ - sequelize.literal("JSON_EXTRACT(measurement, '$.humidity')"), - "max_humidity", - ], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("max_humidity"), "DESC"]], - limit: parseInt(query.limit), - }; - } - if (query.humidity !== undefined && query.humidity === "lowest") { - ormQuery = { - attributes: [ - [ - sequelize.literal("JSON_EXTRACT(measurement, '$.humidity')"), - "min_humidity", - ], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("min_humidity"), "ASC"]], - limit: parseInt(query.limit), - }; - } - if (query.windspeed !== undefined && query.windspeed === "highest") { - ormQuery = { - attributes: [ - [ - sequelize.literal("JSON_EXTRACT(measurement, '$.windspeed')"), - "max_windspeed", - ], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("max_windspeed"), "DESC"]], - limit: parseInt(query.limit), - }; - } - if (query.windspeed !== undefined && query.windspeed === "lowest") { - ormQuery = { - attributes: [ - [ - sequelize.literal("JSON_EXTRACT(measurement, '$.windspeed')"), - "min_windspeed", - ], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("min_windspeed"), "ASC"]], - limit: parseInt(query.limit), - }; - } - if (query.temperature !== undefined && query.temperature === "highest") { - ormQuery = { - attributes: [ - [ - sequelize.literal("JSON_EXTRACT(measurement, '$.temperature')"), - "max_temperature", - ], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("max_temperature"), "DESC"]], - limit: parseInt(query.limit), - }; - } - if (query.temperature !== undefined && query.temperature === "lowest") { - ormQuery = { - attributes: [ - [ - sequelize.literal("JSON_EXTRACT(measurement, '$.temperature')"), - "min_temperature", - ], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("min_temperature"), "ASC"]], - limit: parseInt(query.limit), - }; - } - } - //accept year - else if (query.year !== undefined) { - ormQuery = { - //handle all year, month - where: sequelize.where( - sequelize.fn("YEAR", sequelize.col("createdAt")), - query.year - ), - }; - } else if (query.month !== undefined) { - console.log(typeof query.month); - const validMonths = [ - "1", - "2", - "3", - "4", - "5", - "6", - "7", - "8", - "9", - "10", - "11", - "12", - ]; - if (validMonths.includes(query.month)) { - ormQuery = { - where: sequelize.where( - sequelize.fn("MONTH", sequelize.col("createdAt")), - query.month - ), - ...ormQuery, - }; - } else { - query.month = getMonthFromString(query.month); - ormQuery = { - where: sequelize.where( - sequelize.fn("MONTH", sequelize.col("createdAt")), - query.month - ), - ...ormQuery, - }; - } - //weekly - } else if (query.week !== undefined) { - ormQuery = { - where: sequelize.where( - sequelize.fn("WEEK", sequelize.col("createdAt")), - query.week - ), - ...ormQuery, - }; - } - - //daily data - else if (query.day !== undefined) { - ormQuery = { - where: sequelize.where( - sequelize.fn("DAY", sequelize.col("createdAt")), - query.day - ), - ...ormQuery, - }; - } - - //hourly data - else if (query.hour !== undefined) { - ormQuery = { - where: sequelize.where( - sequelize.fn("HOUR", sequelize.col("createdAt")), - query.hour - ), - ...ormQuery, - }; - } - - //minute data - else if (query.minute !== undefined) { - ormQuery = { - where: sequelize.where( - sequelize.fn("MINUTE", sequelize.col("createdAt")), - query.minute - ), - ...ormQuery, - }; - } else if (query.sensorid !== undefined) { - ormQuery = { - where: { - sensorid: query.sensorid, - }, - ...ormQuery, - }; - } else if (query.locationid !== undefined) { - ormQuery = { - where: { - locationid: query.locationid, - }, - ...ormQuery, - }; } if (!whereClause) { return await sensorDataModel.findAll(ormQuery); } else if (whereClause) { - console.log(whereClause); - //console.log(whereNest); - //console.log(whereDate); - console.log(query); - console.log(ormQuery); - return await sensorDataModel.findAll({ - //limit default value if query.limit is undefined or not provided - limit: query.limit, + limit: queryString.limit || 1000000, //The operators Op.and, Op.or and Op.not can be used to create arbitrarily complex nested logical comparisons. //https://sequelize.org/docs/v6/core-concepts/model-querying-basics/#examples-with-opand-and-opor where: { - //[Op.and]: [whereNest, whereClause], [Op.and]: [whereClause], - createdAt: { - //https://stackoverflow.com/questions/43115151/sequelize-query-to-find-all-records-that-falls-in-between-date-range - [Op.between]: [whereClause.startdate, whereClause.enddate], - }, }, - //only use where clause to lookup based on condition that i put into whereClause ...ormQuery, }); } } -*/ -//if no query is provided -/* -async function getdataFilter(query) { - let allowWords = ["highest", "lowest", "Highest", "Lowest"]; - let ormQuery = {}; - //preset limit if limit is not provided - if (query.limit === undefined) { - query.limit = 10; - } - if ( - allowWords.includes(query.psi) || - allowWords.includes(query.co) || - allowWords.includes(query.o3) || - allowWords.includes(query.no2) || - allowWords.includes(query.so2) || - allowWords.includes(query.humidity) || - allowWords.includes(query.windspeed) || - allowWords.includes(query.temperature) - ) { - //get highest and lowest data of measurement from all data - if (query.psi !== undefined && query.psi === "highest") { - ormQuery = { - attributes: [ - "id", - "sensorid", - "locationid", - [sequelize.literal("JSON_EXTRACT(measurement, '$.psi')"), "max_psi"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("max_psi"), "DESC"]], - limit: parseInt(query.limit), - }; - } - if (query.psi !== undefined && query.psi === "lowest") { - //https://gist.github.com/WunGCQ/c4df1929c5975c2a79133bc8300fcd6e - - ormQuery = { - attributes: [ - "id", - "sensorid", - "locationid", - [sequelize.literal("JSON_EXTRACT(measurement, '$.psi')"), "min_psi"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("min_psi"), "ASC"]], - limit: parseInt(query.limit), - }; - } - if (query.co !== undefined && query.co === "highest") { - ormQuery = { - attributes: [ - "id", - "sensorid", - "locationid", - [sequelize.literal("JSON_EXTRACT(measurement, '$.co')"), "max_co"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("max_co"), "DESC"]], - limit: parseInt(query.limit), - }; - } - if (query.co !== undefined && query.co === "lowest") { - ormQuery = { - attributes: [ - "id", - "sensorid", - "locationid", - [sequelize.literal("JSON_EXTRACT(measurement, '$.co')"), "min_co"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("min_co"), "ASC"]], - limit: parseInt(query.limit), - }; - } - if (query.o3 !== undefined && query.o3 === "highest") { - ormQuery = { - attributes: [ - "id", - "sensorid", - "locationid", - [sequelize.literal("JSON_EXTRACT(measurement, '$.o3')"), "max_o3"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("max_o3"), "DESC"]], - limit: parseInt(query.limit), - }; - } - if (query.o3 !== undefined && query.o3 === "lowest") { - ormQuery = { - attributes: [ - "id", - "sensorid", - "locationid", - [sequelize.literal("JSON_EXTRACT(measurement, '$.o3')"), "min_o3"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("min_o3"), "ASC"]], - limit: parseInt(query.limit), - }; - } - if (query.no2 !== undefined && query.no2 === "highest") { - ormQuery = { - attributes: [ - "id", - "sensorid", - "locationid", - [sequelize.literal("JSON_EXTRACT(measurement, '$.no2')"), "max_no2"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("max_no2"), "DESC"]], - limit: parseInt(query.limit), - }; - } - if (query.no2 !== undefined && query.no2 === "lowest") { - ormQuery = { - attributes: [ - "id", - "sensorid", - "locationid", - [sequelize.literal("JSON_EXTRACT(measurement, '$.no2')"), "min_no2"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("min_no2"), "ASC"]], - limit: parseInt(query.limit), - }; - } - if (query.so2 !== undefined && query.so2 === "highest") { - ormQuery = { - attributes: [ - "id", - "sensorid", - "locationid", - [sequelize.literal("JSON_EXTRACT(measurement, '$.so2')"), "max_so2"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("max_so2"), "DESC"]], - limit: parseInt(query.limit), - }; - } - if (query.so2 !== undefined && query.so2 === "lowest") { - ormQuery = { - attributes: [ - "id", - "sensorid", - "locationid", - [sequelize.literal("JSON_EXTRACT(measurement, '$.so2')"), "min_so2"], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("min_so2"), "ASC"]], - limit: parseInt(query.limit), - }; - } - if (query.humidity !== undefined && query.humidity === "highest") { - ormQuery = { - attributes: [ - "id", - "sensorid", - "locationid", - [ - sequelize.literal("JSON_EXTRACT(measurement, '$.humidity')"), - "max_humidity", - ], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("max_humidity"), "DESC"]], - limit: parseInt(query.limit), - }; - } - if (query.humidity !== undefined && query.humidity === "lowest") { - ormQuery = { - attributes: [ - "id", - "sensorid", - "locationid", - [ - sequelize.literal("JSON_EXTRACT(measurement, '$.humidity')"), - "min_humidity", - ], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("min_humidity"), "ASC"]], - limit: parseInt(query.limit), - }; - } - if (query.windspeed !== undefined && query.windspeed === "highest") { - ormQuery = { - attributes: [ - "id", - "sensorid", - "locationid", - [ - sequelize.literal("JSON_EXTRACT(measurement, '$.windspeed')"), - "max_windspeed", - ], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("max_windspeed"), "DESC"]], - limit: parseInt(query.limit), - }; - } - if (query.windspeed !== undefined && query.windspeed === "lowest") { - ormQuery = { - attributes: [ - "id", - "sensorid", - "locationid", - [ - sequelize.literal("JSON_EXTRACT(measurement, '$.windspeed')"), - "min_windspeed", - ], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("min_windspeed"), "ASC"]], - limit: parseInt(query.limit), - }; - } - if (query.temperature !== undefined && query.temperature === "highest") { - ormQuery = { - attributes: [ - "id", - "sensorid", - "locationid", - [ - sequelize.literal("JSON_EXTRACT(measurement, '$.temperature')"), - "max_temperature", - ], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("max_temperature"), "DESC"]], - limit: parseInt(query.limit), - }; - } - if (query.temperature !== undefined && query.temperature === "lowest") { - ormQuery = { - attributes: [ - "id", - "sensorid", - "locationid", - [ - sequelize.literal("JSON_EXTRACT(measurement, '$.temperature')"), - "min_temperature", - ], - ], - group: ["id", "sensorid", "locationid"], - order: [[sequelize.literal("min_temperature"), "ASC"]], - limit: parseInt(query.limit), - }; - } - - console.log(ormQuery); - console.log(query); - return await sensorDataModel.findAll(ormQuery); - } else { - return "Please enter correct query or Please provide a query"; - } -} -*/ async function getAverage(query) {} /* - */ module.exports = { @@ -895,6 +316,5 @@ module.exports = { deleteSensorData, getSensorDataById, getData, - getdataFilter, getAverage, }; diff --git a/Web-Server/routes/SensorData.js b/Web-Server/routes/SensorData.js index 4a658c2..e4c7e16 100644 --- a/Web-Server/routes/SensorData.js +++ b/Web-Server/routes/SensorData.js @@ -7,7 +7,7 @@ const { deleteSensorData, getSensorDataById, getData, - getdataFilter, + getdataFilter, getAverage, } = require("../functions/apiDatabase.js"); @@ -59,63 +59,17 @@ router.delete("/delete", async (req, res, next) => { }); */ router.get("/data", async (req, res, next) => { - try { - let query = { - //can be desc or asc - order: req.query.order, + try { + const data = await getData(req.query); + res.status(200).json(data); - // number of data to be returned - limit: req.query.limit, - - //can be sensorid, locationid - sensorid: req.query.sensorid, - - //can be sensorid, locationid - locationid: req.query.locationid, - //yearly - year: req.query.year, - - //monthly - month: req.query.month, - - //weekly - week: req.query.week, - - //daily - day: req.query.day, - - //hourly - hour: req.query.hour, - - //minute - minute: req.query.minute, - - //start date - startdate: req.query.startdate, - - //end date - enddate: req.query.enddate, - - //highest or lowest of psi, co, o3, no2, so2, humidity, windspeed, temperature - psi: req.query.psi, - co: req.query.co, - o3: req.query.o3, - no2: req.query.no2, - so2: req.query.so2, - humidity: req.query.humidity, - windspeed: req.query.windspeed, - temperature: req.query.temperature, - - }; - - const data = await getData(query); - res.status(200).json(data); - } catch (error) { - console.error(error); - next(error); - } + } catch (error) { + console.error(error); + next(error); + } }); +/* router.get("/filter", async (req, res, next) => { try { @@ -139,9 +93,9 @@ router.get("/filter", async (req, res, next) => { next(error); } }); +*/ - -//average +//average router.get("/average", async (req, res, next) => { try { const query = { @@ -162,14 +116,12 @@ router.get("/average", async (req, res, next) => { //monthly month: req.query.month, //yearly - year: req.query.year, + year: req.query.year, }; const data = await getAverage(query); - - res.status(200).json(data); - } - catch (error) { + res.status(200).json(data); + } catch (error) { console.error(error); next(error); } @@ -208,4 +160,4 @@ module.exports = router; "temperature": 26 }, -*/ \ No newline at end of file +*/ diff --git a/api.MD b/api.MD index 30e2647..e6beefe 100644 --- a/api.MD +++ b/api.MD @@ -91,8 +91,6 @@ curl localhost/api/v0/sensor-data/delete -X DELETE -H "Content-Type: application //seed curl localhost/api/seed/v0/seed/new -H "Content-Type: application/json" -X POST -d '{"mockLocation": ["Ang Mo Kio", "Bishan" , "Tampines" , "Jurong" , "Marine Parade" , "Woodlands "]}' -//router.use('/seedSensorData ', require('./seedSensorData.js')); - //seed curl localhost/api/seed/v0/seedSensorData/new -H "Content-Type: application/json" -X POST -d '{ "startDate": "2023-01-01T16:00:00.000Z", diff --git a/test.js b/test.js new file mode 100644 index 0000000..0bb6067 --- /dev/null +++ b/test.js @@ -0,0 +1,120 @@ +let ormQuery = {}; +let whereClause = {}; +const validMonths = [ + "1", + "2", + "3", + "4", + "5", + "6", + "7", + "8", + "9", + "10", + "11", + "12", +]; +//handle buildfunc for query +buildQuery = { + year: async function (query) { + if (query.year !== undefined) { + //whereclause assign a value + whereClause.year = sequelize.where( + sequelize.fn("YEAR", sequelize.col("createdAt")), + query.year + ); + + } + }, +}; + +/* +function getData(queryString) + for(let query in queryString){ + if(buildFuncs[query]); buildFuncs[query](queryString) + + if (!whereClause) { + return await sensorDataModel.findAll(ormQuery); + } else if (whereClause) { + + + if (query.year) { + whereClause.year = sequelize.where( + sequelize.fn("YEAR", sequelize.col("createdAt")), + query.year + ); + } + +*/ + + +async function getData(query) { + for (let queryString in query) { + console.log(query); + console.log(queryString); + console.log(whereClause); + console.log(ormQuery); + + if (buildQuery[queryString]); + await buildQuery[queryString](query); + + if (!whereClause) { + return await sensorDataModel.findAll(ormQuery); + } else if (whereClause) { + return await sensorDataModel.findAll({ + limit: query.limit || 10, + //The operators Op.and, Op.or and Op.not can be used to create arbitrarily complex nested logical comparisons. + //https://sequelize.org/docs/v6/core-concepts/model-querying-basics/#examples-with-opand-and-opor + where: { + [Op.and]: [whereClause], + createdAt: { + //https://stackoverflow.com/questions/43115151/sequelize-query-to-find-all-records-that-falls-in-between-date-range + [Op.between]: [whereClause.startdate, whereClause.enddate], + }, + }, + + //only use where clause to lookup based on condition that i put into whereClause + ...ormQuery, + }); + } + } +} + + +async function getData(query) { + for (let queryString in query) { + console.log(query); + console.log(queryString); + console.log(whereClause); + console.log(ormQuery); + + if (buildQuery[queryString]); + await buildQuery[queryString](query); + + if (!whereClause) { + return await sensorDataModel.findAll(ormQuery); + } else if (whereClause) { + return await sensorDataModel.findAll({ + limit: query.limit || 10, + //The operators Op.and, Op.or and Op.not can be used to create arbitrarily complex nested logical comparisons. + //https://sequelize.org/docs/v6/core-concepts/model-querying-basics/#examples-with-opand-and-opor + where: { + [Op.and]: [whereClause], + createdAt: { + //https://stackoverflow.com/questions/43115151/sequelize-query-to-find-all-records-that-falls-in-between-date-range + [Op.between]: [whereClause.startdate, whereClause.enddate], + }, + }, + + //only use where clause to lookup based on condition that i put into whereClause + ...ormQuery, + }); + } + } +} + +query.hour || + query.sensorid || + query.locationid || + query.startdate || + query.enddate \ No newline at end of file