commit
a8a424968e
@ -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");
|
||||
var moment = require("moment");
|
||||
const { Op } = 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
|
||||
@ -173,6 +173,9 @@ async function getSensorDataById(id) {
|
||||
|
||||
async function getData(query) {
|
||||
let ormQuery = {};
|
||||
let whereClause = {};
|
||||
let whereNest = {};
|
||||
|
||||
if (query.limit !== undefined && query.order !== undefined)
|
||||
ormQuery = {
|
||||
limit: parseInt(query.limit),
|
||||
@ -180,27 +183,110 @@ async function getData(query) {
|
||||
order: [["createdAt", query.order.toUpperCase()]],
|
||||
...ormQuery,
|
||||
};
|
||||
else if (query.sensorid !== undefined) {
|
||||
ormQuery = {
|
||||
where: {
|
||||
sensorid: query.sensorid,
|
||||
},
|
||||
...ormQuery,
|
||||
};
|
||||
} else if (query.locationid !== undefined) {
|
||||
ormQuery = {
|
||||
where: {
|
||||
locationid: query.locationid,
|
||||
},
|
||||
...ormQuery,
|
||||
};
|
||||
} else if (query.year !== 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 = {
|
||||
limit: parseInt(query.limit),
|
||||
order: [["createdAt", query.order.toUpperCase()]],
|
||||
...ormQuery,
|
||||
};
|
||||
|
||||
if (query.year) {
|
||||
whereClause.year = sequelize.where(
|
||||
sequelize.fn("YEAR", sequelize.col("createdAt")),
|
||||
query.year
|
||||
);
|
||||
}
|
||||
|
||||
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 {
|
||||
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 = {
|
||||
//handle all year, month
|
||||
where: sequelize.where(
|
||||
sequelize.fn("YEAR", sequelize.col("createdAt")),
|
||||
query.year
|
||||
),
|
||||
...ormQuery,
|
||||
};
|
||||
} else if (query.month !== undefined) {
|
||||
console.log(typeof query.month);
|
||||
@ -248,33 +334,80 @@ async function getData(query) {
|
||||
}
|
||||
|
||||
//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,
|
||||
};
|
||||
}
|
||||
|
||||
//get data by year by specific sensor
|
||||
|
||||
//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
|
||||
|
||||
//get by month by specific location
|
||||
|
||||
//get by week by specific location
|
||||
|
||||
//get by daily by specific location
|
||||
|
||||
//get by hourly by specific location
|
||||
//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,
|
||||
};
|
||||
}
|
||||
|
||||
//get specific data like psi or wtv
|
||||
|
||||
return await sensorDataModel.findAll(ormQuery);
|
||||
//range of date values
|
||||
|
||||
if (!whereClause) {
|
||||
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 = {
|
||||
getLocation,
|
||||
|
@ -67,9 +67,6 @@ module.exports = {
|
||||
isAlphaNumericwithSpaces,
|
||||
isAlphaNumericWithSpacesAndDash,
|
||||
isMacAddress,
|
||||
isJson,
|
||||
isJson,
|
||||
};
|
||||
/*
|
||||
isMACAddress(str [, options])
|
||||
|
||||
*/
|
||||
|
@ -81,6 +81,16 @@ router.get("/data", async (req, res, next) => {
|
||||
//weekly
|
||||
week: req.query.week,
|
||||
|
||||
//daily
|
||||
day: req.query.day,
|
||||
|
||||
//hourly
|
||||
hour: req.query.hour,
|
||||
|
||||
//minute
|
||||
minute: req.query.minute,
|
||||
|
||||
|
||||
};
|
||||
|
||||
const data = await getData(query);
|
||||
|
27
api.MD
27
api.MD
@ -137,29 +137,20 @@ month = 1 or jan
|
||||
//get by week
|
||||
curl http://localhost/api/v0/sensor-data/data?week=1
|
||||
|
||||
week = 1 or wtv
|
||||
|
||||
//get by daily
|
||||
curl http://localhost/api/v0/sensor-data/data?day=1
|
||||
|
||||
day = 1 or wtv
|
||||
|
||||
//get by hourly
|
||||
http://localhost/api/v0/sensor-data/data?hour=1
|
||||
|
||||
//get data by year by specific sensor
|
||||
|
||||
//get by month by specific sensor
|
||||
|
||||
//get by week by specific sensor
|
||||
|
||||
//get by daily by specific sensor
|
||||
|
||||
//get by hourly by specific sensor
|
||||
Hour = 1 or wtv
|
||||
|
||||
|
||||
//get data by year by specific location
|
||||
|
||||
//get by month by specific location
|
||||
|
||||
//get by week by specific location
|
||||
|
||||
//get by daily by specific location
|
||||
|
||||
//get by hourly 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 specific data
|
Loading…
x
Reference in New Issue
Block a user