redistat 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/.travis.yml +8 -0
  2. data/README.md +219 -97
  3. data/lib/redistat.rb +13 -13
  4. data/lib/redistat/buffer.rb +27 -24
  5. data/lib/redistat/collection.rb +5 -5
  6. data/lib/redistat/connection.rb +23 -18
  7. data/lib/redistat/core_ext.rb +1 -1
  8. data/lib/redistat/core_ext/bignum.rb +2 -2
  9. data/lib/redistat/core_ext/date.rb +2 -2
  10. data/lib/redistat/core_ext/fixnum.rb +2 -2
  11. data/lib/redistat/core_ext/hash.rb +4 -4
  12. data/lib/redistat/date.rb +11 -11
  13. data/lib/redistat/event.rb +18 -18
  14. data/lib/redistat/finder.rb +39 -39
  15. data/lib/redistat/finder/date_set.rb +4 -4
  16. data/lib/redistat/key.rb +16 -16
  17. data/lib/redistat/label.rb +14 -14
  18. data/lib/redistat/mixins/database.rb +1 -1
  19. data/lib/redistat/mixins/date_helper.rb +1 -1
  20. data/lib/redistat/mixins/options.rb +8 -8
  21. data/lib/redistat/mixins/synchronize.rb +12 -11
  22. data/lib/redistat/model.rb +25 -17
  23. data/lib/redistat/result.rb +4 -4
  24. data/lib/redistat/scope.rb +5 -5
  25. data/lib/redistat/summary.rb +33 -26
  26. data/lib/redistat/version.rb +1 -1
  27. data/redistat.gemspec +4 -3
  28. data/spec/buffer_spec.rb +27 -25
  29. data/spec/collection_spec.rb +4 -4
  30. data/spec/connection_spec.rb +12 -12
  31. data/spec/core_ext/hash_spec.rb +5 -5
  32. data/spec/database_spec.rb +3 -3
  33. data/spec/date_spec.rb +15 -15
  34. data/spec/event_spec.rb +8 -8
  35. data/spec/finder/date_set_spec.rb +134 -134
  36. data/spec/finder_spec.rb +36 -36
  37. data/spec/key_spec.rb +19 -19
  38. data/spec/label_spec.rb +10 -10
  39. data/spec/model_helper.rb +10 -9
  40. data/spec/model_spec.rb +38 -41
  41. data/spec/options_spec.rb +9 -9
  42. data/spec/result_spec.rb +4 -4
  43. data/spec/scope_spec.rb +6 -6
  44. data/spec/spec_helper.rb +6 -0
  45. data/spec/summary_spec.rb +31 -24
  46. data/spec/synchronize_spec.rb +118 -57
  47. data/spec/thread_safety_spec.rb +6 -6
  48. metadata +88 -126
  49. data/.rvmrc +0 -1
@@ -3,117 +3,117 @@ require 'redistat/finder/date_set'
3
3
  module Redistat
4
4
  class Finder
5
5
  include Database
6
-
6
+
7
7
  class << self
8
8
  def find(*args)
9
9
  new.find(*args)
10
10
  end
11
-
11
+
12
12
  def scope(scope)
13
13
  new.scope(scope)
14
14
  end
15
-
15
+
16
16
  def label(label)
17
17
  new.label(label)
18
18
  end
19
-
19
+
20
20
  def dates(from, till)
21
21
  new.dates(from, till)
22
22
  end
23
23
  alias :date :dates
24
-
24
+
25
25
  def from(date)
26
26
  new.from(date)
27
27
  end
28
-
28
+
29
29
  def till(date)
30
30
  new.till(date)
31
31
  end
32
32
  alias :untill :till
33
-
33
+
34
34
  def depth(unit)
35
35
  new.depth(unit)
36
36
  end
37
-
37
+
38
38
  def interval(unit)
39
39
  new.interval(unit)
40
40
  end
41
41
  end
42
-
42
+
43
43
  attr_reader :options
