2871: add preload methods for collections and logs in application_controller. Use...
authorradhika <radhika@curoverse.com>
Wed, 4 Jun 2014 15:15:24 +0000 (11:15 -0400)
committerradhika <radhika@curoverse.com>
Wed, 4 Jun 2014 15:15:24 +0000 (11:15 -0400)
apps/workbench/app/controllers/application_controller.rb
apps/workbench/app/views/users/_tables.html.erb

index a559b01c7b4e1a532ce48b3d163cc71a59304eca..df41884bac3ec2f5fe07e5447d36068a05fc8055 100644 (file)
@@ -453,7 +453,7 @@ class ApplicationController < ActionController::Base
   helper_method :links_for_object
   def links_for_object object_or_uuid
     uuid = object_or_uuid.is_a?(String) ? object_or_uuid : object_or_uuid.uuid
-    preload_links_for_objects([uuid])
+    preload_links_for_objects([object_or_uuid])
     @all_links_for[uuid]
   end
 
@@ -479,19 +479,81 @@ class ApplicationController < ActionController::Base
   # this can be used to replace any uses of: "dataclass.limit(n)"
   helper_method :get_objects_of_type
   def get_objects_of_type dataclass, size
-    # if the objects_map has a value for this dataclass, and the size used
+    # if the objects_map_for has a value for this dataclass, and the size used
     # to retrieve those objects is greater than equal to size, return it
     size_key = "#{dataclass}_size"
-    if @objects_map && @objects_map[dataclass] && @objects_map[size_key] &&
-        (@objects_map[size_key] >= size)
-      return @objects_map[dataclass] 
+    if @objects_map_for && @objects_map_for[dataclass] && @objects_map_for[size_key] &&
+        (@objects_map_for[size_key] >= size)
+      return @objects_map_for[dataclass] 
     end
 
-    @objects_map = {}
-    @objects_map[dataclass] = dataclass.limit(size)
-    @objects_map[size_key] = size
+    @objects_map_for = {}
+    @objects_map_for[dataclass] = dataclass.limit(size)
+    @objects_map_for[size_key] = size
 
-    return @objects_map[dataclass]
+    return @objects_map_for[dataclass]
+  end
+
+  # helper method to get collection for the given uuid
+  helper_method :get_collection
+  def get_collection uuid
+    preload_collections([uuid])
+    (@all_collections_for[uuid] && @all_collections_for[uuid].first) ?
+          [@all_collections_for[uuid].first] : []
+  end
+
+  # helper method to preload collections for the given uuids
+  helper_method :preload_collections
+  def preload_collections uuids
+    @all_collections_for ||= {}
+    if not uuids.select { |x| @all_collections_for[x].nil? }.any?
+      # already preloaded for all of these uuids
+      return
+    end
+
+    uuids.each do |x|
+      @all_collections_for[x] = []
+    end
+
+    # TODO: make sure we get every page of results from API server
+    Collection.where(uuid: uuids).each do |collection|
+      @all_collections_for[collection.uuid] << collection
+    end
+  end
+
+  # helper method to get log collection for the given log
+  helper_method :get_log_collection
+  def get_log_collection log
+    fixup = /([a-f0-9]{32}\+\d+)(\+?.*)/.match(log)
+    uuid = fixup[1]
+    preload_log_collections([uuid])
+    (@all_log_collections_for[uuid] && @all_log_collections_for[uuid].first) ?
+          [@all_log_collections_for[uuid].first] : []
+  end
+
+  # helper method to preload collections for the given uuids
+  helper_method :preload_log_collections
+  def preload_log_collections logs
+    uuids = []
+    logs.each do |log|
+      fixup = /([a-f0-9]{32}\+\d+)(\+?.*)/.match(log)
+      uuids << fixup[1]
+    end
+
+    @all_log_collections_for ||= {}
+    if not uuids.select { |x| @all_log_collections_for[x].nil? }.any?
+      # already preloaded for all of these uuids
+      return
+    end
+
+    uuids.each do |x|
+      @all_log_collections_for[x] = []
+    end
+
+    # TODO: make sure we get every page of results from API server
+    Collection.limit(100).where(uuid: uuids).each do |collection|
+      @all_log_collections_for[collection.uuid] << collection
+    end
   end
 
 end
index 13cc673de75ad0c9b10bca171e2ada95ef51f9b2..c271f2b5e8d54d15183449e02700c315fba92385 100644 (file)
           <th>Progress</th>
         </tr>
 
+        <% 
+          job_outputs = []
+          job_logs = []
+          @my_jobs[0..6].each do |j|
+            job_outputs << j.output
+            job_logs << j.log
+          end
+
+          preload_collections job_outputs
+          preload_log_collections job_logs
+        %>
         <% @my_jobs[0..6].each do |j| %>
           <tr data-object-uuid="<%= j.uuid %>">
             <td>
             <td>
               <small>
                 <% if j.success and j.output %>
-
                   <a href="<%= collection_path(j.output) %>">
-                    <% Collection.limit(1).where(uuid: j.output).each do |c| %>
-                         <% c.files.each do |file| %>
-                      <%= file[0] == '.' ? file[1] : "#{file[0]}/#{file[1]}" %>
-                    <% end %>
-                <% end %>
-                </a>
-
-        <% end %>
-        </small>
-</td>
+                    <% get_collection(j.output).each do |c| %>
+                      <% c.files.each do |file| %>
+                        <%= file[0] == '.' ? file[1] : "#{file[0]}/#{file[1]}" %>
+                      <% end %>
+                     <% end %>
+                  </a>
+              <% end %>
+            </small>
+          </td>
 
 <td>
   <small>
     <% if j.log %>
-      <% fixup = /([a-f0-9]{32}\+\d+)(\+?.*)/.match(j.log)%>
-      <% Collection.limit(1).where(uuid: fixup[1]).each do |c| %>
+      <% get_log_collection(j.log).each do |c| %>
         <% c.files.each do |file| %>
           <a href="<%= collection_path(j.log) %>/<%= file[1] %>?disposition=inline&size=<%= file[2] %>">Log</a>
         <% end %>