Class: Llv::GroupStore

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/llv/group_store.rb

Overview

Thread-safe store of grouped log items. Maintains insertion order newest-first, evicts oldest beyond ‘limit`, and publishes events to any number of subscribers (web SSE, TUI, tests).

Defined Under Namespace

Classes: Group, Line

Instance Method Summary collapse

Constructor Details

#initialize(limit: 500) ⇒ GroupStore

Returns a new instance of GroupStore.



36
37
38
39
40
41
42
43
# File 'lib/llv/group_store.rb', line 36

def initialize(limit: 500)
  super()
  @limit = limit
  @by_id = {}
  @order = []   # newest first
  @subscribers = []
  @seq = 0
end

Instance Method Details

#fetch(id) ⇒ Object



75
76
77
# File 'lib/llv/group_store.rb', line 75

def fetch(id)
  synchronize { @by_id[id]&.to_h_full }
end

#ingest(event) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/llv/group_store.rb', line 45

def ingest(event)
  synchronize do
    gid = event.group_id || "untagged"
    new_group = !@by_id.key?(gid)
    group = @by_id[gid] ||= build_group(gid, event)

    if new_group
      @order.unshift(gid)
      enforce_limit
    end

    update_title(group, event)
    update_lifecycle(group, event)
    append_line(group, event)

    broadcast(new_group ? :group_created : :group_updated, group)
    broadcast(:group_completed, group) if event.type == :request_completed || event.type == :job_performed
    group
  end
end

#list(limit: nil, kind: nil) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/llv/group_store.rb', line 66

def list(limit: nil, kind: nil)
  synchronize do
    ids = @order
    ids = ids.select { |id| @by_id[id].kind == kind } if kind
    ids = ids.first(limit) if limit
    ids.map { |id| @by_id[id].summary }
  end
end

#sizeObject



88
89
90
# File 'lib/llv/group_store.rb', line 88

def size
  synchronize { @order.length }
end

#subscribe(&block) ⇒ Object



79
80
81
82
# File 'lib/llv/group_store.rb', line 79

def subscribe(&block)
  synchronize { @subscribers << block }
  block
end

#unsubscribe(handle) ⇒ Object



84
85
86
# File 'lib/llv/group_store.rb', line 84

def unsubscribe(handle)
  synchronize { @subscribers.delete(handle) }
end