diff --git a/proxy/db/capacityTracker.js b/proxy/db/capacityTracker.js new file mode 100644 index 0000000..7f385a2 --- /dev/null +++ b/proxy/db/capacityTracker.js @@ -0,0 +1,59 @@ +// proxy/db/capacityTracker.js +// ───────────────────────────────────────────────────────────────────────────── +// MongoDB Capacity & Data Size Breakdown per Network Agent. +// Measures logical document sizes per agent_uuid across all collections. +// ───────────────────────────────────────────────────────────────────────────── + +const mongoose = require('mongoose'); + +async function logCapacityStats(prefix = '[MongoDB]') { + try { + if (!mongoose.connection || !mongoose.connection.db) { + return; + } + const db = mongoose.connection.db; + + const stats = await db.command({ dbStats: 1 }); + const dataSizeMB = (stats.dataSize / (1024 * 1024)).toFixed(2); + const storageSizeMB = (stats.storageSize / (1024 * 1024)).toFixed(2); + console.log(`${prefix} Capacity Used: Data Size = ${dataSizeMB} MB, Storage Size = ${storageSizeMB} MB`); + + const agentSizes = {}; + const collections = await db.listCollections().toArray(); + + for (const colInfo of collections) { + const colName = colInfo.name; + if (colName.startsWith('system.')) continue; + const col = db.collection(colName); + + const sampleDoc = await col.findOne({ agent_uuid: { $ne: null } }); + if (!sampleDoc) continue; + + const pipeline = [ + { $project: { agent_uuid: 1, docSize: { $bsonSize: "$$ROOT" } } }, + { $group: { _id: "$agent_uuid", totalBytes: { $sum: "$docSize" } } } + ]; + + const results = await col.aggregate(pipeline).toArray(); + for (const res of results) { + const agent = res._id || 'Unknown'; + agentSizes[agent] = (agentSizes[agent] || 0) + res.totalBytes; + } + } + + const sortedAgents = Object.entries(agentSizes) + .map(([agent, bytes]) => ({ agent, sizeMB: parseFloat((bytes / (1024 * 1024)).toFixed(2)) })) + .sort((a, b) => b.sizeMB - a.sizeMB); + + if (sortedAgents.length > 0) { + console.log(`${prefix} Data Size Breakdown per Agent:`); + for (const { agent, sizeMB } of sortedAgents) { + console.log(` - ${agent}: ${sizeMB.toFixed(2)} MB`); + } + } + } catch (err) { + console.warn(`${prefix} Could not retrieve DB capacity breakdown:`, err.message); + } +} + +module.exports = { logCapacityStats }; diff --git a/proxy/index.js b/proxy/index.js index a143e29..13efa27 100644 --- a/proxy/index.js +++ b/proxy/index.js @@ -43,7 +43,7 @@ async function connectDB() { socketTimeoutMS: 30000, }); console.log('[MongoDB] ✓ Connected successfully to', MONGODB_URI); - const { logCapacityStats } = require('../backend/db/capacityTracker'); + const { logCapacityStats } = require('./db/capacityTracker'); await logCapacityStats('[MongoDB]'); return true; } catch (err) { @@ -165,8 +165,8 @@ app.get('/agents', async (req, res) => { if (mongoose.connection.readyState !== 1) { return res.status(503).json({ ok: false, error: 'MongoDB not connected.' }); } - const { DashboardSummary } = require('../backend/models/Schemas'); - const agents = await DashboardSummary.distinct('agent_uuid'); + const { Summary } = require('./models/Schemas'); + const agents = await Summary.distinct('agent_uuid'); res.json({ ok: true, data: agents }); } catch (err) { res.status(500).json({ ok: false, error: err.message }); @@ -183,7 +183,7 @@ app.get('/domain-details', async (req, res) => { if (!domain) return res.status(400).json({ ok: false, error: 'domain is required' }); try { - const { Flow, DeviceStat } = require('../backend/models/Schemas'); + const { Flow, DeviceStat } = require('./models/Schemas'); const filter = { domain: domain }; if (agentUuid) { diff --git a/proxy/scheduler.js b/proxy/scheduler.js index dc6da44..e1f14b1 100644 --- a/proxy/scheduler.js +++ b/proxy/scheduler.js @@ -55,7 +55,7 @@ async function runCollection() { const now = Date.now(); if (now - lastCapacityLogAt >= CAPACITY_LOG_INTERVAL_MS) { lastCapacityLogAt = now; - const { logCapacityStats } = require('../backend/db/capacityTracker'); + const { logCapacityStats } = require('./db/capacityTracker'); await logCapacityStats(`[PROXY] [MongoDB] Capacity Used after Run #${runCount}:`); }