Repository: hydroshare/hydroshare

Issue:
Adding content to a collection
Is there anything that can be done to speed up "loading" when switching a collection resource to edit mode to add a resource.  I have a need to add content to https://www.hydroshare.org/resource/b6c4fcad40c64c4cb4dd7d4a25d0db6e/ but switched to edit mode and sat for about 15 min, during which time I wrote this issue and did many other things, then gave up.  Loading never completed, so I could never add what I wanted to.

![image](https://user-images.githubusercontent.com/3456833/228696299-fd3e9a58-813e-402b-8e28-872e98572557.png)




Discussion:
@dtarb I just gave it a try and the delay, if existent, was imperceptible:

https://user-images.githubusercontent.com/17934193/228847439-65076f9b-d669-4132-882f-80fd85cd4999.mov

if you try again, can you do so with your browser console open and see if there are any errors etc when it stalls "loading"

I get
![image](https://user-images.githubusercontent.com/3456833/228861839-9f686fe8-c563-41a8-a951-7b66ae6fecf0.png)

The red error at the bottom took about a minute to appear.
Thanks @dtarb 
POST request to
https://www.hydroshare.org/hsapi/_internal/b6c4fcad40c64c4cb4dd7d4a25d0db6e/get-collectable-resources/
times out for you...
I just tried in Chrome and Firefox, responses ~600ms
![image](https://user-images.githubusercontent.com/17934193/228864272-4b60417d-cf1d-4715-b6c6-ec2835ebc683.png)

First thing I would try is clearing your browser cache/session and trying again. What browser are you using? Thanks...
I am using Chrome.  I cleared all cache within the last week and this was the first time I tried to do this since then.  I can try again, and try another browser but I am pretty sure I'll get the same problem.  My sense is that the front end is trying to load too many resources for me to choose to load and that is holding things up.  It may be because I have more resources than you, but more broadly resource selection is a challenge.
Thanks @dtarb that makes sense it could be a query that blows up with increased # of resources
https://github.com/hydroshare/hydroshare/blob/2.4.1/hs_collection_resource/utils.py#L140
Just tried on Edge after complete cache clean out and reset and received the following errors
![image](https://user-images.githubusercontent.com/3456833/229553681-5a01db4b-6a52-4da0-ae34-cf27b38f0009.png)


Thanks @dtarb you're spot-on, I did a rough timing test this morning and this issue does seem to scale with # of resources.
Taking a closer look here, I see that Pabitra [did some work as recently as May 2022](https://github.com/hydroshare/hydroshare/issues/3554) on the `get_collectable_resources` function. @pkdash, the queries here (https://github.com/hydroshare/hydroshare/blob/2.4/hs_collection_resource/utils.py#L131) look fairly optimized to me--do you see room for further improvement or do you think we need to revise the whole workflow?

Maybe there is room for improvement in the template?
https://github.com/hydroshare/hydroshare/blob/2.4.2/hs_collection_resource/templates/pages/collectable_resources_modal.html#L23
Verified we have a linear complexity problem in the template
this line `collectable_resources_modal_html = render_to_string(template_name, context, request)` will result in large queries for users who have lots of resources
I will fix that

Resolving patch:
diff --git a/hs_collection_resource/views.py b/hs_collection_resource/views.py
--- a/hs_collection_resource/views.py
+++ b/hs_collection_resource/views.py
@@ -10,6 +10,7 @@
 from hs_core.hydroshare.utils import get_resource_by_shortkey, resource_modified, set_dirty_bag_flag
 from hs_core.views.utils import ACTION_TO_AUTHORIZE, authorize
 from .utils import add_or_remove_relation_metadata, get_collectable_resources
+from hs_access_control.models.privilege import UserResourcePrivilege, PrivilegeCodes
 
 logger = logging.getLogger(__name__)
 UI_DATETIME_FORMAT = "%m/%d/%Y"
@@ -31,7 +32,15 @@ def get_collectable_resources_modal(request, shortkey, *args, **kwargs):
             raise Exception(f"Collection {shortkey} is a published collection and can't be changed")
 
         collectable_resources = get_collectable_resources(user, collection_res)
-        context = {'collectable_resources': collectable_resources}
+
+        # fetch the owners of the resources rather than query for each resource
+        collectable_resource_ids = [r.short_id for r in collectable_resources]
+        urp_qs = UserResourcePrivilege.objects \
+            .filter(resource__short_id__in=collectable_resource_ids, privilege=PrivilegeCodes.OWNER)\
+            .select_related("user", "resource") \
+            .distinct()
+
+        context = {'user_resource_privileges': urp_qs}
         template_name = 'pages/collectable_resources_modal.html'
         collectable_resources_modal_html = render_to_string(template_name, context, request)
     except Exception as ex:


Test patch: