X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/0f9bca457448372de1d15dcd9ed4548c324ff14f..8227b8a1be943fbbf3adb23a3d549dec28efbbba:/apps/workbench/app/models/arvados_resource_list.rb diff --git a/apps/workbench/app/models/arvados_resource_list.rb b/apps/workbench/app/models/arvados_resource_list.rb index 7a6a004991..75a9429a43 100644 --- a/apps/workbench/app/models/arvados_resource_list.rb +++ b/apps/workbench/app/models/arvados_resource_list.rb @@ -1,12 +1,29 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: AGPL-3.0 + class ArvadosResourceList include ArvadosApiClientHelper include Enumerable + attr_reader :resource_class + def initialize resource_class=nil @resource_class = resource_class @fetch_multiple_pages = true @arvados_api_token = Thread.current[:arvados_api_token] @reader_tokens = Thread.current[:reader_tokens] + @results = nil + @count = nil + @offset = 0 + @cond = nil + @eager = nil + @select = nil + @orderby_spec = nil + @filters = nil + @distinct = nil + @include_trash = nil + @limit = nil end def eager(bool=true) @@ -14,6 +31,21 @@ class ArvadosResourceList self end + def distinct(bool=true) + @distinct = bool + self + end + + def include_trash(option=nil) + @include_trash = option + self + end + + def recursive(option=nil) + @recursive = option + self + end + def limit(max_results) if not max_results.nil? and not max_results.is_a? Integer raise ArgumentError("argument to limit() must be an Integer or nil") @@ -69,7 +101,7 @@ class ArvadosResourceList end end end - @cond.keys.select { |x| x.match /_kind$/ }.each do |kind_key| + @cond.keys.select { |x| x.match(/_kind$/) }.each do |kind_key| if @cond[kind_key].is_a? Class @cond = @cond.merge({ kind_key => 'arvados#' + arvados_api_client.class_kind(@cond[kind_key]) }) end @@ -77,6 +109,13 @@ class ArvadosResourceList self end + # with_count sets the 'count' parameter to 'exact' or 'none' -- see + # https://doc.arvados.org/api/methods.html#index + def with_count(count_param='exact') + @count = count_param + self + end + def fetch_multiple_pages(f) @fetch_multiple_pages = f self @@ -97,7 +136,6 @@ class ArvadosResourceList @items_available = r.items_available if r.respond_to? :items_available @result_limit = r.limit if r.respond_to? :limit @result_offset = r.offset if r.respond_to? :offset - @result_links = r.links if r.respond_to? :links @results end @@ -107,13 +145,18 @@ class ArvadosResourceList def each(&block) if not @results.nil? - @results.each &block + @results.each(&block) else + results = [] self.each_page do |items| items.each do |i| + results << i block.call i end end + # Cache results only if all were retrieved (block didn't raise + # an exception). + @results = results end self end @@ -147,44 +190,23 @@ class ArvadosResourceList end def items_available + results @items_available end def result_limit + results @result_limit end def result_offset + results @result_offset end - def result_links - @result_links - end - - # Return links provided with API response that point to the - # specified object, and have the specified link_class. If link_class - # is false or omitted, return all links pointing to the specified - # object. + # Obsolete method retained during api transition. def links_for item_or_uuid, link_class=false - return [] if !result_links - unless @links_for_uuid - @links_for_uuid = {} - result_links.each do |link| - if link.respond_to? :head_uuid - @links_for_uuid[link.head_uuid] ||= [] - @links_for_uuid[link.head_uuid] << link - end - end - end - if item_or_uuid.respond_to? :uuid - uuid = item_or_uuid.uuid - else - uuid = item_or_uuid - end - (@links_for_uuid[uuid] || []).select do |link| - link_class == false or link.link_class == link_class - end + [] end protected @@ -193,15 +215,24 @@ class ArvadosResourceList api_params = { _method: 'GET' } + api_params[:count] = @count if @count api_params[:where] = @cond if @cond api_params[:eager] = '1' if @eager - api_params[:limit] = @limit if @limit api_params[:select] = @select if @select api_params[:order] = @orderby_spec if @orderby_spec api_params[:filters] = @filters if @filters + api_params[:distinct] = @distinct if @distinct + api_params[:include_trash] = @include_trash if @include_trash + api_params[:cluster_id] = Rails.configuration.ClusterID + if @fetch_multiple_pages + # Default limit to (effectively) api server's MAX_LIMIT + api_params[:limit] = 2**(0.size*8 - 1) - 1 + end item_count = 0 offset = @offset || 0 + @result_limit = nil + @result_offset = nil begin api_params[:offset] = offset @@ -212,18 +243,22 @@ class ArvadosResourceList reader_tokens: @reader_tokens) items = arvados_api_client.unpack_api_response res - break if items.nil? or not items.any? + @items_available = items.items_available if items.respond_to?(:items_available) + @result_limit = items.limit if (@fetch_multiple_pages == false) and items.respond_to?(:limit) + @result_offset = items.offset if (@fetch_multiple_pages == false) and items.respond_to?(:offset) - @items_available = items.items_available if items.respond_to? :items_available - @result_limit = items.limit if items.respond_to? :limit - @result_offset = items.offset if items.respond_to? :offset + break if items.nil? or not items.any? item_count += items.size - offset = @result_offset + items.size + if items.respond_to?(:offset) + offset = items.offset + items.size + else + offset = item_count + end yield items - break if @limit.is_a? Integer and item_count >= @limit + break if @limit and item_count >= @limit break if items.respond_to? :items_available and offset >= items.items_available end while @fetch_multiple_pages self