Sha256: fabae4d12957ae0dd73b8b2cbda86d4d810c05cfe09f4427c4abec1fd5fb560d

Contents?: true

Size: 1.57 KB

Versions: 4

Compression:

Stored size: 1.57 KB

Contents

class Result < ActiveRecord::Base
  acts_as_paranoid
  belongs_to :query_version
  has_one :query, through: :query_version
  has_many :query_roles, through: :query
  belongs_to :owner, class_name: 'User'

  serialize :sample_data, JSON
  serialize :parameters, JSON
  serialize :headers, JSON

  scope :completed, -> { where(status: 'complete') }
  scope :with_role, ->(role) { references(:query).includes(:query_roles).where(query_roles: { role: role }) }

  delegate :ongoing_row_count, to: :redis_result_row_count

  def mark_running!
    update_attributes!(status: 'running', started_at: Time.now)
  end

  def mark_processing_from_sample(result_sample)
    update_attributes!(status: 'processing', sample_data: result_sample)
  end

  def mark_complete_with_count(row_count)
    update_attributes!(status: 'complete', row_count: row_count, completed_at: Time.now)
  end

  def mark_failed!(message)
    update_attributes!(status: 'failed', error_message: message, completed_at: Time.now)
  end

  def row_count
    super || ongoing_row_count
  end

  def result_csv
    @result_csv ||= ResultCsv.new(id)
  end

  def redis_result_row_count
    @redis_result_row_count ||= RedisResultRowCount.new(self)
  end

  def run_duration
    duration(:started_at, :completed_at)
  end

  def enqueue_duration
    duration(:created_at, :started_at)
  end

  private

  def duration(start_field, end_field)
    start_ts = public_send(start_field)
    end_ts = public_send(end_field)

    if start_ts && end_ts
      end_ts - start_ts
    elsif start_ts
      Time.zone.now - start_ts
    else
      0
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
aleph_analytics-0.2.0 app/models/result.rb
aleph_analytics-0.1.0 app/models/result.rb
aleph_analytics-0.0.6 app/models/result.rb
aleph_analytics-0.0.5 app/models/result.rb