Merge pull request #16 from Newtbot/Dev-branch

data routes done
This commit is contained in:
noot 2024-01-12 20:10:37 +08:00 committed by GitHub
commit 7f81fecb86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 138 additions and 60 deletions

View File

@ -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,6 +697,42 @@ buildFunc = {
};
async function getData(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;
//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,
//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 = {};
@ -719,9 +760,43 @@ async function getData(queryString) {
...ormQuery,
});
}
}
}
async function getDatabyRange(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,
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,
});
} else {
return "Invalid query";
}
} else {
whereDate = {};
for (let query in queryString) {
if (buildFunc[query]) {
@ -746,6 +821,7 @@ async function getDatabyRange(queryString) {
} else {
return "Invalid query";
}
}
}
module.exports = {

View File

@ -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)
}
*/

View File

@ -8,8 +8,6 @@ const {
getSensorDataById,
getData,
getDatabyRange,
getdataFilter,
getAverage,
} = require("../functions/apiDatabase.js");
const express = require("express");

3
api.MD
View File

@ -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
//pagination
http://localhost/api/v0/sensor-data/data?week=1&sensorid=1&locationid=1&page=2&pagesize=10