Module: Vanity::Metric::ActiveRecord

Defined in:
lib/vanity/metric/active_record.rb

Overview

Calling model method on a metric extends it with these modules, redefining the values and track! methods.

Since:

  • 1.3.0

Instance Method Summary collapse

Instance Method Details

#after_create(record) ⇒ Object

AR model after_create callback notifies all the hooks.

Since:

  • 1.3.0



76
77
78
79
80
# File 'lib/vanity/metric/active_record.rb', line 76

def after_create(record)
  return unless @playground.collecting?
  count = @ar_column ? (record.send(@ar_column) || 0) : 1
  call_hooks record.send(@ar_timestamp), nil, [count] if count > 0 && @ar_scoped.exists?(record)
end

#last_update_atObject

Since:

  • 1.3.0



70
71
72
73
# File 'lib/vanity/metric/active_record.rb', line 70

def last_update_at
  record = @ar_scoped.find(:first, :order=>"#@ar_timestamp DESC", :limit=>1, :select=>@ar_timestamp)
  record && record.send(@ar_timestamp)
end

#track!(args = nil) ⇒ Object

This track! method stores nothing, but calls the hooks.

Since:

  • 1.3.0



65
66
67
68
# File 'lib/vanity/metric/active_record.rb', line 65

def track!(args = nil)
  return unless @playground.collecting?
  call_hooks *track_args(args)
end

#values(sdate, edate) ⇒ Object

This values method queries the database.

Since:

  • 1.3.0



57
58
59
60
61
62
# File 'lib/vanity/metric/active_record.rb', line 57

def values(sdate, edate)
  query = { :conditions=>{ @ar_timestamp=>(sdate.to_time...(edate + 1).to_time) },
            :group=>"date(#{@ar_scoped.connection.quote_column_name @ar_timestamp})" }
  grouped = @ar_column ? @ar_scoped.send(@ar_aggregate, @ar_column, query) : @ar_scoped.count(query)
  (sdate..edate).inject([]) { |ordered, date| ordered << (grouped[date.to_s] || 0) }
end