Class: Logster::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/logster/group.rb

Defined Under Namespace

Classes: GroupWeb

Constant Summary collapse

MAX_SIZE =
100

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, messages_keys = [], timestamp: 0, count: 0)
  @key = key
  @messages_keys = messages_keys || []
  @timestamp = timestamp
  @count = count
  @changed = true
end

Instance Attribute Details

#changedObject

Returns the value of attribute changed.



8
9
10
# File 'lib/logster/group.rb', line 8

def changed
  @changed
end

#countObject

Returns the value of attribute count.



8
9
10
# File 'lib/logster/group.rb', line 8

def count
  @count
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/logster/group.rb', line 7

def key
  @key
end

#messagesObject

Returns the value of attribute messages.



7
8
9
# File 'lib/logster/group.rb', line 7

def messages
  @messages
end

#messages_keysObject (readonly)

Returns the value of attribute messages_keys.



7
8
9
# File 'lib/logster/group.rb', line 7

def messages_keys
  @messages_keys
end

#patternObject

Returns the value of attribute pattern.



8
9
10
# File 'lib/logster/group.rb', line 8

def pattern
  @pattern
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



7
8
9
# File 'lib/logster/group.rb', line 7

def timestamp
  @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_sizeObject



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 add_message(message)
  if !@messages_keys.include?(message.key)
    @messages_keys.unshift(message.key)
    @count += 1
    @changed = true
  end
  if @timestamp < message.timestamp
    @timestamp = message.timestamp
    @messages_keys.unshift(@messages_keys.slice!(@messages_keys.index(message.key)))
    @changed = true
  end
  if self.count > max_size
    @messages_keys.slice!(max_size..-1)
    @changed = true
  end
end

#changed?Boolean

Returns:

  • (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 remove_message(message)
  index = @messages_keys.index(message.key)
  if index
    @messages_keys.slice!(index)
    @changed = true
  end
end

#to_hObject



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_webObject



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