prisma 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/prisma/group.rb +39 -13
  2. metadata +3 -3
@@ -31,20 +31,22 @@ module Prisma
31
31
  # group.daily(5.days.ago.to_date..Date.today)
32
32
  # @param [Range] range of days
33
33
  # @return [Hash]
34
- def range(range)
35
- range = range..range if range.is_a? Date
36
- data = {}
37
- range.each do |date|
34
+ def range(range, options={})
35
+ range = (range..range) if range.is_a? Date
36
+ data = range.map do |date|
38
37
  case type
39
38
  when :counter
40
- data[date] = Prisma.redis.get(Prisma.redis_key(name, date)).to_i
39
+ value = Prisma.redis.get(Prisma.redis_key(name, date)).to_i
41
40
  when :bitmap
42
41
  bitstring = Prisma.redis.get(Prisma.redis_key(name, date)) || ''
43
42
  string = bitstring.unpack('b*').first
44
- data[date] = string.count('1')
43
+ value = options[:skip_bitmap_count] ? string : string.count('1')
45
44
  end
45
+
46
+ [date, value]
46
47
  end
47
- data
48
+
49
+ Hash[data]
48
50
  end
49
51
  alias_method :daily, :range
50
52
 
@@ -53,10 +55,15 @@ module Prisma
53
55
  # @param [Range] range of days
54
56
  # @return [Hash]
55
57
  def weekly(range)
56
- data = range(range)
58
+ data = range(range, :skip_bitmap_count => true)
57
59
 
58
60
  data = data.group_by { |date, value| date.beginning_of_week }
59
- sum_up_grouped_data(data)
61
+ case self.type
62
+ when :counter
63
+ sum_up_grouped_data(data)
64
+ when :bitmap
65
+ bitmap_or_grouped_data(data)
66
+ end
60
67
  end
61
68
 
62
69
  # 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.
@@ -64,18 +71,37 @@ module Prisma
64
71
  # @param [Range] range of days
65
72
  # @return [Hash]
66
73
  def monthly(range)
67
- data = range(range)
74
+ data = range(range, :skip_bitmap_count => true)
68
75
 
69
76
  data = data.group_by { |date, value| date.beginning_of_month }
70
- sum_up_grouped_data(data)
77
+ case self.type
78
+ when :counter
79
+ sum_up_grouped_data(data)
80
+ when :bitmap
81
+ bitmap_or_grouped_data(data)
82
+ end
71
83
  end
72
84
 
73
85
  private
74
86
 
75
87
  def sum_up_grouped_data(data)
76
- data.each do |date, values|
77
- data[date] = values.map { |value| value.second }.inject{ |sum, count| sum + count }
88
+ data = data.map do |date, values|
89
+ value = values.inject(0) { |sum, value| sum + value.second.to_i }
90
+
91
+ [date, value]
78
92
  end
93
+
94
+ Hash[data]
95
+ end
96
+
97
+ def bitmap_or_grouped_data(data)
98
+ data = data.map do |date, values|
99
+ value = values.map { |value| value.second.to_i }.inject(:|)
100
+ value = value.to_s.count('1')
101
+ [date, value]
102
+ end
103
+
104
+ Hash[data]
79
105
  end
80
106
  end
81
107
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prisma
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-14 00:00:00.000000000 Z
12
+ date: 2012-07-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -337,7 +337,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
337
337
  version: '0'
338
338
  segments:
339
339
  - 0
340
- hash: -4548465822785429403
340
+ hash: -2365970208530084805
341
341
  required_rubygems_version: !ruby/object:Gem::Requirement
342
342
  none: false
343
343
  requirements: