Class: Modesty::RedisData::MetricData

Inherits:
Datastore::MetricData show all
Defined in:
lib/modesty/datastore/redis.rb

Instance Method Summary collapse

Methods inherited from Datastore::MetricData

#initialize

Constructor Details

This class inherits a constructor from Modesty::Datastore::MetricData

Instance Method Details

#add_counts(date, count) ⇒ Object



60
61
62
# File 'lib/modesty/datastore/redis.rb', line 60

def add_counts(date, count)
  data.incrby(count_key(date),  count)
end

#add_param_counts(date, count, param, id) ⇒ Object

-*- :with => params -*- #



92
93
94
95
# File 'lib/modesty/datastore/redis.rb', line 92

def add_param_counts(date, count, param, id)
#puts "data.hincrby(#{key_for_with(param, date).inspect}, #{id.inspect}, #{count.inspect})"
  data.hincrby(key_for_with(param, date), id, count)
end

#aggregate_by(param, date) ⇒ Object



111
112
113
114
115
# File 'lib/modesty/datastore/redis.rb', line 111

def aggregate_by(param, date)
  agg = data.hgetall(key_for_with(param, date))
  agg.map! { |k,v| [k,v].map(&:to_i?) }
  agg
end

#all(param, date) ⇒ Object



101
102
103
# File 'lib/modesty/datastore/redis.rb', line 101

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

#count(date) ⇒ Object



64
65
66
# File 'lib/modesty/datastore/redis.rb', line 64

def count(date)
  data.get(count_key(date)).to_i
end

#count_key(date) ⇒ Object

-*- raw counts -*- #



56
57
58
# File 'lib/modesty/datastore/redis.rb', line 56

def count_key(date)
  key(date, :count)
end

#count_range(range) ⇒ Object



68
69
70
71
# File 'lib/modesty/datastore/redis.rb', line 68

def count_range(range)
  keys = range.map { |d| count_key(d) }
  data.mget(keys).map(&:to_i?)
end

#count_unidentified_user(date) ⇒ Object

-*- unidentified users -*- #



74
75
76
# File 'lib/modesty/datastore/redis.rb', line 74

def count_unidentified_user(date)
  data.incr(unidentified_users_key(date))
end

#dataObject



47
48
49
# File 'lib/modesty/datastore/redis.rb', line 47

def data
  Modesty.data
end

#distribution_by(param, date) ⇒ Object



105
106
107
108
109
# File 'lib/modesty/datastore/redis.rb', line 105

def distribution_by(param, date)
  dist = data.hvals(key_for_with(param, date)).histogram
  dist.map! { |k,v| [k,v].map(&:to_i?) }
  dist
end

#key(*args) ⇒ Object



43
44
45
# File 'lib/modesty/datastore/redis.rb', line 43

def key(*args)
  RedisData.keyify('metrics', @metric.slug, *args)
end

#key_for_with(*args) ⇒ Object



51
52
53
# File 'lib/modesty/datastore/redis.rb', line 51

def key_for_with(*args)
  key(:with, *args)
end

#track!(count, with_args) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/modesty/datastore/redis.rb', line 117

def track!(count, with_args)
  data.pipelined do
    [:all, Date.today].each do |date|
      self.add_counts(date, count)

      self.count_unidentified_user(date) unless with_args[:user]

      with_args.each do |param, id|
        self.add_param_counts(date, count, param, id)
      end
    end
  end
end

#unidentified_users(date = :all) ⇒ Object



82
83
84
# File 'lib/modesty/datastore/redis.rb', line 82

def unidentified_users(date = :all)
  data.get(unidentified_users_key(date)).to_i
end

#unidentified_users_key(date) ⇒ Object



78
79
80
# File 'lib/modesty/datastore/redis.rb', line 78

def unidentified_users_key(date)
  key_for_with(:users, date, :unidentified)
end

#unidentified_users_range(range) ⇒ Object



86
87
88
89
# File 'lib/modesty/datastore/redis.rb', line 86

def unidentified_users_range(range)
  keys = range.map { |d| unidentified_users_key(d) }
  data.mget(keys).map(&:to_i?)
end

#unique(param, date) ⇒ Object



97
98
99
# File 'lib/modesty/datastore/redis.rb', line 97

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