<commit_before>class CreateInterventionsCosts < ActiveRecord::Migration
  def up
    unless table_exists?(:intervention_costs)
      create_table :intervention_costs do |t|
        t.decimal :inputs_cost
        t.decimal :doers_cost
        t.decimal :tools_cost
        t.decimal :receptions_cost
      end

      add_column :interventions, :intervention_costs_id, :integer

      historical_recovery
    end
  end

  def down
    drop_table :intervention_costs
    remove_column :interventions, :intervention_costs_id
  end

  private


  def historical_recovery
    Intervention.all.each do |intervention|
      costs = {
        inputs_cost: cost(intervention.inputs),
        doers_cost: cost(intervention.doers),
        tools_cost: cost(intervention.tools),
        receptions_cost: intervention.receptions_cost.to_f.round(2)
      }

      intervention_costs = InterventionCosts.new(costs)
      intervention_costs.save

      intervention.intervention_costs_id = intervention_costs.id
      intervention.save
    end
  end

  def cost(relation)
    relation
      .map(&:cost)
      .compact
      .sum
  end
end<commit_msg>Change migration with real_cost<commit_after>class CreateInterventionsCosts < ActiveRecord::Migration
  def up
    unless table_exists?(:intervention_costs)
      create_table :intervention_costs do |t|
        t.decimal :inputs_cost
        t.decimal :doers_cost
        t.decimal :tools_cost
        t.decimal :receptions_cost
      end

      add_column :interventions, :intervention_costs_id, :integer

      historical_recovery
    end
  end

  def down
    drop_table :intervention_costs
    remove_column :interventions, :intervention_costs_id
  end

  private


  def historical_recovery
    Intervention.all.each do |intervention|
      costs = {
        inputs_cost: intervention.decorate.real_cost(:input),
        doers_cost: intervention.decorate.real_cost(:doer),
        tools_cost: intervention.decorate.real_cost(:tool),
        receptions_cost: intervention.receptions_cost.to_f.round(2)
      }

      intervention_costs = InterventionCosts.new(costs)
      intervention_costs.save

      intervention.intervention_costs_id = intervention_costs.id
      intervention.save
    end
  end
end
{'fraction_non_alphanumeric': 0.06645851917930419, 'fraction_numerical': 0.0008920606601248885, 'mean_word_length': 3.108058608058608, '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': '4936014', 'n_tokens_mistral': 693, 'n_tokens_neox': 663, 'n_words': 127}