# typed: false
class LogEntriesController < ApplicationController
  def index
    @uri_filter = Addressable::URI.parse(params[:filter]) if params[:filter]
    if @uri_filter && @uri_filter.scheme.nil?
      @uri_filter = Addressable::URI.parse("https://" + params[:filter])
    end

    if @uri_filter
      query = HttpLogEntry.where("uri_host = ?", @uri_filter.host)

      if @uri_filter.path.present?
        if @uri_filter.query.present?
          query =
            query.where("uri_path = ?", @uri_filter.path).where(
              "uri_query like ?",
              @uri_filter.query + "%",
            )
        else
          query = query.where("uri_path like ?", @uri_filter.path + "%")
        end
      end
    else
      query = HttpLogEntry
    end

    @log_entries =
      query
        .page(params[:page])
        .per(50)
        .joins(:response)
        .includes(:response)
        .order(id: :desc)
        .without_count

    formats.clear
    formats << :html
    render :index
  end

  def stats
    @time_window = (params[:seconds] || 60).to_i.seconds
    @last_window_count = 0
    @last_window_bytes = 0
    @last_window_bytes_stored = 0
    @content_type_counts =
      Hash.new do |hash, key|
        hash[key] = { count: 0, bytes: 0, bytes_stored: 0 }
      end

    @by_domain_counts =
      Hash.new do |hash, key|
        hash[key] = { count: 0, bytes: 0, bytes_stored: 0 }
      end

    HttpLogEntry
      .joins(:response)
      .includes(:response)
      .select("http_log_entries.*, blob_files.size_bytes")
      .find_each(batch_size: 100, order: :desc) do |log_entry|
        break if log_entry.created_at < @time_window.ago
        @last_window_count += 1
        @last_window_bytes += log_entry.response_size
        content_type = log_entry.content_type.split(";").first

        @content_type_counts[content_type][:count] += 1
        @content_type_counts[content_type][:bytes] += log_entry.response_size

        @by_domain_counts[log_entry.uri_host][:count] += 1
        @by_domain_counts[log_entry.uri_host][:bytes] += log_entry.response_size
      end
  end

  def show
    @log_entry =
      HttpLogEntry.includes(
        :caused_by_entry,
        :triggered_entries,
        :response,
      ).find(params[:id])
  end
end
