Class: PruneCloudfilesDbBackups::RetentionCalculator
- Inherits:
-
Object
- Object
- PruneCloudfilesDbBackups::RetentionCalculator
- Defined in:
- lib/prune_cloudfiles_db_backups/retention_calculator.rb
Instance Method Summary collapse
-
#initialize(objects, day_retention = 14, week_retention = 8, month_retention = 12) ⇒ RetentionCalculator
constructor
Initialize the Retention Calculator with a list of cloudfile container objects.
-
#keep_daily_dates ⇒ Set[DateTime]
A calculated array of days to be kept.
-
#keep_monthly_dates ⇒ Set[DateTime]
A calculated array of days to be kept.
-
#keep_weekly_dates ⇒ Set[DateTime]
A calculated array of days to be kept.
-
#list_to_delete ⇒ Set[Backup]
A set of files for deletion.
-
#list_to_keep ⇒ Set[Backup]
A set of files that will be kept.
Constructor Details
#initialize(objects, day_retention = 14, week_retention = 8, month_retention = 12) ⇒ RetentionCalculator
Initialize the Retention Calculator with a list of cloudfile container objects
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/prune_cloudfiles_db_backups/retention_calculator.rb', line 14 def initialize(objects, day_retention = 14, week_retention = 8, month_retention = 12) @day_retention = day_retention || 14 @week_retention = week_retention || 8 @month_retention = month_retention || 12 @now = DateTime.now sets = objects.group_by do |o| /(^.+\.pgdump).*/.match(o)[1] end @backup_sets = sets.map do |_, v| objects = Set.new(v) datetime = Backup.parse_for_datetime(objects) daily = keep_daily_dates.include?(datetime.to_date) weekly = keep_weekly_dates.include?(datetime.to_date) monthly = keep_monthly_dates.include?(datetime.to_date) daily = true if datetime.to_date > @now Backup.new(objects: objects, datetime: datetime, daily: daily, weekly: weekly, monthly: monthly) end.to_set end |
Instance Method Details
#keep_daily_dates ⇒ Set[DateTime]
Returns a calculated array of days to be kept.
49 50 51 52 53 |
# File 'lib/prune_cloudfiles_db_backups/retention_calculator.rb', line 49 def keep_daily_dates @daily_dates ||= (0...@day_retention).map do |i| @now.at_midnight.advance(days: -i).to_date end.to_set end |
#keep_monthly_dates ⇒ Set[DateTime]
Returns a calculated array of days to be kept.
64 65 66 67 68 69 |
# File 'lib/prune_cloudfiles_db_backups/retention_calculator.rb', line 64 def keep_monthly_dates @monthly_dates ||= (0...@month_retention).map do |i| # First Sunday of every month @now.at_beginning_of_month.advance(months: -i).advance(days: 6).beginning_of_week(:sunday).to_date end.to_set end |
#keep_weekly_dates ⇒ Set[DateTime]
Returns a calculated array of days to be kept.
56 57 58 59 60 61 |
# File 'lib/prune_cloudfiles_db_backups/retention_calculator.rb', line 56 def keep_weekly_dates @weekly_dates ||= (0...@week_retention).map do |i| # Sunday on the last couple of weeks @now.at_beginning_of_week(:sunday).advance(weeks: -i).to_date end.to_set end |
#list_to_delete ⇒ Set[Backup]
Returns a set of files for deletion.
72 73 74 75 76 |
# File 'lib/prune_cloudfiles_db_backups/retention_calculator.rb', line 72 def list_to_delete @backup_sets.select do |set| set.deletable? end end |
#list_to_keep ⇒ Set[Backup]
Returns a set of files that will be kept.
79 80 81 82 83 |
# File 'lib/prune_cloudfiles_db_backups/retention_calculator.rb', line 79 def list_to_keep @backup_sets.select do |set| !set.deletable? end end |