Skip to content

Commit

Permalink
Changed the paradigm for distribution_by, and introduced aggregate_by.
Browse files Browse the repository at this point in the history
m.aggregate_by(:users) looks like
{user_id => number of tracks by this user}

m.distribution_by(:users) is now equivalent to
m.aggregate_by(:users).histogram
  • Loading branch information
Jay Adkisson committed May 22, 2010
1 parent 360b28b commit 24f5434
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
25 changes: 12 additions & 13 deletions lib/modesty/datastore/redis.rb
Expand Up @@ -77,22 +77,23 @@ def distribution(date)
end

def unique(param, date)
data.scard(key_for_with(param, date))
data.hlen(key_for_with(param, date))
end

def all(param, date)
data.smembers(key_for_with(param, date)).map(&:to_i?)
data.hkeys(key_for_with(param, date)).map(&:to_i?)
end

def distribution_by(param, date)
ids = data.smembers(key_for_with(param, date)).map(&:to_i?)
h = {}
ids.each do |id|
h[id] = Hash[data.hgetall(key_for_with(param, date, id)).map do |k,v|
[k.to_i?, v.to_i]
end]
end
return h
data.hvals(self.key_for_with(param, date)).map(&:to_i?).histogram
end

def aggregate_by(param, date)
Hash[
data.hgetall(self.key_for_with(param, date)).map do |k,v|
[k.to_i?, v.to_i?]
end
]
end

def track!(count, with_args)
Expand All @@ -113,9 +114,7 @@ def add_counts(date, count)
end

def add_param_counts(date, count, param, id)
data.sadd(key_for_with(:__keys__), param)
data.sadd(key_for_with(param, date), id)
data.hincrby(key_for_with(param, date, id), count, 1)
data.hincrby(key_for_with(param, date), id, count)
end

def count_unidentified_user(date)
Expand Down
12 changes: 10 additions & 2 deletions lib/modesty/metric/data.rb
Expand Up @@ -27,7 +27,10 @@ def parse_date_or_range(start=nil,fin=nil)
end


[:count, :distribution].each do |data_type|
[
:count,
:distribution
].each do |data_type|
data_type_by_range = :"#{data_type}_by_range"
define_method(data_type) do |*dates|
date_or_range = parse_date_or_range(*dates)
Expand All @@ -47,7 +50,12 @@ def parse_date_or_range(start=nil,fin=nil)
end
end

[:all, :unique, :distribution_by].each do |data_type|
[
:all,
:unique,
:distribution_by,
:aggregate_by
].each do |data_type|
by_range = :"#{data_type}_by_range"
define_method(data_type) do |sym, *dates|
sym = sym.to_sym
Expand Down
4 changes: 3 additions & 1 deletion spec/identity_spec.rb
Expand Up @@ -108,7 +108,9 @@

@group_exp.distribution.should == {45 => 1, 30 => 1, 15 => 1}
@group_ctrl.distribution.should == {}
@group_exp.distribution_by(:creators).should == {700 => {45 => 1, 30 => 1, 15 => 1}}
@group_exp.aggregate_by(:creators).should == {700 => 90}
@group_ctrl.aggregate_by(:creators).should == {}
@group_exp.distribution_by(:creators).should == {90 => 1}
@group_ctrl.distribution_by(:creators).should == {}

@group_exp.all(:creators).should == [700]
Expand Down
5 changes: 4 additions & 1 deletion spec/metric_spec.rb
Expand Up @@ -155,9 +155,12 @@
m.unique(:zings, Date.parse('1/1/2002')).should == 0
m.unique(:zings, :all).should == 2

m.distribution_by(:zings).should == {56=>{1=>1}, 97=>{7=>1, 4=>1}}
m.distribution.should == {1 => 1, 7 => 1, 4 => 1}

#one zing has one track, and one zing has 11 (= 4+7) tracks.
m.distribution_by(:zings).should == {1 => 1, 11 => 1}
m.aggregate_by(:zings).should == {56 => 1, 97 => 11}

m.distribution(:all).should == {1 => 1, 7 => 1, 4 => 1}
end
end

0 comments on commit 24f5434

Please sign in to comment.