Skip to content

Commit

Permalink
[WIP] Adding sorting and ordering options to nearby people/nodes (#4597)
Browse files Browse the repository at this point in the history
* adding sorting and ordering options to nearby people/node

* codeclimate

* codeclimate again

* fixing tests

* fixing tests 2
  • Loading branch information
milaaraujo authored and jywarren committed Jan 11, 2019
1 parent b1c5744 commit 2edab84
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 19 deletions.
4 changes: 2 additions & 2 deletions app/api/srch/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class Search < Grape::API
nickname: 'search_tag_locations'

params do
use :geographical, :additional
use :geographical, :additional, :sorting, :ordering
end
get :taglocations do
search_request = SearchRequest.fromRequest(params)
Expand Down Expand Up @@ -259,7 +259,7 @@ class Search < Grape::API
is_array: false,
nickname: 'search_nearby_people'
params do
use :geographical, :sorting, :additional
use :geographical, :additional, :sorting, :ordering
end
get :nearbyPeople do
search_request = SearchRequest.fromRequest(params)
Expand Down
2 changes: 1 addition & 1 deletion app/api/srch/shared_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module SharedParams
end

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

params :sorting do
Expand Down
4 changes: 2 additions & 2 deletions app/services/execute_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def execute(type, search_criteria)
when :peoplelocations
sservice.people_locations(search_criteria.query, search_criteria.tag)
when :taglocations
sservice.tagNearbyNodes(search_criteria.coordinates, search_criteria.tag, search_criteria.limit)
sservice.tagNearbyNodes(search_criteria.coordinates, search_criteria.tag, search_criteria.sort_by, search_criteria.order_direction, search_criteria.limit)
when :nearbyPeople
sservice.tagNearbyPeople(search_criteria.coordinates, search_criteria.tag, search_criteria.sort_by, search_criteria.limit)
sservice.tagNearbyPeople(search_criteria.coordinates, search_criteria.tag, search_criteria.sort_by, search_criteria.order_direction, search_criteria.limit)
else
sresult = []
end
Expand Down
40 changes: 28 additions & 12 deletions app/services/search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def search_questions(input, limit = 25, order = :natural, type = :boolean)
end

# Search nearby nodes with respect to given latitude, longitute and tags
def tagNearbyNodes(coordinates, tag, limit = 10)
def tagNearbyNodes(coordinates, tag, sort_by = nil, order_direction = nil, limit = 10)
raise("Must contain all four coordinates") if coordinates["nwlat"].nil?
raise("Must contain all four coordinates") if coordinates["nwlng"].nil?
raise("Must contain all four coordinates") if coordinates["selat"].nil?
Expand Down Expand Up @@ -109,20 +109,27 @@ def tagNearbyNodes(coordinates, tag, limit = 10)
.where('node.nid IN (?)', nids)
.where('term_data.name LIKE ?', 'lon%')
.where('REPLACE(term_data.name, "lon:", "") BETWEEN ' + coordinates["nwlng"].to_s + ' AND ' + coordinates["selng"].to_s)
.order('node.nid DESC')
.limit(limit)

# selects the items whose node_tags don't have the location:blurred tag
items.select do |item|
item.node_tags.none? do |node_tag|
node_tag.name == "location:blurred"
end
end

# sort nodes by recent activities if the sort_by==recent
items = if sort_by == "recent"
items.order("changed #{order_direction}")
.limit(limit)
else
items.order("created #{order_direction}")
.limit(limit)
end
end

# Search nearby people with respect to given latitude, longitute and tags
# and package up as a DocResult
def tagNearbyPeople(coordinates, tag, sort_by, limit = 10)
def tagNearbyPeople(coordinates, tag, sort_by = nil, order_direction = nil, limit = 10)
raise("Must contain all four coordinates") if coordinates["nwlat"].nil?
raise("Must contain all four coordinates") if coordinates["nwlng"].nil?
raise("Must contain all four coordinates") if coordinates["selat"].nil?
Expand Down Expand Up @@ -161,14 +168,23 @@ def tagNearbyPeople(coordinates, tag, sort_by, limit = 10)
end

# sort users by their recent activities if the sort_by==recent
items = if sort_by == "recent"
items.joins(:revisions).where("node_revisions.status = 1")\
.order("node_revisions.timestamp DESC")
.distinct
else
items.order(id: :desc)
.limit(limit)
end
items =
if sort_by == "recent"
items.joins(:revisions).where("node_revisions.status = 1")\
.order("node_revisions.timestamp #{order_direction}")
.distinct
else if sort_by == "content"
ids = items.collect(&:id).uniq || []
User.select('`rusers`.*, count(`node`.uid) AS ord')
.joins(:node)
.where('rusers.id IN (?)', ids)
.group('`node`.`uid`')
.order("ord #{order_direction}")
else
items.order("created_at #{order_direction}")
.limit(limit)
end
end
end

# Returns the location of people with most recent contributions.
Expand Down
9 changes: 9 additions & 0 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ All the endpoints have the optional parameter `limit` (10 by default) where you

`tag=[string]`: the search can be refined by passing a tag field.

`order_direction=[string]`: It accepts `ASC` or `DESC` (the latter is the default).

`sort_by=[string]`: It accepts `recent`. It sorts the nodes by the most recent activity. If no value
provided, the results are then sorted by node creation (desc).

### NearbyPeople:

* **URL**: `https://publiclab.org/api/srch/nearbyPeople?nwlat=200.0&selat=0.0&nwlng=0.0&selng=200.0`
Expand All @@ -107,6 +112,10 @@ All the endpoints have the optional parameter `limit` (10 by default) where you

`tag=[string]`: the search can be refined by passing a tag field.

`order_direction=[string]`: It accepts `ASC` or `DESC` (the latter is the default).

`sort_by=[string]`: It accepts `recent` and `content`. Sort the profiles by the most recent activity or most nodes created. If no value provided, the results are then sorted by signup date (desc).

### PeopleLocations

* **URL**: `https://publiclab.org/api/srch/peoplelocations?query=10`
Expand Down
4 changes: 2 additions & 2 deletions test/functional/api/search_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ def app

json = JSON.parse(last_response.body)

assert_equal "/profile/steff3", json['items'][0]['doc_url']
assert_equal "/profile/steff2", json['items'][1]['doc_url']
assert_equal "/profile/steff2", json['items'][0]['doc_url']
assert_equal "/profile/steff3", json['items'][1]['doc_url']

assert matcher =~ json
end
Expand Down

0 comments on commit 2edab84

Please sign in to comment.