<commit_before>class GobiertoBudgets::BudgetsController < GobiertoBudgets::ApplicationController
  before_action :load_year, except: [:guide]
  before_action :overrided_root_redirect, only: [:index]

  caches_action(
    :index,
    cache_path: -> { cache_path },
    unless: -> { user_signed_in? }
  )

  def index
    @kind = GobiertoBudgets::BudgetLine::INCOME
    @area_name = GobiertoBudgets::EconomicArea.area_name
    @interesting_area = GobiertoBudgets::FunctionalArea.area_name

    @top_income_budget_lines = GobiertoBudgets::TopBudgetLine.limit(5).where(budget_query_params.merge(kind: GobiertoBudgets::BudgetLine::INCOME)).all
    @top_expense_budget_lines = GobiertoBudgets::TopBudgetLine.limit(5).where(budget_query_params.merge(kind: GobiertoBudgets::BudgetLine::EXPENSE)).all
    load_place_budget_lines
    load_interesting_expenses

    @sample_budget_lines = (@top_income_budget_lines + @top_expense_budget_lines).sample(3)

    load_site_stats
    @budgets_data_updated_at = @site_stats.budgets_data_updated_at
    @budgets_execution_summary = @site_stats.budgets_execution_summary

    @any_custom_income_budget_lines  = GobiertoBudgets::BudgetLine.any_data?(budget_query_params.merge(kind: GobiertoBudgets::BudgetLine::INCOME, area: GobiertoBudgets::CustomArea))
    @any_custom_expense_budget_lines = GobiertoBudgets::BudgetLine.any_data?(budget_query_params.merge(kind: GobiertoBudgets::BudgetLine::EXPENSE, area: GobiertoBudgets::CustomArea))
  end

  def guide
    @year = GobiertoBudgets::SearchEngineConfiguration::Year.last
    load_site_stats

    if current_site.gobierto_budgets_settings && current_site.gobierto_budgets_settings.settings["budgets_guide_page"]
      @page = GobiertoCms::Page.find_by id: current_site.gobierto_budgets_settings.settings["budgets_guide_page"]
    end
  end

  private

  def cache_path
    "#{super}/#{@year}"
  end

  def load_year
    if params[:year].nil?
      default_year = if budgets_elaboration_active?
        GobiertoBudgets::SearchEngineConfiguration::Year.last_year_with_data - 1
      else
        GobiertoBudgets::SearchEngineConfiguration::Year.last_year_with_data
      end

      redirect_to gobierto_budgets_budgets_path(default_year)
    else
      @year = params[:year].to_i
    end
  end

  def load_site_stats
    @site_stats = GobiertoBudgets::SiteStats.new(budget_query_params)
  end

  def budget_query_params
    { site: current_site, year: @year }
  end

  def load_place_budget_lines
    conditions = {
      site: current_site,
      level: 1,
      year: @year,
      kind: @kind,<commit_msg>Prevent loading next year data if elaboration enabled<commit_after>class GobiertoBudgets::BudgetsController < GobiertoBudgets::ApplicationController
  before_action :load_year, except: [:guide]
  before_action :overrided_root_redirect, only: [:index]

  caches_action(
    :index,
    cache_path: -> { cache_path },
    unless: -> { user_signed_in? }
  )

  def index
    @kind = GobiertoBudgets::BudgetLine::INCOME
    @area_name = GobiertoBudgets::EconomicArea.area_name
    @interesting_area = GobiertoBudgets::FunctionalArea.area_name

    @top_income_budget_lines = GobiertoBudgets::TopBudgetLine.limit(5).where(budget_query_params.merge(kind: GobiertoBudgets::BudgetLine::INCOME)).all
    @top_expense_budget_lines = GobiertoBudgets::TopBudgetLine.limit(5).where(budget_query_params.merge(kind: GobiertoBudgets::BudgetLine::EXPENSE)).all
    load_place_budget_lines
    load_interesting_expenses

    @sample_budget_lines = (@top_income_budget_lines + @top_expense_budget_lines).sample(3)

    load_site_stats
    @budgets_data_updated_at = @site_stats.budgets_data_updated_at
    @budgets_execution_summary = @site_stats.budgets_execution_summary

    @any_custom_income_budget_lines  = GobiertoBudgets::BudgetLine.any_data?(budget_query_params.merge(kind: GobiertoBudgets::BudgetLine::INCOME, area: GobiertoBudgets::CustomArea))
    @any_custom_expense_budget_lines = GobiertoBudgets::BudgetLine.any_data?(budget_query_params.merge(kind: GobiertoBudgets::BudgetLine::EXPENSE, area: GobiertoBudgets::CustomArea))
  end

  def guide
    @year = GobiertoBudgets::SearchEngineConfiguration::Year.last
    load_site_stats

    if current_site.gobierto_budgets_settings && current_site.gobierto_budgets_settings.settings["budgets_guide_page"]
      @page = GobiertoCms::Page.find_by id: current_site.gobierto_budgets_settings.settings["budgets_guide_page"]
    end
  end

  private

  def cache_path
    "#{super}/#{@year}"
  end

  def load_year
    default_year = if budgets_elaboration_active?
                     GobiertoBudgets::SearchEngineConfiguration::Year.last_year_with_data - 1
                   else
                     GobiertoBudgets::SearchEngineConfiguration::Year.last_year_with_data
                   end

    if params[:year].nil?
      redirect_to gobierto_budgets_budgets_path(default_year)
    else
      @year = params[:year].to_i
      if @year > default_year
        redirect_to gobierto_budgets_budgets_path(default_year)
      end
    end
  end

  def load_site_stats
    @site_stats = GobiertoBudgets::SiteStats.new(budget_query_params)
  end

  def budget_query_params
    { site: current_site, year: @year }
  end

  def load_place_budget_lines
    conditions = {
      site: current_site,
      level: 1,
      year: @year,
      kind: @kind,
{'fraction_non_alphanumeric': 0.08907938874394335, 'fraction_numerical': 0.0018635855385762206, 'mean_word_length': 4.9173098125689085, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '6956352', 'n_tokens_mistral': 1910, 'n_tokens_neox': 1800, 'n_words': 238}