44
-
44
+
45
45
  def initialize(opts = {})
46
46
  set_options(opts)
47
47
  end
48
-
48
+
49
49
  def options
50
50
  @options ||= {}
51
51
  end
52
-
52
+
53
53
  def all(reload = false)
54
54
  @result = nil if reload
55
55
  @result ||= find
56
56
  end
57
-
57
+
58
58
  def total
59
59
  all.total
60
60
  end
61
-
61
+
62
62
  def each(&block)
63
63
  all.each(&block)
64
64
  end
65
-
65
+
66
66
  def map(&block)
67
67
  all.map(&block)
68
68
  end
69
-
69
+
70
70
  def each_with_index(&block)
71
71
  all.each_with_index(&block)
72
72
  end
73
-
73
+
74
74
  def parent
75
75
  @parent ||= self.class.new(options.merge(:label => options[:label].parent)) unless options[:label].nil?
76
76
  end
77
-
77
+
78
78
  def children
79
79
  build_key.children.map { |key|
80
80
  self.class.new(options.merge(:label => key.label.to_s))
81
81
  }
82
82
  end
83
-
83
+
84
84
  def connection_ref(ref = nil)
85
85
  return options[:connection_ref] if ref.nil?
86
86
  reset! if options[:connection_ref] != ref
87
87
  options[:connection_ref] = ref
88
88
  self
89
89
  end
90
-
90
+
91
91
  def scope(input = nil)
92
92
  return options[:scope] if input.nil?
93
93
  reset! if !options[:scope].nil? && options[:scope].to_s != input.to_s
94
94
  options[:scope] = Scope.new(input)
95
95
  self
96
96
  end
97
-
97
+
98
98
  def label(input = nil)
99
99
  return options[:label] if input.nil?
100
100
  reset! if options.has_key?(:label) && options[:label].to_s != input.to_s
101
101
  options[:label] = (!input.nil?) ? Label.new(input) : nil
102
102
  self
103
103
  end
104
-
104
+
105
105
  def dates(start, finish)
106
106
  from(start).till(finish)
107
107
  end
108
108
  alias :date :dates
109
-
109
+
110
110
  def from(date = nil)
111
111
  return options[:from] if date.nil?
112
112
  reset! if options[:from] != date
113
113
  options[:from] = date
114
114
  self
115
115
  end
116
-
116
+
117
117
  def till(date = nil)
118
118
  return options[:till] if date.nil?
119
119
  reset! if options[:till] != date
@@ -121,21 +121,21 @@ module Redistat
121
121
  self
122
122
  end
123
123
  alias :until :till
124
-
124
+
125
125
  def depth(unit = nil)
126
126
  return options[:depth] if unit.nil?
127
127
  reset! if options[:depth] != unit
128
128
  options[:depth] = unit
129
129
  self
130
130
  end
131
-
131
+
132
132
  def interval(unit = nil)
133
133
  return options[:interval] if unit.nil?
134
134
  reset! if options[:interval] != unit
135
135
  options[:interval] = unit
136
136
  self
137
137
  end
138
-
138
+
139
139
  def find(opts = {})
140
140
  set_options(opts)
141
141
  raise InvalidOptions.new if !valid_options?
@@ -145,9 +145,9 @@ module Redistat
145
145
  find_by_interval
146
146
  end
147
147
  end
148
-
148
+
149
149
  private
150
-
150
+
151
151
  def set_options(opts = {})
152
152
  opts = opts.clone
153
153
  opts.each do |key, value|
@@ -155,7 +155,7 @@ module Redistat
155
155
  end
156
156
  self.options.merge!(opts)
157
157
  end
158
-
158
+
159
159
  def find_by_interval
160
160
  raise InvalidOptions.new if !valid_options?
161
161
  key = build_key
@@ -174,7 +174,7 @@ module Redistat
174
174
  end
175
175
  col
176
176
  end
177
-
177
+
178
178
  def find_by_magic
