module Octo::Trendable
Public Instance Methods
aggregate!(ts = Time.now.floor)
click to toggle source
Aggregates and attempts to store it into the database. This would only
work if the class that extends Octo::Counter includes from Cequel::Record
# File lib/octocore/trendable.rb, line 29 def aggregate!(ts = Time.now.floor) unless self.ancestors.include?Cequel::Record raise NoMethodError, 'aggregate! not defined for this counter' end aggr = aggregate(ts) sum = aggregate_sum(aggr) aggr.each do |_ts, counterVals| counterVals.each do |obj, count| counter = self.new counter.enterprise_id = obj.enterprise.id counter.uid = obj.unique_id counter.count = count counter.type = Octo::Counter::TYPE_MINUTE counter.ts = _ts totalCount = sum[_ts][obj.enterprise_id.to_s].to_f counter.obp = (count * 1.0)/totalCount baseline_value = get_baseline_value(:TYPE_MINUTE, obj) counter.divergence = kl_divergence(counter.obp, baseline_value) counter.save! end end call_completion_hook(Octo::Counter::TYPE_MINUTE, ts) end
baseline(klass)
click to toggle source
Define the baseline class for this trend
# File lib/octocore/trendable.rb, line 17 def baseline(klass) @baseline_klass = klass end
trendables()
click to toggle source
Define the columns necessary for a trendable model
# File lib/octocore/trendable.rb, line 11 def trendables column :divergence, :float column :obp, :float end
trends_class(klass)
click to toggle source
Define the class for trends
# File lib/octocore/trendable.rb, line 22 def trends_class(klass) @trends_klass = klass end
Private Instance Methods
aggregate_sum(aggr)
click to toggle source
Aggregates to find the sum of all counters for an enterprise
at a time
@param [Hash] aggr The aggregated hash @return [Hash] The summed up hash
# File lib/octocore/trendable.rb, line 62 def aggregate_sum(aggr) sum = {} aggr.each do |ts, counterVals| sum[ts] = {} unless sum.has_key?ts counterVals.each do |obj, count| if obj.respond_to?(:enterprise_id) eid = obj.public_send(:enterprise_id).to_s sum[ts][eid] = sum[ts].fetch(eid, 0) + count end end end sum end
get_baseline_value(baseline_type, object)
click to toggle source
Get the baseline value for an object. @param [Fixnum] baseline_type The type of baseline to fetch @param [Object] object The object for which baseline is to
be fetched
# File lib/octocore/trendable.rb, line 80 def get_baseline_value(baseline_type, object) clazz = @baseline_klass.constantize clazz.public_send(:get_baseline_value, baseline_type, object) end