more routes
This commit is contained in:
parent
b5e2820bc7
commit
d4510dace4
@ -26,88 +26,3 @@ 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
|
||||
|
||||
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -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 } = require("sequelize");
|
||||
const { Op, Sequelize } = 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
|
||||
@ -172,6 +172,19 @@ async function getSensorDataById(id) {
|
||||
}
|
||||
var ormQuery = {};
|
||||
var whereClause = {};
|
||||
var whereDate = {};
|
||||
const allowedQuery = [
|
||||
"limit",
|
||||
"order",
|
||||
"year",
|
||||
"month",
|
||||
"week",
|
||||
"day",
|
||||
"hour",
|
||||
"minute",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
];
|
||||
const validMonths = [
|
||||
"1",
|
||||
"2",
|
||||
@ -211,16 +224,13 @@ buildQuery = {
|
||||
}
|
||||
},
|
||||
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 {
|
||||
queryString.month = getMonthFromString(queryString.month);
|
||||
whereClause.month = sequelize.where(
|
||||
sequelize.fn("MONTH", sequelize.col("createdAt")),
|
||||
queryString.month
|
||||
@ -260,26 +270,444 @@ buildQuery = {
|
||||
);
|
||||
}
|
||||
},
|
||||
sensorid: async function (queryString) {
|
||||
if (queryString.sensorid !== undefined) {
|
||||
whereClause.sensorid = sequelize.where(
|
||||
sequelize.col("sensorid"),
|
||||
queryString.sensorid
|
||||
);
|
||||
}
|
||||
},
|
||||
locationid: async function (queryString) {
|
||||
if (queryString.locationid !== undefined) {
|
||||
whereClause.locationid = sequelize.where(
|
||||
sequelize.col("locationid"),
|
||||
queryString.locationid
|
||||
);
|
||||
}
|
||||
},
|
||||
psi: async function (queryString) {
|
||||
if (queryString.psi !== undefined && queryString.psi === "highest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.psi')"), "psi"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("psi"), "DESC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
if (queryString.psi !== undefined && queryString.psi === "lowest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.psi')"), "psi"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("psi"), "ASC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
},
|
||||
co: async function (queryString) {
|
||||
if (queryString.co !== undefined && queryString.co === "highest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.co')"), "co"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("co"), "DESC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
if (queryString.co !== undefined && queryString.co === "lowest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.co')"), "co"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("co"), "ASC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
},
|
||||
o3: async function (queryString) {
|
||||
if (queryString.o3 !== undefined && queryString.o3 === "highest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.o3')"), "o3"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("o3"), "DESC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
if (queryString.o3 !== undefined && queryString.o3 === "lowest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.o3')"), "o3"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("o3"), "ASC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
},
|
||||
no2: async function (queryString) {
|
||||
if (queryString.no2 !== undefined && queryString.no2 === "highest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.no2')"), "no2"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("no2"), "DESC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
if (queryString.no2 !== undefined && queryString.no2 === "lowest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.no2')"), "no2"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("no2"), "ASC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
},
|
||||
so2: async function (queryString) {
|
||||
if (queryString.so2 !== undefined && queryString.so2 === "highest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.so2')"), "so2"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("so2"), "DESC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
if (queryString.so2 !== undefined && queryString.so2 === "lowest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.so2')"), "so2"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("so2"), "ASC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
},
|
||||
humidity: async function (queryString) {
|
||||
if (
|
||||
queryString.humidity !== undefined &&
|
||||
queryString.humidity === "highest"
|
||||
) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[
|
||||
sequelize.literal("JSON_EXTRACT(measurement, '$.humidity')"),
|
||||
"humidity",
|
||||
],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("humidity"), "DESC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
if (
|
||||
queryString.humidity !== undefined &&
|
||||
queryString.humidity === "lowest"
|
||||
) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[
|
||||
sequelize.literal("JSON_EXTRACT(measurement, '$.humidity')"),
|
||||
"humidity",
|
||||
],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("humidity"), "ASC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
},
|
||||
windspeed: async function (queryString) {
|
||||
if (
|
||||
queryString.windspeed !== undefined &&
|
||||
queryString.windspeed === "highest"
|
||||
) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[
|
||||
sequelize.literal("JSON_EXTRACT(measurement, '$.windspeed')"),
|
||||
"windspeed",
|
||||
],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("windspeed"), "DESC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
if (
|
||||
queryString.windspeed !== undefined &&
|
||||
queryString.windspeed === "lowest"
|
||||
) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[
|
||||
sequelize.literal("JSON_EXTRACT(measurement, '$.windspeed')"),
|
||||
"windspeed",
|
||||
],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("windspeed"), "ASC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
},
|
||||
temperature: async function (queryString) {
|
||||
if (
|
||||
queryString.temperature !== undefined &&
|
||||
queryString.temperature === "highest"
|
||||
) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[
|
||||
sequelize.literal("JSON_EXTRACT(measurement, '$.temperature')"),
|
||||
"temperature",
|
||||
],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("temperature"), "DESC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
if (
|
||||
queryString.temperature !== undefined &&
|
||||
queryString.temperature === "lowest"
|
||||
) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[
|
||||
sequelize.literal("JSON_EXTRACT(measurement, '$.temperature')"),
|
||||
"temperature",
|
||||
],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("temperature"), "ASC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
},
|
||||
//average
|
||||
avg: async function (queryString) {
|
||||
if (queryString.avg !== undefined) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
//round to 2 decimal places
|
||||
[
|
||||
sequelize.fn(
|
||||
"ROUND",
|
||||
sequelize.fn(
|
||||
"AVG",
|
||||
Sequelize.literal(
|
||||
`JSON_EXTRACT(measurement, '$.${queryString.avg}')`
|
||||
)
|
||||
),
|
||||
2
|
||||
),
|
||||
"avg of " + queryString.avg,
|
||||
],
|
||||
],
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
sum: async function (queryString) {
|
||||
if (queryString.sum !== undefined) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
[
|
||||
sequelize.fn(
|
||||
"SUM",
|
||||
Sequelize.literal(
|
||||
`JSON_EXTRACT(measurement, '$.${queryString.sum}')`
|
||||
)
|
||||
),
|
||||
"sum of " + queryString.sum,
|
||||
],
|
||||
],
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
//total number of records
|
||||
total: async function (queryString) {
|
||||
if (queryString.total !== undefined) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
[sequelize.fn("COUNT", sequelize.col("id")), "total id / records"],
|
||||
],
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
buildFunc = {
|
||||
startdate: async function (queryString) {
|
||||
if (queryString.startdate !== undefined) {
|
||||
whereDate.startdate = new Date(queryString.startdate);
|
||||
}
|
||||
},
|
||||
enddate: async function (queryString) {
|
||||
if (queryString.enddate !== undefined) {
|
||||
whereDate.enddate = new Date(queryString.enddate);
|
||||
}
|
||||
},
|
||||
//total by startdate and enddate
|
||||
total: async function (queryString) {
|
||||
if (queryString.total !== undefined) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
[sequelize.fn("COUNT", sequelize.col("id")), "total id / records"],
|
||||
],
|
||||
};
|
||||
}
|
||||
},
|
||||
//average
|
||||
avg: async function (queryString) {
|
||||
if (queryString.avg !== undefined) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
//round to 2 decimal places
|
||||
[
|
||||
sequelize.fn(
|
||||
"ROUND",
|
||||
sequelize.fn(
|
||||
"AVG",
|
||||
Sequelize.literal(
|
||||
`JSON_EXTRACT(measurement, '$.${queryString.avg}')`
|
||||
)
|
||||
),
|
||||
2
|
||||
),
|
||||
"avg of " + queryString.avg,
|
||||
],
|
||||
],
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
sum: async function (queryString) {
|
||||
if (queryString.sum !== undefined) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
[
|
||||
sequelize.fn(
|
||||
"SUM",
|
||||
Sequelize.literal(
|
||||
`JSON_EXTRACT(measurement, '$.${queryString.sum}')`
|
||||
)
|
||||
),
|
||||
"sum of " + queryString.sum,
|
||||
],
|
||||
],
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
//total number of records
|
||||
total: async function (queryString) {
|
||||
if (queryString.total !== undefined) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
[sequelize.fn("COUNT", sequelize.col("id")), "total id / records"],
|
||||
],
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
async function getData(queryString) {
|
||||
// reset keys...
|
||||
//reset keys in whereClause and ormQuery. else it will keep appending to the previous query
|
||||
ormQuery = {};
|
||||
whereClause = {};
|
||||
for (let query in queryString) {
|
||||
console.log(queryString);
|
||||
console.log(query);
|
||||
console.log(whereClause);
|
||||
//console.log(ormQuery);
|
||||
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.
|
||||
@ -293,11 +721,32 @@ async function getData(queryString) {
|
||||
}
|
||||
}
|
||||
|
||||
async function getAverage(query) {}
|
||||
|
||||
/*
|
||||
|
||||
*/
|
||||
async function getDatabyRange(queryString) {
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getLocation,
|
||||
@ -316,5 +765,6 @@ module.exports = {
|
||||
deleteSensorData,
|
||||
getSensorDataById,
|
||||
getData,
|
||||
getDatabyRange,
|
||||
getAverage,
|
||||
};
|
||||
|
@ -115,4 +115,18 @@ 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)
|
||||
}
|
||||
|
||||
*/
|
||||
|
@ -7,6 +7,7 @@ const {
|
||||
deleteSensorData,
|
||||
getSensorDataById,
|
||||
getData,
|
||||
getDatabyRange,
|
||||
getdataFilter,
|
||||
getAverage,
|
||||
} = require("../functions/apiDatabase.js");
|
||||
@ -60,6 +61,7 @@ router.delete("/delete", async (req, res, next) => {
|
||||
*/
|
||||
router.get("/data", async (req, res, next) => {
|
||||
try {
|
||||
console.log(req.query);
|
||||
const data = await getData(req.query);
|
||||
res.status(200).json(data);
|
||||
|
||||
@ -69,63 +71,19 @@ router.get("/data", async (req, res, next) => {
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
|
||||
router.get("/filter", async (req, res, next) => {
|
||||
//date range
|
||||
router.get("/range", async (req, res, next) => {
|
||||
try {
|
||||
const query = {
|
||||
limit: req.query.limit,
|
||||
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,
|
||||
|
||||
//sensorid and locationid
|
||||
};
|
||||
const data = await getdataFilter(query);
|
||||
console.log(req.query);
|
||||
const data = await getDatabyRange(req.query);
|
||||
res.status(200).json(data);
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
*/
|
||||
|
||||
//average
|
||||
router.get("/average", async (req, res, next) => {
|
||||
try {
|
||||
const query = {
|
||||
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,
|
||||
//daily
|
||||
day: req.query.day,
|
||||
//hourly
|
||||
hour: req.query.hour,
|
||||
//weekly
|
||||
week: req.query.week,
|
||||
//monthly
|
||||
month: req.query.month,
|
||||
//yearly
|
||||
year: req.query.year,
|
||||
};
|
||||
const data = await getAverage(query);
|
||||
|
||||
res.status(200).json(data);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/:id", async (req, res, next) => {
|
||||
try {
|
||||
@ -138,26 +96,4 @@ router.get("/:id", async (req, res, next) => {
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
/*
|
||||
**Aggregate Sensor Data (e.g., Average PSI):**
|
||||
|
||||
- **Route:** `GET /api/v0/sensor-data/aggregate`
|
||||
- **Query Parameter:** `metric` (e.g., `psi`, `co`, `o3`)
|
||||
- **Description:** Calculate aggregate metrics for the specified parameter.
|
||||
|
||||
|
||||
*/
|
||||
|
||||
/*
|
||||
"measurement": {
|
||||
"co": 8,
|
||||
"o3": 89,
|
||||
"no2": 31,
|
||||
"psi": 34,
|
||||
"so2": 17,
|
||||
"humidity": 86,
|
||||
"windspeed": 10,
|
||||
"temperature": 26
|
||||
},
|
||||
|
||||
*/
|
||||
|
4
api.MD
4
api.MD
@ -150,7 +150,9 @@ Hour = 1 or wtv
|
||||
|
||||
//strong optional chaining
|
||||
curl 'http://localhost/api/v0/sensor-data/data?year=2023&month=1&week=1&day=1&sensorid=1&locationid=1'
|
||||
http://localhost/api/v0/sensor-data/data?startdate=2023-01-01T00:00:00.000&enddate=2023-01-03T&sensorid=1&locationid=1
|
||||
|
||||
|
||||
|
||||
|
||||
//get specific data
|
||||
http://localhost/api/v0/sensor-data/filter?windspeed=highest&limit=1
|
||||
|
295
package-lock.json
generated
295
package-lock.json
generated
@ -27,6 +27,9 @@
|
||||
"sanitize-html": "^2.11.0",
|
||||
"sequelize": "^6.35.2",
|
||||
"validator": "^13.11.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^3.0.2"
|
||||
}
|
||||
},
|
||||
"node_modules/@babel/runtime": {
|
||||
@ -205,6 +208,19 @@
|
||||
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/anymatch": {
|
||||
"version": "3.1.3",
|
||||
"resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz",
|
||||
"integrity": "sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"normalize-path": "^3.0.0",
|
||||
"picomatch": "^2.0.4"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 8"
|
||||
}
|
||||
},
|
||||
"node_modules/aproba": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/aproba/-/aproba-2.0.0.tgz",
|
||||
@ -282,6 +298,15 @@
|
||||
"node": ">= 10.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/binary-extensions": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz",
|
||||
"integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/bl": {
|
||||
"version": "6.0.10",
|
||||
"resolved": "https://registry.npmjs.org/bl/-/bl-6.0.10.tgz",
|
||||
@ -336,6 +361,18 @@
|
||||
"balanced-match": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/braces": {
|
||||
"version": "3.0.2",
|
||||
"resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz",
|
||||
"integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"fill-range": "^7.0.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/buffer": {
|
||||
"version": "6.0.3",
|
||||
"resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz",
|
||||
@ -408,6 +445,33 @@
|
||||
"url": "https://github.com/chalk/chalk?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/chokidar": {
|
||||
"version": "3.5.3",
|
||||
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz",
|
||||
"integrity": "sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw==",
|
||||
"dev": true,
|
||||
"funding": [
|
||||
{
|
||||
"type": "individual",
|
||||
"url": "https://paulmillr.com/funding/"
|
||||
}
|
||||
],
|
||||
"dependencies": {
|
||||
"anymatch": "~3.1.2",
|
||||
"braces": "~3.0.2",
|
||||
"glob-parent": "~5.1.2",
|
||||
"is-binary-path": "~2.1.0",
|
||||
"is-glob": "~4.0.1",
|
||||
"normalize-path": "~3.0.0",
|
||||
"readdirp": "~3.6.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 8.10.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"fsevents": "~2.3.2"
|
||||
}
|
||||
},
|
||||
"node_modules/chownr": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/chownr/-/chownr-2.0.0.tgz",
|
||||
@ -997,6 +1061,18 @@
|
||||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"node_modules/fill-range": {
|
||||
"version": "7.0.1",
|
||||
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
|
||||
"integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"to-regex-range": "^5.0.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/finalhandler": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz",
|
||||
@ -1082,6 +1158,20 @@
|
||||
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
|
||||
"integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw=="
|
||||
},
|
||||
"node_modules/fsevents": {
|
||||
"version": "2.3.3",
|
||||
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
|
||||
"integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",
|
||||
"dev": true,
|
||||
"hasInstallScript": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"darwin"
|
||||
],
|
||||
"engines": {
|
||||
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/function-bind": {
|
||||
"version": "1.1.2",
|
||||
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
|
||||
@ -1158,6 +1248,18 @@
|
||||
"url": "https://github.com/sponsors/isaacs"
|
||||
}
|
||||
},
|
||||
"node_modules/glob-parent": {
|
||||
"version": "5.1.2",
|
||||
"resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz",
|
||||
"integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"is-glob": "^4.0.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 6"
|
||||
}
|
||||
},
|
||||
"node_modules/gopd": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/gopd/-/gopd-1.0.1.tgz",
|
||||
@ -1360,6 +1462,12 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"node_modules/ignore-by-default": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/ignore-by-default/-/ignore-by-default-1.0.1.tgz",
|
||||
"integrity": "sha512-Ius2VYcGNk7T90CppJqcIkS5ooHUZyIQK+ClZfMfMNFEF9VSE73Fq+906u/CWu92x4gzZMWOwfFYckPObzdEbA==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/inflection": {
|
||||
"version": "1.13.4",
|
||||
"resolved": "https://registry.npmjs.org/inflection/-/inflection-1.13.4.tgz",
|
||||
@ -1390,6 +1498,27 @@
|
||||
"node": ">= 0.10"
|
||||
}
|
||||
},
|
||||
"node_modules/is-binary-path": {
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz",
|
||||
"integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"binary-extensions": "^2.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/is-extglob": {
|
||||
"version": "2.1.1",
|
||||
"resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz",
|
||||
"integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/is-fullwidth-code-point": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz",
|
||||
@ -1398,6 +1527,27 @@
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/is-glob": {
|
||||
"version": "4.0.3",
|
||||
"resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz",
|
||||
"integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"is-extglob": "^2.1.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/is-number": {
|
||||
"version": "7.0.0",
|
||||
"resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz",
|
||||
"integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">=0.12.0"
|
||||
}
|
||||
},
|
||||
"node_modules/is-plain-object": {
|
||||
"version": "5.0.0",
|
||||
"resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-5.0.0.tgz",
|
||||
@ -1799,6 +1949,55 @@
|
||||
"node": ">=6.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/nodemon": {
|
||||
"version": "3.0.2",
|
||||
"resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.0.2.tgz",
|
||||
"integrity": "sha512-9qIN2LNTrEzpOPBaWHTm4Asy1LxXLSickZStAQ4IZe7zsoIpD/A7LWxhZV3t4Zu352uBcqVnRsDXSMR2Sc3lTA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"chokidar": "^3.5.2",
|
||||
"debug": "^4",
|
||||
"ignore-by-default": "^1.0.1",
|
||||
"minimatch": "^3.1.2",
|
||||
"pstree.remy": "^1.1.8",
|
||||
"semver": "^7.5.3",
|
||||
"simple-update-notifier": "^2.0.0",
|
||||
"supports-color": "^5.5.0",
|
||||
"touch": "^3.1.0",
|
||||
"undefsafe": "^2.0.5"
|
||||
},
|
||||
"bin": {
|
||||
"nodemon": "bin/nodemon.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/nodemon"
|
||||
}
|
||||
},
|
||||
"node_modules/nodemon/node_modules/has-flag": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz",
|
||||
"integrity": "sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
}
|
||||
},
|
||||
"node_modules/nodemon/node_modules/supports-color": {
|
||||
"version": "5.5.0",
|
||||
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz",
|
||||
"integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"has-flag": "^3.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
}
|
||||
},
|
||||
"node_modules/nopt": {
|
||||
"version": "5.0.0",
|
||||
"resolved": "https://registry.npmjs.org/nopt/-/nopt-5.0.0.tgz",
|
||||
@ -1813,6 +2012,15 @@
|
||||
"node": ">=6"
|
||||
}
|
||||
},
|
||||
"node_modules/normalize-path": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz",
|
||||
"integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/npmlog": {
|
||||
"version": "5.0.1",
|
||||
"resolved": "https://registry.npmjs.org/npmlog/-/npmlog-5.0.1.tgz",
|
||||
@ -1971,6 +2179,18 @@
|
||||
"resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz",
|
||||
"integrity": "sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ=="
|
||||
},
|
||||
"node_modules/picomatch": {
|
||||
"version": "2.3.1",
|
||||
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz",
|
||||
"integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">=8.6"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/jonschlinkert"
|
||||
}
|
||||
},
|
||||
"node_modules/pngjs": {
|
||||
"version": "5.0.0",
|
||||
"resolved": "https://registry.npmjs.org/pngjs/-/pngjs-5.0.0.tgz",
|
||||
@ -2031,6 +2251,12 @@
|
||||
"node": ">= 0.10"
|
||||
}
|
||||
},
|
||||
"node_modules/pstree.remy": {
|
||||
"version": "1.1.8",
|
||||
"resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz",
|
||||
"integrity": "sha512-77DZwxQmxKnu3aR542U+X8FypNzbfJ+C5XQDk3uWjWxn6151aIMGthWYRXTqT1E5oJvg+ljaa2OJi+VfvCOQ8w==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/qrcode": {
|
||||
"version": "1.5.3",
|
||||
"resolved": "https://registry.npmjs.org/qrcode/-/qrcode-1.5.3.tgz",
|
||||
@ -2107,6 +2333,18 @@
|
||||
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/readdirp": {
|
||||
"version": "3.6.0",
|
||||
"resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz",
|
||||
"integrity": "sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"picomatch": "^2.2.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/regenerator-runtime": {
|
||||
"version": "0.14.1",
|
||||
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz",
|
||||
@ -2362,6 +2600,18 @@
|
||||
"resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.7.tgz",
|
||||
"integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ=="
|
||||
},
|
||||
"node_modules/simple-update-notifier": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/simple-update-notifier/-/simple-update-notifier-2.0.0.tgz",
|
||||
"integrity": "sha512-a2B9Y0KlNXl9u/vsW6sTIu9vGEpfKu2wRV6l1H3XEas/0gUIzGzBoP/IouTcUQbm9JWZLH3COxyn03TYlFax6w==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"semver": "^7.5.3"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"node_modules/source-map-js": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.0.2.tgz",
|
||||
@ -2480,6 +2730,18 @@
|
||||
"node": ">=0.2.6"
|
||||
}
|
||||
},
|
||||
"node_modules/to-regex-range": {
|
||||
"version": "5.0.1",
|
||||
"resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz",
|
||||
"integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"is-number": "^7.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8.0"
|
||||
}
|
||||
},
|
||||
"node_modules/toidentifier": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz",
|
||||
@ -2493,6 +2755,33 @@
|
||||
"resolved": "https://registry.npmjs.org/toposort-class/-/toposort-class-1.0.1.tgz",
|
||||
"integrity": "sha512-OsLcGGbYF3rMjPUf8oKktyvCiUxSbqMMS39m33MAjLTC1DVIH6x3WSt63/M77ihI09+Sdfk1AXvfhCEeUmC7mg=="
|
||||
},
|
||||
"node_modules/touch": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/touch/-/touch-3.1.0.tgz",
|
||||
"integrity": "sha512-WBx8Uy5TLtOSRtIq+M03/sKDrXCLHxwDcquSP2c43Le03/9serjQBIztjRz6FkJez9D/hleyAXTBGLwwZUw9lA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"nopt": "~1.0.10"
|
||||
},
|
||||
"bin": {
|
||||
"nodetouch": "bin/nodetouch.js"
|
||||
}
|
||||
},
|
||||
"node_modules/touch/node_modules/nopt": {
|
||||
"version": "1.0.10",
|
||||
"resolved": "https://registry.npmjs.org/nopt/-/nopt-1.0.10.tgz",
|
||||
"integrity": "sha512-NWmpvLSqUrgrAC9HCuxEvb+PSloHpqVu+FqcO4eeF2h5qYRhA7ev6KvelyQAKtegUbC6RypJnlEOhd8vloNKYg==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"abbrev": "1"
|
||||
},
|
||||
"bin": {
|
||||
"nopt": "bin/nopt.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/tr46": {
|
||||
"version": "0.0.3",
|
||||
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
|
||||
@ -2531,6 +2820,12 @@
|
||||
"node": ">= 0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/undefsafe": {
|
||||
"version": "2.0.5",
|
||||
"resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.5.tgz",
|
||||
"integrity": "sha512-WxONCrssBM8TSPRqN5EmsjVrsv4A8X12J4ArBiiayv3DyyG3ZlIg6yysuuSYdZsVz3TKcTg2fd//Ujd4CHV1iA==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/undici-types": {
|
||||
"version": "5.26.5",
|
||||
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",
|
||||
|
@ -35,5 +35,8 @@
|
||||
"sanitize-html": "^2.11.0",
|
||||
"sequelize": "^6.35.2",
|
||||
"validator": "^13.11.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^3.0.2"
|
||||
}
|
||||
}
|
||||
|
120
test.js
120
test.js
@ -1,120 +0,0 @@
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user