Class: Threshold::Thresholds

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/threshold/thresholds.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thresholds = []) ⇒ Thresholds

Returns a new instance of Thresholds.



17
18
19
# File 'lib/threshold/thresholds.rb', line 17

def initialize(thresholds = [])
  @thresholds = thresholds
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



13
14
15
# File 'lib/threshold/thresholds.rb', line 13

def file
  @file
end

#readonlyObject

Returns the value of attribute readonly.



13
14
15
# File 'lib/threshold/thresholds.rb', line 13

def readonly
  @readonly
end

Instance Method Details

#&(an0ther) ⇒ Object

Returns a new Threshold Object



162
163
164
# File 'lib/threshold/thresholds.rb', line 162

def &(an0ther)
  Thresholds.new(@thresholds & an0ther.to_a)
end

#+(an0ther) ⇒ Object

+ (concat) Returns a new Threshold Object



151
152
153
# File 'lib/threshold/thresholds.rb', line 151

def +(an0ther)
  Thresholds.new(@thresholds + an0ther.to_a)
end

#-(an0ther) ⇒ Object

  • (Difference)

Returns a new Threshold Object



168
169
170
# File 'lib/threshold/thresholds.rb', line 168

def -(an0ther)
  Thresholds.new(@thresholds - an0ther.to_a)
end

#event_filters(&blk) ⇒ Object

Returns a new Threshold Object with just event_filters



182
183
184
185
186
187
188
# File 'lib/threshold/thresholds.rb', line 182

def event_filters(&blk)
  if block_given?
    self.event_filters.select(&blk)
  else
    Thresholds.new(@thresholds.select{|t| t.class.to_s == "Threshold::EventFilter"})
  end
end

#flushObject

Write changes to the file



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/threshold/thresholds.rb', line 22

def flush
  begin
    valid_existing_file?(@file)
    raise ReadOnlyThresholdsFile if @readonly
    hash = current_hash
    file = File.open(@file, 'w+')
    raise ThresholdAtomicLockFailure, 'The @file state/hash changed before we could flush the file' unless stored_hash == hash
    file.write self.sort.to_s
    file.close

  rescue NonExistantThresholdFile
    raise ReadOnlyThresholdsFile if @readonly
    file = File.open(@file, 'w')
    file.write self.sort.to_s
    file.close
  end

  stored_hash=current_hash
  return true
end

#loadfileObject

Append in the thresholds.conf file to current collection



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/threshold/thresholds.rb', line 50

def loadfile
  valid_existing_file?(@file)

  results = Threshold::Parser.new(@file)
  @stored_hash= results.filehash
  #puts stored_hash
  results.caps.each do |result|
    builder = Threshold::Builder.new(result)
    self << builder.build
  end

end

#loadfile!Object

Clears current collection and Read in the thresholds.conf file



44
45
46
47
# File 'lib/threshold/thresholds.rb', line 44

def loadfile!
  @thresholds.clear
  loadfile
end

#rate_filters(&blk) ⇒ Object

Returns a new Threshold Object with just rate_filters



191
192
193
194
195
196
197
# File 'lib/threshold/thresholds.rb', line 191

def rate_filters(&blk)
  if block_given?
    self.rate_filters.select(&blk)
  else
    Thresholds.new(@thresholds.select{|t| t.class.to_s == "Threshold::RateFilter"})
  end
end

#reject(&blk) ⇒ Object

Returns a new Threshold Object



119
120
121
122
123
124
125
# File 'lib/threshold/thresholds.rb', line 119

def reject(&blk)
  if block_given?
    Thresholds.new(@thresholds.reject(&blk))
  else
    Thresholds.new(@thresholds.reject)
  end
end

#reverseObject

Returns a new Threshold Object



109
110
111
# File 'lib/threshold/thresholds.rb', line 109

def reverse
  Thresholds.new(@thresholds.reverse)
end

#select(&blk) ⇒ Object

Returns a new Threshold Object



128
129
130
131
132
133
134
# File 'lib/threshold/thresholds.rb', line 128

def select(&blk)
  if block_given?
    Thresholds.new(@thresholds.select(&blk))
  else
    Thresholds.new(@thresholds.select)
  end
end

#shuffleObject

Returns a new Threshold Object



114
115
116
# File 'lib/threshold/thresholds.rb', line 114

def shuffle
  Thresholds.new(@thresholds.shuffle)
end

#sortObject

Returns a new Threshold Object



104
105
106
# File 'lib/threshold/thresholds.rb', line 104

def sort
  Thresholds.new(@thresholds.sort)
end

#stored_hashObject

The calculated hash of the threshold.conf file at load time.



92
93
94
# File 'lib/threshold/thresholds.rb', line 92

def stored_hash
  @stored_hash
end

#suppressions(&blk) ⇒ Object

Returns a new Threshold Object with just suppressions



173
174
175
176
177
178
179
# File 'lib/threshold/thresholds.rb', line 173

def suppressions(&blk)
  if block_given?
    self.suppressions.select(&blk)
  else
    Thresholds.new(@thresholds.select{|t| t.class.to_s == "Threshold::Suppression"})
  end
end

#to_aObject



95
96
97
# File 'lib/threshold/thresholds.rb', line 95

def to_a
  @thresholds
end

#to_s(skip = false) ⇒ Object

Pass (true) to_s to skip the printing of InternalObjects.comment



80
81
82
83
84
85
86
87
88
89
# File 'lib/threshold/thresholds.rb', line 80

def to_s(skip = false)
  output = ""

  raise InvalidThresholdsObject, "Container object has unknown objects" unless valid?

  self.each do |threshold|
    output << threshold.to_s(skip) + "\n"
  end
  return output
end

#uniq(&blk) ⇒ Object

Uniques by default to printable output Returns a new Threshold Object



138
139
140
141
142
143
144
# File 'lib/threshold/thresholds.rb', line 138

def uniq(&blk)
  if block_given?
    Thresholds.new(@thresholds.uniq(&blk))
  else
    Thresholds.new(@thresholds.uniq{ |lineitem| lineitem.to_s(true) })
  end
end

#valid?Boolean

Check if all objects in the Threshold Instance report .valid?

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/threshold/thresholds.rb', line 64

def valid?
  begin
    self.each do |threshold|
      if threshold.respond_to?(:valid?)
        return false unless threshold.valid?
      else
        raise InvalidThresholdsObject, "Container object has unknown objects"
      end
    end
    return true
  rescue InvalidThresholdsObject
    return false
  end
end

#|(an0ther) ⇒ Object

| (intersect) Returns a new Threshold Object



157
158
159
# File 'lib/threshold/thresholds.rb', line 157

def |(an0ther)
  Thresholds.new(@thresholds | an0ther.to_a)
end