<commit_before>class AccountsController < ApplicationController

  skip_before_filter :require_user, :only => :show
  before_filter :build_registers, :only => :show
  
  # GET /accounts
  # GET /accounts.xml
  def index
    @accounts = Account.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @accounts }
    end
  end

  # GET /accounts/1
  # GET /accounts/1.xml
  def show
    if @account.current_front_page.blank?
      render :text => "This site is currently offline", :status => 503
      return
    end
    @newspaper = Liquid::NewspaperDrop.new(@account)
    
    @front_page = @account.current_front_page
    @current_template = @front_page.template
    
    # Build query of only the necessary ids
    schema_ids = Array.new

    # Only load the Front Page schema if it's provided  
    if @front_page.schema.respond_to?(:each_key)
      @front_page.schema.each_key do |item|
        schema_ids += @front_page.schema[item]['ids']
      end
    end    

    found_widgets = @current_template.widgets
    found_widgets += @current_template.current_layout.widgets if @current_template.current_layout
    found_widgets.each do |widget|
      widget.schema.each_key do |item|
        schema_ids += widget.schema[item]['ids']
      end
    end
    
    @registers[:account] = @account
    @registers[:design] = @current_template.design
    
    # Variables for data sorting
    data_for_render = {}
    widget_data = {}
    schema_articles = {}
    
    unless schema_ids.blank?          
      # One request to find them all
      article_resources = Article.find(:all, :ids => schema_ids.reject{ |i| i.blank? }, :account_id => @account.account_resource_id, :as => @account.access_token)  unless schema_ids.blank?
  
      # Recontruct front page schema as hash keyed by entity name
      article_resources.each do |article|
        schema_articles.merge!(article.id.to_s => article)
      end
    
      if @front_page.schema.respond_to?(:each_key)
        @front_page.schema.each_key do |item|
          item_array = @front_page.schema[item]['ids'].collect{ |i| schema_articles[i] }
          data_for_render.merge!( item => item_array )
        end
      end
      
      found_widgets.each do |widget|
        widget.schema.each_key do |item|
          item_array = widget.schema[item]['ids'].collect{ |i| schema_articles[i] }
          widget_data.merge!( "#{item}_#{widget.name}" => item_array )
        end
      end
    
      @registers[:widget_data] = widget_data
    end
    
    page_html = @current_template.parsed_code.render(data_for_render.merge('newspaper' => @newspaper), :registers => @registers )
    
    if @current_template.current_layout
      render :text => @current_template.current_layout.parsed_code.render({'page_content' => page_html, 'newspaper' => @newspaper}, :registers => @registers )<commit_msg>Use squid to cache account front pages.<commit_after>class AccountsController < ApplicationController

  skip_before_filter :require_user, :only => :show
  before_filter :build_registers, :only => :show
  
  # GET /accounts
  # GET /accounts.xml
  def index
    @accounts = Account.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @accounts }
    end
  end

  # GET /accounts/1
  # GET /accounts/1.xml
  def show
    if @account.current_front_page.blank?
      render :text => "This site is currently offline", :status => 503
      return
    end
    @newspaper = Liquid::NewspaperDrop.new(@account)
    
    @front_page = @account.current_front_page
    @current_template = @front_page.template
    
    # Build query of only the necessary ids
    schema_ids = Array.new

    # Only load the Front Page schema if it's provided  
    if @front_page.schema.respond_to?(:each_key)
      @front_page.schema.each_key do |item|
        schema_ids += @front_page.schema[item]['ids']
      end
    end    

    found_widgets = @current_template.widgets
    found_widgets += @current_template.current_layout.widgets if @current_template.current_layout
    found_widgets.each do |widget|
      widget.schema.each_key do |item|
        schema_ids += widget.schema[item]['ids']
      end
    end
    
    @registers[:account] = @account
    @registers[:design] = @current_template.design
    
    # Variables for data sorting
    data_for_render = {}
    widget_data = {}
    schema_articles = {}
    
    unless schema_ids.blank?          
      # One request to find them all
      article_resources = Article.find(:all, :ids => schema_ids.reject{ |i| i.blank? }, :account_id => @account.account_resource_id, :as => @account.access_token)  unless schema_ids.blank?
  
      # Recontruct front page schema as hash keyed by entity name
      article_resources.each do |article|
        schema_articles.merge!(article.id.to_s => article)
      end
    
      if @front_page.schema.respond_to?(:each_key)
        @front_page.schema.each_key do |item|
          item_array = @front_page.schema[item]['ids'].collect{ |i| schema_articles[i] }
          data_for_render.merge!( item => item_array )
        end
      end
      
      found_widgets.each do |widget|
        widget.schema.each_key do |item|
          item_array = widget.schema[item]['ids'].collect{ |i| schema_articles[i] }
          widget_data.merge!( "#{item}_#{widget.name}" => item_array )
        end
      end
    
      @registers[:widget_data] = widget_data
    end
    
    page_html = @current_template.parsed_code.render(data_for_render.merge('newspaper' => @newspaper), :registers => @registers )
    
    # Squid reverse proxy caching headers
    expires_in 2.minutes, :public => true
    
    if @current_template.current_layout
      render :text => @current_template.current_layout.parsed_code.render({'page_content' => page_html, 'newspaper' => @newspaper}, :registers => @registers )
{'fraction_non_alphanumeric': 0.1095310648383274, 'fraction_numerical': 0.00186219739292365, 'mean_word_length': 2.946559786239145, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '3000774', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1828, 'n_words': 399}