Class: PruneCloudfilesDbBackups::RetentionCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/prune_cloudfiles_db_backups/retention_calculator.rb

Instance Method Summary collapse

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

Parameters:

  • objects (Array[String])

    from cloudfiles

  • day_retention (Integer) (defaults to: 14)
  • week_retention (Integer) (defaults to: 8)
  • month_retention (Integer) (defaults to: 12)


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_datesSet[DateTime]

Returns a calculated array of days to be kept.

Returns:

  • (Set[DateTime])

    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_datesSet[DateTime]

Returns a calculated array of days to be kept.

Returns:

  • (Set[DateTime])

    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_datesSet[DateTime]

Returns a calculated array of days to be kept.

Returns:

  • (Set[DateTime])

    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_deleteSet[Backup]

Returns a set of files for deletion.

Returns:

  • (Set[Backup])

    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_keepSet[Backup]

Returns a set of files that will be kept.

Returns:

  • (Set[Backup])

    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