From 71c66323a0ac9ce0cacf3ed3585a07562553096d Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Thu, 19 Jun 2014 10:29:01 -0400 Subject: [PATCH] 2879: Improve sorting in Collection.uuids_for_docker_image. This code is simpler, handles more error cases correctly, and returns the most recent image at the head of the list as expected. --- .../app/controllers/arvados/v1/jobs_controller.rb | 2 +- services/api/app/models/collection.rb | 13 +++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/services/api/app/controllers/arvados/v1/jobs_controller.rb b/services/api/app/controllers/arvados/v1/jobs_controller.rb index 5b1fc9aeda..7ce5a62471 100644 --- a/services/api/app/controllers/arvados/v1/jobs_controller.rb +++ b/services/api/app/controllers/arvados/v1/jobs_controller.rb @@ -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 diff --git a/services/api/app/models/collection.rb b/services/api/app/models/collection.rb index 2d573e536f..24e4ccc0ab 100644 --- a/services/api/app/models/collection.rb +++ b/services/api/app/models/collection.rb @@ -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) -- 2.30.2