more routes
This commit is contained in:
@ -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,64 +71,20 @@ router.get("/data", async (req, res, next) => {
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
|
||||
router.get("/filter", 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);
|
||||
//date range
|
||||
router.get("/range", async (req, res, next) => {
|
||||
try {
|
||||
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);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
router.get("/:id", async (req, res, next) => {
|
||||
try {
|
||||
const sensor = await getSensorDataById(req.params.id);
|
||||
@ -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
|
||||
},
|
||||
|
||||
*/
|
||||
|
Reference in New Issue
Block a user