Skip to content

Commit

Permalink
Track and count within a user context
Browse files Browse the repository at this point in the history
  • Loading branch information
elcuervo committed Oct 15, 2015
1 parent 383a08f commit f8116a8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 16 deletions.
28 changes: 20 additions & 8 deletions lib/minuteman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,35 @@ def track(action, users = nil, time = Time.now.utc)
users
end

def count(action, time = Time.now.utc)
def add(action, time = Time.now.utc, users = [])
time_spans.each do |time_span|
counter = Minuteman::Counter.create(
counter = Minuteman::Counter.create({
type: action,
time: patterns[time_span].call(time)
)
})

counter.incr
end

Array(users).each do |user|
time_spans.each do |time_span|
counter = Minuteman::Counter::User.create({
user_id: user.id,
type: action,
time: patterns[time_span].call(time)
})

counter.incr
end
end
end

def analyze(action)
analyzers_cache[action]
end

def statistics(action)
statistics_cache[action]
def count(action)
counters_cache[action]
end

private
Expand All @@ -69,8 +81,8 @@ def analyzers_cache
end
end

def statistics_cache
@_statistics_cache ||= Hash.new do |h,k|
def counters_cache
@_counters_cache ||= Hash.new do |h,k|
h[k] = Minuteman::Analyzer.new(k, Minuteman::Counter)
end
end
Expand All @@ -82,7 +94,7 @@ def Minuteman(action)
end

def Counterman(action)
Minuteman.statistics(action)
Minuteman.count(action)
end

require 'minuteman/user'
Expand Down
10 changes: 5 additions & 5 deletions lib/minuteman/analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

module Minuteman
class Analyzer
def initialize(action, klass = Minuteman::Event)
@action = action

def initialize(action, klass = Minuteman::Event, user = nil)
Minuteman.patterns.keys.each do |method|
define_singleton_method(method) do |time = Time.now.utc|
if !Minuteman.patterns.include?(method)
raise MissingPattern.new(method)
end

key = Minuteman.patterns[method].call(time)
klass.find_or_create(type: action, time: key)
search = { type: action, time: key }
search[:user_id] = user.id if !user.nil?

klass.find_or_create(search)
end
end
end
end

end
8 changes: 8 additions & 0 deletions lib/minuteman/counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

module Minuteman
class Counter < Minuteman::Model
class User < Counter
attribute :user_id

def key
"#{super}:#{user_id}"
end
end

def incr
Minuteman.config.redis.call("INCR", key)
end
Expand Down
12 changes: 12 additions & 0 deletions lib/minuteman/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ def save
super
end

def track(action, time = Time.now.utc)
Minuteman.track(action, self, time)
end

def add(action, time = Time.now.utc)
Minuteman.add(action, time, self)
end

def count(action, time = Time.now.utc)
Minuteman::Analyzer.new(action, Minuteman::Counter::User, self)
end

def promote(identifier)
self.identifier = identifier
save
Expand Down
20 changes: 17 additions & 3 deletions test/minuteman_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
end

test "count a given event" do
10.times { Minuteman.count("enter:new_landing") }
10.times { Minuteman.add("enter:new_landing") }

assert Counterman("enter:new_landing").day.count == 10
end
Expand All @@ -182,9 +182,23 @@
day = Time.new(2015, 10, 15)
next_day = Time.new(2015, 10, 16)

5.times { Minuteman.count("drink:beer", day) }
2.times { Minuteman.count("drink:beer", next_day) }
5.times { Minuteman.add("drink:beer", day) }
2.times { Minuteman.add("drink:beer", next_day) }

assert Counterman("drink:beer").month(day).count == 7
assert Counterman("drink:beer").day(day).count == 5
end

scope "do actions through a user" do
test "track an event" do
user = Minuteman::User.create
user.track("login:page")

3.times { user.add("login:attempts") }
2.times { Minuteman.add("login:attempts") }

assert Minuteman("login:page").day.include?(user)
assert Counterman("login:attempts").day.count == 5
assert user.count("login:attempts").day.count == 3
end
end

0 comments on commit f8116a8

Please sign in to comment.