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 29 |
# 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
31 32 33 |
# File 'lib/logster/group.rb', line 31 def self.max_size (defined?(@max_size) && @max_size) || MAX_SIZE end |
Instance Method Details
#add_message(message) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/logster/group.rb', line 54 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
99 100 101 |
# File 'lib/logster/group.rb', line 99 def changed? @changed end |
#remove_message(message) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/logster/group.rb', line 71 def () index = @messages_keys.index(.key) if index @messages_keys.slice!(index) @changed = true end end |
#to_h ⇒ Object
35 36 37 |
# File 'lib/logster/group.rb', line 35 def to_h { key: @key, messages_keys: @messages_keys, timestamp: @timestamp, count: @count } end |
#to_h_web ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/logster/group.rb', line 39 def to_h_web { regex: @key, count: @count, timestamp: @timestamp, messages: @messages, severity: -1, group: true, } end |
#to_json(opts = nil) ⇒ Object
50 51 52 |
# File 'lib/logster/group.rb', line 50 def to_json(opts = nil) JSON.fast_generate(self.to_h, opts) end |