Skip to content

Commit be3e577

Browse files
committed
sorting projects on the dashboard by title
Moving code to grab the list of projects to a service This should keep the tests from randomly failing
1 parent c82ca8c commit be3e577

6 files changed

Lines changed: 63 additions & 16 deletions

File tree

app/models/mediaflux/project_list_request.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
# frozen_string_literal: true
22
module Mediaflux
33
class ProjectListRequest < Request
4-
attr_reader :aql_query, :collection, :action, :deep_search, :iterator, :size
4+
attr_reader :aql_query, :action, :size
55

66
# Constructor
77
# @param session_token [String] the API token for the authenticated session
88
# @param aql_query [String] Optional AQL query string
9-
# @param collection [Integer] Optional collection id
109
# @param action [String] Optional, by default it uses get-name but it could also be get-meta to get all
1110
# the fields for the assets or `get-values` to get a limited list of fields.
12-
# @param deep_search [Bool] Optional, false by default. When true queries the collection and it subcollections.
13-
# @param iterator [Bool] Optional, true by default. When true returns an iterator. When false returns a list of results
14-
def initialize(session_token:, aql_query: nil, action: "get-meta", deep_search: true)
11+
def initialize(session_token:, aql_query: nil, action: "get-meta")
1512
super(session_token: session_token)
1613
@aql_query = aql_query
17-
@collection = collection
1814
@action = action
19-
@deep_search = deep_search
2015
# For now we hard-code the size to infinity since only Administrators will fetch a large
2116
# number of projects and they should get them all. At some point in the future we might
2217
# want to implement pagination for this list but not now..
@@ -56,6 +51,11 @@ def build_http_request_body(name:)
5651
xml.where aql_query if aql_query.present?
5752
xml.action action if action.present?
5853
xml.size size if size.present?
54+
# I tried sorting by path and that slowed down the query to unusable
55+
# We will need to be considerate of performance when sorting the results
56+
xml.sort do
57+
xml.key "name"
58+
end
5959
end
6060
end
6161
end

app/models/mediaflux/query_request.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ def build_http_request_body(name:)
4949
xml.action action if action.present?
5050
declare_get_values_fields(xml) if action == "get-values"
5151
xml.as "iterator" if iterator
52+
# # TODO I believe this is the sort needed for the file list
53+
# # I am thinking we should create a separate class, but I do not want to loose the syntax
54+
# xml.sort do
55+
# xml.key "@collection" do
56+
# xml.parent.set_attribute("order", "desc")
57+
# end
58+
# xml.key "name"
59+
# end
5260
end
5361
end
5462
end

app/models/project.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,7 @@ def self.users_projects(user)
108108

109109
# Returns the projects that the current user has access in Mediaflux given their credentials
110110
def self.all_projects(user, aql_query = "xpath(tigerdata:project/ProjectID) has value")
111-
request = Mediaflux::ProjectListRequest.new(session_token: user.mediaflux_session, aql_query:)
112-
request.resolve
113-
if request.error?
114-
Rails.logger.error("Error fetching project list for user #{user&.uid}: #{request.response_error[:message]}")
115-
Honeybadger.notify("Error fetching project list for user #{user&.uid}: #{request.response_error[:message]}")
116-
[]
117-
else
118-
request.results
119-
end
111+
ProjectList.new(user, aql_query).all_projects
120112
end
121113

122114
def created_by_user

app/services/project_list.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class ProjectList
2+
attr_reader :user, :project, :aql_query
3+
4+
def initialize(user, aql_query = "xpath(tigerdata:project/ProjectID) has value")
5+
@user = user
6+
@aql_query = aql_query
7+
end
8+
9+
def all_projects
10+
@all_projects ||= begin
11+
request = Mediaflux::ProjectListRequest.new(session_token: user.mediaflux_session, aql_query:)
12+
request.resolve
13+
if request.error?
14+
Rails.logger.error("Error fetching project list for user #{user&.uid}: #{request.response_error[:message]}")
15+
Honeybadger.notify("Error fetching project list for user #{user&.uid}: #{request.response_error[:message]}")
16+
[]
17+
else
18+
request.results.sort_by { |project| project[:title] }
19+
end
20+
end
21+
end
22+
end

spec/models/mediaflux/project_list_request_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,15 @@
1111
expect(results.count).to be 1
1212
expect(results[0][:project_directory]).to eq "tigerdata/RDSS/testing-project"
1313
end
14+
15+
it "orders the results by project name which is the last section of the path" do
16+
request = described_class.new(session_token: user.mediaflux_session, aql_query: "namespace>='/princeton/tigerdataNS/RDSSNS' and xpath(tigerdata:project/ProjectID) has value")
17+
results = request.results
18+
expect(results.count).to eq(4)
19+
expect(results.pluck(:project_directory)).to eq ["tigerdata/RDSS/Query/AProject",
20+
"tigerdata/RDSS/Query/BProject",
21+
"tigerdata/RDSS/Query/CProject",
22+
"tigerdata/RDSS/testing-project"]
23+
end
1424
end
1525
end

spec/services/project_list_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
require "rails_helper"
3+
4+
RSpec.describe ProjectList do
5+
let(:user) { FactoryBot.create(:user, mediaflux_session: SystemUser.mediaflux_session) }
6+
let(:project_list) { described_class.new(user, "namespace>='/princeton/tigerdataNS/RDSSNS' and xpath(tigerdata:project/ProjectID) has value") }
7+
8+
describe "#all_projects" do
9+
it "returns the projects that the user has access to in Mediaflux" do
10+
projects = project_list.all_projects
11+
expect(projects.count).to eq(4)
12+
expect(projects.pluck(:title)).to eq(["Project A", "Project B", "Project C", "Test Project"])
13+
end
14+
end
15+
end

0 commit comments

Comments
 (0)