Skip to content

Commit

Permalink
update predict method
Browse files Browse the repository at this point in the history
  • Loading branch information
Nguyen-Duc-Khai committed Jan 16, 2024
1 parent b58a9fb commit 5a0006c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
4 changes: 2 additions & 2 deletions backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const startAgenda = async () => {
// Local
// await agenda.every(CronTime.everyDayAt(23), "retrieveDailyAqi");

await agenda.every(CronTime.everyDayAt(16, 5), "retrieveDailyAqi");
await agenda.every(CronTime.everyHour(), "getHourlyAqi");
await agenda.every(CronTime.everyDayAt(16, 55), "retrieveDailyAqi");
// await agenda.every(CronTime.everyHour(), "getHourlyAqi");
};

mongoose
Expand Down
63 changes: 58 additions & 5 deletions backend/src/jobs/definitions/retrieveDailyAqi.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const { aqiApiToken, predictionServiceUrl } = require("../../config/config");
const axios = require("axios");
const logger = require("../../config/logger");
const HourlyAQIModel = require("../../models/HourlyAQI");

const AirQualityModel = require("../../models/AirQuality");
const { mapServerTimeToVnTime } = require("../../helper/serverDate");

const retrieveDailyAqi = async (agenda) => {
try {
Expand All @@ -10,10 +12,63 @@ const retrieveDailyAqi = async (agenda) => {
"retrieveDailyAqi",
async function (job, done) {
try {
const hourlyAqi = await HourlyAQIModel.find();
const { offsetToday } = mapServerTimeToVnTime();
const gmt7Offset = 7 * 60 * 60 * 1000;
var offsetNextDay = new Date(offsetToday.getTime() + 24 * 3600 * 1000)
.toISOString()
.replace(/T(.+)/g, "T00:00:00.000Z");

let hourlyAqi = await AirQualityModel.aggregate([
{
$addFields: {
// Convert the createdAt field to GMT+7 by adding the offset
createdAtGMT7: {
$add: ["$createdAt", gmt7Offset],
},
},
},
{
$match: {
createdAtGMT7: {
$gte: new Date(
offsetToday
.toISOString()
.replace(/T(.+)/g, "T00:00:00.000Z")
),
$lt: new Date(offsetNextDay),
},
},
},
{
$project: {
_id: 1,
aqi: 1,
humidity: 1,
temperature: 1,
co: 1,
co2: 1,
tvoc: 1,
o3: 1,
pm25: 1,
calc_aqi: 1,
createdAt: "$createdAtGMT7",
},
},
]).exec();

console.log(hourlyAqi);

hourlyAqi = hourlyAqi.map((x) => {
return {
aqi: x.calc_aqi ?? x.aqi,
dateTime: `${x.createdAt.toISOString()}`,
};
});

console.log(hourlyAqi);

const compositeAqiObject = hourlyAqi.reduce((prev, curr) =>
prev > curr ? prev : curr
prev.aqi > curr.aqi ? prev : curr
);

var requestBody = {
Expand All @@ -29,8 +84,6 @@ const retrieveDailyAqi = async (agenda) => {
data: requestBody,
});

await HourlyAQIModel.deleteMany();

done();
} catch (error) {
console.error(error);
Expand Down
2 changes: 1 addition & 1 deletion client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ <h4 style="margin:6px 0">Solutions: </h4>
<div class="divider"></div>

<section class="forecast section">
<p data-i18n="forecast.title" style="margin-bottom: 1.5em;">Forecasting</p>
<p data-i18n="forecast.title" style="margin-bottom: 1.5em;">Forecast</p>

<div class="inner-wrap">
<div class="forecast-box">
Expand Down
2 changes: 1 addition & 1 deletion iot/arduino/arduino.ino
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void measureOpticalDustSensor() {
Serial.print("PM2.5 voltage: ");
Serial.println(voltage);

pm25 = 170 * voltage - 0.1; // unit: ug/m3
pm25 = (170 * voltage - 0.1) / 2; // unit: ug/m3

Serial.print("PM2.5: ");
Serial.print(pm25);
Expand Down
2 changes: 1 addition & 1 deletion prediction-service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
best_model = load_model(best_model_filename)
best_scaler = joblib.load(best_scaler_filename)

MONGODB_URL="mongodb+srv://admin:[email protected]/aircheckerdb?retryWrites=true&w=majority"
MONGODB_URL="mongodb://localhost:27017/aircheckerdb"
# MONGODB_URL="mongodb://localhost:27017/aircheckerdb"

# Connect to MongoDB
Expand Down

0 comments on commit 5a0006c

Please sign in to comment.