1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
8 class Arvados::V1::CollectionsController < ApplicationController
10 include TrashableController
12 def self._index_requires_parameters
16 type: 'boolean', required: false, description: "Include collections whose is_trashed attribute is true."
18 include_old_versions: {
19 type: 'boolean', required: false, description: "Include past collection versions."
24 def self._show_requires_parameters
28 type: 'boolean', required: false, description: "Show collection even if its is_trashed attribute is true."
30 include_old_versions: {
31 type: 'boolean', required: false, description: "Include past collection versions."
37 if resource_attrs[:uuid] and (loc = Keep::Locator.parse(resource_attrs[:uuid]))
38 resource_attrs[:portable_data_hash] = loc.to_s
39 resource_attrs.delete :uuid
41 resource_attrs.delete :version
42 resource_attrs.delete :current_version_uuid
46 def find_objects_for_index
48 if params[:include_trash] || ['destroy', 'trash', 'untrash'].include?(action_name)
49 opts.update({include_trash: true})
51 if params[:include_old_versions] || @include_old_versions
52 opts.update({include_old_versions: true})
54 @objects = Collection.readable_by(*@read_users, opts) if !opts.empty?
58 def find_object_by_uuid
59 @include_old_versions = true
61 if loc = Keep::Locator.parse(params[:id])
64 # It matters which Collection object we pick because we use it to get signed_manifest_text,
65 # the value of which is affected by the value of trash_at.
67 # From postgres doc: "By default, null values sort as if larger than any non-null
68 # value; that is, NULLS FIRST is the default for DESC order, and
69 # NULLS LAST otherwise."
71 # "trash_at desc" sorts null first, then latest to earliest, so
72 # it will select the Collection object with the longest
75 if c = Collection.readable_by(*@read_users).where({ portable_data_hash: loc.to_s }).order("trash_at desc").limit(1).first
77 uuid: c.portable_data_hash,
78 portable_data_hash: c.portable_data_hash,
79 manifest_text: c.signed_manifest_text,
89 if @object.is_a? Collection
90 # Omit unsigned_manifest_text
91 @select ||= model_class.selectable_attributes - ["unsigned_manifest_text"]
99 def find_collections(visited, sp, ignore_columns=[], &b)
102 sp.class.columns.each do |c|
103 find_collections(visited, sp[c.name.to_sym], &b) if !ignore_columns.include?(c.name)
107 find_collections(visited, v, &b)
111 find_collections(visited, v, &b)
114 if m = /[a-f0-9]{32}\+\d+/.match(sp)
116 elsif m = Collection.uuid_regex.match(sp)
122 def search_edges(visited, uuid, direction)
123 if uuid.nil? or uuid.empty? or visited[uuid]
127 if loc = Keep::Locator.parse(uuid)
129 return if visited[loc.to_s]
133 # uuid is a portable_data_hash
134 collections = Collection.readable_by(*@read_users).where(portable_data_hash: loc.to_s)
135 c = collections.limit(2).all
137 visited[loc.to_s] = c[0]
139 name = collections.limit(1).where("name <> ''").first
141 visited[loc.to_s] = {
142 portable_data_hash: c[0].portable_data_hash,
143 name: "#{name.name} + #{collections.count-1} more"
146 visited[loc.to_s] = {
147 portable_data_hash: c[0].portable_data_hash,
153 if direction == :search_up
154 # Search upstream for jobs where this locator is the output of some job
155 if !Rails.configuration.API.DisabledAPIs["jobs.list"]
156 Job.readable_by(*@read_users).where(output: loc.to_s).each do |job|
157 search_edges(visited, job.uuid, :search_up)
160 Job.readable_by(*@read_users).where(log: loc.to_s).each do |job|
161 search_edges(visited, job.uuid, :search_up)
165 Container.readable_by(*@read_users).where(output: loc.to_s).each do |c|
166 search_edges(visited, c.uuid, :search_up)
169 Container.readable_by(*@read_users).where(log: loc.to_s).each do |c|
170 search_edges(visited, c.uuid, :search_up)
172 elsif direction == :search_down
173 if loc.to_s == "d41d8cd98f00b204e9800998ecf8427e+0"
174 # Special case, don't follow the empty collection.
178 # Search downstream for jobs where this locator is in script_parameters
179 if !Rails.configuration.API.DisabledAPIs["jobs.list"]
180 Job.readable_by(*@read_users).where(["jobs.script_parameters like ?", "%#{loc.to_s}%"]).each do |job|
181 search_edges(visited, job.uuid, :search_down)
184 Job.readable_by(*@read_users).where(["jobs.docker_image_locator = ?", "#{loc.to_s}"]).each do |job|
185 search_edges(visited, job.uuid, :search_down)
189 Container.readable_by(*@read_users).where([Container.full_text_trgm + " like ?", "%#{loc.to_s}%"]).each do |c|
190 if c.output != loc.to_s && c.log != loc.to_s
191 search_edges(visited, c.uuid, :search_down)
196 # uuid is a regular Arvados UUID
197 rsc = ArvadosModel::resource_class_for_uuid uuid
199 Job.readable_by(*@read_users).where(uuid: uuid).each do |job|
200 visited[uuid] = job.as_api_response
201 if direction == :search_up
202 # Follow upstream collections referenced in the script parameters
203 find_collections(visited, job) do |hash, col_uuid|
204 search_edges(visited, hash, :search_up) if hash
205 search_edges(visited, col_uuid, :search_up) if col_uuid
207 elsif direction == :search_down
208 # Follow downstream job output
209 search_edges(visited, job.output, direction)
212 elsif rsc == Container
213 c = Container.readable_by(*@read_users).where(uuid: uuid).limit(1).first
215 visited[uuid] = c.as_api_response
216 if direction == :search_up
217 # Follow upstream collections referenced in the script parameters
218 find_collections(visited, c, ignore_columns=["log", "output"]) do |hash, col_uuid|
219 search_edges(visited, hash, :search_up) if hash
220 search_edges(visited, col_uuid, :search_up) if col_uuid
222 elsif direction == :search_down
223 # Follow downstream job output
224 search_edges(visited, c.output, :search_down)
227 elsif rsc == ContainerRequest
228 c = ContainerRequest.readable_by(*@read_users).where(uuid: uuid).limit(1).first
230 visited[uuid] = c.as_api_response
231 if direction == :search_up
232 # Follow upstream collections
233 find_collections(visited, c, ignore_columns=["log_uuid", "output_uuid"]) do |hash, col_uuid|
234 search_edges(visited, hash, :search_up) if hash
235 search_edges(visited, col_uuid, :search_up) if col_uuid
237 elsif direction == :search_down
238 # Follow downstream job output
239 search_edges(visited, c.output_uuid, :search_down)
242 elsif rsc == Collection
243 c = Collection.readable_by(*@read_users).where(uuid: uuid).limit(1).first
245 if direction == :search_up
246 visited[c.uuid] = c.as_api_response
248 if !Rails.configuration.API.DisabledAPIs["jobs.list"]
249 Job.readable_by(*@read_users).where(output: c.portable_data_hash).each do |job|
250 search_edges(visited, job.uuid, :search_up)
253 Job.readable_by(*@read_users).where(log: c.portable_data_hash).each do |job|
254 search_edges(visited, job.uuid, :search_up)
258 ContainerRequest.readable_by(*@read_users).where(output_uuid: uuid).each do |cr|
259 search_edges(visited, cr.uuid, :search_up)
262 ContainerRequest.readable_by(*@read_users).where(log_uuid: uuid).each do |cr|
263 search_edges(visited, cr.uuid, :search_up)
265 elsif direction == :search_down
266 search_edges(visited, c.portable_data_hash, :search_down)
270 rsc.where(uuid: uuid).each do |r|
271 visited[uuid] = r.as_api_response
276 if direction == :search_up
277 # Search for provenance links pointing to the current uuid
278 Link.readable_by(*@read_users).
279 where(head_uuid: uuid, link_class: "provenance").
281 visited[link.uuid] = link.as_api_response
282 search_edges(visited, link.tail_uuid, direction)
284 elsif direction == :search_down
285 # Search for provenance links emanating from the current uuid
286 Link.readable_by(current_user).
287 where(tail_uuid: uuid, link_class: "provenance").
289 visited[link.uuid] = link.as_api_response
290 search_edges(visited, link.head_uuid, direction)
298 search_edges(visited, @object[:uuid], :search_up)
300 search_edges(visited, @object[:portable_data_hash], :search_up)
308 search_edges(visited, @object[:uuid], :search_down)
310 search_edges(visited, @object[:portable_data_hash], :search_down)
317 def load_limit_offset_order_params *args
319 if action_name == 'index'
320 # Omit manifest_text and unsigned_manifest_text from index results unless expressly selected.
321 @select ||= model_class.selectable_attributes - ["manifest_text", "unsigned_manifest_text"]