<commit_before>require "grape/middleware/base"

module Sapience
  module Extensions
    module Grape
      module Middleware
        class Logging < ::Grape::Middleware::Base
          ActiveSupport::Notifications.subscribe("sql.active_record") do |*args|
            event = ActiveSupport::Notifications::Event.new(*args)
            Grape::Timings.append_db_runtime(event)
          end if defined?(ActiveRecord)

          def initialize(app, options = {})
            super
            @logger = @options[:logger]
          end

          def before
            reset_db_runtime
            start_time
          end

          def after
            stop_time
            @logger.info(parameters)
            nil
          end

          protected

          def parameters # rubocop:disable AbcSize
            {
              method: request.request_method,
              request_path: request.path,
              format: "json",
              status: response.try(:status) || 404,
              class_name: env["api.endpoint"].options[:for].to_s,
              action: "index",
              host: request.host,
              ip: (request.env["HTTP_X_FORWARDED_FOR"] || request.env["REMOTE_ADDR"]),
              ua: request.env["HTTP_USER_AGENT"],
              # route: "test_controller#index",
              # message: "Completed #index",
              tags: Sapience.tags,
              params: request.params,
              runtimes: {
                total: total_runtime,
                view: view_runtime,
                db: db_runtime,
              },
            }
          end

          private

          def request<commit_msg>[+MH] We have to put this rescue to be able to render 404<commit_after>require "grape/middleware/base"

module Sapience
  module Extensions
    module Grape
      module Middleware
        class Logging < ::Grape::Middleware::Base
          ActiveSupport::Notifications.subscribe("sql.active_record") do |*args|
            event = ActiveSupport::Notifications::Event.new(*args)
            Grape::Timings.append_db_runtime(event)
          end if defined?(ActiveRecord)

          def initialize(app, options = {})
            super
            @logger = @options[:logger]
          end

          def before
            reset_db_runtime
            start_time
          end

          def after
            stop_time
            @logger.info(parameters)
            nil
          end

          protected

          # TODO: Is NoMethodError :[] equal to `404`?
          def response
            super
          rescue
            nil
          end

          def parameters # rubocop:disable AbcSize
            {
              method: request.request_method,
              request_path: request.path,
              format: "json",
              status: response.try(:status) || 404,
              class_name: env["api.endpoint"].options[:for].to_s,
              action: "index",
              host: request.host,
              ip: (request.env["HTTP_X_FORWARDED_FOR"] || request.env["REMOTE_ADDR"]),
              ua: request.env["HTTP_USER_AGENT"],
              # route: "test_controller#index",
              # message: "Completed #index",
              tags: Sapience.tags,
              params: request.params,
              runtimes: {
                total: total_runtime,
                view: view_runtime,
                db: db_runtime,
              },
            }
          end

          private

          def request
{'fraction_non_alphanumeric': 0.08911174785100287, 'fraction_numerical': 0.0034383954154727794, 'mean_word_length': 1.4549929676511955, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '366648', 'n_tokens_mistral': 910, 'n_tokens_neox': 872, 'n_words': 193}