web/app/models/benchmark_result.rb
author Marcel Hlopko <marcel.hlopko@gmail.com>
Thu, 15 Aug 2013 10:43:11 +0200
changeset 157 91374e0a6062
parent 156 c2db68d42669
child 174 b8e773677056
child 177 8e7f0029550d
permissions -rw-r--r--
migrate compare charts to rickshaw

# == Schema Information
#
# Table name: benchmark_results
#
#  id                 :integer          not null, primary key
#  benchmark_info_id  :integer
#  created_at         :datetime
#  updated_at         :datetime
#  benchmark_batch_id :integer
#

class BenchmarkResult < ActiveRecord::Base

  belongs_to :benchmark_info, inverse_of: :benchmark_results
  belongs_to :benchmark_batch, inverse_of: :benchmark_results
  has_many :benchmark_durations, dependent: :destroy, inverse_of: :benchmark_result

  def average_duration
    sum = benchmark_durations.inject(0) { |acc, val| acc += val.duration }
    sum / benchmark_durations.size
  end

  def min_duration
    benchmark_durations.map { |duration| duration.duration }.min
  end

  def max_duration
    benchmark_durations.map { |duration| duration.duration }.max
  end

  
  def to_json
    { 
      timestamp: performed_at.to_i, 
      value: self.min_duration
    }
  end

  def performed_at
    benchmark_batch.performed_at
  end


end