2879: Improve sorting in Collection.uuids_for_docker_image.
authorBrett Smith <brett@curoverse.com>
Thu, 19 Jun 2014 14:29:01 +0000 (10:29 -0400)
committerBrett Smith <brett@curoverse.com>
Thu, 19 Jun 2014 14:29:01 +0000 (10:29 -0400)
This code is simpler, handles more error cases correctly, and returns
the most recent image at the head of the list as expected.

services/api/app/controllers/arvados/v1/jobs_controller.rb
services/api/app/models/collection.rb

index 5b1fc9aedaa193055bdbd84a3d9aaecf705d33a8..7ce5a62471c3ad527c6e9cf2cee6223aa1355b3b 100644 (file)
@@ -41,7 +41,7 @@ class Arvados::V1::JobsController < ApplicationController
         if image_search = resource_attrs[:runtime_constraints].andand["docker_image"]
           image_tag = resource_attrs[:runtime_constraints]["docker_image_tag"]
           image_locator = Collection.
-            uuids_for_docker_image(image_search, image_tag, @read_users).last
+            uuids_for_docker_image(image_search, image_tag, @read_users).first
           return super if image_locator.nil?  # We won't find anything to reuse.
           @filters.append(["docker_image_locator", "=", image_locator])
         else
index 2d573e536f75aa5bfa4424bf81f7d6cd791d972f..24e4ccc0ab744e32dcf1027c171251b7ae6455a7 100644 (file)
@@ -179,16 +179,13 @@ class Collection < ArvadosModel
     # Generate an order key for each result.  We want to order the results
     # so that anything with an image timestamp is considered more recent than
     # anything without; then we use the link's created_at as a tiebreaker.
-    results = {}
+    uuid_timestamps = {}
     matches.find_each do |link|
-      sort_key = []
-      if timestamp = link.properties["image_timestamp"]
-        sort_key.push("Z", timestamp.to_s)
-      end
-      sort_key.push("Y", link.created_at.to_s(:db))
-      results[link] = sort_key.join("")
+      uuid_timestamps[link.head_uuid] =
+        [(-link.properties["image_timestamp"].to_datetime.to_i rescue 0),
+         -link.created_at.to_i]
     end
-    results.keys.sort_by { |link| results[link] }.map { |link| link.head_uuid }
+    uuid_timestamps.keys.sort_by { |uuid| uuid_timestamps[uuid] }
   end
 
   def self.for_latest_docker_image(search_term, search_tag=nil, readers=nil)