<commit_before>class Test < ApplicationRecord

  belongs_to :request

  serialize :key
  serialize :value
  serialize :history

  scope :failing, -> { where(:status => false) }
  scope :passing, -> { where(:status => true) }

  def run

    purge_history
  	
    if request.response_body.nil? or request.response_code.nil?
      new_failure = self.new_failure? false
      self.status = false
      add_history false
      self.save
  	  return new_failure
  	end

  	begin
      body = nil
      if request.format == "xml"
        body = Hash.from_xml(request.response_body)
      else
        body = JSON.parse(request.response_body)
      end

      code = request.response_code
      
      #Replace every instance of _body_ with the actual body of the response. 
      #Replace every instance of _code_ with the actual code of the response.
      decoded_key = self.key.gsub('_body_', body.to_s).gsub('_code_', code.to_s)      
      
      result = eval(decoded_key)
      new_status = (result.to_s == value.to_s)
      new_failure = self.new_failure? new_status
      self.status = new_status
      add_history new_status
      self.save
      return new_failure
    rescue
      new_failure = self.new_failure? false
      self.status = false
      add_history false
      self.save
      return new_failure 
    end

  end

  def new_failure? new_status
    if self.status and not new_status
      return true
    else
      return false
    end
  end

  # Delete all history over 2 weeks old 
  # todo: make this 2 weeks a configurable
  def purge_history
    unless history.nil?
      two_weeks_ago = Time.now - 2.weeks
      self.history.delete_if { |x| x[:time] < two_weeks_ago }
      self.save
    end
  end

  def add_history result
    self.history << {time: Time.now, result: result}
  end

  def pass_rate
    # Successes / Total 
    (history.count{ |x| x[:result] == true }).to_f / history.count.to_f
  end

end<commit_msg>Fix issue with creating new history<commit_after>class Test < ApplicationRecord

  belongs_to :request

  serialize :key
  serialize :value
  serialize :history

  scope :failing, -> { where(:status => false) }
  scope :passing, -> { where(:status => true) }

  def run

    purge_history
  	
    if request.response_body.nil? or request.response_code.nil?
      new_failure = self.new_failure? false
      self.status = false
      add_history false
      self.save
  	  return new_failure
  	end

  	begin
      body = nil
      if request.format == "xml"
        body = Hash.from_xml(request.response_body)
      else
        body = JSON.parse(request.response_body)
      end

      code = request.response_code
      
      #Replace every instance of _body_ with the actual body of the response. 
      #Replace every instance of _code_ with the actual code of the response.
      decoded_key = self.key.gsub('_body_', body.to_s).gsub('_code_', code.to_s)      
      
      result = eval(decoded_key)
      new_status = (result.to_s == value.to_s)
      new_failure = self.new_failure? new_status
      self.status = new_status
      add_history new_status
      self.save
      return new_failure
    rescue
      new_failure = self.new_failure? false
      self.status = false
      add_history false
      self.save
      return new_failure 
    end

  end

  def new_failure? new_status
    if self.status and not new_status
      return true
    else
      return false
    end
  end

  # Delete all history over 2 weeks old 
  # todo: make this 2 weeks a configurable
  def purge_history
    unless history.nil?
      two_weeks_ago = Time.now - 2.weeks
      self.history.delete_if { |x| x[:time] < two_weeks_ago }
      self.save
    end
  end

  def add_history result
    if self.history.nil?
      self.history = []
    end
    self.history << {time: Time.now, result: result}
  end

  def pass_rate
    # Successes / Total 
    (history.count{ |x| x[:result] == true }).to_f / history.count.to_f
  end

end
{'fraction_non_alphanumeric': 0.07789261406604488, 'fraction_numerical': 0.0015124779430299975, 'mean_word_length': 2.3485232067510546, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '2835596', 'n_tokens_mistral': 1279, 'n_tokens_neox': 1230, 'n_words': 350}