changes in consumption

master
varun 1 year ago
parent 09962b330c
commit 1e98abbada

@ -936,13 +936,6 @@ let supplier_tanks = [];
function parseDateTime(dateTimeStr) {
const [datePart, timePart] = dateTimeStr.split(' - ');
const [day, month, year] = datePart.split('-');
const monthIndex = new Date(Date.parse(month +" 1, 2024")).getMonth(); // Parse the month name to get its index
const [hours, minutes] = timePart.split(':');
return new Date(year, monthIndex, day, hours, minutes);
}
@ -952,10 +945,11 @@ exports.consumption = async (request, reply) => {
const { customerId } = request.params; const { customerId } = request.params;
const { startDate, stopDate } = request.body; const { startDate, stopDate } = request.body;
const start = parseDateTime(startDate); const start = startDate;
const end = parseDateTime(stopDate); const end = stopDate;
const tanks = await Tank.find({ customerId, tankLocation: "overhead" }); const tanks = await Tank.find({ customerId, tankLocation: "overhead" });
const tankData = []; const tankData = [];
console.log(start,end)
for (const tank of tanks) { for (const tank of tanks) {
const tankId = tank._id; const tankId = tank._id;
@ -974,6 +968,8 @@ exports.consumption = async (request, reply) => {
} }
}); });
console.log(tankConsumptions)
const total_consumption_from_records = tankConsumptions.reduce((acc, record) => { const total_consumption_from_records = tankConsumptions.reduce((acc, record) => {
return acc + parseInt(record.consumption, 10); return acc + parseInt(record.consumption, 10);
}, 0); }, 0);
@ -2330,31 +2326,43 @@ cron.schedule('0 0 * * *', updatewaterlevelsatmidnight, {
const updatetotalConsumptiontillmidnight = async () => {
const updatetotalConsumptiontillmidnight = async () => {
console.log('Cron job triggered at:', moment().tz('Asia/Kolkata').format()); console.log('Cron job triggered at:', moment().tz('Asia/Kolkata').format());
try { try {
const tanks = await Tank.find({}); const tanks = await Tank.find({});
for (const tank of tanks) { for (const tank of tanks) {
const waterlevel_at_midnight = parseInt((tank.waterlevel_at_midnight).replace(/,/g, ''), 10); 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 total_water_added_from_midnight = parseInt((tank.total_water_added_from_midnight).replace(/,/g, ''), 10);
const waterlevel = parseInt((tank.waterlevel).replace(/,/g, ''), 10); const waterlevel = parseInt((tank.waterlevel).replace(/,/g, ''), 10);
const totalconsumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel const totalconsumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel;
const newTankConsumption = new TankConsumptionSchema({
// Format the date in the desired format
const formattedDate = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
// Check if the record already exists
const existingRecord = await TankConsumptionSchema.findOne({
customerId: tank.customerId, customerId: tank.customerId,
tankName: tank.tankName, tankName: tank.tankName,
tankLocation: tank.tankLocation, tankLocation: tank.tankLocation,
consumption: totalconsumption.toString(), time: formattedDate
time: new Date().toISOString()
}); });
// Save the new document if (!existingRecord) {
await newTankConsumption.save(); // Create and save the new document if it doesn't exist
const newTankConsumption = new TankConsumptionSchema({
customerId: tank.customerId,
tankName: tank.tankName,
tankLocation: tank.tankLocation,
consumption: totalconsumption.toString(),
time: formattedDate // Save the formatted date
});
await newTankConsumption.save();
console.log(`Created new record for tank ${tank.tankName} at ${formattedDate}`);
} else {
console.log(`Record already exists for tank ${tank.tankName} at ${formattedDate}`);
}
} }
console.log('Waterlevel noted in waterlevel_at_midnight'); console.log('Waterlevel noted in waterlevel_at_midnight');
} catch (error) { } catch (error) {
@ -2362,14 +2370,54 @@ const updatetotalConsumptiontillmidnight = async () => {
} }
}; };
// Schedule the task to run every day at 13:49 IST (1:49 PM IST) // Schedule the task to run every day at 12:49 PM IST
cron.schedule('55 23 * * *', updatetotalConsumptiontillmidnight, { cron.schedule('55 23 * * *', updatetotalConsumptiontillmidnight, {
timezone: "Asia/Kolkata" timezone: "Asia/Kolkata"
}); });
// const updatetotalConsumptiontillmidnight = async () => {
// console.log('Cron job triggered at:', moment().tz('Asia/Kolkata').format());
// try {
// const tanks = await Tank.find({});
// for (const tank of tanks) {
// 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 totalconsumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel;
// // Format the date in the desired format
// const formattedDate = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
// const newTankConsumption = new TankConsumptionSchema({
// customerId: tank.customerId,
// tankName: tank.tankName,
// tankLocation: tank.tankLocation,
// consumption: totalconsumption.toString(),
// time: formattedDate // Save the formatted date
// });
// // Save the new document
// await newTankConsumption.save();
// }
// console.log('Waterlevel noted in waterlevel_at_midnight');
// } catch (error) {
// console.error('Error occurred:', error);
// }
// };
// Schedule the task to run every day at 23:55 IST (11:55 PM IST)
// cron.schedule('55 23 * * *', updatetotalConsumptiontillmidnight, {
// timezone: "Asia/Kolkata"
// });
exports.deletemotordatarecordsbefore7days = async (req, reply) => { exports.deletemotordatarecordsbefore7days = async (req, reply) => {
try { try {

Loading…
Cancel
Save