Module: Legion::Apollo::Routes::ApolloHelpers

Includes:
Logging::Helper
Defined in:
lib/legion/apollo/routes.rb

Overview

Helper methods mixed into the Sinatra app context

Instance Method Summary collapse

Instance Method Details

#apollo_api_available?Boolean

Returns:

  • (Boolean)


186
187
188
189
190
191
# File 'lib/legion/apollo/routes.rb', line 186

def apollo_api_available?
  defined?(Legion::Apollo) && Legion::Apollo.respond_to?(:query) && Legion::Apollo.respond_to?(:ingest)
rescue StandardError => e
  handle_exception(e, level: :debug, operation: :apollo_api_available?)
  false
end

#apollo_data_connected?Boolean

Returns:

  • (Boolean)


193
194
195
196
197
198
# File 'lib/legion/apollo/routes.rb', line 193

def apollo_data_connected?
  defined?(Legion::Data) && Legion::Data.respond_to?(:connection) && !Legion::Data.connection.nil?
rescue StandardError => e
  handle_exception(e, level: :debug, operation: :apollo_data_connected?)
  false
end

#apollo_expertise_mapObject



265
266
267
268
269
270
271
272
273
274
275
# File 'lib/legion/apollo/routes.rb', line 265

def apollo_expertise_map
  return { error: 'Apollo runner unavailable' } unless apollo_runner_available?
  unless apollo_runner.respond_to?(:expertise_map)
    return { error: 'Apollo expertise_map not supported by runner' }
  end

  apollo_runner.expertise_map
rescue StandardError => e
  handle_exception(e, level: :debug, operation: :apollo_expertise_map)
  { error: 'apollo_expertise_map unavailable' }
end

#apollo_graph_topologyObject



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/legion/apollo/routes.rb', line 253

def apollo_graph_topology
  return { error: 'Apollo runner unavailable' } unless apollo_runner_available?
  unless apollo_runner.respond_to?(:graph_topology)
    return { error: 'Apollo graph_topology not supported by runner' }
  end

  apollo_runner.graph_topology
rescue StandardError => e
  handle_exception(e, level: :debug, operation: :apollo_graph_topology)
  { error: 'apollo_graph_topology unavailable' }
end

#apollo_loaded?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/legion/apollo/routes.rb', line 182

def apollo_loaded?
  apollo_runner_available? && apollo_data_connected?
end

#apollo_maintenance_runnerObject

rubocop:disable Metrics/MethodLength



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/legion/apollo/routes.rb', line 228

def apollo_maintenance_runner # rubocop:disable Metrics/MethodLength
  @apollo_maintenance_runner ||= begin
    unless defined?(Legion::Extensions::Apollo::Runners::Maintenance)
      halt 503, json_error('maintenance_unavailable', 'Apollo maintenance runner is not loaded')
    end

    runner = Object.new.extend(Legion::Extensions::Apollo::Runners::Maintenance)
    required = %i[run_decay_cycle check_corroboration]
    unless required.all? { |m| runner.respond_to?(m) }
      halt 503, json_error('maintenance_unavailable', 'Apollo maintenance runner is missing required actions')
    end

    runner
  end
end

#apollo_runnerObject



200
201
202
# File 'lib/legion/apollo/routes.rb', line 200

def apollo_runner
  Legion::Extensions::Apollo::Runners::Knowledge
end

#apollo_runner_available?Boolean

Returns:

  • (Boolean)


172
173
174
175
176
177
178
179
180
# File 'lib/legion/apollo/routes.rb', line 172

def apollo_runner_available?
  return false unless defined?(Legion::Extensions::Apollo::Runners::Knowledge)

  required = %i[handle_query handle_ingest related_entries]
  required.all? { |m| Legion::Extensions::Apollo::Runners::Knowledge.respond_to?(m) }
rescue StandardError => e
  handle_exception(e, level: :debug, operation: :apollo_runner_available?)
  false
end

#apollo_statsObject



277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/legion/apollo/routes.rb', line 277

def apollo_stats
  return { total_entries: 0, error: 'Apollo runner unavailable' } unless apollo_runner_available?
  unless apollo_runner.respond_to?(:stats)
    return { total_entries: 0,
             error:         'Apollo stats not supported by runner' }
  end

  apollo_runner.stats
rescue StandardError => e
  handle_exception(e, level: :debug, operation: :apollo_stats)
  { total_entries: 0, error: 'apollo_stats unavailable' }
end

#apollo_status_code(result, success_status: 200) ⇒ Object

rubocop:disable Metrics/MethodLength



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/legion/apollo/routes.rb', line 212

def apollo_status_code(result, success_status: 200) # rubocop:disable Metrics/MethodLength
  return 202 if result[:success] && result[:mode] == :async
  return success_status if result[:success]

  case result[:error]
  when :no_path_available, :not_started, :local_not_started,
       :upstream_query_failed, :backend_query_failed
    503
  else
    500
  end
rescue StandardError => e
  handle_exception(e, level: :debug, operation: :apollo_status_code)
  500
end

#normalize_scope(scope) ⇒ Object



204
205
206
207
208
209
210
# File 'lib/legion/apollo/routes.rb', line 204

def normalize_scope(scope)
  value = scope&.to_sym
  %i[global local all].include?(value) ? value : :global
rescue StandardError => e
  handle_exception(e, level: :debug, operation: :normalize_scope)
  :global
end

#run_maintenance(action) ⇒ Object



244
245
246
247
248
249
250
251
# File 'lib/legion/apollo/routes.rb', line 244

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