Skip to content

Commit

Permalink
Refactoring getRecentProfiles method
Browse files Browse the repository at this point in the history
  • Loading branch information
milaaraujo authored and Stefanni committed Jul 27, 2018
1 parent e54f2a9 commit 90603f5
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 33 deletions.
12 changes: 8 additions & 4 deletions app/api/srch/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class Search < Grape::API
Search.execute(:all, params)
end

# Request URL should be /api/srch/profiles?srchString=QRY[&order=RECENTDESC&seq=KEYCOUNT&showCount=NUM_ROWS&pageNum=PAGE_NUM]
# Request URL should be /api/srch/profiles?srchString=QRY[&order_by=recent&sort_direction=desc&seq=KEYCOUNT&showCount=NUM_ROWS&pageNum=PAGE_NUM]
# Basic implementation from classic plots2 SearchController
desc 'Perform a search of profiles', hidden: false,
is_array: false,
nickname: 'srchGetProfiles'

params do
use :common, :sortorder
use :common, :sorting, :ordering
end
get :profiles do
Search.execute(:profiles, params)
Expand Down Expand Up @@ -105,9 +105,13 @@ def self.execute(endpoint, params)
sresult = DocList.new
search_query = params[:srchString]
tag_query = params[:tagName]
order_query = params[:order]
order_query = params[:order_by]
sort_query = params[:sort_direction]
search_type = endpoint
search_criteria = SearchCriteria.new(search_query, tag_query, order_query)
search_criteria = SearchCriteria.new(search_query,
tag: tag_query,
order_by: order_query,
sort_direction: sort_query)

if search_criteria.valid?
sresult = ExecuteSearch.new.by(search_type, search_criteria)
Expand Down
8 changes: 6 additions & 2 deletions app/api/srch/shared_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ module SharedParams
optional :tagName, type: String, documentation: { example: 'awesome' }
end

params :sortorder do
optional :order, type: String, documentation: { example: 'recentdesc' }
params :ordering do
optional :order_direction, type: String, documentation: { example: 'desc' }
end

params :sorting do
optional :sort_by, type: String, documentation: { example: 'recent' }
end

params :commontypeahead do
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class User < ActiveRecord::Base
has_many :following_users, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower
has_many :likes
has_many :revisions, through: :node

validates_with UniqueUsernameValidator, on: :create
validates_format_of :username, with: /\A[A-Za-z\d_\-]+\z/
Expand Down
2 changes: 1 addition & 1 deletion app/services/execute_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def execute(type, search_criteria)
when :all
sresult = sservice.textSearch_all(search_criteria.query)
when :profiles
sresult = sservice.textSearch_profiles(search_criteria.query, search_criteria.order)
sresult = sservice.textSearch_profiles(search_criteria)
when :notes
sresult = sservice.textSearch_notes(search_criteria.query)
when :questions
Expand Down
23 changes: 20 additions & 3 deletions app/services/search_criteria.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
class SearchCriteria
attr_reader :query, :tag, :order
attr_reader :query, :tag, :order_by

def initialize(query, tag = nil, order = nil)
def initialize(query, tag: nil, order_by: nil, sort_direction: "DESC")
@query = query
@tag = tag
@order = order
@order_by = order_by
@sort_direction = sort_direction
end

def valid?
!query.nil? && query != 0
end

def sort_direction
sanitize_direction(@sort_direction)
end

private

def sanitize_direction(direction)
if direction.present?
direction = direction.upcase
options = ['DESC', 'ASC']
options.include?(direction) ? direction : "DESC"
else
"DESC"
end
end
end
65 changes: 45 additions & 20 deletions app/services/search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,31 @@ def textSearch_all(srchString)
# Search profiles for matching text and package up as a DocResult
# If order == "recentdesc", search for more recently updated profiles for matching name.
# Otherwise, show profiles without any ordering
def textSearch_profiles(srchString, order = nil)
sresult = DocList.new
def textSearch_profiles(search_criteria)
# filter profiles by username
# recent eh o sort
# order profiles in desc order
# search profiles whre username Like = 'data' order by Node.changed DESC

user_scope = SrchScope.find_users(search_criteria.query, limit = nil)\
.reorder('') # removing order by id from find_users()

if search_criteria.order_by == "recent"
user_scope = user_scope.joins(:revisions)\
.order("timestamp #{search_criteria.sort_direction}")\
.distinct\
# do we need unique/distincit on user id?
end

else
# TODO what to use as the ordering when order_by is not present? username?
users_user_scope = user_scope.order(id: :desc)
end

users =
if order == "recentdesc"
getRecentProfiles(srchString)
else
SrchScope.find_users(srchString, limit = nil)
end
result = user_scope.limit(10) # TODO verify limit

# User profiles
# TODO ...
sresult = DocList.new
users.each do |match|
doc = DocResult.fromSearch(0, 'user', '/profile/' + match.name, match.name, '', 0)
sresult.addDoc(doc)
Expand Down Expand Up @@ -177,7 +191,10 @@ def tagNearbyNodes(srchString, tagName)
# X = srchString
def recentPeople(srchString, tagName = nil)
sresult = DocList.new
users = getRecentProfilesTag(tagName)

users = getRecentProfiles
if tagName then users = filterUserTag(users, tagName) end

count = 0

users.each do |user|
Expand All @@ -196,31 +213,39 @@ def recentPeople(srchString, tagName = nil)
end

# Get users with matching name ordering by most recent activity
def getRecentProfiles(srchString)
def getRecentProfiles
sresult = DocList.new

nodes = Node.all.order("changed DESC").limit(100).distinct
users = []
nodes.each do |node|
next unless (node.author.status != 0) && (node.author.name.downcase.include? srchString.downcase)
next unless node.author.status != 0
users << node.author.user
end

users = users.uniq
end

def filterMatchingUserName(users, srchString)
usersMatchingName = []
users.each do |user|
next unless user.name.downcase.include? srchString.downcase
usersMatchingName << user
end

usersMatchingName
end

# If a tagName is provided,
# method returns users with the specific tag ordering by most recent activity.
# Otherwise, it returns users with most recent activity.
def getRecentProfilesTag(tagName = nil)
nodes = Node.all.order("changed DESC").limit(100).distinct
users = []

nodes.each do |node|
next unless node.author.status != 0 && ((tagName && node.author.user.has_tag(tagName)) || tagName.nil?)
users << node.author.user
def filterUserTag(users, tagName)
usersWithTag = []
users.each do |user|
next unless user.has_tag(tagName)
usersWithTag << user
end

users = users.uniq
usersWithTag
end
end
6 changes: 3 additions & 3 deletions app/services/srch_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ class SrchScope
def self.find_users(input, limit)
if ActiveRecord::Base.connection.adapter_name == 'Mysql2'
User.search(input)
.order('id DESC')
.where(status: 1)
.order('id DESC') # do we need this line?
.where('rusers.status = ?', 1)
.limit(limit)
else
User.order('id DESC')
.where('username LIKE ? AND status = 1', '%' + input + '%')
.where('username LIKE ? AND rusers.status = 1', '%' + input + '%')
.limit(limit)
end
end
Expand Down

0 comments on commit 90603f5

Please sign in to comment.