Make it proxy as independent docker image
This commit is contained in:
1 parent
4882108068
commit
52282e3822
3 files changed
+64
-5
No files matched your search
@@ -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 };
|
||||
+4
-4
@@ -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) {
|
||||
|
||||
+1
-1
@@ -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}:`);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user