Module: Octo::Trendable

Includes:
KLDivergence
Included in:
CategoryHit, ProductHit, TagHit
Defined in:
lib/octocore-mongo/trendable.rb

Instance Method Summary collapse

Methods included from KLDivergence

#kl_divergence

Instance Method Details

#aggregate!(ts = Time.now.floor) ⇒ Object

Aggregates and attempts to store it into the database. This would only

work if the class that extends Octo::Counter includes from
Cequel::Record


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/octocore-mongo/trendable.rb', line 29

def aggregate!(ts = Time.now.floor)
  unless self.ancestors.include?MongoMapper::Document
    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) ⇒ Object

Define the baseline class for this trend



17
18
19
# File 'lib/octocore-mongo/trendable.rb', line 17

def baseline(klass)
  @baseline_klass = klass
end

#trendablesObject

Define the columns necessary for a trendable model



11
12
13
14
# File 'lib/octocore-mongo/trendable.rb', line 11

def trendables
  key :divergence, Float
  key :obp, Float
end

Define the class for trends



22
23
24
# File 'lib/octocore-mongo/trendable.rb', line 22

def trends_class(klass)
  @trends_klass = klass
end