From 4bde1ae7d697a3dc18d4f08eb6bbe8d3c70e7f45 Mon Sep 17 00:00:00 2001 From: Stefan Date: Fri, 30 Jan 2026 18:21:53 +0100 Subject: [PATCH] Added more statuses for notifications regarding how long time has passed since last scan for individual dirs --- .../org/pwss/controller/HomeController.java | 13 +- .../org/pwss/util/MonitoredDirectoryUtil.java | 131 +++++++++++------- 2 files changed, 88 insertions(+), 56 deletions(-) diff --git a/src/main/java/org/pwss/controller/HomeController.java b/src/main/java/org/pwss/controller/HomeController.java index 61d761c..543b6e3 100644 --- a/src/main/java/org/pwss/controller/HomeController.java +++ b/src/main/java/org/pwss/controller/HomeController.java @@ -67,6 +67,7 @@ import org.pwss.util.ErrorUtil; import org.pwss.util.LiveFeedUtil; import org.pwss.util.MonitoredDirectoryUtil; +import org.pwss.util.MonitoredDirectoryUtil.DirNotificationStatus; import org.pwss.util.OSUtil; import org.pwss.util.ReportUtil; import org.pwss.util.ScanUtil; @@ -530,15 +531,9 @@ public Component getListCellRendererComponent(JList list, Object value, int i if (value instanceof MonitoredDirectory dir) { setText(dir.path()); - if (!dir.baselineEstablished()) { - setForeground(Color.YELLOW); - setToolTipText(StringConstants.TOOLTIP_BASELINE_NOT_ESTABLISHED); - } else if (MonitoredDirectoryUtil.isScanOlderThanAWeek(dir)) { - setForeground(Color.ORANGE); - setToolTipText(StringConstants.TOOLTIP_OLD_SCAN); - } else { - setToolTipText(dir.path()); - } + DirNotificationStatus status = MonitoredDirectoryUtil.getDirNotificationStatus(dir); + setForeground(status.getForegroundColor()); + setToolTipText(status.getTooltipText()); } return this; } diff --git a/src/main/java/org/pwss/util/MonitoredDirectoryUtil.java b/src/main/java/org/pwss/util/MonitoredDirectoryUtil.java index 73507b1..afbbb46 100644 --- a/src/main/java/org/pwss/util/MonitoredDirectoryUtil.java +++ b/src/main/java/org/pwss/util/MonitoredDirectoryUtil.java @@ -1,5 +1,6 @@ package org.pwss.util; +import java.awt.Color; import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; @@ -19,6 +20,50 @@ public final class MonitoredDirectoryUtil { */ private final static Logger log = org.slf4j.LoggerFactory.getLogger(MonitoredDirectoryUtil.class); + /** + * Enumeration representing the notification status of a monitored directory + * based on its last scan time. + */ + public enum DirNotificationStatus { + UP_TO_DATE, + NEVER_SCANNED, + NO_BASELINE, + WEEK_OLD, + TWO_WEEKS_OLD, + MONTH_OLD, + YEAR_OLD; + + /** + * Gets the color associated with the notification status. + * + * @return Color representing the notification status + */ + public Color getForegroundColor() { + return switch (this) { + case UP_TO_DATE -> Color.GREEN; + case WEEK_OLD, TWO_WEEKS_OLD -> Color.ORANGE; + case NEVER_SCANNED, NO_BASELINE, YEAR_OLD, MONTH_OLD -> Color.RED; + }; + } + + /** + * Gets the tooltip text associated with the notification status. + * + * @return String containing the tooltip text + */ + public String getTooltipText() { + return switch (this) { + case UP_TO_DATE -> "Directory is up to date."; + case NEVER_SCANNED -> "Directory has never been scanned."; + case NO_BASELINE -> "Directory has no established baseline."; + case WEEK_OLD -> "Directory was last scanned over a week ago."; + case TWO_WEEKS_OLD -> "Directory was last scanned over two weeks ago."; + case MONTH_OLD -> "Directory was last scanned over a month ago."; + case YEAR_OLD -> "Directory was last scanned over a year ago."; + }; + } + } + /** * Private constructor to prevent instantiation */ @@ -41,65 +86,57 @@ public static String getMonitoredDirectoryNotificationMessage(List message.append( + "Directory '").append(dir.path()).append("' has never been scanned.\n"); + case DirNotificationStatus.NO_BASELINE -> message.append( + "Directory '").append(dir.path()).append("' has no established baseline.\n"); + case DirNotificationStatus.WEEK_OLD -> message.append( + "Directory '").append(dir.path()).append("' was last scanned over a week ago.\n"); + case DirNotificationStatus.TWO_WEEKS_OLD -> message.append( + "Directory '").append(dir.path()).append("' was last scanned over two weeks ago.\n"); + case DirNotificationStatus.MONTH_OLD -> message.append( + "Directory '").append(dir.path()).append("' was last scanned over a month ago.\n"); + case DirNotificationStatus.YEAR_OLD -> message.append( + "Directory '").append(dir.path()).append("' was last scanned over a year ago.\n"); + case DirNotificationStatus.UP_TO_DATE -> { + // No notification needed for up-to-date directories + } } } return message.toString(); } - + /** - * Checks if the time since the last scan of the given directory - * is more than one week (7 days). + * Determines the scan age status of a monitored directory based on the time + * since its last scan. * * @param dir the MonitoredDirectory to check - * @return true if more than 7 days have passed since last scan, false otherwise + * @return the ScanAgeStatus representing the age of the last scan */ - public static boolean isScanOlderThanAWeek(MonitoredDirectory dir) { - if (dir == null) { - log.error("Can not check if scan is older than a week due to a null error"); - log.debug("Monitored Directory is null in method: isScanOlderThanAWeek"); - return false; - } - - if (dir.lastScanned() == null) { - log.debug("Monitored Directory with id {} has not been scanned for 7 days", dir.id()); - return true; // treat null as "needs scan" + public static DirNotificationStatus getDirNotificationStatus(MonitoredDirectory dir) { + if (!dir.baselineEstablished()) { + return DirNotificationStatus.NO_BASELINE; + } else if (dir.lastScanned() == null) { + return DirNotificationStatus.NEVER_SCANNED; } Instant lastScan = dir.lastScanned().toInstant(); - Instant oneWeekAgo = Instant.now().minus(Duration.ofDays(7)); - - return lastScan.isBefore(oneWeekAgo); - } - - /** - * NOTE: FOR TESTING PURPOSES ONLY - * Checks if the time since the last scan of the given directory - * is more than one minute. - * - * @param dir the MonitoredDirectory to check - * @return true if more than 1 minute has passed since last scan, false - * otherwise - */ - public static boolean isScanOlderThan1Minute(MonitoredDirectory dir) { - if (dir == null) { - log.error("Can not check if scan is older than a minute due to a null error"); - log.debug("Monitored Directory is null in method: isScanOlderThan1Minute"); - return false; + Instant now = Instant.now(); + + long daysSinceLastScan = Duration.between(lastScan, now).toDays(); + + if (daysSinceLastScan > 365) { + return DirNotificationStatus.YEAR_OLD; + } else if (daysSinceLastScan > 30) { + return DirNotificationStatus.MONTH_OLD; + } else if (daysSinceLastScan > 14) { + return DirNotificationStatus.TWO_WEEKS_OLD; + } else if (daysSinceLastScan > 7) { + return DirNotificationStatus.WEEK_OLD; + } else { + return DirNotificationStatus.UP_TO_DATE; } - - if (dir.lastScanned() == null) { - log.debug("Monitored Directory with id {} has not been scanned for 1 minute", dir.id()); - return true; // treat null as "needs scan" - } - - Instant lastScan = dir.lastScanned().toInstant(); - Instant oneMinuteAgo = Instant.now().minus(Duration.ofMinutes(1)); - - return lastScan.isBefore(oneMinuteAgo); } /**