aa route
optional query
This commit is contained in:
parent
64e022e2ea
commit
c2dc608d5b
@ -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");
|
||||||
var moment = require("moment");
|
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
|
||||||
@ -173,6 +173,9 @@ async function getSensorDataById(id) {
|
|||||||
|
|
||||||
async function getData(query) {
|
async function getData(query) {
|
||||||
let ormQuery = {};
|
let ormQuery = {};
|
||||||
|
let whereClause = {};
|
||||||
|
let whereNest = {};
|
||||||
|
|
||||||
if (query.limit !== undefined && query.order !== undefined)
|
if (query.limit !== undefined && query.order !== undefined)
|
||||||
ormQuery = {
|
ormQuery = {
|
||||||
limit: parseInt(query.limit),
|
limit: parseInt(query.limit),
|
||||||
@ -180,27 +183,110 @@ async function getData(query) {
|
|||||||
order: [["createdAt", query.order.toUpperCase()]],
|
order: [["createdAt", query.order.toUpperCase()]],
|
||||||
...ormQuery,
|
...ormQuery,
|
||||||
};
|
};
|
||||||
else if (query.sensorid !== undefined) {
|
//handle year and month and week and day and hour and minute and sensorid and locationid through optional chaining
|
||||||
|
else if (
|
||||||
|
query.limit ||
|
||||||
|
query.order ||
|
||||||
|
query.year ||
|
||||||
|
query.month ||
|
||||||
|
query.week ||
|
||||||
|
query.day ||
|
||||||
|
query.hour ||
|
||||||
|
query.minute ||
|
||||||
|
query.sensorid ||
|
||||||
|
query.locationid
|
||||||
|
) {
|
||||||
|
if (query.limit !== undefined && query.order !== undefined)
|
||||||
ormQuery = {
|
ormQuery = {
|
||||||
where: {
|
limit: parseInt(query.limit),
|
||||||
sensorid: query.sensorid,
|
order: [["createdAt", query.order.toUpperCase()]],
|
||||||
},
|
|
||||||
...ormQuery,
|
...ormQuery,
|
||||||
};
|
};
|
||||||
} else if (query.locationid !== undefined) {
|
|
||||||
ormQuery = {
|
if (query.year) {
|
||||||
where: {
|
whereClause.year = sequelize.where(
|
||||||
locationid: query.locationid,
|
sequelize.fn("YEAR", sequelize.col("createdAt")),
|
||||||
},
|
query.year
|
||||||
...ormQuery,
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (query.month) {
|
||||||
|
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 if (query.year !== undefined) {
|
} else {
|
||||||
|
query.month = getMonthFromString(query.month);
|
||||||
|
whereClause = {
|
||||||
|
where: sequelize.where(
|
||||||
|
sequelize.fn("MONTH", sequelize.col("createdAt")),
|
||||||
|
query.month
|
||||||
|
),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (query.week) {
|
||||||
|
whereClause.week = sequelize.where(
|
||||||
|
sequelize.fn("WEEK", sequelize.col("createdAt")),
|
||||||
|
query.week
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (query.day) {
|
||||||
|
whereClause.day = sequelize.where(
|
||||||
|
sequelize.fn("DAY", sequelize.col("createdAt")),
|
||||||
|
query.day
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (query.hour) {
|
||||||
|
whereClause.hour = sequelize.where(
|
||||||
|
sequelize.fn("HOUR", sequelize.col("createdAt")),
|
||||||
|
query.hour
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (query.minute) {
|
||||||
|
whereClause.minute = sequelize.where(
|
||||||
|
sequelize.fn("MINUTE", sequelize.col("createdAt")),
|
||||||
|
query.minute
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (query.sensorid) {
|
||||||
|
whereNest.sensorid = sequelize.where(
|
||||||
|
sequelize.col("sensorid"),
|
||||||
|
query.sensorid
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (query.locationid) {
|
||||||
|
whereNest.locationid = sequelize.where(
|
||||||
|
sequelize.col("locationid"),
|
||||||
|
query.locationid
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//accept year
|
||||||
|
else if (query.year !== undefined) {
|
||||||
ormQuery = {
|
ormQuery = {
|
||||||
|
//handle all year, month
|
||||||
where: sequelize.where(
|
where: sequelize.where(
|
||||||
sequelize.fn("YEAR", sequelize.col("createdAt")),
|
sequelize.fn("YEAR", sequelize.col("createdAt")),
|
||||||
query.year
|
query.year
|
||||||
),
|
),
|
||||||
...ormQuery,
|
|
||||||
};
|
};
|
||||||
} else if (query.month !== undefined) {
|
} else if (query.month !== undefined) {
|
||||||
console.log(typeof query.month);
|
console.log(typeof query.month);
|
||||||
@ -248,33 +334,80 @@ async function getData(query) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//daily data
|
//daily data
|
||||||
|
else if (query.day !== undefined) {
|
||||||
|
ormQuery = {
|
||||||
|
where: sequelize.where(
|
||||||
|
sequelize.fn("DAY", sequelize.col("createdAt")),
|
||||||
|
query.day
|
||||||
|
),
|
||||||
|
...ormQuery,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
//hourly data
|
//hourly data
|
||||||
|
else if (query.hour !== undefined) {
|
||||||
|
ormQuery = {
|
||||||
|
where: sequelize.where(
|
||||||
|
sequelize.fn("HOUR", sequelize.col("createdAt")),
|
||||||
|
query.hour
|
||||||
|
),
|
||||||
|
...ormQuery,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
//get data by year by specific sensor
|
//minute data
|
||||||
|
else if (query.minute !== undefined) {
|
||||||
//get by month by specific sensor
|
ormQuery = {
|
||||||
|
where: sequelize.where(
|
||||||
//get by week by specific sensor
|
sequelize.fn("MINUTE", sequelize.col("createdAt")),
|
||||||
|
query.minute
|
||||||
//get by daily by specific sensor
|
),
|
||||||
|
...ormQuery,
|
||||||
//get by hourly by specific sensor
|
};
|
||||||
|
} else if (query.sensorid !== undefined) {
|
||||||
//get data by year by specific location
|
ormQuery = {
|
||||||
|
where: {
|
||||||
//get by month by specific location
|
sensorid: query.sensorid,
|
||||||
|
},
|
||||||
//get by week by specific location
|
...ormQuery,
|
||||||
|
};
|
||||||
//get by daily by specific location
|
} else if (query.locationid !== undefined) {
|
||||||
|
ormQuery = {
|
||||||
//get by hourly by specific location
|
where: {
|
||||||
|
locationid: query.locationid,
|
||||||
|
},
|
||||||
|
...ormQuery,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
//get specific data like psi or wtv
|
//get specific data like psi or wtv
|
||||||
|
|
||||||
|
//range of date values
|
||||||
|
|
||||||
|
if (!whereClause) {
|
||||||
return await sensorDataModel.findAll(ormQuery);
|
return await sensorDataModel.findAll(ormQuery);
|
||||||
|
} else {
|
||||||
|
console.log(whereClause);
|
||||||
|
console.log(whereNest);
|
||||||
|
console.log(query);
|
||||||
|
console.log(ormQuery);
|
||||||
|
return await sensorDataModel.findAll({
|
||||||
|
//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],
|
||||||
|
},
|
||||||
|
|
||||||
|
//only use where clause to lookup based on condition that i put into whereClause
|
||||||
|
//where: whereClause,
|
||||||
|
...ormQuery,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getLocation,
|
getLocation,
|
||||||
|
@ -69,7 +69,4 @@ module.exports = {
|
|||||||
isMacAddress,
|
isMacAddress,
|
||||||
isJson,
|
isJson,
|
||||||
};
|
};
|
||||||
/*
|
|
||||||
isMACAddress(str [, options])
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
@ -81,6 +81,16 @@ router.get("/data", async (req, res, next) => {
|
|||||||
//weekly
|
//weekly
|
||||||
week: req.query.week,
|
week: req.query.week,
|
||||||
|
|
||||||
|
//daily
|
||||||
|
day: req.query.day,
|
||||||
|
|
||||||
|
//hourly
|
||||||
|
hour: req.query.hour,
|
||||||
|
|
||||||
|
//minute
|
||||||
|
minute: req.query.minute,
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const data = await getData(query);
|
const data = await getData(query);
|
||||||
|
27
api.MD
27
api.MD
@ -137,29 +137,20 @@ month = 1 or jan
|
|||||||
//get by week
|
//get by week
|
||||||
curl http://localhost/api/v0/sensor-data/data?week=1
|
curl http://localhost/api/v0/sensor-data/data?week=1
|
||||||
|
|
||||||
|
week = 1 or wtv
|
||||||
|
|
||||||
//get by daily
|
//get by daily
|
||||||
|
curl http://localhost/api/v0/sensor-data/data?day=1
|
||||||
|
|
||||||
|
day = 1 or wtv
|
||||||
|
|
||||||
//get by hourly
|
//get by hourly
|
||||||
|
http://localhost/api/v0/sensor-data/data?hour=1
|
||||||
|
|
||||||
//get data by year by specific sensor
|
Hour = 1 or wtv
|
||||||
|
|
||||||
//get by month by specific sensor
|
|
||||||
|
|
||||||
//get by week by specific sensor
|
|
||||||
|
|
||||||
//get by daily by specific sensor
|
|
||||||
|
|
||||||
//get by hourly by specific sensor
|
|
||||||
|
|
||||||
|
|
||||||
//get data by year by specific location
|
//strong optional chaining
|
||||||
|
curl 'http://localhost/api/v0/sensor-data/data?year=2023&month=1&week=1&day=1&sensorid=1&locationid=1'
|
||||||
//get by month by specific location
|
|
||||||
|
|
||||||
//get by week by specific location
|
|
||||||
|
|
||||||
//get by daily by specific location
|
|
||||||
|
|
||||||
//get by hourly by specific location
|
|
||||||
|
|
||||||
//get specific data
|
//get specific data
|
Loading…
x
Reference in New Issue
Block a user