Class: Prisma::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/prisma/group.rb

Overview

Represents a configured group, has convenience methods for getting data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Group

Initialize Group from a hash

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
# File 'lib/prisma/group.rb', line 19

def initialize(options={})
  options.reverse_merge!(type: :counter)
  raise ArgumentError.new("Type #{options[:type].inspect} not allowed") unless [:counter, :bitmap].include? options[:type]

  self.name = options[:name]
  self.type = options[:type]
  self.description = options[:description]
  self.block = options[:block]
end

Instance Attribute Details

#blockObject

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

#descriptionObject

The description of the group, typcially a String



11
12
13
# File 'lib/prisma/group.rb', line 11

def description
  @description
end

#nameObject

The name of the group, typically a Symbol



5
6
7
# File 'lib/prisma/group.rb', line 5

def name
  @name
end

#typeObject

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)

Parameters:

  • range (Range)

    of days

Returns:

  • (Hash)


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)

Parameters:

  • range (Range)

    of days

Returns:

  • (Hash)


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, options={})
  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 = options[: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)

Parameters:

  • range (Range)

    of days

Returns:

  • (Hash)


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