179
179
  raise InvalidOptions.new if !valid_options?
180
180
  key = build_key
@@ -196,20 +196,20 @@ module Redistat
196
196
  @result = nil
197
197
  @parent = nil
198
198
  end
199
-
199
+
200
200
  def valid_options?
201
201
  return true if !options[:scope].blank? && !options[:label].blank? && !options[:from].blank? && !options[:till].blank?
202
202
  false
203
203
  end
204
-
204
+
205
205
  def build_date_sets
206
206
  Finder::DateSet.new(options[:from], options[:till], options[:depth], options[:interval])
207
207
  end
208
-
208
+
209
209
  def build_key
210
210
  Key.new(options[:scope], options[:label])
211
211
  end
212
-
212
+
213
213
  def summarize_add_keys(sets, key, sum)
214
214
  sets.each do |date|
215
215
  db.hgetall("#{key.prefix}#{date}").each do |k, v|
@@ -218,7 +218,7 @@ module Redistat
218
218
  end
219
219
  sum
220
220
  end
221
-
221
+
222
222
  def summarize_rem_keys(sets, key, sum)
223
223
  sets.each do |date|
224
224
  db.hgetall("#{key.prefix}#{date}").each do |k, v|
@@ -227,10 +227,10 @@ module Redistat
227
227
  end
228
228
  sum
229
229
  end
230
-
230
+
231
231
  def db
232
232
  super(options[:connection_ref])
233
233
  end
234
-
234
+
235
235
  end
236
- end
236
+ end
@@ -1,7 +1,7 @@
1
1
  module Redistat
2
2
  class Finder
3
3
  class DateSet < Array
4
-
4
+
5
5
  def initialize(start_date = nil, end_date = nil, depth = nil, interval = false)
6
6
  if !start_date.nil? && !end_date.nil?
7
7
  find_date_sets(start_date, end_date, depth, interval)
@@ -71,7 +71,7 @@ module Redistat
71
71
  end
72
72
  { :add => add, :rem => [] }
73
73
  elsif has_nunit
