commit
c06ca3bc79
@ -24,3 +24,90 @@ log_facility 5
|
|||||||
log_dest file /var/log/mosquitto/mosquitto.log
|
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
|
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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
Vivian/functions/validateData.js
Normal file
33
Vivian/functions/validateData.js
Normal file
@ -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,
|
||||||
|
};
|
||||||
|
|
@ -2,7 +2,7 @@ const { sequelize } = require("../../Database/mySql.js");
|
|||||||
const { locationModel } = require("../../Database/model/locationModel.js");
|
const { locationModel } = require("../../Database/model/locationModel.js");
|
||||||
const { sensorModel } = require("../../Database/model/sensorModel.js");
|
const { sensorModel } = require("../../Database/model/sensorModel.js");
|
||||||
const { sensorDataModel } = require("../../Database/model/sensorDataModel.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
|
//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
|
//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;
|
return sensorData;
|
||||||
}
|
}
|
||||||
|
var ormQuery = {};
|
||||||
async function getData(query) {
|
var whereClause = {};
|
||||||
let ormQuery = {};
|
const validMonths = [
|
||||||
let whereClause = {};
|
"1",
|
||||||
//let whereNest = {};
|
"2",
|
||||||
//let whereDate = {};
|
"3",
|
||||||
//limit default value if query.limit is undefined
|
"4",
|
||||||
if (query.limit === undefined) {
|
"5",
|
||||||
query.limit = 10;
|
"6",
|
||||||
}
|
"7",
|
||||||
if (query.limit !== undefined && query.order !== undefined)
|
"8",
|
||||||
ormQuery = {
|
"9",
|
||||||
limit: parseInt(query.limit),
|
"10",
|
||||||
//console.log(sentence.toUpperCase());
|
"11",
|
||||||
order: [["createdAt", query.order.toUpperCase()]],
|
"12",
|
||||||
...ormQuery,
|
];
|
||||||
};
|
//handle buildfunc for query
|
||||||
}
|
buildQuery = {
|
||||||
|
limit: async function (queryString) {
|
||||||
/*
|
if (queryString.limit !== undefined) {
|
||||||
//handle year and month and week and day and hour and minute and sensorid and locationid through optional chaining
|
ormQuery.limit = parseInt(queryString.limit);
|
||||||
else if (
|
}
|
||||||
query.limit ||
|
},
|
||||||
query.order ||
|
order: async function (queryString) {
|
||||||
query.year ||
|
if (queryString.order !== undefined) {
|
||||||
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)
|
|
||||||
ormQuery = {
|
ormQuery = {
|
||||||
limit: parseInt(query.limit),
|
|
||||||
order: [["createdAt", query.order.toUpperCase()]],
|
|
||||||
...ormQuery,
|
...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(
|
whereClause.year = sequelize.where(
|
||||||
sequelize.fn("YEAR", sequelize.col("createdAt")),
|
sequelize.fn("YEAR", sequelize.col("createdAt")),
|
||||||
query.year
|
queryString.year
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
if (query.month) {
|
month: async function (queryString) {
|
||||||
const validMonths = [
|
console.log("queryString month:", queryString.month, queryString);
|
||||||
"1",
|
if (queryString.month !== undefined) {
|
||||||
"2",
|
console.log("queryString month:", "i should not be here");
|
||||||
"3",
|
if (validMonths.includes(queryString.month)) {
|
||||||
"4",
|
whereClause.month = sequelize.where(
|
||||||
"5",
|
sequelize.fn("MONTH", sequelize.col("createdAt")),
|
||||||
"6",
|
queryString.month
|
||||||
"7",
|
);
|
||||||
"8",
|
|
||||||
"9",
|
|
||||||
"10",
|
|
||||||
"11",
|
|
||||||
"12",
|
|
||||||
];
|
|
||||||
if (validMonths.includes(query.month)) {
|
|
||||||
whereClause = {
|
|
||||||
where: sequelize.where(
|
|
||||||
sequelize.fn("MONTH", sequelize.col("createdAt")),
|
|
||||||
query.month
|
|
||||||
),
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
query.month = getMonthFromString(query.month);
|
queryString.month = getMonthFromString(queryString.month);
|
||||||
whereClause = {
|
whereClause.month = sequelize.where(
|
||||||
where: sequelize.where(
|
sequelize.fn("MONTH", sequelize.col("createdAt")),
|
||||||
sequelize.fn("MONTH", sequelize.col("createdAt")),
|
queryString.month
|
||||||
query.month
|
);
|
||||||
),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (query.week) {
|
},
|
||||||
|
week: async function (queryString) {
|
||||||
|
if (queryString.week !== undefined) {
|
||||||
whereClause.week = sequelize.where(
|
whereClause.week = sequelize.where(
|
||||||
sequelize.fn("WEEK", sequelize.col("createdAt")),
|
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(
|
whereClause.day = sequelize.where(
|
||||||
sequelize.fn("DAY", sequelize.col("createdAt")),
|
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(
|
whereClause.hour = sequelize.where(
|
||||||
sequelize.fn("HOUR", sequelize.col("createdAt")),
|
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(
|
whereClause.minute = sequelize.where(
|
||||||
sequelize.fn("MINUTE", sequelize.col("createdAt")),
|
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
|
async function getData(queryString) {
|
||||||
if (query.sensorid) {
|
// reset keys...
|
||||||
whereClause.sensorid = sequelize.where(
|
ormQuery = {};
|
||||||
sequelize.col("sensorid"),
|
whereClause = {};
|
||||||
query.sensorid
|
for (let query in queryString) {
|
||||||
);
|
console.log(queryString);
|
||||||
}
|
console.log(query);
|
||||||
if (query.locationid) {
|
console.log(whereClause);
|
||||||
whereClause.locationid = sequelize.where(
|
//console.log(ormQuery);
|
||||||
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
|
|
||||||
|
|
||||||
ormQuery = {
|
if (buildQuery[query]) {
|
||||||
attributes: [
|
await buildQuery[query](queryString);
|
||||||
[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: [
|
|
||||||
[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) {
|
if (!whereClause) {
|
||||||
return await sensorDataModel.findAll(ormQuery);
|
return await sensorDataModel.findAll(ormQuery);
|
||||||
} else if (whereClause) {
|
} else if (whereClause) {
|
||||||
console.log(whereClause);
|
|
||||||
//console.log(whereNest);
|
|
||||||
//console.log(whereDate);
|
|
||||||
console.log(query);
|
|
||||||
console.log(ormQuery);
|
|
||||||
|
|
||||||
return await sensorDataModel.findAll({
|
return await sensorDataModel.findAll({
|
||||||
//limit default value if query.limit is undefined or not provided
|
limit: queryString.limit || 1000000,
|
||||||
limit: query.limit,
|
|
||||||
//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: {
|
||||||
//[Op.and]: [whereNest, whereClause],
|
|
||||||
[Op.and]: [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
|
//only use where clause to lookup based on condition that i put into whereClause
|
||||||
...ormQuery,
|
...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) {}
|
async function getAverage(query) {}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
@ -895,6 +316,5 @@ module.exports = {
|
|||||||
deleteSensorData,
|
deleteSensorData,
|
||||||
getSensorDataById,
|
getSensorDataById,
|
||||||
getData,
|
getData,
|
||||||
getdataFilter,
|
|
||||||
getAverage,
|
getAverage,
|
||||||
};
|
};
|
||||||
|
@ -7,7 +7,7 @@ const {
|
|||||||
deleteSensorData,
|
deleteSensorData,
|
||||||
getSensorDataById,
|
getSensorDataById,
|
||||||
getData,
|
getData,
|
||||||
getdataFilter,
|
getdataFilter,
|
||||||
getAverage,
|
getAverage,
|
||||||
} = require("../functions/apiDatabase.js");
|
} = require("../functions/apiDatabase.js");
|
||||||
|
|
||||||
@ -59,63 +59,17 @@ router.delete("/delete", async (req, res, next) => {
|
|||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
router.get("/data", async (req, res, next) => {
|
router.get("/data", async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
let query = {
|
const data = await getData(req.query);
|
||||||
//can be desc or asc
|
res.status(200).json(data);
|
||||||
order: req.query.order,
|
|
||||||
|
|
||||||
// number of data to be returned
|
} catch (error) {
|
||||||
limit: req.query.limit,
|
console.error(error);
|
||||||
|
next(error);
|
||||||
//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);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
router.get("/filter", async (req, res, next) => {
|
router.get("/filter", async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
@ -139,7 +93,7 @@ router.get("/filter", async (req, res, next) => {
|
|||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
//average
|
//average
|
||||||
router.get("/average", async (req, res, next) => {
|
router.get("/average", async (req, res, next) => {
|
||||||
@ -167,9 +121,7 @@ router.get("/average", async (req, res, next) => {
|
|||||||
const data = await getAverage(query);
|
const data = await getAverage(query);
|
||||||
|
|
||||||
res.status(200).json(data);
|
res.status(200).json(data);
|
||||||
|
} catch (error) {
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
|
2
api.MD
2
api.MD
@ -91,8 +91,6 @@ curl localhost/api/v0/sensor-data/delete -X DELETE -H "Content-Type: application
|
|||||||
//seed
|
//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 "]}'
|
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
|
//seed
|
||||||
curl localhost/api/seed/v0/seedSensorData/new -H "Content-Type: application/json" -X POST -d '{
|
curl localhost/api/seed/v0/seedSensorData/new -H "Content-Type: application/json" -X POST -d '{
|
||||||
"startDate": "2023-01-01T16:00:00.000Z",
|
"startDate": "2023-01-01T16:00:00.000Z",
|
||||||
|
120
test.js
Normal file
120
test.js
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user