<commit_before>require 'ostruct'

class TicketsController < ApplicationController
  before_filter :login_required
  before_filter :get_token
  before_filter :set_project

  def index
    page = params[:page] || 1
    page = page.to_i unless page == 1
    @lh_project = Project.find_by_lighthouse_project(current_user, params[:project_id])
    db_project = current_user.projects.find_by_lighthouse_id(@lh_project.id)
    @bin = @lh_project.bins.find {|bin| bin.id.to_s == params[:bin_id].to_s}
    if @bin
      @search_query = @bin.query
      @lh_tickets = Ticket.search(db_project, @bin.query, page)
    else
      @search_query = session[:ticket_search]
      @lh_tickets = Ticket.search(db_project, @search_query) if @search_query
    end
    if @lh_tickets.blank?
      flash[:error] = "No tickets found for '#{h @search_query}'"
      redirect_to :controller => 'projects', :action => 'index'
    else
      if @search_query.include?('sort:number') || !@search_query.include?('sort:')
        @lh_tickets.sort! {|x, y| x.number <=> y.number}
      end
      prep_bucket_form
      session[:bin_id] = params[:bin_id]
      session[:tickets] = @lh_tickets.collect(&:number)
      session[:ticket_index] = 0
      session[:ticket_search] = @search_query
    end
    if @bin
      @ticket_count = @bin.tickets_count
    else
      @ticket_count = @lh_tickets.length
    end
    if page != 1 && request.xhr?
      @page = page
      render :partial => 'later_ticket_pages', :locals => { :tickets => @lh_tickets, :project => @lh_project }
    end
  end

  def refresh
    lh_project = Project.find_by_lighthouse_project(current_user, params[:project_id])
    db_project = current_user.projects.find_by_lighthouse_id(lh_project.id)
    db_ticket = db_project.tickets.find_by_number(params[:id])
    db_ticket.update_from_lighthouse(Ticket.retrieve(db_project, db_ticket.number))
    redirect_to :action => 'show', :project_id => params[:project_id], :id => db_ticket.number
  end

  def show
    if params[:id].blank?
      flash[:error] = "Ticket to view is blank"
      redirect_to :action => 'index', :project_id => params[:project_id]
      return
    end
    @lh_project = Project.find_by_lighthouse_project(current_user, params[:project_id])
    db_project = current_user.projects.find_by_lighthouse_id(@lh_project.id)
    db_ticket = db_project.tickets.find_by_number(params[:id])
    if db_ticket.nil?<commit_msg>Handle AJAX request for the next page.<commit_after>require 'ostruct'

class TicketsController < ApplicationController
  before_filter :login_required
  before_filter :get_token
  before_filter :set_project

  def index
    page = params[:page] || 1
    page = page.to_i unless page == 1
    @lh_project = Project.find_by_lighthouse_project(current_user, params[:project_id])
    db_project = current_user.projects.find_by_lighthouse_id(@lh_project.id)
    @bin = @lh_project.bins.find {|bin| bin.id.to_s == params[:bin_id].to_s}
    if @bin
      @search_query = @bin.query
      @lh_tickets = Ticket.search(db_project, @bin.query, page)
    else
      @search_query = session[:ticket_search]
      @lh_tickets = Ticket.search(db_project, @search_query) if @search_query
    end
    if @lh_tickets.blank?
      flash[:error] = "No tickets found for '#{h @search_query}'"
      redirect_to :controller => 'projects', :action => 'index'
    else
      if @search_query.include?('sort:number') || !@search_query.include?('sort:')
        @lh_tickets.sort! {|x, y| x.number <=> y.number}
      end
      prep_bucket_form
      session[:bin_id] = params[:bin_id]
      if request.xhr?
        session[:tickets] += @lh_tickets.collect(&:number)
      else
        session[:tickets] = @lh_tickets.collect(&:number)
        session[:ticket_index] = 0
      end
      session[:ticket_search] = @search_query
    end
    if @bin
      @ticket_count = @bin.tickets_count
    else
      @ticket_count = @lh_tickets.length
    end
    if page != 1 && request.xhr?
      @page = page
      render :partial => 'later_ticket_pages', :locals => { :tickets => @lh_tickets, :project => @lh_project }
    end
  end

  def refresh
    lh_project = Project.find_by_lighthouse_project(current_user, params[:project_id])
    db_project = current_user.projects.find_by_lighthouse_id(lh_project.id)
    db_ticket = db_project.tickets.find_by_number(params[:id])
    db_ticket.update_from_lighthouse(Ticket.retrieve(db_project, db_ticket.number))
    redirect_to :action => 'show', :project_id => params[:project_id], :id => db_ticket.number
  end

  def show
    if params[:id].blank?
      flash[:error] = "Ticket to view is blank"
      redirect_to :action => 'index', :project_id => params[:project_id]
      return
    end
    @lh_project = Project.find_by_lighthouse_project(current_user, params[:project_id])
    db_project = current_user.projects.find_by_lighthouse_id(@lh_project.id)
    db_ticket = db_project.tickets.find_by_number(params[:id])
    if db_ticket.nil?
{'fraction_non_alphanumeric': 0.12190706095353047, 'fraction_numerical': 0.0016093341380004024, 'mean_word_length': 3.8131655372700872, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, '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': '3295524', 'n_tokens_mistral': 1753, 'n_tokens_neox': 1693, 'n_words': 313}