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
- #apollo_api_available? ⇒ Boolean
- #apollo_data_connected? ⇒ Boolean
- #apollo_expertise_map ⇒ Object
- #apollo_graph_topology ⇒ Object
- #apollo_loaded? ⇒ Boolean
-
#apollo_maintenance_runner ⇒ Object
rubocop:disable Metrics/MethodLength.
- #apollo_runner ⇒ Object
- #apollo_runner_available? ⇒ Boolean
- #apollo_stats ⇒ Object
-
#apollo_status_code(result, success_status: 200) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #normalize_scope(scope) ⇒ Object
- #run_maintenance(action) ⇒ Object
Instance Method Details
#apollo_api_available? ⇒ Boolean
192 193 194 195 196 197 |
# File 'lib/legion/apollo/routes.rb', line 192 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
199 200 201 202 203 204 |
# File 'lib/legion/apollo/routes.rb', line 199 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_map ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/legion/apollo/routes.rb', line 271 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_topology ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/legion/apollo/routes.rb', line 259 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
188 189 190 |
# File 'lib/legion/apollo/routes.rb', line 188 def apollo_loaded? apollo_runner_available? && apollo_data_connected? end |
#apollo_maintenance_runner ⇒ Object
rubocop:disable Metrics/MethodLength
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/legion/apollo/routes.rb', line 234 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_runner ⇒ Object
206 207 208 |
# File 'lib/legion/apollo/routes.rb', line 206 def apollo_runner Legion::Extensions::Apollo::Runners::Knowledge end |
#apollo_runner_available? ⇒ Boolean
178 179 180 181 182 183 184 185 186 |
# File 'lib/legion/apollo/routes.rb', line 178 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_stats ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/legion/apollo/routes.rb', line 283 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
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/legion/apollo/routes.rb', line 218 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
210 211 212 213 214 215 216 |
# File 'lib/legion/apollo/routes.rb', line 210 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
250 251 252 253 254 255 256 257 |
# File 'lib/legion/apollo/routes.rb', line 250 def run_maintenance(action) case action when :decay_cycle apollo_maintenance_runner.run_decay_cycle when :corroboration apollo_maintenance_runner.check_corroboration end end |