all_seeing_eye 0.0.16 → 0.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.16
1
+ 0.0.17
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{all_seeing_eye}
8
- s.version = "0.0.16"
8
+ s.version = "0.0.17"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Josh Symonds"]
@@ -47,12 +47,12 @@ class AllSeeingEye::Model
47
47
  all
48
48
  end
49
49
 
50
- def self.count(options = {})
51
- options[:start] = options[:start].to_i if options[:start] && (options[:start].is_a?(Time) || options[:start].is_a?(DateTime) || options[:start].is_a?(Date))
52
- options[:stop] = options[:stop].to_i if options[:stop] && (options[:stop].is_a?(Time) || options[:stop].is_a?(DateTime) || options[:stop].is_a?(Date))
53
- options = {:start => '-inf', :stop => '+inf'}.merge(options)
54
-
55
- AllSeeingEye.redis.zrangebyscore("allseeingeye:#{self.model_name}:fields:created_at", options[:start], options[:stop], :with_scores => false).collect{|id| self.find(id)}.size
50
+ def self.count
51
+ AllSeeingEye.redis.scard("allseeingeye:#{self.model_name}:all").to_i
52
+ end
53
+
54
+ def self.count_by_field(field, options = {})
55
+ AllSeeingEye.redis.llen("allseeingeye:#{self.model_name}:fields:#{field}:#{options[:value]}").to_i
56
56
  end
57
57
 
58
58
  def self.find_by_field(field, options = {})
@@ -70,16 +70,21 @@ class AllSeeingEye::Model
70
70
  end
71
71
  end
72
72
 
73
- def self.count_by_field(field, options = {})
74
- options = {:start => '-inf', :stop => '+inf', :offset => nil, :limit => nil}.merge(options)
73
+ def self.list_by_field(field, options = {})
74
+ options = {:start => '-inf', :stop => '+inf', :page => nil}.merge(options)
75
+ limit = if options[:page]
76
+ [(options[:page] - 1) * 10, 10]
77
+ else
78
+ nil
79
+ end
75
80
 
76
81
  raw_list, needs_pruning = if options[:value]
77
- [AllSeeingEye.redis.zrangebyscore("allseeingeye:#{self.model_name}:fields:#{field}:#{options[:value]}:count", '-inf', '+inf', :with_scores => true), true]
82
+ [AllSeeingEye.redis.zrangebyscore("allseeingeye:#{self.model_name}:fields:#{field}:#{options[:value]}:count", '-inf', '+inf', :with_scores => true, :limit => limit), true]
78
83
  else
79
84
  if AllSeeingEye.redis.exists("allseeingeye:#{self.model_name}:fields:#{field}:count")
80
- [AllSeeingEye.redis.zrangebyscore("allseeingeye:#{self.model_name}:fields:#{field}:count", '-inf', '+inf', :with_scores => true), true]
85
+ [AllSeeingEye.redis.zrangebyscore("allseeingeye:#{self.model_name}:fields:#{field}:count", '-inf', '+inf', :with_scores => true, :limit => limit), true]
81
86
  else
82
- [AllSeeingEye.redis.zrangebyscore("allseeingeye:#{self.model_name}:fields:#{field}", options[:start], options[:stop], :with_scores => true), false]
87
+ [AllSeeingEye.redis.zrangebyscore("allseeingeye:#{self.model_name}:fields:#{field}", options[:start], options[:stop], :with_scores => true, :limit => limit), false]
83
88
  end
84
89
  end
85
90
 
@@ -111,13 +116,13 @@ class AllSeeingEye::Model
111
116
  start = Chronic.parse(query.split(' to ').first, :context => :past).to_i
112
117
  stop = Chronic.parse(query.split(' to ').last, :context => :past).to_i
113
118
  if options[:count]
114
- return self.count_by_field('created_at', :start => start, :stop => stop)
119
+ return self.list_by_field('created_at', :start => start, :stop => stop)
115
120
  else
116
121
  return AllSeeingEye.redis.zrangebyscore("allseeingeye:#{self.model_name}:fields:created_at", start, stop, :with_scores => false).collect{|id| self.find(id)}
117
122
  end
