<commit_before>class MemProfiler

  def initialize(app)
    @app = app
  end

  def call(env)
    if is_assets_path?(env)
      continue_request env
    else
      make_memory_snapshot env
    end

    [@status, @headers, @response]
  end

  private

  def continue_request(env)
    @status, @headers, @response = @app.call(env)
  end

  def is_assets_path?(env)
    env['REQUEST_PATH'].match /^\/assets\//
  end

  def make_memory_snapshot(env)
    snapshot = get_request_memory_snapshot env
    request_params = get_request_path_params env
    save_memory_snapshot snapshot, request_params
  end

  def get_request_memory_snapshot(env)
    MemoryProfiler.report do
      continue_request(env)
    end
  end

  def save_memory_snapshot(snapshot, request_params)
    action = request_params[:action]
    controller = format_controller request_params[:controller]

    # p snapshot.allocated_memory_by_gem
    # p snapshot.allocated_objects_by_gem

    # p snapshot.allocated_memory_by_file<commit_msg>Ingore requests to the engine<commit_after>class MemProfiler

  ENGINE_TITLE = 'memleak'

  def initialize(app)
    @app = app
  end

  def call(env)
    if is_assets_path?(env) or is_engine_action?(env)
      continue_request env
    else
      make_memory_snapshot env
    end

    [@status, @headers, @response]
  end

  private

  def continue_request(env)
    @status, @headers, @response = @app.call(env)
  end

  def is_assets_path?(env)
    env['REQUEST_PATH'].match /^\/assets\//
  end

  def is_engine_action?(env)
    env['REQUEST_PATH'].match /^\/#{ENGINE_TITLE}/
  end

  def make_memory_snapshot(env)
    snapshot = get_request_memory_snapshot env
    request_params = get_request_path_params env
    save_memory_snapshot snapshot, request_params
  end

  def get_request_memory_snapshot(env)
    MemoryProfiler.report do
      continue_request(env)
    end
  end

  def save_memory_snapshot(snapshot, request_params)
    action = request_params[:action]
    controller = format_controller request_params[:controller]

    # p snapshot.allocated_memory_by_gem
    # p snapshot.allocated_objects_by_gem

    # p snapshot.allocated_memory_by_file
{'fraction_non_alphanumeric': 0.0778138026864289, 'fraction_numerical': 0.0, 'mean_word_length': 3.736842105263158, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6915404', 'n_tokens_mistral': 710, 'n_tokens_neox': 705, 'n_words': 146}