summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPatrick J Cherry <patrick@bytemark.co.uk>2014-12-05 17:50:54 +0000
committerPatrick J Cherry <patrick@bytemark.co.uk>2014-12-05 17:50:54 +0000
commit5129820f2f2535086c0634cb0b6ea14f5e3d5d1c (patch)
treefd4de769d3a0a46d548745808dba5208e2916213 /lib
parentea336814acc5fd51c385ae837f280670d0fc3a45 (diff)
Re-jigged least-relevant backup selection. ALSO PARSE FILE PATHS AS TIMES CORRECTLY
Diffstat (limited to 'lib')
-rw-r--r--lib/byteback/backup_directory.rb42
1 files changed, 25 insertions, 17 deletions
diff --git a/lib/byteback/backup_directory.rb b/lib/byteback/backup_directory.rb
index 6404cd8..a31c290 100644
--- a/lib/byteback/backup_directory.rb
+++ b/lib/byteback/backup_directory.rb
@@ -8,27 +8,35 @@ module Byteback
# Order backups by their closeness to defined backup times, which are
# listed in a set order (i.e. today's backup is more important than yesterday's).
#
- BACKUP_IMPORTANCE = [0, 1, 2, 7, 14, 21, 28, 56, 112]
+ BACKUP_IMPORTANCE = [1, 2, 7, 14, 21, 28, 56, 112]
def sort_by_importance(snapshots_unsorted, now=Time.now)
snapshots_sorted = []
+ scores = Array.new{|h,k| h[k] = []}
+ times = snapshots_unsorted.map(&:time)
- # FIXME: takes about a minute to sort 900 items,
- # seems like that ought to be quicker than O(n^2)
+ BACKUP_IMPORTANCE.each_with_index do |days, backup_idx|
+ target_time = now.to_i - (days*86400)
+ weight = days.to_f - (backup_idx == 0 ? 0 : BACKUP_IMPORTANCE[backup_idx-1])
+ scores << times.map{|t| (t.to_i - target_time).abs/weight }
+ end
+
+ #
+ # Find the index of the lowest score from the list of BACKUP_IMPORTANCE
#
- while !snapshots_unsorted.empty?
- BACKUP_IMPORTANCE.each do |days|
- target_time = now - (days*86400)
- closest = snapshots_unsorted.inject(nil) do |best, snapshot|
- if best.nil? || (snapshot.time-target_time).abs < (best.time-target_time).abs
- snapshot
- else
- best
- end
- end
- break unless closest
- snapshots_sorted << snapshots_unsorted.delete(closest)
- end
+ nearest_target = scores.transpose.map{|s| s.find_index(s.min)}
+
+ BACKUP_IMPORTANCE.each_index do |backup_idx|
+ #
+ # Find the indicies of the snapshots that match the current BACKUP_IMPORTANCE index, and sort them according to their score.
+ best_snapshot_idxs = nearest_target.each_index.
+ select{|i| nearest_target[i] == backup_idx}.
+ sort{|a,b| scores[backup_idx][a] <=> scores[backup_idx][b]}
+
+ #
+ # Append them to the array.
+ #
+ snapshots_sorted += snapshots_unsorted.values_at(*best_snapshot_idxs)
end
snapshots_sorted
@@ -45,7 +53,7 @@ module Byteback
end
def time
- Time.parse(path)
+ Time.parse(File.basename(path))
end
def <=>(b)