diff --git a/Web-Server/functions/APIDatabase.js b/Web-Server/functions/APIDatabase.js index c469d30..f3e5af1 100644 --- a/Web-Server/functions/APIDatabase.js +++ b/Web-Server/functions/APIDatabase.js @@ -617,6 +617,11 @@ buildQuery = { }; buildFunc = { + limit: async function (queryString) { + if (queryString.limit !== undefined) { + ormQuery.limit = parseInt(queryString.limit); + } + }, startdate: async function (queryString) { if (queryString.startdate !== undefined) { whereDate.startdate = new Date(queryString.startdate); @@ -692,59 +697,130 @@ buildFunc = { }; async function getData(queryString) { - //reset keys in whereClause and ormQuery. else it will keep appending to the previous query - ormQuery = {}; - whereClause = {}; - whereDate = {}; + if (queryString.pagesize || queryString.page) { + //https://blog.bitsrc.io/pagination-with-sequelize-explained-83054df6e041 + //pass pageSize taken from page=4 or default to 50 + queryString.pagesize = queryString.pagesize || 50; + let offset = (queryString.page || 0) * queryString.pagesize; + queryString.limit = queryString.pagesize; + //reset keys in whereClause and ormQuery. else it will keep appending to the previous query + ormQuery = {}; + whereClause = {}; + whereDate = {}; - for (let query in queryString) { - if (buildQuery[query]) { - await buildQuery[query](queryString); + for (let query in queryString) { + if (buildQuery[query]) { + await buildQuery[query](queryString); + } + } + if (!whereClause) { + return await sensorDataModel.findAll(ormQuery); + } else if (whereClause) { + console.log(whereClause); + console.log(ormQuery); + console.log(whereDate); + return await sensorDataModel.findAll({ + limit: queryString.limit || 1000000, + //https://sequelize.org/docs/v6/core-concepts/model-querying-basics/#limits-and-pagination + offset: parseInt(offset), + //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], + }, + //only use where clause to lookup based on condition that i put into whereClause + ...ormQuery, + }); + } + } else { + //reset keys in whereClause and ormQuery. else it will keep appending to the previous query + ormQuery = {}; + whereClause = {}; + whereDate = {}; + + for (let query in queryString) { + if (buildQuery[query]) { + await buildQuery[query](queryString); + } + } + if (!whereClause) { + return await sensorDataModel.findAll(ormQuery); + } else if (whereClause) { + console.log(whereClause); + console.log(ormQuery); + console.log(whereDate); + return await sensorDataModel.findAll({ + 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]: [whereClause], + }, + //only use where clause to lookup based on condition that i put into whereClause + ...ormQuery, + }); } - } - if (!whereClause) { - return await sensorDataModel.findAll(ormQuery); - } else if (whereClause) { - console.log(whereClause); - console.log(ormQuery); - console.log(whereDate); - return await sensorDataModel.findAll({ - 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]: [whereClause], - }, - //only use where clause to lookup based on condition that i put into whereClause - ...ormQuery, - }); } } async function getDatabyRange(queryString) { - whereDate = {}; - for (let query in queryString) { - if (buildFunc[query]) { - await buildFunc[query](queryString); + if (queryString.pagesize || queryString.page) { + //https://blog.bitsrc.io/pagination-with-sequelize-explained-83054df6e041 + //pass pageSize taken from page=4 or default to 50 + queryString.pagesize = queryString.pagesize || 50; + let offset = (queryString.page || 0) * queryString.pagesize; + queryString.limit = queryString.pagesize; + + whereDate = {}; + for (let query in queryString) { + if (buildFunc[query]) { + await buildFunc[query](queryString); + } } - } - if (whereClause) { - console.log(ormQuery); - console.log(whereDate); - return await sensorDataModel.findAll({ - 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: { - createdAt: { - [Op.between]: [whereDate.startdate, whereDate.enddate], + if (whereClause) { + console.log(ormQuery); + console.log(whereDate); + return await sensorDataModel.findAll({ + limit: queryString.limit || 1000000, + offset: offset, + //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: { + createdAt: { + [Op.between]: [whereDate.startdate, whereDate.enddate], + }, }, - }, - //only use where clause to lookup based on condition that i put into whereClause - ...ormQuery, - }); + //only use where clause to lookup based on condition that i put into whereClause + ...ormQuery, + }); + } else { + return "Invalid query"; + } } else { - return "Invalid query"; + whereDate = {}; + for (let query in queryString) { + if (buildFunc[query]) { + await buildFunc[query](queryString); + } + } + if (whereClause) { + console.log(ormQuery); + console.log(whereDate); + return await sensorDataModel.findAll({ + 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: { + createdAt: { + [Op.between]: [whereDate.startdate, whereDate.enddate], + }, + }, + //only use where clause to lookup based on condition that i put into whereClause + ...ormQuery, + }); + } else { + return "Invalid query"; + } } } diff --git a/Web-Server/routes/SeedsensorData.js b/Web-Server/routes/SeedsensorData.js index 6c5da9f..d223ea0 100644 --- a/Web-Server/routes/SeedsensorData.js +++ b/Web-Server/routes/SeedsensorData.js @@ -28,7 +28,6 @@ async function seedSensorData(seedOptions) { } await sensorDataModel.bulkCreate(rows) - //console.log(rows); } function convertDateToUTC(startDate) { @@ -85,6 +84,22 @@ function numberWithinPercent(inputNumber) { return Math.floor(newNumber); } +/* +function randomizeDataPoint(value, delta, maxDelta){ + // https://stackoverflow.com/a/36756480 + delta = Math.random() < 0.9 ? delta : maxDelta + return Math.floor(Math.random() * ((value+delta) - Math.abs(delta-value)) + Math.abs(delta-value)); + } + + let count = 0 + let currentValue = 85 + while(count <50){ + count++ + console.log(currentValue) + currentValue = randomizeDataPoint(currentValue, 2, 5) + } + +*/ //add seed router.post("/new", async (req, res, next) => { @@ -115,18 +130,6 @@ POST /api/v0/seed/sensordata 2) nextDataRow(lastRow, interval) 3) seedSensorData({post object from abovr}) -function randomizeDataPoint(value, delta, maxDelta){ - // https://stackoverflow.com/a/36756480 - delta = Math.random() < 0.9 ? delta : maxDelta - return Math.floor(Math.random() * ((value+delta) - Math.abs(delta-value)) + Math.abs(delta-value)); -} -let count = 0 -let currentValue = 85 -while(count <50){ - count++ - console.log(currentValue) - currentValue = randomizeDataPoint(currentValue, 2, 5) -} */ diff --git a/Web-Server/routes/SensorData.js b/Web-Server/routes/SensorData.js index fee3d74..5c0ede7 100644 --- a/Web-Server/routes/SensorData.js +++ b/Web-Server/routes/SensorData.js @@ -8,8 +8,6 @@ const { getSensorDataById, getData, getDatabyRange, - getdataFilter, - getAverage, } = require("../functions/apiDatabase.js"); const express = require("express"); diff --git a/api.MD b/api.MD index 2d4fe88..89733c3 100644 --- a/api.MD +++ b/api.MD @@ -157,4 +157,5 @@ curl 'http://localhost/api/v0/sensor-data/data?year=2023&month=1&week=1&day=1&se //get specific data http://localhost/api/v0/sensor-data/filter?windspeed=highest&limit=1 -//average \ No newline at end of file +//pagination +http://localhost/api/v0/sensor-data/data?week=1&sensorid=1&locationid=1&page=2&pagesize=10 \ No newline at end of file