ashok 1 year ago
commit 3be529ca60

@ -940,24 +940,33 @@ let supplier_tanks = [];
exports.consumption = async (request, reply) => {
try {
try {SS
const { customerId } = request.params;
const { startDate, stopDate } = request.body;
const { startDate, stopDate, block, typeofwater } = request.body;
const start = startDate;
const end = stopDate;
const tanks = await Tank.find({ customerId, tankLocation: "overhead" });
// Construct the query object based on block and typeofwater inputs
const tankQuery = { customerId, tankLocation: "overhead" };
if (block !== "all") {
tankQuery.blockName = block; // Filter by specific block if not "all"
}
if (typeofwater !== "all") {
tankQuery.typeOfWater = typeofwater; // Filter by specific type of water if not "all"
}
const tanks = await Tank.find(tankQuery);
const tankData = [];
console.log(start,end)
for (const tank of tanks) {
const tankId = tank._id;
const waterlevel_at_midnight = parseInt(tank.waterlevel_at_midnight.replace(/,/g, ''), 10);
const total_water_added_from_midnight = parseInt(tank.total_water_added_from_midnight.replace(/,/g, ''), 10);
const waterlevel = parseInt(tank.waterlevel.replace(/,/g, ''), 10);
const tankname = tank.tankName;
console.log(waterlevel_at_midnight,total_water_added_from_midnight,waterlevel)
const tankConsumptions = await TankConsumptionSchema.find({
customerId,
tankName: tank.tankName,
@ -965,18 +974,26 @@ exports.consumption = async (request, reply) => {
time: {
$gte: start,
$lte: end
}
},
...(block !== "all" && { block: tank.blockName }), // Ensure correct field names
...(typeofwater !== "all" && { typeofwater: tank.typeOfWater }) // Ensure correct field names
});
console.log(tankConsumptions)
const total_consumption_from_records = tankConsumptions.reduce((acc, record) => {
return acc + parseInt(record.consumption, 10);
}, 0);
const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records;
tankData.push({ tankname, totalConsumption: consumption,block:tank.blockName,TypeofWater:tank.typeOfWater,location:tank.tankLocation,capacity:tank.capacity,waterlevel:tank.waterlevel });
tankData.push({
tankname,
totalConsumption: consumption,
block: tank.blockName,
TypeofWater: tank.typeOfWater,
location: tank.tankLocation,
capacity: tank.capacity,
waterlevel: tank.waterlevel
});
}
reply.send({ status_code: 200, tankData });
@ -988,6 +1005,8 @@ exports.consumption = async (request, reply) => {
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
//const moment = require('moment'); // Import moment.js for date/time operations
@ -2325,7 +2344,17 @@ cron.schedule('0 0 * * *', updatewaterlevelsatmidnight, {
});
let consumptionTask;
// Function to clear the specific scheduled task
const clearConsumptionSchedule = () => {
if (consumptionTask) {
consumptionTask.stop(); // Stop the existing task if it exists
consumptionTask = null; // Clear the reference
}
};
// Function to update total consumption till midnight
const updatetotalConsumptiontillmidnight = async () => {
console.log('Cron job triggered at:', moment().tz('Asia/Kolkata').format());
@ -2355,7 +2384,9 @@ const updatetotalConsumptiontillmidnight = async () => {
tankName: tank.tankName,
tankLocation: tank.tankLocation,
consumption: totalconsumption.toString(),
time: formattedDate // Save the formatted date
time: formattedDate, // Save the formatted date
block:tank.blockName,
typeofwater:tank.typeOfWater
});
await newTankConsumption.save();
@ -2370,11 +2401,16 @@ const updatetotalConsumptiontillmidnight = async () => {
}
};
// Schedule the task to run every day at 12:49 PM IST
cron.schedule('55 23 * * *', updatetotalConsumptiontillmidnight, {
// Clear the existing schedule for this task before creating a new one
clearConsumptionSchedule();
// Schedule the task to run every day at 12:49 PM IST and store the reference
consumptionTask = cron.schedule('55 23 * * *', updatetotalConsumptiontillmidnight, {
timezone: "Asia/Kolkata"
});
console.log('Scheduled task to update total consumption till midnight.');

@ -146,6 +146,8 @@ const tankconsumptionSchema = new mongoose.Schema({
tankName: { type: String },
tankLocation: { type: String },
consumption: { type: String },
block:{type: String},
typeofwater:{type:String},
time: { type: String }
});

@ -389,6 +389,8 @@ module.exports = function (fastify, opts, next) {
properties: {
startDate:{ type: "string" },
stopDate:{type:"string"},
block:{type:"string"},
typeofwater:{type:"string"},
},

Loading…
Cancel
Save