data routes done
This commit is contained in:
parent
21edf3aa07
commit
b3f2f4e5f6
@ -617,6 +617,11 @@ buildQuery = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
buildFunc = {
|
buildFunc = {
|
||||||
|
limit: async function (queryString) {
|
||||||
|
if (queryString.limit !== undefined) {
|
||||||
|
ormQuery.limit = parseInt(queryString.limit);
|
||||||
|
}
|
||||||
|
},
|
||||||
startdate: async function (queryString) {
|
startdate: async function (queryString) {
|
||||||
if (queryString.startdate !== undefined) {
|
if (queryString.startdate !== undefined) {
|
||||||
whereDate.startdate = new Date(queryString.startdate);
|
whereDate.startdate = new Date(queryString.startdate);
|
||||||
@ -692,59 +697,130 @@ buildFunc = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
async function getData(queryString) {
|
async function getData(queryString) {
|
||||||
//reset keys in whereClause and ormQuery. else it will keep appending to the previous query
|
if (queryString.pagesize || queryString.page) {
|
||||||
ormQuery = {};
|
//https://blog.bitsrc.io/pagination-with-sequelize-explained-83054df6e041
|
||||||
whereClause = {};
|
//pass pageSize taken from page=4 or default to 50
|
||||||
whereDate = {};
|
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) {
|
for (let query in queryString) {
|
||||||
if (buildQuery[query]) {
|
if (buildQuery[query]) {
|
||||||
await buildQuery[query](queryString);
|
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) {
|
async function getDatabyRange(queryString) {
|
||||||
whereDate = {};
|
if (queryString.pagesize || queryString.page) {
|
||||||
for (let query in queryString) {
|
//https://blog.bitsrc.io/pagination-with-sequelize-explained-83054df6e041
|
||||||
if (buildFunc[query]) {
|
//pass pageSize taken from page=4 or default to 50
|
||||||
await buildFunc[query](queryString);
|
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) {
|
||||||
if (whereClause) {
|
console.log(ormQuery);
|
||||||
console.log(ormQuery);
|
console.log(whereDate);
|
||||||
console.log(whereDate);
|
return await sensorDataModel.findAll({
|
||||||
return await sensorDataModel.findAll({
|
limit: queryString.limit || 1000000,
|
||||||
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.
|
//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
|
//https://sequelize.org/docs/v6/core-concepts/model-querying-basics/#examples-with-opand-and-opor
|
||||||
where: {
|
where: {
|
||||||
createdAt: {
|
createdAt: {
|
||||||
[Op.between]: [whereDate.startdate, whereDate.enddate],
|
[Op.between]: [whereDate.startdate, whereDate.enddate],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
//only use where clause to lookup based on condition that i put into whereClause
|
||||||
//only use where clause to lookup based on condition that i put into whereClause
|
...ormQuery,
|
||||||
...ormQuery,
|
});
|
||||||
});
|
} else {
|
||||||
|
return "Invalid query";
|
||||||
|
}
|
||||||
} else {
|
} 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";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +28,6 @@ async function seedSensorData(seedOptions) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await sensorDataModel.bulkCreate(rows)
|
await sensorDataModel.bulkCreate(rows)
|
||||||
//console.log(rows);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function convertDateToUTC(startDate) {
|
function convertDateToUTC(startDate) {
|
||||||
@ -85,6 +84,22 @@ function numberWithinPercent(inputNumber) {
|
|||||||
|
|
||||||
return Math.floor(newNumber);
|
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
|
//add seed
|
||||||
router.post("/new", async (req, res, next) => {
|
router.post("/new", async (req, res, next) => {
|
||||||
@ -115,18 +130,6 @@ POST /api/v0/seed/sensordata
|
|||||||
2) nextDataRow(lastRow, interval)
|
2) nextDataRow(lastRow, interval)
|
||||||
3) seedSensorData({post object from abovr})
|
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
@ -8,8 +8,6 @@ const {
|
|||||||
getSensorDataById,
|
getSensorDataById,
|
||||||
getData,
|
getData,
|
||||||
getDatabyRange,
|
getDatabyRange,
|
||||||
getdataFilter,
|
|
||||||
getAverage,
|
|
||||||
} = require("../functions/apiDatabase.js");
|
} = require("../functions/apiDatabase.js");
|
||||||
|
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
|
3
api.MD
3
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
|
//get specific data
|
||||||
http://localhost/api/v0/sensor-data/filter?windspeed=highest&limit=1
|
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
|
Loading…
x
Reference in New Issue
Block a user