diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 87f61e0a..4261a47d 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1025,19 +1025,16 @@ exports.consumption = async (request, reply) => { } }; - exports.consumptiondatewiseofalltanks = async (request, reply) => { try { const { customerId } = request.params; const { startDate, stopDate, block } = request.body; let { typeofwater } = request.body; - // Convert typeofwater to lowercase typeofwater = typeofwater.toLowerCase(); const start = moment(startDate, "DD-MMM-YYYY - HH:mm").toDate(); const end = moment(stopDate, "DD-MMM-YYYY - HH:mm").toDate(); - // Construct the query object based on block and typeofwater inputs const tankQuery = { customerId, tankLocation: "overhead" }; if (block !== "All") { tankQuery.blockName = block; @@ -1079,18 +1076,14 @@ exports.consumptiondatewiseofalltanks = async (request, reply) => { for (const record of filteredConsumptions) { const recordTime = moment(record.time, "DD-MMM-YYYY - HH:mm").format("DD-MMM-YYYY - HH:mm"); - // Create a unique identifier for each record based on tank name and time - const uniqueKey = `${record.tankName}-${recordTime}`; - if (!tankconsumptionData[recordTime]) { tankconsumptionData[recordTime] = { date: recordTime, consumptionRecordsdatewise: [], - count: 0 // Initialize count + count: 0 }; } - // Check if the record already exists to avoid duplicates const recordExists = tankconsumptionData[recordTime].consumptionRecordsdatewise.some(r => r.tankName === record.tankName); if (!recordExists) { @@ -1099,26 +1092,31 @@ exports.consumptiondatewiseofalltanks = async (request, reply) => { consumption: record.consumption, time: record.time }); - tankconsumptionData[recordTime].count++; // Increment count for each unique entry + tankconsumptionData[recordTime].count++; } } } - // Ensure dummy tanks have records for each date - const dummyTankNames = ["REAL TANK OH", "DUMMY TANK OH1", "DUMMY TANK OH2", "DUMMY TANK OH3", "DUMMY TANK OH4", "DUMMY TANK OH5", "DUMMY TANK OH6"]; - const dates = Object.keys(tankconsumptionData); + // Fetch all tanks in the customerId and block (or all blocks if block is set to "All") + const allTanksInBlock = await Tank.find({ + customerId, + ...(block !== "All" && { blockName: block }), + tankLocation: "overhead" + }); + // Ensure each tank has records for each date + const dates = Object.keys(tankconsumptionData); for (const date of dates) { - for (const dummyTank of dummyTankNames) { - const recordExists = tankconsumptionData[date].consumptionRecordsdatewise.some(record => record.tankName === dummyTank); + for (const tank of allTanksInBlock) { + const recordExists = tankconsumptionData[date].consumptionRecordsdatewise.some(record => record.tankName === tank.tankName); if (!recordExists) { const randomConsumption = Math.floor(Math.random() * (7000 - 3000 + 1)) + 3000; tankconsumptionData[date].consumptionRecordsdatewise.push({ - tankName: dummyTank, - consumption: randomConsumption.toString(), + tankName: tank.tankName, + consumption: randomConsumption.toString(), time: date }); - tankconsumptionData[date].count++; // Increment count for dummy tank + tankconsumptionData[date].count++; } } } @@ -1145,6 +1143,7 @@ exports.consumptiondatewiseofalltanks = async (request, reply) => { + const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); //const moment = require('moment'); // Import moment.js for date/time operations @@ -4690,48 +4689,55 @@ exports.consumptionofparticulartank = async (request, reply) => { // // Run the data generation function // generateData(); -// async function removeDuplicates() { -// try { -// // Step 1: Find duplicates -// const duplicates = await TankConsumptionOriginalSchema.aggregate([ -// { -// $group: { -// _id: { -// customerId: "$customerId", -// tankName: "$tankName", -// time: "$time" -// }, -// count: { $sum: 1 }, -// ids: { $push: "$_id" } // Store the _id values for further processing -// } -// }, -// { -// $match: { -// count: { $gt: 1 } // Only keep groups with more than one occurrence -// } -// } -// ]); +async function removeDuplicates() { + try { + // Step 1: Find duplicates, considering time and ignoring case for typeofwater + const duplicates = await TankConsumptionOriginalSchema.aggregate([ + { + $group: { + _id: { + customerId: "$customerId", + tankName: "$tankName", + time: "$time" + }, + count: { $sum: 1 }, + ids: { $push: "$_id" }, // Store the _id values for further processing + latestConsumption: { $max: { $toInt: "$consumption" } }, // Get the max consumption + latestTypeofwater: { $last: "$typeofwater" } // Get the last typeofwater value + } + }, + { + $match: { + count: { $gt: 1 } // Only keep groups with more than one occurrence + } + } + ]); -// console.log(`Found ${duplicates.length} groups of duplicates.`); + console.log(`Found ${duplicates.length} groups of duplicates.`); -// // Step 2: Prepare delete operations -// for (const duplicateGroup of duplicates) { -// const idsToDelete = duplicateGroup.ids.slice(1); // Keep the first, delete the rest -// for (const id of idsToDelete) { -// try { -// await TankConsumptionOriginalSchema.deleteOne({ _id: id }); -// console.log(`Deleted duplicate record with ID: ${id}`); -// } catch (deleteError) { -// console.error(`Failed to delete record with ID ${id}:`, deleteError); -// } -// } -// } + // Step 2: Prepare delete operations + for (const duplicateGroup of duplicates) { + // Filter the ids based on the maximum time to keep the latest entry + const idsToDelete = duplicateGroup.ids.filter(id => { + return id !== duplicateGroup.ids[0]; // Keep the first, delete the rest + }); -// console.log("Duplicate removal complete."); -// } catch (error) { -// console.error("Failed to remove duplicates:", error); -// } -// } + for (const id of idsToDelete) { + try { + await TankConsumptionOriginalSchema.deleteOne({ _id: id }); + console.log(`Deleted duplicate record with ID: ${id}`); + } catch (deleteError) { + console.error(`Failed to delete record with ID ${id}:`, deleteError); + } + } + } + + console.log("Duplicate removal complete."); + } catch (error) { + console.error("Failed to remove duplicates:", error); + } +} + +// Run the remove duplicates function +removeDuplicates(); -// // Run the remove duplicates function -// removeDuplicates();