Files
Deep-Package-Inspection/proxy/dataRetention.js
T
rafif dd4c8f6876 feat(source2): push all latest files - device labeling, help system, proxy docs, database isolation fix
- Added DOKUMENTASI-FILTER-PER-SITE.md (site isolation docs)
- Fixed start-with-env.js to force-load .env.production
- Fixed MONGODB_URI hostname from mongodb-netify to mongodb.prod.proit.id
- Updated .gitignore to exclude sensitive scripts and credential files
- Minor UI and labeling improvements
2026-07-29 14:14:29 +07:00

37 lines
1.8 KiB
JavaScript

// proxy/dataRetention.js
// ─────────────────────────────────────────────────────────────────────────────
// Pruning process for BackOne MongoDB data retention.
// Removes telemetry records older than 7 days to conserve database space.
// ─────────────────────────────────────────────────────────────────────────────
const mongoose = require('mongoose');
const Schemas = require('./models/Schemas');
/**
* Prune all time-series documents older than 7 days.
*/
async function pruneOldData() {
const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
const timeString = thirtyDaysAgo.toLocaleString('id-ID', { timeZone: 'Asia/Jakarta' }) + ' WIB';
console.log(`[Collector] [Retention] Checking for telemetry data older than 30 days (before ${timeString})...`);
// Prune from all collections in Schemas except CustomDeviceLabel
const collections = Object.keys(Schemas).filter(name => name !== 'CustomDeviceLabel');
for (const name of collections) {
try {
const Model = Schemas[name];
if (typeof Model.deleteMany === 'function') {
const res = await Model.deleteMany({ timestamp: { $lt: thirtyDaysAgo } });
if (res.deletedCount > 0) {
console.log(`[Collector] [Retention] ✓ Cleaned up ${res.deletedCount} old records from ${name}`);
}
}
} catch (err) {
console.error(`[Collector] [Retention] ✗ Failed to prune ${name}: ${err.message}`);
}
}
}
module.exports = { pruneOldData };