<commit_before>class Award < ActiveRecord::Base
  belongs_to :public_body
  belongs_to :bidder

  def self.load_from_hash(properties)
    # Load the data only if it's a public works award
    # FIXME: Let's load the whole thing temporarily and use the database to check
    # whether we're doing it correctly.
    # return unless properties['[QCLO] Es Obra Pública']=='S'

    public_body = PublicBody.where(name: properties['Entidad adjudicadora - Organismo']).first_or_create
    bidder = Bidder.where(name: properties['Formalización del contrato - Contratista']).first_or_create
    Award.create!({
        public_body: public_body,
        bidder: bidder, 
        award_date: properties['[QCLO] Fecha de adjudicación'],
        category: properties['Análisis - Tipo'],
        process_type: properties['Análisis - Procedimiento'],
        process_track: properties['Análisis - Tramitación'],
        amount: properties['Análisis - Importe']*100, # in cents
        properties: properties
      })
  end
end<commit_msg>Parse amount correctly and store in cents<commit_after>require 'bigdecimal'

class Award < ActiveRecord::Base
  belongs_to :public_body
  belongs_to :bidder

  def self.load_from_hash(properties)
    # Load the data only if it's a public works award
    # FIXME: Let's load the whole thing temporarily and use the database to check
    # whether we're doing it correctly.
    # return unless properties['[QCLO] Es Obra Pública']=='S'

    # Find or create related entities
    public_body = PublicBody.where(name: properties['Entidad adjudicadora - Organismo']).first_or_create
    bidder = Bidder.where(name: properties['Formalización del contrato - Contratista']).first_or_create

    # Read amount and store it in cents. We are being extra careful here not to change any amount
    # even in one cent. That's why we are avoiding floats, not a good fit when exact precision is needed.
    amount = BigDecimal.new(properties['Análisis - Importe'])*100

    # Store the award
    Award.create!({
        public_body: public_body,
        bidder: bidder, 
        award_date: properties['[QCLO] Fecha de adjudicación'],
        category: properties['Análisis - Tipo'],
        process_type: properties['Análisis - Procedimiento'],
        process_track: properties['Análisis - Tramitación'],
        amount: amount,
        properties: properties
      })
  end
end
{'fraction_non_alphanumeric': 0.09049015500628405, 'fraction_numerical': 0.0025136154168412233, 'mean_word_length': 3.5834932821497123, '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': '1026082', 'n_tokens_mistral': 702, 'n_tokens_neox': 691, 'n_words': 216}