From 1c36f3006f04374843c5286782a107deabc51a19 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 25 Jul 2024 14:25:26 +0530 Subject: [PATCH 1/3] added waterlevel percentage in tanks schema --- src/controllers/tanksController.js | 31 +++++++- src/models/tanks.js | 123 +++++++++++++++-------------- 2 files changed, 92 insertions(+), 62 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 6b633ec7..b9cdc799 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -318,9 +318,38 @@ exports.getTanklevels = async (req, reply) => { updated_data.forEach((tank) => { const waterlevel = parseInt(tank.waterlevel.replace(/,/g, ''), 10); const capacity = parseInt(tank.capacity.replace(/,/g, ''), 10); + const waterlevelPercentage = ((waterlevel / capacity) * 100).toFixed(2); + tank.waterlevelPercentage = waterlevelPercentage; // Add water level percentage to each tank object console.log(`Processing tank: ${tank.tankName}`); - console.log(`Type of Water: ${tank.typeOfWater}, Location: ${tank.tankLocation}, Waterlevel: ${waterlevel}, Capacity: ${capacity}`); + console.log(`Type of Water: ${tank.typeOfWater}, Location: ${tank.tankLocation}, Waterlevel: ${waterlevel}, Capacity: ${capacity}, Waterlevel Percentage: ${waterlevelPercentage}%`); + + // Calculate and add water level percentages for inputConnections + if (tank.connections.inputConnections) { + tank.connections.inputConnections.forEach(inputConnection => { + if (inputConnection.water_level && inputConnection.capacity) { + const inputWaterLevel = parseInt(inputConnection.water_level.replace(/,/g, ''), 10); + const inputCapacity = parseInt(inputConnection.capacity.replace(/,/g, ''), 10); + inputConnection.waterlevelPercentage = ((inputWaterLevel / inputCapacity) * 100).toFixed(2); + } else { + inputConnection.waterlevelPercentage = null; + } + }); + } + + // Calculate and add water level percentages for outputConnections + if (tank.connections.outputConnections) { + tank.connections.outputConnections.forEach(outputConnection => { + if (outputConnection.water_level && outputConnection.capacity) { + const outputWaterLevel = parseInt(outputConnection.water_level.replace(/,/g, ''), 10); + const outputCapacity = parseInt(outputConnection.capacity.replace(/,/g, ''), 10); + outputConnection.waterlevelPercentage = ((outputWaterLevel / outputCapacity) * 100).toFixed(2); + } else { + outputConnection.waterlevelPercentage = null; + } + }); + } + // Summing up the total water levels and capacities if (tank.tankLocation === 'sump' && tank.typeOfWater === 'Drinking Water') { sumSumpDrinkingWater += waterlevel; sumSumpDrinkingWaterCapacity += capacity; diff --git a/src/models/tanks.js b/src/models/tanks.js index d5dc5b16..c0bba543 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -32,67 +32,68 @@ const RoleSchema = new Schema({ name: String }); const tanksSchema = new mongoose.Schema({ - hardwareId: { type: String }, - InstallerId: { type: String, default: null }, - tankhardwareId: { type: String }, - hardwareId_type: { type: String }, - hardwareId_company: { type: String }, - customerId: { type: String, default: null }, - tankName: { type: String, default: null }, - blockName: { type: String, default: null }, - capacity: { type: String, default: "0" }, - height: { type: String, default: "0" }, - tankLocation: { type: String, default: null }, - waterCapacityPerCm:{ type: String, default: "0" }, - typeOfWater: { type: String, default: null }, - waterlevel: { type: String, default: "0" }, - waterlevel_at_midnight:{ type: String,default:"0" }, - total_water_added_from_midnight:{ type: String,default:"0" }, - auto_min_percentage :{ type: String, default: "20" }, - reserved_percentage:{type: String,default:"20"}, - auto_max_percentage :{ type: String, default: "80" }, - - connections: { - source: { type: String }, - inputConnections: [ - { - inputConnections: { type: String }, - input_type: { type: String }, - inputismotor: { type: Boolean, }, - motor_id:{ type: String ,default: null}, - motor_status: { type: String, default: "0" }, - auto_mode:{type:String,default:"inactive"}, - motor_stop_status: { type: String, default: "1" }, - motor_on_type :{ type: String, default: "manual" }, - capacity:{ type: String ,default: null}, - water_level:{ type: String ,default: null}, - manual_threshold_percentage:{type: String, default: "90"}, - manual_threshold_time:{type: String, default: null}, - threshold_type:{type: String, default: "percentage"}, - startTime:{type: String,default:null}, - start_instance_id:{type:String,default:null}, - stopTime:{type: String,default:null} - } - ], - outputConnections: [ - { - outputConnections: { type: String }, - output_type: { type: String }, - outputismotor: { type: Boolean, }, - motor_id:{ type: String ,default: null}, - motor_status: { type: String, default: "0" }, - motor_stop_status: { type: String, default: "1" }, - capacity:{ type: String ,default: null}, - water_level:{ type: String ,default: null}, - manual_threshold_percentage:{type: String, default: "90"}, - manual_threshold_time:{type: String, default: null}, - threshold_type:{type: String, default: "percentage"}, - - } - ] - } - }); - + hardwareId: { type: String }, + InstallerId: { type: String, default: null }, + tankhardwareId: { type: String }, + hardwareId_type: { type: String }, + hardwareId_company: { type: String }, + customerId: { type: String, default: null }, + tankName: { type: String, default: null }, + blockName: { type: String, default: null }, + capacity: { type: String, default: "0" }, + height: { type: String, default: "0" }, + tankLocation: { type: String, default: null }, + waterCapacityPerCm: { type: String, default: "0" }, + typeOfWater: { type: String, default: null }, + waterlevel: { type: String, default: "0" }, + waterlevel_at_midnight: { type: String, default: "0" }, + total_water_added_from_midnight: { type: String, default: "0" }, + auto_min_percentage: { type: String, default: "20" }, + reserved_percentage: { type: String, default: "20" }, + auto_max_percentage: { type: String, default: "80" }, + + connections: { + source: { type: String }, + inputConnections: [ + { + inputConnections: { type: String }, + input_type: { type: String }, + inputismotor: { type: Boolean }, + motor_id: { type: String, default: null }, + motor_status: { type: String, default: "0" }, + auto_mode: { type: String, default: "inactive" }, + motor_stop_status: { type: String, default: "1" }, + motor_on_type: { type: String, default: "manual" }, + capacity: { type: String, default: null }, + water_level: { type: String, default: null }, + manual_threshold_percentage: { type: String, default: "90" }, + manual_threshold_time: { type: String, default: null }, + threshold_type: { type: String, default: "percentage" }, + startTime: { type: String, default: null }, + start_instance_id: { type: String, default: null }, + stopTime: { type: String, default: null }, + waterlevelPercentage: { type: String, default: null } // Added field + } + ], + outputConnections: [ + { + outputConnections: { type: String }, + output_type: { type: String }, + outputismotor: { type: Boolean }, + motor_id: { type: String, default: null }, + motor_status: { type: String, default: "0" }, + motor_stop_status: { type: String, default: "1" }, + capacity: { type: String, default: null }, + water_level: { type: String, default: null }, + manual_threshold_percentage: { type: String, default: "90" }, + manual_threshold_time: { type: String, default: null }, + threshold_type: { type: String, default: "percentage" }, + waterlevelPercentage: { type: String, default: null } // Added field + } + ] + } +}); + From a7051491c29076395fbc7dc775fd09fe8efdda7f Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Mon, 29 Jul 2024 13:52:33 +0530 Subject: [PATCH 2/3] percentage wate level --- src/controllers/tanksController.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index b9cdc799..cc624405 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -311,6 +311,11 @@ exports.getTanklevels = async (req, reply) => { let sumOverheadDrinkingWaterCapacity = 0; let sumSumpBoreWaterCapacity = 0; let sumOverheadBoreWaterCapacity = 0; + + let totalInputPercentage = 0; + let inputCount = 0; + let totalOutputPercentage = 0; + let outputCount = 0; const updated_data = await Tank.find({ customerId: customerId }); console.log("updated_data", updated_data); @@ -330,6 +335,8 @@ exports.getTanklevels = async (req, reply) => { const inputWaterLevel = parseInt(inputConnection.water_level.replace(/,/g, ''), 10); const inputCapacity = parseInt(inputConnection.capacity.replace(/,/g, ''), 10); inputConnection.waterlevelPercentage = ((inputWaterLevel / inputCapacity) * 100).toFixed(2); + totalInputPercentage += parseFloat(inputConnection.waterlevelPercentage); + inputCount++; } else { inputConnection.waterlevelPercentage = null; } @@ -343,6 +350,8 @@ exports.getTanklevels = async (req, reply) => { const outputWaterLevel = parseInt(outputConnection.water_level.replace(/,/g, ''), 10); const outputCapacity = parseInt(outputConnection.capacity.replace(/,/g, ''), 10); outputConnection.waterlevelPercentage = ((outputWaterLevel / outputCapacity) * 100).toFixed(2); + totalOutputPercentage += parseFloat(outputConnection.waterlevelPercentage); + outputCount++; } else { outputConnection.waterlevelPercentage = null; } @@ -369,9 +378,15 @@ exports.getTanklevels = async (req, reply) => { const buildingName = user ? user.buildingName : null; const address1 = user ? user.profile.address1 : null; + // Calculate average percentages + const inputWaterlevelPercentage = inputCount > 0 ? (totalInputPercentage / inputCount).toFixed(2) : null; + const outputWaterlevelPercentage = outputCount > 0 ? (totalOutputPercentage / outputCount).toFixed(2) : null; + const responseData = { status_code: 200, data: updated_data, + inputWaterlevelPercentage: inputWaterlevelPercentage, + outputWaterlevelPercentage: outputWaterlevelPercentage, totalDrinkingWaterInSump: sumSumpDrinkingWater, totalDrinkingWaterInOverhead: sumOverheadDrinkingWater, totalBoreWaterInSump: sumSumpBoreWater, @@ -398,7 +413,6 @@ exports.getTanklevels = async (req, reply) => { - const intervals = {}; let sump_water_levels=[]; let supplier_tanks = []; From 5092a6ae55cd5f0b19f45ddc0de58fade5652b5e Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 30 Jul 2024 11:18:47 +0530 Subject: [PATCH 3/3] waterlevel percentage --- src/controllers/tanksController.js | 40 ++++++++++++++++-------------- src/models/tanks.js | 10 ++++---- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index cc624405..baac6874 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -311,29 +311,30 @@ exports.getTanklevels = async (req, reply) => { let sumOverheadDrinkingWaterCapacity = 0; let sumSumpBoreWaterCapacity = 0; let sumOverheadBoreWaterCapacity = 0; - - let totalInputPercentage = 0; - let inputCount = 0; - let totalOutputPercentage = 0; - let outputCount = 0; const updated_data = await Tank.find({ customerId: customerId }); console.log("updated_data", updated_data); updated_data.forEach((tank) => { - const waterlevel = parseInt(tank.waterlevel.replace(/,/g, ''), 10); - const capacity = parseInt(tank.capacity.replace(/,/g, ''), 10); + const waterlevel = parseInt(tank.waterlevel ? tank.waterlevel.replace(/,/g, '') : '0', 10); + const capacity = parseInt(tank.capacity ? tank.capacity.replace(/,/g, '') : '0', 10); const waterlevelPercentage = ((waterlevel / capacity) * 100).toFixed(2); tank.waterlevelPercentage = waterlevelPercentage; // Add water level percentage to each tank object console.log(`Processing tank: ${tank.tankName}`); console.log(`Type of Water: ${tank.typeOfWater}, Location: ${tank.tankLocation}, Waterlevel: ${waterlevel}, Capacity: ${capacity}, Waterlevel Percentage: ${waterlevelPercentage}%`); + let totalInputPercentage = 0; + let inputCount = 0; + let totalOutputPercentage = 0; + let outputCount = 0; + // Calculate and add water level percentages for inputConnections if (tank.connections.inputConnections) { tank.connections.inputConnections.forEach(inputConnection => { - if (inputConnection.water_level && inputConnection.capacity) { - const inputWaterLevel = parseInt(inputConnection.water_level.replace(/,/g, ''), 10); - const inputCapacity = parseInt(inputConnection.capacity.replace(/,/g, ''), 10); + const inputWaterLevel = inputConnection.water_level ? parseInt(inputConnection.water_level.replace(/,/g, ''), 10) : 0; + const inputCapacity = inputConnection.capacity ? parseInt(inputConnection.capacity.replace(/,/g, ''), 10) : 0; + + if (inputCapacity > 0) { inputConnection.waterlevelPercentage = ((inputWaterLevel / inputCapacity) * 100).toFixed(2); totalInputPercentage += parseFloat(inputConnection.waterlevelPercentage); inputCount++; @@ -341,14 +342,18 @@ exports.getTanklevels = async (req, reply) => { inputConnection.waterlevelPercentage = null; } }); + + // Add the average input water level percentage to the tank's connections object + tank.connections.inputWaterlevelPercentage = inputCount > 0 ? (totalInputPercentage / inputCount).toFixed(2) : null; } // Calculate and add water level percentages for outputConnections if (tank.connections.outputConnections) { tank.connections.outputConnections.forEach(outputConnection => { - if (outputConnection.water_level && outputConnection.capacity) { - const outputWaterLevel = parseInt(outputConnection.water_level.replace(/,/g, ''), 10); - const outputCapacity = parseInt(outputConnection.capacity.replace(/,/g, ''), 10); + const outputWaterLevel = outputConnection.water_level ? parseInt(outputConnection.water_level.replace(/,/g, ''), 10) : 0; + const outputCapacity = outputConnection.capacity ? parseInt(outputConnection.capacity.replace(/,/g, ''), 10) : 0; + + if (outputCapacity > 0) { outputConnection.waterlevelPercentage = ((outputWaterLevel / outputCapacity) * 100).toFixed(2); totalOutputPercentage += parseFloat(outputConnection.waterlevelPercentage); outputCount++; @@ -356,6 +361,9 @@ exports.getTanklevels = async (req, reply) => { outputConnection.waterlevelPercentage = null; } }); + + // Add the average output water level percentage to the tank's connections object + tank.connections.outputWaterlevelPercentage = outputCount > 0 ? (totalOutputPercentage / outputCount).toFixed(2) : null; } // Summing up the total water levels and capacities @@ -378,15 +386,9 @@ exports.getTanklevels = async (req, reply) => { const buildingName = user ? user.buildingName : null; const address1 = user ? user.profile.address1 : null; - // Calculate average percentages - const inputWaterlevelPercentage = inputCount > 0 ? (totalInputPercentage / inputCount).toFixed(2) : null; - const outputWaterlevelPercentage = outputCount > 0 ? (totalOutputPercentage / outputCount).toFixed(2) : null; - const responseData = { status_code: 200, data: updated_data, - inputWaterlevelPercentage: inputWaterlevelPercentage, - outputWaterlevelPercentage: outputWaterlevelPercentage, totalDrinkingWaterInSump: sumSumpDrinkingWater, totalDrinkingWaterInOverhead: sumOverheadDrinkingWater, totalBoreWaterInSump: sumSumpBoreWater, diff --git a/src/models/tanks.js b/src/models/tanks.js index c0bba543..8c753ece 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -51,7 +51,6 @@ const tanksSchema = new mongoose.Schema({ auto_min_percentage: { type: String, default: "20" }, reserved_percentage: { type: String, default: "20" }, auto_max_percentage: { type: String, default: "80" }, - connections: { source: { type: String }, inputConnections: [ @@ -72,7 +71,7 @@ const tanksSchema = new mongoose.Schema({ startTime: { type: String, default: null }, start_instance_id: { type: String, default: null }, stopTime: { type: String, default: null }, - waterlevelPercentage: { type: String, default: null } // Added field + waterlevelPercentage: { type: String, default: null } } ], outputConnections: [ @@ -88,13 +87,14 @@ const tanksSchema = new mongoose.Schema({ manual_threshold_percentage: { type: String, default: "90" }, manual_threshold_time: { type: String, default: null }, threshold_type: { type: String, default: "percentage" }, - waterlevelPercentage: { type: String, default: null } // Added field + waterlevelPercentage: { type: String, default: null } } - ] + ], + inputWaterlevelPercentage: { type: String, default: null }, + outputWaterlevelPercentage: { type: String, default: null } } }); - const motordataSchema = new mongoose.Schema({