diff --git a/lib/modesty/datastore/redis.rb b/lib/modesty/datastore/redis.rb index f01b47a..48b5df6 100644 --- a/lib/modesty/datastore/redis.rb +++ b/lib/modesty/datastore/redis.rb @@ -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) @@ -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) diff --git a/lib/modesty/metric/data.rb b/lib/modesty/metric/data.rb index d8c0594..5ed6e9e 100644 --- a/lib/modesty/metric/data.rb +++ b/lib/modesty/metric/data.rb @@ -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) @@ -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 diff --git a/spec/identity_spec.rb b/spec/identity_spec.rb index 3a6d739..f46d759 100644 --- a/spec/identity_spec.rb +++ b/spec/identity_spec.rb @@ -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] diff --git a/spec/metric_spec.rb b/spec/metric_spec.rb index 5160e6f..fa42d3f 100644 --- a/spec/metric_spec.rb +++ b/spec/metric_spec.rb @@ -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