74
- { :add => [end_date.beginning_of(nunit).to_rs.to_s(nunit)],
74
+ { :add => [end_date.beginning_of(nunit).to_rs.to_s(nunit)],
75
75
  :rem => end_date.map_beginning_of_each(unit, :include_start => !lowest_depth).until(end_date.end_of(nunit)) { |t| t.to_rs.to_s(unit) } }
76
76
  else
77
77
  { :add => [], :rem => [] }
@@ -93,7 +93,7 @@ module Redistat
93
93
  { :add => [], :rem => [] }
94
94
  end
95
95
  end
96
-
96
+
97
97
  end
98
98
  end
99
- end
99
+ end
@@ -2,56 +2,56 @@ module Redistat
2
2
  class Key
3
3
  include Database
4
4
  include Options
5
-
5
+
6
6
  def default_options
7
7
  { :depth => :hour }
8
8
  end
9
-
9
+
10
10
  def initialize(scope, label_name = nil, time_stamp = nil, opts = {})
11
11
  parse_options(opts)
12
12
  self.scope = scope
13
13
  self.label = label_name if !label_name.nil?
14
14
  self.date = time_stamp ||= Time.now
15
15
  end
16
-
16
+
17
17
  def prefix
18
18
  key = "#{@scope}"
19
19
  key << "/#{label.name}" if !label.nil?
20
20
  key << ":"
21
21
  key
22
22
  end
23
-
23
+
24
24
  def date=(input)
25
25
  @date = (input.instance_of?(Redistat::Date)) ? input : Date.new(input) # Redistat::Date, not ::Date
26
26
  end
27
27
  attr_reader :date
28
-
28
+
29
29
  def depth
30
30
  options[:depth]
31
31
  end
32
-
32
+
33
33
  # def scope
34
34
  # @scope.to_s
35
35
  # end
36
-
36
+
37
37
  def scope=(input)
38
38
  @scope = (input.instance_of?(Redistat::Scope)) ? input : Scope.new(input)
39
39
  end
40
40
  attr_reader :scope
41
-
41
+
42
42
  def label=(input)
43
43
  @label = (input.instance_of?(Redistat::Label)) ? input : Label.create(input, @options)
44
44
  end
45
45
  attr_reader :label
46
-
46
+
47
47
  def label_hash
48
48
  @label.hash
49
49
  end
50
-
50
+
51
51
  def parent
52
52
  @parent ||= self.class.new(self.scope, @label.parent, self.date, @options) unless @label.parent.nil?
53
53
  end
54
-
54
+
55
55
  def children
56
56
  members = db.smembers("#{scope}#{LABEL_INDEX}#{@label}") || [] # older versions of Redis returns nil
57
57
  members.map { |member|
@@ -59,26 +59,26 @@ module Redistat
59
59
  self.class.new(self.scope, child_label.join(GROUP_SEPARATOR), self.date, @options)
60
60
  }
61
61
  end
62
-
62
+
63
63
  def update_index
64
64
  @label.groups.each do |label|
65
65
  parent = (label.parent || "")
66
66
  db.sadd("#{scope}#{LABEL_INDEX}#{parent}", label.me)
67
67
  end
68
68
  end
69
-
69
+
70
70
  def groups
71
71
  @groups ||= @label.groups.map do |label|
72
72
  self.class.new(@scope, label, self.date, @options)
73
73
  end
74
74
  end
75
-
75
+
76
76
  def to_s(depth = nil)
77
77
  depth ||= @options[:depth]
78
78
  key = self.prefix
79
79
  key << @date.to_s(depth)
80
80
  key
81
81
  end
82
-
82
+
83
83
  end
84
- end
84
+ end
@@ -2,55 +2,55 @@ module Redistat
2
2
  class Label
3
3
  include Database
4
4
  include Options
5
-
5
+
6
6
  def default_options
7
7
  { :hashed_label => false }
8
8
  end
9
-
9
+
10
10
  def self.create(name, opts = {})
11
11
  self.new(name, opts).save
12
12
  end
13
-
13
+
14
14
  def self.join(*args)
15
15
  args = args.map {|i| i.to_s}
16
16
  self.new(args.reject {|i| i.blank? }.join(GROUP_SEPARATOR))
17
17
  end
18
-
18
+
19
19
  def initialize(str, opts = {})
20
20
  parse_options(opts)
21
21
  @raw = str.to_s
22
22
  end
23
-
23
+
24
24
  def to_s
25
25
  @raw
26
26
  end
27
-
27
+
28
28
  def name
29
29
  @options[:hashed_label] ? hash : self.to_s
30
30
  end
31
-
31
+
32
32
  def hash
33
33
  @hash ||= Digest::SHA1.hexdigest(self.to_s)
34
34
  end
35
-
35
+
36
36
  def save
37
37
  @saved = db.hset(KEY_LABELS, hash, self.to_s) if @options[:hashed_label]
38
38
  self
39
39
  end
40
-
40
+
41
41
  def saved?
42
42
  return true unless @options[:hashed_label]
43
43
  @saved ||= false
44
44
  end
45
-
45
+
46
46
  def parent
47
47
  @parent ||= groups[1] if groups.size > 1
48
48
  end
49
-
49
+
50
50
  def me
51
51
  self.to_s.split(GROUP_SEPARATOR).last
52
52
  end
53
-
53
+
54
54
  def groups
55
55
  return @groups unless @groups.nil?
56
56
  @groups = []
@@ -64,6 +64,6 @@ module Redistat
64
64
  end
65
65
  @groups.reverse!
66
66
  end
67
-
67
+
68
68
  end
69
- end
69
+ end
@@ -8,4 +8,4 @@ module Redistat
8
8
  Redistat.connection(ref)
9
9
  end
10
10
  end
11
- end
11
+ end