/**
* Flags potentially anomalous directories based on size and modification time.
*
* @param {string[]} directories An array of directory paths to analyze.
* @param {number} sizeThreshold The size threshold in bytes above which a directory is flagged.
* @param {number} ageThreshold The age threshold in days above which a directory is flagged.
* @returns {object} An object containing arrays of flagged directories (size and age).
*/
function flagAnomalousDirectories(directories, sizeThreshold, ageThreshold) {
const flaggedBySize = [];
const flaggedByAge = [];
for (const directory of directories) {
try {
const stats = require('fs').statSync(directory); // Use fs.statSync for synchronous operation
const size = stats.size;
const lastModified = stats.mtime;
// Check size anomaly
if (size > sizeThreshold) {
flaggedBySize.push({ directory, size });
}
// Check age anomaly
const now = new Date();
const ageInDays = Math.floor((now - lastModified) / (1000 * 60 * 60 * 24)); // Calculate age in days
if (ageInDays > ageThreshold) {
flaggedByAge.push({ directory, age: ageInDays });
}
} catch (error) {
console.error(`Error processing directory ${directory}: ${error.message}`);
}
}
return { flaggedBySize, flaggedByAge };
}
Add your comment