1. /**
  2. * Flags potentially anomalous directories based on size and modification time.
  3. *
  4. * @param {string[]} directories An array of directory paths to analyze.
  5. * @param {number} sizeThreshold The size threshold in bytes above which a directory is flagged.
  6. * @param {number} ageThreshold The age threshold in days above which a directory is flagged.
  7. * @returns {object} An object containing arrays of flagged directories (size and age).
  8. */
  9. function flagAnomalousDirectories(directories, sizeThreshold, ageThreshold) {
  10. const flaggedBySize = [];
  11. const flaggedByAge = [];
  12. for (const directory of directories) {
  13. try {
  14. const stats = require('fs').statSync(directory); // Use fs.statSync for synchronous operation
  15. const size = stats.size;
  16. const lastModified = stats.mtime;
  17. // Check size anomaly
  18. if (size > sizeThreshold) {
  19. flaggedBySize.push({ directory, size });
  20. }
  21. // Check age anomaly
  22. const now = new Date();
  23. const ageInDays = Math.floor((now - lastModified) / (1000 * 60 * 60 * 24)); // Calculate age in days
  24. if (ageInDays > ageThreshold) {
  25. flaggedByAge.push({ directory, age: ageInDays });
  26. }
  27. } catch (error) {
  28. console.error(`Error processing directory ${directory}: ${error.message}`);
  29. }
  30. }
  31. return { flaggedBySize, flaggedByAge };
  32. }

Add your comment