From d96036c3d5f9e8e8aa03eeb45830bef52a03ca2f Mon Sep 17 00:00:00 2001 From: baalmart Date: Sun, 17 Nov 2024 16:34:58 +0300 Subject: [PATCH 1/3] centralising the AQI logic --- src/device-registry/models/Event.js | 438 ++++++++++++++++++++------ src/device-registry/models/Reading.js | 31 ++ 2 files changed, 369 insertions(+), 100 deletions(-) diff --git a/src/device-registry/models/Event.js b/src/device-registry/models/Event.js index 682031e718..496e905982 100644 --- a/src/device-registry/models/Event.js +++ b/src/device-registry/models/Event.js @@ -856,6 +856,16 @@ async function fetchData(model, filter) { "site_image.airqloud_id": 0, }) .project(projection) + .addFields({ + aqi_ranges: { + good: { min: 0, max: 9.0 }, + moderate: { min: 9.1, max: 35.4 }, + u4sg: { min: 35.5, max: 55.4 }, + unhealthy: { min: 55.5, max: 125.4 }, + very_unhealthy: { min: 125.5, max: 225.4 }, + hazardous: { min: 225.5, max: null }, + }, + }) .facet({ total: [{ $count: "device" }], data: [ @@ -868,8 +878,8 @@ async function fetchData(model, filter) { { case: { $and: [ - { $gte: ["$pm2_5.value", 0] }, - { $lte: ["$pm2_5.value", 9.0] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.good.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.good.max"] }, ], }, then: "00e400", @@ -877,8 +887,12 @@ async function fetchData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 9.0] }, - { $lte: ["$pm2_5.value", 35.4] }, + { + $gte: ["$pm2_5.value", "$aqi_ranges.moderate.min"], + }, + { + $lte: ["$pm2_5.value", "$aqi_ranges.moderate.max"], + }, ], }, then: "ffff00", @@ -886,8 +900,8 @@ async function fetchData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 35.4] }, - { $lte: ["$pm2_5.value", 55.4] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.u4sg.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.u4sg.max"] }, ], }, then: "ff7e00", @@ -895,8 +909,12 @@ async function fetchData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 55.4] }, - { $lte: ["$pm2_5.value", 125.4] }, + { + $gte: ["$pm2_5.value", "$aqi_ranges.unhealthy.min"], + }, + { + $lte: ["$pm2_5.value", "$aqi_ranges.unhealthy.max"], + }, ], }, then: "ff0000", @@ -904,14 +922,26 @@ async function fetchData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 125.4] }, - { $lte: ["$pm2_5.value", 225.4] }, + { + $gte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.min", + ], + }, + { + $lte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.max", + ], + }, ], }, then: "8f3f97", }, { - case: { $gt: ["$pm2_5.value", 225.4] }, + case: { + $gte: ["$pm2_5.value", "$aqi_ranges.hazardous.max"], + }, then: "7e0023", }, ], @@ -925,8 +955,8 @@ async function fetchData(model, filter) { { case: { $and: [ - { $gte: ["$pm2_5.value", 0] }, - { $lte: ["$pm2_5.value", 9.0] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.good.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.good.max"] }, ], }, then: "Good", @@ -934,8 +964,12 @@ async function fetchData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 9.0] }, - { $lte: ["$pm2_5.value", 35.4] }, + { + $gte: ["$pm2_5.value", "$aqi_ranges.moderate.min"], + }, + { + $lte: ["$pm2_5.value", "$aqi_ranges.moderate.max"], + }, ], }, then: "Moderate", @@ -943,8 +977,8 @@ async function fetchData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 35.4] }, - { $lte: ["$pm2_5.value", 55.4] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.u4sg.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.u4sg.max"] }, ], }, then: "Unhealthy for Sensitive Groups", @@ -952,8 +986,12 @@ async function fetchData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 55.4] }, - { $lte: ["$pm2_5.value", 125.4] }, + { + $gte: ["$pm2_5.value", "$aqi_ranges.unhealthy.min"], + }, + { + $lte: ["$pm2_5.value", "$aqi_ranges.unhealthy.max"], + }, ], }, then: "Unhealthy", @@ -961,15 +999,29 @@ async function fetchData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 125.4] }, - { $lte: ["$pm2_5.value", 225.4] }, + { + $gte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.min", + ], + }, + { + $lte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.max", + ], + }, ], }, then: "Very Unhealthy", }, { case: { - $and: [{ $gt: ["$pm2_5.value", 225.4] }], + $and: [ + { + $gte: ["$pm2_5.value", "$aqi_ranges.hazardous.min"], + }, + ], }, then: "Hazardous", }, @@ -983,8 +1035,8 @@ async function fetchData(model, filter) { { case: { $and: [ - { $gte: ["$pm2_5.value", 0] }, - { $lte: ["$pm2_5.value", 9.0] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.good.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.good.max"] }, ], }, then: "Green", @@ -992,8 +1044,12 @@ async function fetchData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 9.0] }, - { $lte: ["$pm2_5.value", 35.4] }, + { + $gte: ["$pm2_5.value", "$aqi_ranges.moderate.min"], + }, + { + $lte: ["$pm2_5.value", "$aqi_ranges.moderate.max"], + }, ], }, then: "Yellow", @@ -1001,8 +1057,8 @@ async function fetchData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 35.4] }, - { $lte: ["$pm2_5.value", 55.4] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.u4sg.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.u4sg.max"] }, ], }, then: "Orange", @@ -1010,8 +1066,12 @@ async function fetchData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 55.4] }, - { $lte: ["$pm2_5.value", 125.4] }, + { + $gte: ["$pm2_5.value", "$aqi_ranges.unhealthy.min"], + }, + { + $lte: ["$pm2_5.value", "$aqi_ranges.unhealthy.max"], + }, ], }, then: "Red", @@ -1019,15 +1079,29 @@ async function fetchData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 125.4] }, - { $lte: ["$pm2_5.value", 225.4] }, + { + $gte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.min", + ], + }, + { + $lte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.max", + ], + }, ], }, then: "Purple", }, { case: { - $and: [{ $gt: ["$pm2_5.value", 225.4] }], + $and: [ + { + $gte: ["$pm2_5.value", "$aqi_ranges.hazardous.min"], + }, + ], }, then: "Maroon", }, @@ -1035,6 +1109,7 @@ async function fetchData(model, filter) { default: "Unknown", }, }, + aqi_ranges: "$aqi_ranges", }, }, ], @@ -1466,6 +1541,16 @@ async function signalData(model, filter) { "site_image.airqloud_id": 0, }) .project(projection) + .addFields({ + aqi_ranges: { + good: { min: 0, max: 9.0 }, + moderate: { min: 9.1, max: 35.4 }, + u4sg: { min: 35.5, max: 55.4 }, + unhealthy: { min: 55.5, max: 125.4 }, + very_unhealthy: { min: 125.5, max: 225.4 }, + hazardous: { min: 225.5, max: null }, + }, + }) .facet({ total: [{ $count: "device" }], data: [ @@ -1478,8 +1563,8 @@ async function signalData(model, filter) { { case: { $and: [ - { $gte: ["$pm2_5.value", 0] }, - { $lte: ["$pm2_5.value", 9.0] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.good.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.good.max"] }, ], }, then: "00e400", @@ -1487,8 +1572,8 @@ async function signalData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 9.0] }, - { $lte: ["$pm2_5.value", 35.4] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.moderate.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.moderate.max"] }, ], }, then: "ffff00", @@ -1496,8 +1581,8 @@ async function signalData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 35.4] }, - { $lte: ["$pm2_5.value", 55.4] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.u4sg.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.u4sg.max"] }, ], }, then: "ff7e00", @@ -1505,8 +1590,8 @@ async function signalData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 55.4] }, - { $lte: ["$pm2_5.value", 125.4] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.unhealthy.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.unhealthy.max"] }, ], }, then: "ff0000", @@ -1514,14 +1599,26 @@ async function signalData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 125.4] }, - { $lte: ["$pm2_5.value", 225.4] }, + { + $gte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.min", + ], + }, + { + $lte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.max", + ], + }, ], }, then: "8f3f97", }, { - case: { $gt: ["$pm2_5.value", 225.4] }, + case: { + $gte: ["$pm2_5.value", "$aqi_ranges.hazardous.min"], + }, then: "7e0023", }, ], @@ -1535,8 +1632,8 @@ async function signalData(model, filter) { { case: { $and: [ - { $gte: ["$pm2_5.value", 0] }, - { $lte: ["$pm2_5.value", 9.0] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.good.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.good.max"] }, ], }, then: "Good", @@ -1544,8 +1641,8 @@ async function signalData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 9.0] }, - { $lte: ["$pm2_5.value", 35.4] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.moderate.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.moderate.max"] }, ], }, then: "Moderate", @@ -1553,8 +1650,8 @@ async function signalData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 35.4] }, - { $lte: ["$pm2_5.value", 55.4] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.u4sg.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.u4sg.max"] }, ], }, then: "Unhealthy for Sensitive Groups", @@ -1562,8 +1659,8 @@ async function signalData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 55.4] }, - { $lte: ["$pm2_5.value", 125.4] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.unhealthy.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.unhealthy.max"] }, ], }, then: "Unhealthy", @@ -1571,15 +1668,27 @@ async function signalData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 125.4] }, - { $lte: ["$pm2_5.value", 225.4] }, + { + $gte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.min", + ], + }, + { + $lte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.max", + ], + }, ], }, then: "Very Unhealthy", }, { case: { - $and: [{ $gt: ["$pm2_5.value", 225.4] }], + $and: [ + { $gte: ["$pm2_5.value", "$aqi_ranges.hazardous.min"] }, + ], }, then: "Hazardous", }, @@ -1593,8 +1702,8 @@ async function signalData(model, filter) { { case: { $and: [ - { $gte: ["$pm2_5.value", 0] }, - { $lte: ["$pm2_5.value", 9.0] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.good.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.good.max"] }, ], }, then: "Green", @@ -1602,8 +1711,8 @@ async function signalData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 9.0] }, - { $lte: ["$pm2_5.value", 35.4] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.moderate.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.moderate.max"] }, ], }, then: "Yellow", @@ -1611,8 +1720,8 @@ async function signalData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 35.4] }, - { $lte: ["$pm2_5.value", 55.4] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.u4sg.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.u4sg.max"] }, ], }, then: "Orange", @@ -1620,8 +1729,8 @@ async function signalData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 55.4] }, - { $lte: ["$pm2_5.value", 125.4] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.unhealthy.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.unhealthy.max"] }, ], }, then: "Red", @@ -1629,15 +1738,27 @@ async function signalData(model, filter) { { case: { $and: [ - { $gt: ["$pm2_5.value", 125.4] }, - { $lte: ["$pm2_5.value", 225.4] }, + { + $gte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.min", + ], + }, + { + $lte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.max", + ], + }, ], }, then: "Purple", }, { case: { - $and: [{ $gt: ["$pm2_5.value", 225.4] }], + $and: [ + { $gte: ["$pm2_5.value", "$aqi_ranges.hazardous.min"] }, + ], }, then: "Maroon", }, @@ -1645,6 +1766,7 @@ async function signalData(model, filter) { default: "Unknown", }, }, + aqi_ranges: "$aqi_ranges", }, }, ], @@ -2106,6 +2228,16 @@ eventSchema.statics.list = async function( "site_image.airqloud_id": 0, }) .project(projection) + .addFields({ + aqi_ranges: { + good: { min: 0, max: 9.0 }, + moderate: { min: 9.1, max: 35.4 }, + u4sg: { min: 35.5, max: 55.4 }, + unhealthy: { min: 55.5, max: 125.4 }, + very_unhealthy: { min: 125.5, max: 225.4 }, + hazardous: { min: 225.5, max: null }, + }, + }) .facet({ total: [{ $count: "device" }], data: [ @@ -2118,8 +2250,8 @@ eventSchema.statics.list = async function( { case: { $and: [ - { $gte: ["$pm2_5.value", 0] }, - { $lte: ["$pm2_5.value", 9.0] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.good.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.good.max"] }, ], }, then: "00e400", @@ -2127,8 +2259,18 @@ eventSchema.statics.list = async function( { case: { $and: [ - { $gt: ["$pm2_5.value", 9.0] }, - { $lte: ["$pm2_5.value", 35.4] }, + { + $gte: [ + "$pm2_5.value", + "$aqi_ranges.moderate.min", + ], + }, + { + $lte: [ + "$pm2_5.value", + "$aqi_ranges.moderate.max", + ], + }, ], }, then: "ffff00", @@ -2136,8 +2278,8 @@ eventSchema.statics.list = async function( { case: { $and: [ - { $gt: ["$pm2_5.value", 35.4] }, - { $lte: ["$pm2_5.value", 55.4] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.u4sg.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.u4sg.max"] }, ], }, then: "ff7e00", @@ -2145,8 +2287,18 @@ eventSchema.statics.list = async function( { case: { $and: [ - { $gt: ["$pm2_5.value", 55.4] }, - { $lte: ["$pm2_5.value", 125.4] }, + { + $gte: [ + "$pm2_5.value", + "$aqi_ranges.unhealthy.min", + ], + }, + { + $lte: [ + "$pm2_5.value", + "$aqi_ranges.unhealthy.max", + ], + }, ], }, then: "ff0000", @@ -2154,29 +2306,40 @@ eventSchema.statics.list = async function( { case: { $and: [ - { $gt: ["$pm2_5.value", 125.4] }, - { $lte: ["$pm2_5.value", 225.4] }, + { + $gte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.min", + ], + }, + { + $lte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.max", + ], + }, ], }, then: "8f3f97", }, { - case: { $gt: ["$pm2_5.value", 225.4] }, + case: { + $gte: ["$pm2_5.value", "$aqi_ranges.hazardous.min"], + }, then: "7e0023", }, ], default: "Unknown", }, }, - aqi_category: { $switch: { branches: [ { case: { $and: [ - { $gte: ["$pm2_5.value", 0] }, - { $lte: ["$pm2_5.value", 9.0] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.good.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.good.max"] }, ], }, then: "Good", @@ -2184,8 +2347,18 @@ eventSchema.statics.list = async function( { case: { $and: [ - { $gt: ["$pm2_5.value", 9.0] }, - { $lte: ["$pm2_5.value", 35.4] }, + { + $gte: [ + "$pm2_5.value", + "$aqi_ranges.moderate.min", + ], + }, + { + $lte: [ + "$pm2_5.value", + "$aqi_ranges.moderate.max", + ], + }, ], }, then: "Moderate", @@ -2193,8 +2366,8 @@ eventSchema.statics.list = async function( { case: { $and: [ - { $gt: ["$pm2_5.value", 35.4] }, - { $lte: ["$pm2_5.value", 55.4] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.u4sg.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.u4sg.max"] }, ], }, then: "Unhealthy for Sensitive Groups", @@ -2202,8 +2375,18 @@ eventSchema.statics.list = async function( { case: { $and: [ - { $gt: ["$pm2_5.value", 55.4] }, - { $lte: ["$pm2_5.value", 125.4] }, + { + $gte: [ + "$pm2_5.value", + "$aqi_ranges.unhealthy.min", + ], + }, + { + $lte: [ + "$pm2_5.value", + "$aqi_ranges.unhealthy.max", + ], + }, ], }, then: "Unhealthy", @@ -2211,15 +2394,32 @@ eventSchema.statics.list = async function( { case: { $and: [ - { $gt: ["$pm2_5.value", 125.4] }, - { $lte: ["$pm2_5.value", 225.4] }, + { + $gte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.min", + ], + }, + { + $lte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.max", + ], + }, ], }, then: "Very Unhealthy", }, { case: { - $and: [{ $gt: ["$pm2_5.value", 225.4] }], + $and: [ + { + $gte: [ + "$pm2_5.value", + "$aqi_ranges.hazardous.min", + ], + }, + ], }, then: "Hazardous", }, @@ -2233,8 +2433,8 @@ eventSchema.statics.list = async function( { case: { $and: [ - { $gte: ["$pm2_5.value", 0] }, - { $lte: ["$pm2_5.value", 9.0] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.good.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.good.max"] }, ], }, then: "Green", @@ -2242,8 +2442,18 @@ eventSchema.statics.list = async function( { case: { $and: [ - { $gt: ["$pm2_5.value", 9.0] }, - { $lte: ["$pm2_5.value", 35.4] }, + { + $gte: [ + "$pm2_5.value", + "$aqi_ranges.moderate.min", + ], + }, + { + $lte: [ + "$pm2_5.value", + "$aqi_ranges.moderate.max", + ], + }, ], }, then: "Yellow", @@ -2251,8 +2461,8 @@ eventSchema.statics.list = async function( { case: { $and: [ - { $gt: ["$pm2_5.value", 35.4] }, - { $lte: ["$pm2_5.value", 55.4] }, + { $gte: ["$pm2_5.value", "$aqi_ranges.u4sg.min"] }, + { $lte: ["$pm2_5.value", "$aqi_ranges.u4sg.max"] }, ], }, then: "Orange", @@ -2260,8 +2470,18 @@ eventSchema.statics.list = async function( { case: { $and: [ - { $gt: ["$pm2_5.value", 55.4] }, - { $lte: ["$pm2_5.value", 125.4] }, + { + $gte: [ + "$pm2_5.value", + "$aqi_ranges.unhealthy.min", + ], + }, + { + $lte: [ + "$pm2_5.value", + "$aqi_ranges.unhealthy.max", + ], + }, ], }, then: "Red", @@ -2269,15 +2489,32 @@ eventSchema.statics.list = async function( { case: { $and: [ - { $gt: ["$pm2_5.value", 125.4] }, - { $lte: ["$pm2_5.value", 225.4] }, + { + $gte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.min", + ], + }, + { + $lte: [ + "$pm2_5.value", + "$aqi_ranges.very_unhealthy.max", + ], + }, ], }, then: "Purple", }, { case: { - $and: [{ $gt: ["$pm2_5.value", 225.4] }], + $and: [ + { + $gte: [ + "$pm2_5.value", + "$aqi_ranges.hazardous.min", + ], + }, + ], }, then: "Maroon", }, @@ -2285,6 +2522,7 @@ eventSchema.statics.list = async function( default: "Unknown", }, }, + aqi_ranges: "$aqi_ranges", }, }, ], diff --git a/src/device-registry/models/Reading.js b/src/device-registry/models/Reading.js index 8e42727c89..01b696ad24 100644 --- a/src/device-registry/models/Reading.js +++ b/src/device-registry/models/Reading.js @@ -46,6 +46,36 @@ const SiteDetailsSchema = new Schema( { _id: false } ); +const AqiRangeSchema = new Schema( + { + good: { + min: { type: Number, required: true }, + max: { type: Number, required: true }, + }, + moderate: { + min: { type: Number, required: true }, + max: { type: Number, required: true }, + }, + u4sg: { + min: { type: Number, required: true }, + max: { type: Number, required: true }, + }, + unhealthy: { + min: { type: Number, required: true }, + max: { type: Number, required: true }, + }, + very_unhealthy: { + min: { type: Number, required: true }, + max: { type: Number, required: true }, + }, + hazardous: { + min: { type: Number, required: true }, + max: { type: Number }, // max can be null + }, + }, + { _id: false } +); + const ReadingsSchema = new Schema( { device: String, @@ -59,6 +89,7 @@ const ReadingsSchema = new Schema( frequency: String, no2: { value: Number }, siteDetails: SiteDetailsSchema, + aqi_ranges: AqiRangeSchema, timeDifferenceHours: Number, aqi_color: String, aqi_category: String, From afd8d49aaaa82c315877fb457664ea8873524c71 Mon Sep 17 00:00:00 2001 From: baalmart Date: Sun, 17 Nov 2024 16:42:35 +0300 Subject: [PATCH 2/3] adding aqi_ranges to the JSON returned --- src/device-registry/models/Reading.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/device-registry/models/Reading.js b/src/device-registry/models/Reading.js index 01b696ad24..d5e26ae7ed 100644 --- a/src/device-registry/models/Reading.js +++ b/src/device-registry/models/Reading.js @@ -133,6 +133,7 @@ ReadingsSchema.methods = { no2: this.no2, siteDetails: this.siteDetails, timeDifferenceHours: this.timeDifferenceHours, + aqi_ranges: this.aqi_ranges, aqi_color: this.aqi_color, aqi_category: this.aqi_category, aqi_color_name: this.aqi_color_name, From c488c5c29406f4280fc14ec447afa3a729e3b674 Mon Sep 17 00:00:00 2001 From: baalmart Date: Sun, 17 Nov 2024 16:47:36 +0300 Subject: [PATCH 3/3] just reducing repeatitions --- src/device-registry/models/Event.js | 38 ++++++++++------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/src/device-registry/models/Event.js b/src/device-registry/models/Event.js index 496e905982..b2cdec92aa 100644 --- a/src/device-registry/models/Event.js +++ b/src/device-registry/models/Event.js @@ -23,6 +23,15 @@ const DEFAULT_SKIP = 0; const DEFAULT_PAGE = 1; const UPTIME_CHECK_THRESHOLD = 168; +const AQI_RANGES = { + good: { min: 0, max: 9.0 }, + moderate: { min: 9.1, max: 35.4 }, + u4sg: { min: 35.5, max: 55.4 }, + unhealthy: { min: 55.5, max: 125.4 }, + very_unhealthy: { min: 125.5, max: 225.4 }, + hazardous: { min: 225.5, max: null }, +}; + const valueSchema = new Schema({ time: { type: Date, @@ -857,14 +866,7 @@ async function fetchData(model, filter) { }) .project(projection) .addFields({ - aqi_ranges: { - good: { min: 0, max: 9.0 }, - moderate: { min: 9.1, max: 35.4 }, - u4sg: { min: 35.5, max: 55.4 }, - unhealthy: { min: 55.5, max: 125.4 }, - very_unhealthy: { min: 125.5, max: 225.4 }, - hazardous: { min: 225.5, max: null }, - }, + aqi_ranges: AQI_RANGES, }) .facet({ total: [{ $count: "device" }], @@ -940,7 +942,7 @@ async function fetchData(model, filter) { }, { case: { - $gte: ["$pm2_5.value", "$aqi_ranges.hazardous.max"], + $gte: ["$pm2_5.value", "$aqi_ranges.hazardous.min"], }, then: "7e0023", }, @@ -1542,14 +1544,7 @@ async function signalData(model, filter) { }) .project(projection) .addFields({ - aqi_ranges: { - good: { min: 0, max: 9.0 }, - moderate: { min: 9.1, max: 35.4 }, - u4sg: { min: 35.5, max: 55.4 }, - unhealthy: { min: 55.5, max: 125.4 }, - very_unhealthy: { min: 125.5, max: 225.4 }, - hazardous: { min: 225.5, max: null }, - }, + aqi_ranges: AQI_RANGES, }) .facet({ total: [{ $count: "device" }], @@ -2229,14 +2224,7 @@ eventSchema.statics.list = async function( }) .project(projection) .addFields({ - aqi_ranges: { - good: { min: 0, max: 9.0 }, - moderate: { min: 9.1, max: 35.4 }, - u4sg: { min: 35.5, max: 55.4 }, - unhealthy: { min: 55.5, max: 125.4 }, - very_unhealthy: { min: 125.5, max: 225.4 }, - hazardous: { min: 225.5, max: null }, - }, + aqi_ranges: AQI_RANGES, }) .facet({ total: [{ $count: "device" }],