Module: Legion::API::Routes::Stats

Defined in:
lib/legion/api/stats.rb

Constant Summary collapse

EXTENSION_IVARS =
{
  loaded:       :@loaded_extensions,
  discovered:   :@extensions,
  subscription: :@subscription_tasks,
  every:        :@timer_tasks,
  poll:         :@poll_tasks,
  once:         :@once_tasks,
  loop:         :@loop_tasks,
  running:      :@running_instances
}.freeze

Class Method Summary collapse

Class Method Details

.collect_apiObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/legion/api/stats.rb', line 191

def collect_api
  port = Legion::Settings.dig(:api, :port) || Legion::Settings.dig(:http, :port) || 4567
  info = { port: port }

  # Puma thread pool stats if available
  puma_server = Puma::Server.current if defined?(Puma::Server) && Puma::Server.respond_to?(:current)
  if puma_server.respond_to?(:pool_capacity)
    info[:puma] = {
      pool_capacity: puma_server.pool_capacity,
      max_threads:   puma_server.max_threads,
      running:       puma_server.running,
      backlog:       puma_server.backlog
    }
  end

  info[:routes] = begin
    Legion::API.routes.values.flatten.count
  rescue StandardError
    nil
  end
  info
rescue StandardError => e
  { error: e.message }
end

.collect_cacheObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/legion/api/stats.rb', line 79

def collect_cache
  return { connected: false } unless defined?(Legion::Cache)

  info = { connected: Legion::Cache.connected? }
  info[:using_local]  = Legion::Cache.using_local? if Legion::Cache.respond_to?(:using_local?)
  info[:using_memory] = Legion::Cache.instance_variable_get(:@using_memory) == true
  info[:driver] = begin
    Legion::Settings[:cache][:driver]
  rescue StandardError
    nil
  end

  if Legion::Cache.connected? && Legion::Cache.respond_to?(:size)
    info[:pool_size] = begin
      Legion::Cache.size
    rescue StandardError
      nil
    end
    info[:pool_available] = begin
      Legion::Cache.available
    rescue StandardError
      nil
    end
  end
  info
rescue StandardError => e
  { error: e.message }
end

.collect_cache_localObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/legion/api/stats.rb', line 108

def collect_cache_local
  return { connected: false } unless defined?(Legion::Cache::Local)

  info = { connected: Legion::Cache::Local.connected? }
  if Legion::Cache::Local.connected?
    info[:pool_size] = begin
      Legion::Cache::Local.size
    rescue StandardError
      nil
    end
    info[:pool_available] = begin
      Legion::Cache::Local.available
    rescue StandardError
      nil
    end
  end
  info
rescue StandardError => e
  { error: e.message }
end

.collect_dataObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/legion/api/stats.rb', line 162

def collect_data
  return { connected: false } unless defined?(Legion::Data) && Legion::Settings[:data][:connected]

  if Legion::Data.respond_to?(:stats)
    stats = Legion::Data.stats
    stats[:shared] || stats
  else
    { connected: true, adapter: begin
      Legion::Data::Connection.adapter
    rescue StandardError
      nil
    end }
  end
rescue StandardError => e
  { error: e.message }
end

.collect_data_localObject



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/legion/api/stats.rb', line 179

def collect_data_local
  return { connected: false } unless defined?(Legion::Data::Local) && Legion::Data::Local.connected?

  if Legion::Data::Local.respond_to?(:stats)
    Legion::Data::Local.stats
  else
    { connected: true }
  end
rescue StandardError => e
  { error: e.message }
end

.collect_extensionsObject



35
36
37
38
39
40
# File 'lib/legion/api/stats.rb', line 35

def collect_extensions
  ext = Legion::Extensions
  EXTENSION_IVARS.transform_values { |ivar| ext.instance_variable_get(ivar)&.count || 0 }
rescue StandardError => e
  { error: e.message }
end

.collect_gaiaObject



42
43
44
45
46
47
48
# File 'lib/legion/api/stats.rb', line 42

def collect_gaia
  return { started: false } unless defined?(Legion::Gaia) && Legion::Gaia.respond_to?(:started?) && Legion::Gaia.started?

  Legion::Gaia.status
rescue StandardError => e
  { error: e.message }
end

.collect_llmObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/legion/api/stats.rb', line 129

def collect_llm
  return { started: false } unless defined?(Legion::LLM) && Legion::LLM.started?

  info = { started: true }
  s = Legion::LLM.settings
  info[:default_model]    = s[:default_model]
  info[:default_provider] = s[:default_provider]
  info[:pipeline_enabled] = s[:pipeline_enabled] == true

  if defined?(Legion::LLM::Router) && Legion::LLM::Router.routing_enabled?
    info[:routing_enabled] = true
    tracker = Legion::LLM::Router.health_tracker
    if tracker
      providers = s[:providers] || {}
      info[:provider_health] = providers.each_with_object({}) do |(name, _cfg), h|
        h[name] = { circuit: tracker.circuit_state(name)&.to_s }
      rescue StandardError
        nil
      end
    end
  else
    info[:routing_enabled] = false
  end

  if defined?(Legion::LLM::ConversationStore)
    store = Legion::LLM::ConversationStore
    info[:conversations] = store.respond_to?(:size) ? store.size : nil
  end
  info
rescue StandardError => e
  { error: e.message }
end

.collect_transportObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/legion/api/stats.rb', line 50

def collect_transport
  conn = Legion::Transport::Connection
  connected = begin
    Legion::Settings[:transport][:connected]
  rescue StandardError
    false
  end
  connector = defined?(Legion::Transport::TYPE) ? Legion::Transport::TYPE.to_s : 'unknown'

  info = { connected: connected, connector: connector }

  session = conn.session
  if session.respond_to?(:open?) && session.open?
    info[:session_open] = true
    info[:channel_max] = session.channel_max if session.respond_to?(:channel_max)
    # Bunny tracks open channels in @channels hash
    channels = session.instance_variable_get(:@channels)
    info[:channels_open] = channels.is_a?(Hash) ? channels.count : nil
  else
    info[:session_open] = false
  end

  info[:build_session_open] = conn.build_session_open?
  info[:lite_mode] = conn.lite_mode?
  info
rescue StandardError => e
  { error: e.message }
end

.registered(app) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/legion/api/stats.rb', line 7

def self.registered(app)
  app.get '/api/stats' do
    result = {}
    result[:extensions]  = Routes::Stats.collect_extensions
    result[:gaia]        = Routes::Stats.collect_gaia
    result[:transport]   = Routes::Stats.collect_transport
    result[:cache]       = Routes::Stats.collect_cache
    result[:cache_local] = Routes::Stats.collect_cache_local
    result[:llm]         = Routes::Stats.collect_llm
    result[:data]        = Routes::Stats.collect_data
    result[:data_local]  = Routes::Stats.collect_data_local
    result[:api]         = Routes::Stats.collect_api
    json_response(result)
  end
end