From 53d780a40c7d80ede0e993fa7675d2b356f1b917 Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Wed, 23 May 2012 18:08:05 +0200 Subject: [PATCH] SONAR-3378 Add new DB Cleaner parameter => Make configurable the number of hours before keeping only one snapshot per day --- .../sonar/plugins/dbcleaner/DbCleanerPlugin.java | 15 +++++++++++---- .../dbcleaner/api/DbCleanerConstants.java | 1 + .../sonar/plugins/dbcleaner/period/Filters.java | 16 +++++++++++----- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java index 6b2921e61a4..ea37994b990 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java @@ -31,17 +31,24 @@ import org.sonar.plugins.dbcleaner.period.DefaultPeriodCleaner; import java.util.List; @Properties({ + @Property(key = DbCleanerConstants.HOURS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_DAY, defaultValue = "24", + name = "Number of hours before starting to keep only one snapshot per day", + description = "After this number of hours, if there are several snapshots during the same day, " + + "the DbCleaner keeps the most recent one and fully delete the other ones.", + global = true, + project = true, + type = PropertyType.INTEGER), @Property(key = DbCleanerConstants.WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK, defaultValue = "4", - name = "Number of weeks before starting to keep only one snapshot by week", + name = "Number of weeks before starting to keep only one snapshot per week", description = "After this number of weeks, if there are several snapshots during the same week, " - + "the DbCleaner keeps the first one and fully delete the other ones.", + + "the DbCleaner keeps the most recent one and fully delete the other ones.", global = true, project = true, type = PropertyType.INTEGER), @Property(key = DbCleanerConstants.WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH, defaultValue = "52", - name = "Number of weeks before starting to keep only one snapshot by month", + name = "Number of weeks before starting to keep only one snapshot per month", description = "After this number of weeks, if there are several snapshots during the same month, " - + "the DbCleaner keeps the first one and fully delete the other ones.", + + "the DbCleaner keeps the most recent one and fully delete the other ones.", global = true, project = true, type = PropertyType.INTEGER), diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/DbCleanerConstants.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/DbCleanerConstants.java index 0dcca8e9424..655b9b0b641 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/DbCleanerConstants.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/DbCleanerConstants.java @@ -25,6 +25,7 @@ public interface DbCleanerConstants { String PLUGIN_NAME = "DbCleaner"; String PROPERTY_CLEAN_DIRECTORY = "sonar.dbcleaner.cleanDirectory"; + String HOURS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_DAY = "sonar.dbcleaner.hoursBeforeKeepingOnlyOneSnapshotByDay"; String WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK = "sonar.dbcleaner.weeksBeforeKeepingOnlyOneSnapshotByWeek"; String WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH = "sonar.dbcleaner.weeksBeforeKeepingOnlyOneSnapshotByMonth"; String WEEKS_BEFORE_DELETING_ALL_SNAPSHOTS = "sonar.dbcleaner.weeksBeforeDeletingAllSnapshots"; diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Filters.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Filters.java index 3b599513860..8edac58daa6 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Filters.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Filters.java @@ -32,11 +32,12 @@ class Filters { private final List filters = Lists.newArrayList(); Filters(Settings settings) { - Date dateToStartKeepingOneSnapshotByWeek = getDate(settings, DbCleanerConstants.WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK); - Date dateToStartKeepingOneSnapshotByMonth = getDate(settings, DbCleanerConstants.WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH); - Date dateToStartDeletingAllSnapshots = getDate(settings, DbCleanerConstants.WEEKS_BEFORE_DELETING_ALL_SNAPSHOTS); + Date dateToStartKeepingOneSnapshotByDay = getDateFromHours(settings, DbCleanerConstants.HOURS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_DAY); + Date dateToStartKeepingOneSnapshotByWeek = getDateFromWeeks(settings, DbCleanerConstants.WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK); + Date dateToStartKeepingOneSnapshotByMonth = getDateFromWeeks(settings, DbCleanerConstants.WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH); + Date dateToStartDeletingAllSnapshots = getDateFromWeeks(settings, DbCleanerConstants.WEEKS_BEFORE_DELETING_ALL_SNAPSHOTS); - filters.add(new KeepOneFilter(dateToStartKeepingOneSnapshotByWeek, new Date(), Calendar.DAY_OF_YEAR, "day")); + filters.add(new KeepOneFilter(dateToStartKeepingOneSnapshotByWeek, dateToStartKeepingOneSnapshotByDay, Calendar.DAY_OF_YEAR, "day")); filters.add(new KeepOneFilter(dateToStartKeepingOneSnapshotByMonth, dateToStartKeepingOneSnapshotByWeek, Calendar.WEEK_OF_YEAR, "week")); filters.add(new KeepOneFilter(dateToStartDeletingAllSnapshots, dateToStartKeepingOneSnapshotByMonth, Calendar.MONTH, "month")); filters.add(new DeleteAllFilter(dateToStartDeletingAllSnapshots)); @@ -46,8 +47,13 @@ class Filters { return filters; } - static Date getDate(Settings settings, String propertyKey) { + static Date getDateFromWeeks(Settings settings, String propertyKey) { int weeks = settings.getInt(propertyKey); return DateUtils.addWeeks(new Date(), -weeks); } + + static Date getDateFromHours(Settings settings, String propertyKey) { + int hours = settings.getInt(propertyKey); + return DateUtils.addHours(new Date(), -hours); + } } -- 2.39.5