Class: Prisma::Group
- Inherits:
-
Object
- Object
- Prisma::Group
- Defined in:
- lib/prisma/group.rb
Overview
Represents a configured group, has convenience methods for getting data.
Instance Attribute Summary collapse
-
#block ⇒ Object
Block which gets called to evaluate if request should be counted and depending on the type how the request should be counted.
-
#description ⇒ Object
The description of the group, typcially a
String
. -
#name ⇒ Object
The name of the group, typically a
Symbol
. -
#type ⇒ Object
The type of the group,
:counter
or:bitmap
.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Group
constructor
Initialize
Group
from a hash. -
#monthly(range) ⇒ Hash
Get a
Hash
with theDate
as key and amount of items as the value. -
#range(range, options = {}) ⇒ Hash
(also: #daily)
Get a
Hash
with theDate
as key and amount of items as the value. -
#weekly(range) ⇒ Hash
Get a
Hash
with theDate
as key and amount of items as the value.
Constructor Details
#initialize(options = {}) ⇒ Group
Initialize Group
from a hash
19 20 21 22 23 24 25 26 27 |
# File 'lib/prisma/group.rb', line 19 def initialize(={}) .reverse_merge!(type: :counter) raise ArgumentError.new("Type #{[:type].inspect} not allowed") unless [:counter, :bitmap].include? [:type] self.name = [:name] self.type = [:type] self.description = [:description] self.block = [:block] end |
Instance Attribute Details
#block ⇒ Object
Block which gets called to evaluate if request should be counted and depending on the type how the request should be counted. When type
is :counter
the request is getting counted as long as the return value is not nil or false. When type
is :bitmap
the request is getting counted as long as the return value is an integer.
16 17 18 |
# File 'lib/prisma/group.rb', line 16 def block @block end |
#description ⇒ Object
The description of the group, typcially a String
11 12 13 |
# File 'lib/prisma/group.rb', line 11 def description @description end |
#name ⇒ Object
The name of the group, typically a Symbol
5 6 7 |
# File 'lib/prisma/group.rb', line 5 def name @name end |
#type ⇒ Object
The type of the group, :counter
or :bitmap
8 9 10 |
# File 'lib/prisma/group.rb', line 8 def type @type end |
Instance Method Details
#monthly(range) ⇒ Hash
Get a Hash
with the Date
as key and amount of items as the value. Grouped by month, key represents a Date
object of the first day of the month.
group.monthly(1.month.ago.to_date..Date.today)
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/prisma/group.rb', line 73 def monthly(range) data = range(range, :skip_bitmap_count => true) data = data.group_by { |date, value| date.beginning_of_month } case self.type when :counter sum_up_grouped_data(data) when :bitmap bitmap_or_grouped_data(data) end end |
#range(range, options = {}) ⇒ Hash Also known as: daily
Get a Hash
with the Date
as key and amount of items as the value. Grouped by day.
group.range(5.days.ago.to_date..Date.today)
group.daily(5.days.ago.to_date..Date.today)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/prisma/group.rb', line 34 def range(range, ={}) range = (range..range) if range.is_a? Date data = range.map do |date| case type when :counter value = Prisma.redis.get(Prisma.redis_key(name, date)).to_i when :bitmap bitstring = Prisma.redis.get(Prisma.redis_key(name, date)) || '' string = bitstring.unpack('b*').first value = [:skip_bitmap_count] ? string : string.count('1') end [date, value] end Hash[data] end |
#weekly(range) ⇒ Hash
Get a Hash
with the Date
as key and amount of items as the value. Grouped by week, key represents a Date
object of the first day of the week.
group.weekly(1.week.ago.to_date..Date.today)
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/prisma/group.rb', line 57 def weekly(range) data = range(range, :skip_bitmap_count => true) data = data.group_by { |date, value| date.beginning_of_week } case self.type when :counter sum_up_grouped_data(data) when :bitmap bitmap_or_grouped_data(data) end end |