diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 6e673184..87f61e0a 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1026,7 +1026,6 @@ exports.consumption = async (request, reply) => { }; - exports.consumptiondatewiseofalltanks = async (request, reply) => { try { const { customerId } = request.params; @@ -1078,25 +1077,35 @@ exports.consumptiondatewiseofalltanks = async (request, reply) => { totalConsumptionForSelectedBlockAndTypeOfWater += consumption; for (const record of filteredConsumptions) { - const recordDate = moment(record.time, "DD-MMM-YYYY - HH:mm").format("DD-MMM-YYYY - HH:mm"); + 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[recordDate]) { - tankconsumptionData[recordDate] = { - date: recordDate, - consumptionRecordsdatewise: [] + if (!tankconsumptionData[recordTime]) { + tankconsumptionData[recordTime] = { + date: recordTime, + consumptionRecordsdatewise: [], + count: 0 // Initialize count }; } - tankconsumptionData[recordDate].consumptionRecordsdatewise.push({ - tankName: tankname, - consumption: record.consumption, - time: record.time - }); + // Check if the record already exists to avoid duplicates + const recordExists = tankconsumptionData[recordTime].consumptionRecordsdatewise.some(r => r.tankName === record.tankName); + + if (!recordExists) { + tankconsumptionData[recordTime].consumptionRecordsdatewise.push({ + tankName: record.tankName, + consumption: record.consumption, + time: record.time + }); + tankconsumptionData[recordTime].count++; // Increment count for each unique entry + } } } // 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 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); for (const date of dates) { @@ -1106,9 +1115,10 @@ exports.consumptiondatewiseofalltanks = async (request, reply) => { const randomConsumption = Math.floor(Math.random() * (7000 - 3000 + 1)) + 3000; tankconsumptionData[date].consumptionRecordsdatewise.push({ tankName: dummyTank, - consumption: randomConsumption.toString(), + consumption: randomConsumption.toString(), time: date }); + tankconsumptionData[date].count++; // Increment count for dummy tank } } } @@ -1133,6 +1143,8 @@ 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 @@ -4606,6 +4618,120 @@ exports.consumptionofparticulartank = async (request, reply) => { } }; -console.log("test") +// // Set start and end dates +// const startDate = new Date("2024-08-20T00:00:00Z"); +// const endDate = new Date("2024-11-04T00:00:00Z"); + +// // Tank names array with respective blocks +// const tanks = [ +// { tankName: "REAL TANK OH", block: "A" }, +// { tankName: "DUMMY TANK OH1", block: "BLOCK C" }, +// { tankName: "DUMMY TANK OH2", block: "BLOCK D" }, +// { tankName: "DUMMY TANK OH3", block: "BLOCK C" }, +// { tankName: "DUMMY TANK OH4", block: "BLOCK C" }, +// { tankName: "DUMMY TANK OH5", block: "BLOCK C" }, +// { tankName: "DUMMY TANK OH6", block: "BLOCK C" } +// ]; + +// const customerId = "AWSUSKY4"; +// const tankLocation = "overhead"; +// const typeofwater = "Bore Water"; + +// // Function to format date to "DD-MMM-YYYY - HH:mm" +// function formatDateCustom(date) { +// const options = { day: '2-digit', month: 'short', year: 'numeric' }; +// return date.toLocaleDateString('en-GB', options).replace(/ /g, '-') + " - 23:55"; +// } + +// // Main function to generate data +// async function generateData() { +// for (let date = new Date(startDate); date <= endDate; date.setDate(date.getDate() + 1)) { +// const formattedDate = formatDateCustom(date); // Format date to "DD-MMM-YYYY - 23:55" + +// for (const { tankName, block } of tanks) { +// try { +// const existingRecord = await TankConsumptionOriginalSchema.findOne({ +// customerId: customerId, +// tankName: tankName, +// tankLocation: tankLocation, +// time: formattedDate +// }).exec(); + +// console.log(`Checking record for ${tankName} on ${formattedDate}: ${existingRecord ? 'Exists' : 'Does not exist'}`); + +// if (!existingRecord) { +// // Random consumption between 7000 and 8000 +// const randomConsumption = Math.floor(Math.random() * (8000 - 7000 + 1)) + 7000; + +// // Create a new document and save it +// const newRecord = new TankConsumptionOriginalSchema({ +// customerId: customerId, +// tankName: tankName, +// tankLocation: tankLocation, +// consumption: randomConsumption.toString(), +// time: formattedDate, +// block: block, +// typeofwater: typeofwater, +// __v: 0 +// }); +// await newRecord.save(); // Use .save() method to insert the record +// console.log(`Inserted record for ${tankName} on ${formattedDate}`); +// } +// } catch (error) { +// console.error(`Failed to check or insert record for ${tankName} on ${formattedDate}:`, error); +// } +// } +// } +// console.log("Data generation complete."); +// } + +// // 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 +// } +// } +// ]); + +// 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); +// } +// } +// } + +// console.log("Duplicate removal complete."); +// } catch (error) { +// console.error("Failed to remove duplicates:", error); +// } +// } + +// // Run the remove duplicates function +// removeDuplicates();