<commit_before>module GobiertoBudgets
  module Api
    class DataController < ApplicationController
      include GobiertoBudgets::ApplicationHelper

      caches_action(
        :lines,
        :budget_per_inhabitant,
        :budget,
        cache_path: proc { |c| "#{c.request.url}?locale=#{I18n.locale}" }
      )

      skip_before_action :authenticate_user_in_site, only: [:budget_line, :available_years]

      def budget
        @year = params[:year].to_i
        @area = params[:area]
        @kind = params[:kind]
        @code = params[:code]

        @category_name = @kind == 'G' ? I18n.t("controllers.gobierto_budgets.api.data.expense") : I18n.t("controllers.gobierto_budgets.api.data.income")

        budget_data = budget_data(@year, 'amount')
        budget_data_previous_year = budget_data(@year - 1, 'amount')
        position = budget_data[:position].to_i
        sign = sign(budget_data[:value], budget_data_previous_year[:value])

        respond_to do |format|
          format.json do
            render json: {
              title: @category_name,
              sign: sign,
              value: format_currency(budget_data[:value]),
              delta_percentage: helpers.number_with_precision(delta_percentage(budget_data[:value], budget_data_previous_year[:value]), precision: 2),
              ranking_position: position,
              ranking_total_elements: helpers.number_with_precision(budget_data[:total_elements], precision: 0),
              ranking_url: ''
            }.to_json
          end
        end
      end

      def budget_line
        result = GobiertoBudgets::BudgetLine.find_details(
          type: params[:area],
          id: params[:id]
        )

        render json: result
      rescue BudgetLine::RecordNotFound
        render json: { error: "not-found" }, status: 404
      end

      def available_years
        years = ::GobiertoBudgets::SearchEngineConfiguration::Year.with_data(
          index: ::GobiertoBudgets::SearchEngineConfiguration::BudgetLine.index_forecast
        ).sort

        render json: years
      end

      def budget_per_inhabitant
        @year = params[:year].to_i
        @area = params[:area]
        @kind = params[:kind]
        @code = params[:code]

        @category_name = @kind == 'G' ? I18n.t("controllers.gobierto_budgets.api.data.expense") : I18n.t("controllers.gobierto_budgets.api.data.income")
        budget_data = budget_data(@year, 'amount_per_inhabitant')
        budget_data_previous_year = budget_data(@year - 1, 'amount_per_inhabitant')
        position = budget_data[:position].to_i
        sign = sign(budget_data[:value], budget_data_previous_year[:value])

        respond_to do |format|
          format.json do
            render json: {
              sign: sign,
              title: "#{@category_name} por habitante",
              value: format_currency(budget_data[:value]),
              delta_percentage: helpers.number_with_precision(delta_percentage(budget_data[:value], budget_data_previous_year[:value]), precision: 2),
              ranking_position: position,
              ranking_total_elements: helpers.number_with_precision(budget_data[:total_elements], precision: 0),
              ranking_url: ''
            }.to_json
          end
        end
      end

      def lines
        data_line = GobiertoBudgets::Data::Lines.new place: current_site.place, organization_id: current_site.organization_id,
          organization_name: current_site.organization_name, year: params[:year], what: params[:what], kind: params[:kind],
          code: params[:code], area: params[:area], include_next_year: params[:include_next_year],
          comparison: params[:comparison]
<commit_msg>Fix error when data is missing<commit_after>module GobiertoBudgets
  module Api
    class DataController < ApplicationController
      include GobiertoBudgets::ApplicationHelper

      caches_action(
        :lines,
        :budget_per_inhabitant,
        :budget,
        cache_path: proc { |c| "#{c.request.url}?locale=#{I18n.locale}" }
      )

      skip_before_action :authenticate_user_in_site, only: [:budget_line, :available_years]

      def budget
        @year = params[:year].to_i
        @area = params[:area]
        @kind = params[:kind]
        @code = params[:code]

        @category_name = @kind == 'G' ? I18n.t("controllers.gobierto_budgets.api.data.expense") : I18n.t("controllers.gobierto_budgets.api.data.income")
        budget_data = budget_data(@year, 'amount')
        budget_data_previous_year = budget_data(@year - 1, 'amount')

        result = if budget_data.present? && budget_data_previous_year.present?
          position = budget_data[:position].to_i
          sign = sign(budget_data[:value], budget_data_previous_year[:value])

          {
            title: @category_name,
            sign: sign,
            value: format_currency(budget_data[:value]),
            delta_percentage: helpers.number_with_precision(delta_percentage(budget_data[:value], budget_data_previous_year[:value]), precision: 2),
            ranking_position: position,
            ranking_total_elements: helpers.number_with_precision(budget_data[:total_elements], precision: 0),
            ranking_url: ''
          }
        else
          {}
        end

        respond_to do |format|
          format.json do
            render json: result
          end
        end
      end

      def budget_line
        result = GobiertoBudgets::BudgetLine.find_details(
          type: params[:area],
          id: params[:id]
        )

        render json: result
      rescue BudgetLine::RecordNotFound
        render json: { error: "not-found" }, status: 404
      end

      def available_years
        years = ::GobiertoBudgets::SearchEngineConfiguration::Year.with_data(
          index: ::GobiertoBudgets::SearchEngineConfiguration::BudgetLine.index_forecast
        ).sort

        render json: years
      end

      def budget_per_inhabitant
        @year = params[:year].to_i
        @area = params[:area]
        @kind = params[:kind]
        @code = params[:code]

        @category_name = @kind == 'G' ? I18n.t("controllers.gobierto_budgets.api.data.expense") : I18n.t("controllers.gobierto_budgets.api.data.income")
        budget_data = budget_data(@year, 'amount_per_inhabitant')
        budget_data_previous_year = budget_data(@year - 1, 'amount_per_inhabitant')
        result = if budget_data.present? && budget_data_previous_year.present?
          position = budget_data[:position].to_i
          sign = sign(budget_data[:value], budget_data_previous_year[:value])

          {
            sign: sign,
            title: "#{@category_name} por habitante",
            value: format_currency(budget_data[:value]),
            delta_percentage: helpers.number_with_precision(delta_percentage(budget_data[:value], budget_data_previous_year[:value]), precision: 2),
            ranking_position: position,
            ranking_total_elements: helpers.number_with_precision(budget_data[:total_elements], precision: 0),
            ranking_url: ''
          }
        else
          {}
        end

        respond_to do |format|
          format.json do
            render json: result
          end
        end
      end

      def lines
        data_line = GobiertoBudgets::Data::Lines.new place: current_site.place, organization_id: current_site.organization_id,
          organization_name: current_site.organization_name, year: params[:year], what: params[:what], kind: params[:kind],
          code: params[:code], area: params[:area], include_next_year: params[:include_next_year],
          comparison: params[:comparison]

{'fraction_non_alphanumeric': 0.10567514677103718, 'fraction_numerical': 0.004957599478147424, 'mean_word_length': 2.671455938697318, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '932467', 'n_tokens_mistral': 2300, 'n_tokens_neox': 2207, 'n_words': 399}