Module: Legion::API::Routes::ApolloHelpers

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

Instance Method Summary collapse

Instance Method Details

#apollo_data_connected?Boolean

Returns:

  • (Boolean)


124
125
126
127
128
129
# File 'lib/legion/api/apollo.rb', line 124

def apollo_data_connected?
  defined?(Legion::Data) && Legion::Data.respond_to?(:connection) && !Legion::Data.connection.nil?
rescue StandardError => e
  Legion::Logging.debug("Apollo#apollo_data_connected? check failed: #{e.message}") if defined?(Legion::Logging)
  false
end

#apollo_expertise_mapObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/legion/api/apollo.rb', line 176

def apollo_expertise_map
  conn = Legion::Data.connection
  rows = conn[:apollo_expertise].order(Sequel.desc(:proficiency)).all

  by_domain = {}
  rows.each do |row|
    domain = row[:domain] || 'general'
    by_domain[domain] ||= []
    by_domain[domain] << {
      agent_id:    row[:agent_id],
      proficiency: row[:proficiency]&.round(3),
      entry_count: row[:entry_count]
    }
  end

  { domains: by_domain, total_agents: rows.map { |r| r[:agent_id] }.uniq.size,
    total_domains: by_domain.size }
rescue Sequel::Error => e
  { error: e.message }
end

#apollo_graph_topologyObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/legion/api/apollo.rb', line 148

def apollo_graph_topology
  conn = Legion::Data.connection
  entries = conn[:apollo_entries]
  relations = conn[:apollo_relations]

  by_domain = entries.group_and_count(:knowledge_domain).all
                     .to_h { |r| [r[:knowledge_domain] || 'general', r[:count]] }
  by_agent = entries.group_and_count(:source_agent).all
                    .to_h { |r| [r[:source_agent] || 'unknown', r[:count]] }
  by_relation = relations.group_and_count(:relation_type).all
                         .to_h { |r| [r[:relation_type], r[:count]] }
  disputed = entries.where(status: 'disputed').count
  confirmed = entries.where(status: 'confirmed').count
  candidates = entries.where(status: 'candidate').count

  {
    domains:          by_domain,
    agents:           by_agent,
    relation_types:   by_relation,
    total_relations:  relations.count,
    disputed_entries: disputed,
    confirmed:        confirmed,
    candidates:       candidates
  }
rescue Sequel::Error => e
  { error: e.message }
end

#apollo_loaded?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/legion/api/apollo.rb', line 120

def apollo_loaded?
  defined?(Legion::Extensions::Apollo::Runners::Knowledge) && apollo_data_connected?
end

#apollo_maintenance_runnerObject



135
136
137
# File 'lib/legion/api/apollo.rb', line 135

def apollo_maintenance_runner
  @apollo_maintenance_runner ||= Object.new.extend(Legion::Extensions::Apollo::Runners::Maintenance)
end

#apollo_runnerObject



131
132
133
# File 'lib/legion/api/apollo.rb', line 131

def apollo_runner
  @apollo_runner ||= Object.new.extend(Legion::Extensions::Apollo::Runners::Knowledge)
end

#apollo_statsObject



197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/legion/api/apollo.rb', line 197

def apollo_stats
  entries = Legion::Data.connection[:apollo_entries]
  {
    total_entries:   entries.count,
    by_status:       entries.group_and_count(:status).all.to_h { |r| [r[:status], r[:count]] },
    by_content_type: entries.group_and_count(:content_type).all.to_h { |r| [r[:content_type], r[:count]] },
    recent_24h:      entries.where { created_at >= (Time.now.utc - 86_400) }.count,
    avg_confidence:  entries.avg(:confidence)&.round(3) || 0.0
  }
rescue Sequel::Error
  { total_entries: 0, error: 'apollo_entries table not available' }
end

#run_maintenance(action) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/legion/api/apollo.rb', line 139

def run_maintenance(action)
  case action
  when :decay_cycle
    apollo_maintenance_runner.run_decay_cycle
  when :corroboration
    apollo_maintenance_runner.check_corroboration
  end
end