118
123
  elsif query =~ /^(#{self.field_names.join('|')}):(.*)$/
119
124
  if options[:count]
120
- self.count_by_field($1, :value => $2)
125
+ self.list_by_field($1, :value => $2)
121
126
  else
122
127
  self.find_by_field($1, :value => $2)
123
128
  end
@@ -1,12 +1,20 @@
1
1
  <%= partial(:left) %>
2
2
 
3
- <% paged_requests = @requests[((@page - 1) * 10)..(@page * 10)] %>
3
+ <%
4
+ if @count
5
+ count = @count
6
+ paged_requests = @requests
7
+ else
8
+ count = @requests.size
9
+ paged_requests = @requests[((@page - 1) * 10)..(@page * 10)]
10
+ end
11
+ %>
4
12
 
5
13
  <div id='right'>
6
14
  <% if @query %>
7
15
  <h1>Search &raquo; <%= humanize @query %> &raquo; <%= humanize @field %></h1>
8
16
  <% elsif @view %>
9
- <h1><a href='<%=u "/fields/<%= @field %>" %>'><%= humanize @field %></a> &raquo; <%= humanize @id %> &raquo; <%= humanize @view %></h1>
17
+ <h1><a href='<%=u "/fields/#{@field}" %>'><%= humanize @field %></a> &raquo; <%= humanize @id %> &raquo; <%= humanize @view %></h1>
10
18
  <% else %>
11
19
  <h1><%= humanize @field %></h1>
12
20
  <% end %>
@@ -18,7 +26,7 @@
18
26
 
19
27
  <div id='pager'>
20
28
  <% if @page != 1 %><a href='<%= u "#{current_page}?page=#{@page - 1}"%>' id='previous'>&laquo; previous</a><% end %>
21
- <% if @requests.count > @page * 10 %><a href='<%= u "#{current_page}?page=#{@page + 1}"%>' id='next'>next &raquo;</a><% end %>
29
+ <% if count > @page * 10 %><a href='<%= u "#{current_page}?page=#{@page + 1}"%>' id='next'>next &raquo;</a><% end %>
22
30
  </div>
23
31
 
24
32
  <script type='text/javascript'>
@@ -24,14 +24,14 @@ class AllSeeingEye
24
24
  end
25
25
 
26
26
  def url_path(*path_parts)
27
- path_parts = path_parts.collect{|p| p.gsub(path_prefix, '')}
27
+ path_parts = path_parts.collect{|p| p.to_s.gsub(path_prefix, '')}
28
28
  path = [ path_prefix, path_parts ].compact.join("/")
29
- ('/' + path).squeeze('/').squeeze(path_prefix + '/')
29
+ ('/' + path).squeeze('/')
30
30
  end
31
31
  alias_method :u, :url_path
32
32
 
33
33
  def path_prefix
34
- ENV['PATH_PREFIX']
34
+ ENV['PATH_PREFIX'] || ''
35
35
  end
36
36
 
37
37
  def class_if_current(path = '')
@@ -67,7 +67,6 @@ class AllSeeingEye
67
67
 
68
68
  def show(page, layout = true)
69
69
  response["Cache-Control"] = "max-age=0, private, must-revalidate"
70
- @page = params[:page].blank? ? 1 : params[:page].to_i if page == :field || page == 'field'
71
70
  erb page.to_sym, {:layout => layout}
72
71
  end
73
72
 
@@ -76,7 +75,7 @@ class AllSeeingEye
76
75
  end
77
76
 
78
77
  get '/total' do
79
- @counts = AllSeeingEye::Request.count_by_field('created_at', :start => (DateTime.now - 2.days).to_i, :stop => DateTime.now.to_i)
78
+ @counts = AllSeeingEye::Request.list_by_field('created_at', :start => (DateTime.now - 2.days).to_i, :stop => DateTime.now.to_i)
80
79
  show :total
81
80
  end
82
81
 
@@ -97,8 +96,10 @@ class AllSeeingEye
97
96
 
98
97
  AllSeeingEye::Request.field_keys.each do |field|
99
98
  get "/fields/#{field}" do
99
+ @page = params[:page].blank? ? 1 : params[:page].to_i
100
100
  @field = field
101
- @requests = AllSeeingEye::Request.count_by_field(@field)
101
+ @count = AllSeeingEye::Request.count
102
+ @requests = AllSeeingEye::Request.list_by_field(@field, :page => @page)
102
103
  show :field
103
104
  end
104
105
 
@@ -128,7 +129,7 @@ class AllSeeingEye
128
129
  get %r{/fields/#{field}/(.*)$} do
129
130
  @field = field
130
131
  @id = params[:captures].first
131
- @counts = AllSeeingEye::Request.count_by_field(@field, :value => @id)
132
+ @counts = AllSeeingEye::Request.list_by_field(@field, :value => @id)
132
133
  show :total
133
134
  end
134
135
  end
data/test/model_test.rb CHANGED
@@ -176,10 +176,6 @@ context 'class methods with setup' do
176
176
  end
177
177
 
178
178
  test 'count model objects' do
179
- assert_equal 2, AllSeeingEye::Model.count(:start => @time - 11 * 60, :stop => @time - 4 * 60)
180
- assert_equal 2, AllSeeingEye::Model.count(:start => @time + 2 * 60, :stop => @time + 2 * 60)
181
- assert_equal 4, AllSeeingEye::Model.count(:stop => @time)
182
- assert_equal 2, AllSeeingEye::Model.count(:start => @time)
183
179
  assert_equal 6, AllSeeingEye::Model.count
184
180
  end
185
181
 
@@ -192,7 +188,7 @@ context 'class methods with setup' do
192
188
  end
193
189
 
194
190
  test 'return count in a field' do
195
- counts = AllSeeingEye::Model.count_by_field('uri')
191
+ counts = AllSeeingEye::Model.list_by_field('uri')
196
192
 
197
193
  assert_equal '/test/', counts.first.first
198
194
  assert_equal 5, counts.first.last
@@ -247,7 +243,7 @@ context 'many objects' do
247
243
  end
248
244
 
249
245
  test 'get the created count' do
250
- counts = AllSeeingEye::Model.count_by_field('created_at')
246
+ counts = AllSeeingEye::Model.list_by_field('created_at')
251
247
 
252
248
  assert_equal counts, AllSeeingEye::Model.search('ten years ago to one year from now', :count => true)
253
249
  assert_equal (@obj999.created_at.to_i / 60) * 60, counts.first.first.to_i
@@ -256,10 +252,36 @@ context 'many objects' do
256
252
  end
257
253
 
258
254
  test 'get the created count for a value' do
259
- counts = AllSeeingEye::Model.count_by_field('ip', :value => '192.168.0.1')
255
+ counts = AllSeeingEye::Model.list_by_field('ip', :value => '192.168.0.1')
260
256
 
261
257
  assert_equal 1000, counts.inject(0) {|sum, k| sum + k.last}
262
258
  end
259
+
260
+ test 'get only the first page of the count' do
261
+ counts = AllSeeingEye::Model.list_by_field('created_at', :page => 1)
262
+
263
+ assert_equal 10, counts.size
264
+ assert_equal (@obj999.created_at.to_i / 60) * 60, counts.first.first.to_i
265
+ assert_equal (@obj990.created_at.to_i / 60) * 60, counts.last.first.to_i
266
+ end
267
+
268
+ test 'get only the second page of the count' do
269
+ counts = AllSeeingEye::Model.list_by_field('created_at', :page => 2)
270
+
271
+ assert_equal 10, counts.size
272
+ assert_equal (@obj989.created_at.to_i / 60) * 60, counts.first.first.to_i
273
+ assert_equal (@obj980.created_at.to_i / 60) * 60, counts.last.first.to_i
274
+ end
275
+
276
+ test 'get only the first page of the count with a value' do
277
+ counts = AllSeeingEye::Model.list_by_field('ip', :value => '192.168.0.1', :page => 1)
278
+
279
+ assert_equal 10, counts.size
280
+ end
281
+
282
+ test 'return the count for a field with a given value' do
283
+ assert_equal 1000, AllSeeingEye::Model.count_by_field('ip', :value => '192.168.0.1')
284
+ end
263
285
  end
264
286
 
265
287
  context 'instance methods' do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: all_seeing_eye
3
3
  version: !ruby/object:Gem::Version
4
- hash: 63
4
+ hash: 61
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 16
10
- version: 0.0.16
9
+ - 17
10
+ version: 0.0.17
11
11
  platform: ruby
12
12
  authors:
13
13
  - Josh Symonds