Class: Logster::Group
- Inherits:
-
Object
- Object
- Logster::Group
- Defined in:
- lib/logster/group.rb
Defined Under Namespace
Classes: GroupWeb
Constant Summary collapse
- MAX_SIZE =
100
Instance Attribute Summary collapse
-
#changed ⇒ Object
Returns the value of attribute changed.
-
#count ⇒ Object
Returns the value of attribute count.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#messages ⇒ Object
Returns the value of attribute messages.
-
#messages_keys ⇒ Object
readonly
Returns the value of attribute messages_keys.
-
#pattern ⇒ Object
Returns the value of attribute pattern.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
Class Method Summary collapse
Instance Method Summary collapse
- #add_message(message) ⇒ Object
- #changed? ⇒ Boolean
-
#initialize(key, messages_keys = [], timestamp: 0, count: 0) ⇒ Group
constructor
A new instance of Group.
- #remove_message(message) ⇒ Object
- #to_h ⇒ Object
- #to_h_web ⇒ Object
- #to_json(opts = nil) ⇒ Object
Constructor Details
#initialize(key, messages_keys = [], timestamp: 0, count: 0) ⇒ Group
Returns a new instance of Group.
10 11 12 13 14 15 16 |
# File 'lib/logster/group.rb', line 10 def initialize(key, = [], timestamp: 0, count: 0) @key = key @messages_keys = || [] @timestamp = @count = count @changed = true end |
Instance Attribute Details
#changed ⇒ Object
Returns the value of attribute changed.
8 9 10 |
# File 'lib/logster/group.rb', line 8 def changed @changed end |
#count ⇒ Object
Returns the value of attribute count.
8 9 10 |
# File 'lib/logster/group.rb', line 8 def count @count end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/logster/group.rb', line 7 def key @key end |
#messages ⇒ Object
Returns the value of attribute messages.
7 8 9 |
# File 'lib/logster/group.rb', line 7 def @messages end |
#messages_keys ⇒ Object (readonly)
Returns the value of attribute messages_keys.
7 8 9 |
# File 'lib/logster/group.rb', line 7 def @messages_keys end |
#pattern ⇒ Object
Returns the value of attribute pattern.
8 9 10 |
# File 'lib/logster/group.rb', line 8 def pattern @pattern end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
7 8 9 |
# File 'lib/logster/group.rb', line 7 def @timestamp end |
Class Method Details
.from_json(json) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/logster/group.rb', line 18 def self.from_json(json) hash = JSON.parse(json) group = new( hash["key"], hash["messages_keys"], timestamp: hash["timestamp"] || 0, count: hash["count"] || 0 ) group.changed = false group end |
.max_size ⇒ Object
30 31 32 |
# File 'lib/logster/group.rb', line 30 def self.max_size (defined?(@max_size) && @max_size) || MAX_SIZE end |
Instance Method Details
#add_message(message) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/logster/group.rb', line 58 def () if !@messages_keys.include?(.key) @messages_keys.unshift(.key) @count += 1 @changed = true end if @timestamp < . @timestamp = . @messages_keys.unshift(@messages_keys.slice!(@messages_keys.index(.key))) @changed = true end if self.count > max_size @messages_keys.slice!(max_size..-1) @changed = true end end |
#changed? ⇒ Boolean
104 105 106 |
# File 'lib/logster/group.rb', line 104 def changed? @changed end |
#remove_message(message) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/logster/group.rb', line 75 def () index = @messages_keys.index(.key) if index @messages_keys.slice!(index) @changed = true end end |
#to_h ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/logster/group.rb', line 34 def to_h { key: @key, messages_keys: @messages_keys, timestamp: @timestamp, count: @count } end |
#to_h_web ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/logster/group.rb', line 43 def to_h_web { regex: @key, count: @count, timestamp: @timestamp, messages: @messages, severity: -1, group: true } end |
#to_json(opts = nil) ⇒ Object
54 55 56 |
# File 'lib/logster/group.rb', line 54 def to_json(opts = nil) JSON.fast_generate(self.to_h, opts) end |