Skip to content

Commit

Permalink
can use a scope to search for end user ids within end user segments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Youch committed May 24, 2010
1 parent 3b8e6eb commit cfd3154
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
3 changes: 2 additions & 1 deletion app/models/end_user_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def get_end_user_data
self.end_user.name,
self.end_user.user_class.name,
self.end_user.source,
self.end_user.registered? ? 'registered' : nil
self.end_user.registered? ? 'registered' : nil,
self.end_user.tag_names.join(" ")
]
end

Expand Down
20 changes: 13 additions & 7 deletions app/models/user_segment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,26 +108,32 @@ def paginate(page,args = {})
}, items ]
end

def search(offset=0, args={})
def search(args={})
args = args.clone.symbolize_keys!

page_size = args.delete(:per_page).to_i
page_size = 20 if page_size <= 0
offset = args.delete(:offset).to_i
limit = args.delete(:limit).to_i
limit = 20 if limit <= 0

cache_offset = offset % UserSegmentCache::SIZE

if args[:scope].nil?
args[:scope] = EndUser.scoped(:conditions => args.delete(:conditions), :joins => args.delete(:joins))
args[:end_user_field] = :id
end

ids = []
((offset / UserSegmentCache::SIZE).to_i..self.user_segment_caches.length-1).each do |position|
cache = self.user_segment_caches.find_by_position(position)
cache_offset, cache_ids = cache.search(cache_offset, args.merge(:limit => page_size-ids.length))
cache_offset, cache_ids = cache.search(cache_offset, args.merge(:limit => limit-ids.length))
ids = ids + cache_ids
offset = UserSegmentCache::SIZE * position + cache_offset
cache_offset = 0
break if ids.length >= page_size
break if ids.length >= limit
end

args.delete(:conditions)
args.delete(:joins)
args.delete(:scope)
args.delete(:end_user_field)
users = EndUser.find(:all, args.merge(:conditions => {:id => ids}))
return [offset, users]
end
Expand Down
7 changes: 4 additions & 3 deletions app/models/user_segment_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ def search(offset=0, opts={})
num_chunks = (self.id_list.size / batch_size).to_i
num_chunks = num_chunks.succ if (self.id_list.length % batch_size) > 0

base_scope = EndUser.scoped(opts)
base_scope = opts.delete(:scope) || EndUser
end_user_field = opts.delete(:end_user_field) || :end_user_id

ids = []
((offset/batch_size).to_i..num_chunks-1).each do |chunk|
sub_list = self.id_list[offset..(offset+batch_size-1)]
scope = base_scope.scoped(:select => 'id', :conditions => {:id => sub_list})
users_by_id = scope.find(:all).index_by(&:id)
scope = base_scope.scoped(:select => end_user_field.to_s, :conditions => {end_user_field.to_sym => sub_list})
users_by_id = scope.find(:all).index_by(&end_user_field.to_sym)
ids = ids + sub_list.map { |id| users_by_id[id] ? id : nil }.compact unless users_by_id.empty?
offset = offset + batch_size
break if ids.length > limit
Expand Down

0 comments on commit cfd3154

Please sign in to comment.