<commit_before>class SwarmsController < ApplicationController
  before_filter :scope_to_swarm, :except => %w{index draft live closed new create mine}
  before_filter :check_for_user, :except => %w{index live closed show embed public_csv}
  before_filter :count_swarms, :only => %w{index draft live closed}
  before_filter :check_user_can_alter_swarm, :only => %w{edit update fields update_fields preview open close code}
  before_filter :check_user_can_destroy_swarm, :only => %w{spike do_spike delete destroy}

  respond_to :html, :json

  def index
    if @current_user
      @swarms = Swarm.unspiked.paginate(:page => params[:page])
    else
      @swarms = Swarm.publicly_visible.paginate(:page => params[:page])
    end
  end

  def draft
    @swarms = Swarm.unspiked.draft.paginate(:page => params[:page])
  end

  def live
    @swarms = Swarm.unspiked.live.paginate(:page => params[:page])
  end
  
  def closed
    @swarms = Swarm.unspiked.closed.paginate(:page => params[:page])
  end

  def mine
    redirect_to user_path(@current_user)
  end

  def show
    @current_page = 1

    if @swarm.closed? || @current_user
      begin
        @rows, @total_pages = @swarm.search.all(@current_page, 10)
        @total_count = @swarm.search.count_all
      rescue Faraday::TimeoutError, Faraday::ConnectionFailed
        @rows, @total_pages = [], 0
        @elasticsearch_error = "There was an error connecting to Elasticsearch, and results cannot be shown."
      rescue Elasticsearch::Transport::Transport::Errors::NotFound
        @rows, @total_pages = [], 0
        @elasticsearch_error = "There are no results in Elasticsearch yet."
      end
    end
    respond_with @swarm
  end

  def csv
    results = @swarm.search.entirety
    formatted_results = SwarmResultsFormatter.new(@swarm,results)
    send_data formatted_results.to_csv, 
              filename: "#{@swarm.token}.csv"
  end

  def public_csv
    results = @swarm.search.entirety
    formatted_results = SwarmResultsFormatter.new(@swarm,results)
    send_data formatted_results.to_public_csv, 
              filename: "#{@swarm.token}.csv"
  end

  def delete
  end

  def destroy
    @swarm.destroy
    redirect_to swarms_path
  end

  def spike
  end

  def do_spike
    @swarm.spike!
    flash[:success] = "'#{@swarm.name}' was spiked, and is no longer visible."
    redirect_to swarms_path
  end

  def new
    @swarm = Swarm.new
  end

  def fields
  end

  def update_fields<commit_msg>Public cache control header.<commit_after>class SwarmsController < ApplicationController
  before_filter :scope_to_swarm, :except => %w{index draft live closed new create mine}
  before_filter :check_for_user, :except => %w{index live closed show embed public_csv}
  before_filter :count_swarms, :only => %w{index draft live closed}
  before_filter :check_user_can_alter_swarm, :only => %w{edit update fields update_fields preview open close code}
  before_filter :check_user_can_destroy_swarm, :only => %w{spike do_spike delete destroy}

  respond_to :html, :json

  def index
    if @current_user
      @swarms = Swarm.unspiked.paginate(:page => params[:page])
    else
      @swarms = Swarm.publicly_visible.paginate(:page => params[:page])
    end
  end

  def draft
    @swarms = Swarm.unspiked.draft.paginate(:page => params[:page])
  end

  def live
    @swarms = Swarm.unspiked.live.paginate(:page => params[:page])
  end
  
  def closed
    @swarms = Swarm.unspiked.closed.paginate(:page => params[:page])
  end

  def mine
    redirect_to user_path(@current_user)
  end

  def show
    @current_page = 1

    if @swarm.closed? || @current_user
      begin
        @rows, @total_pages = @swarm.search.all(@current_page, 10)
        @total_count = @swarm.search.count_all
      rescue Faraday::TimeoutError, Faraday::ConnectionFailed
        @rows, @total_pages = [], 0
        @elasticsearch_error = "There was an error connecting to Elasticsearch, and results cannot be shown."
      rescue Elasticsearch::Transport::Transport::Errors::NotFound
        @rows, @total_pages = [], 0
        @elasticsearch_error = "There are no results in Elasticsearch yet."
      end
    end
    respond_with @swarm
  end

  def csv
    results = @swarm.search.entirety
    formatted_results = SwarmResultsFormatter.new(@swarm,results)
    send_data formatted_results.to_csv, 
              filename: "#{@swarm.token}.csv"
  end

  def public_csv
    results = @swarm.search.entirety
    formatted_results = SwarmResultsFormatter.new(@swarm,results)

    expires_in 1.year, :public => true # we need to set this to be public
    send_data formatted_results.to_public_csv, 
              filename: "#{@swarm.token}.csv"
  end

  def delete
  end

  def destroy
    @swarm.destroy
    redirect_to swarms_path
  end

  def spike
  end

  def do_spike
    @swarm.spike!
    flash[:success] = "'#{@swarm.name}' was spiked, and is no longer visible."
    redirect_to swarms_path
  end

  def new
    @swarm = Swarm.new
  end

  def fields
  end

  def update_fields
{'fraction_non_alphanumeric': 0.09792580773833266, 'fraction_numerical': 0.0021938571998404467, 'mean_word_length': 3.3875765529308834, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, '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': '6341331', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1600, 'n_words': 408}