Class: Artifactory::Cleaner::ArtifactBucket

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/artifactory/cleaner/artifact_bucket.rb

Overview

A collection of Artifacts within a date range

An Artifactory::Cleaner::ArtifactBucket represents an “age bucket” when analyzing Artifact usage; Artifacts are grouped into buckets of time to aid in developing an archive strategy.

Artifactory::Cleaner::ArtifactBucket is largely just an Array of Artifactory::Resource::Artifact instances, with logic to maintain a filesize count and properties fr the age of the artifacts within.

This class works with the Artifactory::Cleaner::ArtifactBucketCollection class, which maintains a collection of Artifactory::Cleaner::ArtifactBucket instances and handles selecting the proper one for a given Artifact

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min, max = nil) ⇒ ArtifactBucket

ArtifactBucket constructor

Params:

min

Lower bound (in days) for the age of artifacts this bucket should contain

max

Upper bound (in days) for the age of artifacts this bucket should contain, defaults to none (infinity)



32
33
34
35
36
37
# File 'lib/artifactory/cleaner/artifact_bucket.rb', line 32

def initialize(min,max=nil)
  @min = min
  @max = max.nil? ? Float::INFINITY : max
  @filesize = 0
  @collection = []
end

Instance Attribute Details

#filesizeObject (readonly)

Returns the value of attribute filesize.



24
25
26
# File 'lib/artifactory/cleaner/artifact_bucket.rb', line 24

def filesize
  @filesize
end

#maxObject (readonly)

Returns the value of attribute max.



23
24
25
# File 'lib/artifactory/cleaner/artifact_bucket.rb', line 23

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



22
23
24
# File 'lib/artifactory/cleaner/artifact_bucket.rb', line 22

def min
  @min
end

Instance Method Details

#[]=(key, artifact) ⇒ Object

Update an artifact in the bucket

TODO: This method does not validate if the artifact still belongs in this bucket by age

Raises:

  • (TypeError)


51
52
53
54
55
56
# File 'lib/artifactory/cleaner/artifact_bucket.rb', line 51

def []=(key, artifact)
  raise TypeError, "expected Artifactory::Resource::Artifact, got #{artifact.class.name}" unless artifact.is_a? Artifactory::Resource::Artifact
  @filesize -= @collection[key].size if @collection[key].is_a? Artifactory::Resource::Artifact
  @filesize += artifact.size
  @collection[key] = artifact
end

#covers?(age) ⇒ Boolean

Given an age (in days) return true if this bucket covers that age (if it’s within the min and max of this bucket)

Returns:

  • (Boolean)


43
44
45
# File 'lib/artifactory/cleaner/artifact_bucket.rb', line 43

def covers?(age)
  age >= @min && age < @max
end

#push(artifact) ⇒ Object Also known as: <<

Add an artifact to the end of this bucket

Calls push on the Array which backs this bucket

Aliased as method ‘<<`

TODO: This method does not validate if the artifact belongs in this bucket by age

@see: Array#push

Raises:

  • (TypeError)


68
69
70
71
72
73
# File 'lib/artifactory/cleaner/artifact_bucket.rb', line 68

def push(artifact)
  raise TypeError, "expected Artifactory::Resource::Artifact, got #{artifact.class.name}" unless artifact.is_a? Artifactory::Resource::Artifact
  @collection.push artifact
  @filesize += artifact.size
  self
end

#recalculate_filesizeObject

Recalculate the file size of this bucket by adding up the size of all artifacts it contains

This method forces recalculation of the total fize size of all artifacts within this bucket; the filesize is tracked automatically as artifacts are added, so thi method should be unnecessary. It is av available in case the tracking built in to ‘push`/`unshift`/`[]=` hsa a bug, or in case artifact sizes somehow change



97
98
99
# File 'lib/artifactory/cleaner/artifact_bucket.rb', line 97

def recalculate_filesize
  @filesize = @collection.reduce(0) {|sum,asset| sum + asset.size}
end

#unshift(artifact) ⇒ Object

Add an artifact to the beginning of this bucket

Calls unshift on the Array which backs this bucket

TODO: This method does not validate if the artifact belongs in this bucket by age

Raises:

  • (TypeError)

See Also:

  • Array#unshift


84
85
86
87
88
89
# File 'lib/artifactory/cleaner/artifact_bucket.rb', line 84

def unshift(artifact)
  raise TypeError, "expected Artifactory::Resource::Artifact, got #{artifact.class.name}" unless artifact.is_a? Artifactory::Resource::Artifact
  @collection.unshift artifact
  @filesize += artifact.size
  self
end