<commit_before>class ActivityProductionDecorator < Draper::Decorator
  delegate_all

  def plants?
    object
      .products
      .select { |product| product.is_a?(Plant) }
      .any?
  end

  def production_costs
    production_global_costs = global_costs

    {
      global_costs: human_costs(production_global_costs),
      cultivated_hectare_costs: human_cultivated_hectare_costs(production_global_costs.clone),
      working_hectare_costs: human_working_hectare_costs
    }
  end

  def global_costs
    calcul_global_costs
  end

  def human_global_costs
    human_costs(global_costs)
  end

  def cultivated_hectare_costs(costs)
    divider_costs(costs, object.net_surface_area.in(:hectare).round(2).to_d)
    total_costs(costs)

    costs
  end

  def human_cultivated_hectare_costs(costs)
    human_costs(cultivated_hectare_costs(costs))
  end

  def working_hectare_costs
    costs = calcul_global_costs(with_working_zone_area: true)
    total_costs(costs)

    costs
  end

  def human_working_hectare_costs
    human_costs(working_hectare_costs)
  end

  def working_zone_area
    interventions = decorated_interventions
    working_zone = 0.in(:hectare)

    interventions.each do |intervention|
      working_zone += intervention_working_zone_area(intervention).in(:hectare)
    end

    working_zone
  end

  def human_working_zone_area
    working_zone_area
      .in(:hectare)
      .round(3)
      .l
  end

  private

  def calcul_global_costs(with_working_zone_area: false)
    interventions = decorated_interventions
    costs = new_costs_hash
    working_zone = 0.in(:hectare)

    interventions.each do |intervention|
      global_costs = intervention.global_costs

      calcul_with_surface_area(intervention, global_costs) if intervention.many_targets?
      sum_costs(costs, global_costs)

      working_zone += intervention_working_zone_area(intervention).in(:hectare).round(2) if with_working_zone_area
    end

    calcul_with_working_zone_area(costs, working_zone) if with_working_zone_area
    total_costs(costs)
    costs
  end

  def total_costs(costs)
    costs = costs.except!(:total) if costs.key?(:total)

    costs[:total] = costs.values.sum
  end

  def multiply_costs(costs, multiplier)
    costs.each { |key, value| costs[key] = value * multiplier }
  end

  def divider_costs(costs, divider)
    costs.each { |key, value| costs[key] = value / divider }
  end

  def sum_costs(plant_costs, costs)
    plant_costs.each { |key, _value| plant_costs[key] = plant_costs[key] + costs[key] }
  end

  def human_costs(costs)
    costs.each { |key, _value| costs[key] = costs[key].to_i }
  end

  def new_costs_hash
    { total: 0, inputs: 0, doers: 0, tools: 0, receptions: 0 }
  end<commit_msg>Prevent dividing by zero when computing production costs<commit_after>class ActivityProductionDecorator < Draper::Decorator
  delegate_all

  def plants?
    object
      .products
      .select { |product| product.is_a?(Plant) }
      .any?
  end

  def production_costs
    production_global_costs = global_costs

    {
      global_costs: human_costs(production_global_costs),
      cultivated_hectare_costs: human_cultivated_hectare_costs(production_global_costs.clone),
      working_hectare_costs: human_working_hectare_costs
    }
  end

  def global_costs
    calcul_global_costs
  end

  def human_global_costs
    human_costs(global_costs)
  end

  def cultivated_hectare_costs(costs)
    divider_costs(costs, object.net_surface_area.in(:hectare).round(2).to_d)
    total_costs(costs)

    costs
  end

  def human_cultivated_hectare_costs(costs)
    human_costs(cultivated_hectare_costs(costs))
  end

  def working_hectare_costs
    costs = calcul_global_costs(with_working_zone_area: true)
    total_costs(costs)

    costs
  end

  def human_working_hectare_costs
    human_costs(working_hectare_costs)
  end

  def working_zone_area
    interventions = decorated_interventions
    working_zone = 0.in(:hectare)

    interventions.each do |intervention|
      working_zone += intervention_working_zone_area(intervention).in(:hectare)
    end

    working_zone
  end

  def human_working_zone_area
    working_zone_area
      .in(:hectare)
      .round(3)
      .l
  end

  private

  def calcul_global_costs(with_working_zone_area: false)
    interventions = decorated_interventions
    costs = new_costs_hash
    working_zone = 0.in(:hectare)

    interventions.each do |intervention|
      global_costs = intervention.global_costs

      calcul_with_surface_area(intervention, global_costs) if intervention.many_targets?
      sum_costs(costs, global_costs)

      working_zone += intervention_working_zone_area(intervention).in(:hectare).round(2) if with_working_zone_area
    end

    calcul_with_working_zone_area(costs, working_zone) if with_working_zone_area
    total_costs(costs)
    costs
  end

  def total_costs(costs)
    costs = costs.except!(:total) if costs.key?(:total)

    costs[:total] = costs.values.sum
  end

  def multiply_costs(costs, multiplier)
    costs.each { |key, value| costs[key] = value * multiplier }
  end

  def divider_costs(costs, divider)
    costs.each { |key, value| costs[key] = value / divider unless divider.zero? }
  end

  def sum_costs(plant_costs, costs)
    plant_costs.each { |key, _value| plant_costs[key] = plant_costs[key] + costs[key] }
  end

  def human_costs(costs)
    costs.each { |key, _value| costs[key] = costs[key].to_i }
  end

  def new_costs_hash
    { total: 0, inputs: 0, doers: 0, tools: 0, receptions: 0 }
  end
{'fraction_non_alphanumeric': 0.07814761215629522, 'fraction_numerical': 0.00361794500723589, 'mean_word_length': 3.9146666666666667, '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': '667334', 'n_tokens_mistral': 2032, 'n_tokens_neox': 1952, 'n_words': 346}