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, default: false, description: "Include collections whose is_trashed attribute is true.",
18 include_old_versions: {
19 type: 'boolean', required: false, default: false, description: "Include past collection versions.",
24 def self._show_requires_parameters
28 type: 'boolean', required: false, default: false, description: "Show collection even if its is_trashed attribute is true.",
30 include_old_versions: {
31 type: 'boolean', required: false, default: true, 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
47 # preserve_version should be disabled unless explicitly asked otherwise.
48 if !resource_attrs[:preserve_version]
49 resource_attrs[:preserve_version] = false
54 def find_objects_for_index
56 include_trash: params[:include_trash] || ['destroy', 'trash', 'untrash'].include?(action_name),
57 include_old_versions: params[:include_old_versions] || false,
59 @objects = Collection.readable_by(*@read_users, opts) if !opts.empty?
63 def find_object_by_uuid
64 if loc = Keep::Locator.parse(params[:id])
68 include_trash: params[:include_trash],
69 include_old_versions: params[:include_old_versions],
72 # It matters which Collection object we pick because blob
73 # signatures depend on the value of trash_at.
75 # From postgres doc: "By default, null values sort as if larger
76 # than any non-null value; that is, NULLS FIRST is the default
77 # for DESC order, and NULLS LAST otherwise."
79 # "trash_at desc" sorts null first, then latest to earliest, so
80 # it will select the Collection object with the longest
83 select_attrs = (@select || ["manifest_text"]) | ["portable_data_hash", "trash_at"]
85 readable_by(*@read_users, opts).
86 where({ portable_data_hash: loc.to_s }).
87 order("trash_at desc").
88 select(select_attrs.join(", ")).
92 uuid: c.portable_data_hash,
93 portable_data_hash: c.portable_data_hash,
96 if select_attrs.index("manifest_text")
97 @object[:manifest_text] = c.manifest_text
106 if @object.is_a? Collection
107 # Omit unsigned_manifest_text
108 @select ||= model_class.selectable_attributes - ["unsigned_manifest_text"]
116 def find_collections(visited, sp, ignore_columns=[], &b)
119 sp.class.columns.each do |c|
120 find_collections(visited, sp[c.name.to_sym], &b) if !ignore_columns.include?(c.name)
124 find_collections(visited, v, &b)
128 find_collections(visited, v, &b)
131 if m = /[a-f0-9]{32}\+\d+/.match(sp)
133 elsif m = Collection.uuid_regex.match(sp)
139 def search_edges(visited, uuid, direction)
140 if uuid.nil? or uuid.empty? or visited[uuid]
144 if loc = Keep::Locator.parse(uuid)
146 return if visited[loc.to_s]
150 # uuid is a portable_data_hash
151 collections = Collection.readable_by(*@read_users).where(portable_data_hash: loc.to_s)
152 c = collections.limit(2).all
154 visited[loc.to_s] = c[0]
156 name = collections.limit(1).where("name <> ''").first
158 visited[loc.to_s] = {
159 portable_data_hash: c[0].portable_data_hash,
160 name: "#{name.name} + #{collections.count-1} more"
163 visited[loc.to_s] = {
164 portable_data_hash: c[0].portable_data_hash,
170 if direction == :search_up
171 # Search upstream for jobs where this locator is the output of some job
172 if !Rails.configuration.API.DisabledAPIs["jobs.list"]
173 Job.readable_by(*@read_users).where(output: loc.to_s).each do |job|
174 search_edges(visited, job.uuid, :search_up)
177 Job.readable_by(*@read_users).where(log: loc.to_s).each do |job|
178 search_edges(visited, job.uuid, :search_up)
182 Container.readable_by(*@read_users).where(output: loc.to_s).each do |c|
183 search_edges(visited, c.uuid, :search_up)
186 Container.readable_by(*@read_users).where(log: loc.to_s).each do |c|
187 search_edges(visited, c.uuid, :search_up)
189 elsif direction == :search_down
190 if loc.to_s == "d41d8cd98f00b204e9800998ecf8427e+0"
191 # Special case, don't follow the empty collection.
195 # Search downstream for jobs where this locator is in script_parameters
196 if !Rails.configuration.API.DisabledAPIs["jobs.list"]
197 Job.readable_by(*@read_users).where(["jobs.script_parameters like ?", "%#{loc.to_s}%"]).each do |job|
198 search_edges(visited, job.uuid, :search_down)
201 Job.readable_by(*@read_users).where(["jobs.docker_image_locator = ?", "#{loc.to_s}"]).each do |job|
202 search_edges(visited, job.uuid, :search_down)
206 Container.readable_by(*@read_users).where([Container.full_text_trgm + " like ?", "%#{loc.to_s}%"]).each do |c|
207 if c.output != loc.to_s && c.log != loc.to_s
208 search_edges(visited, c.uuid, :search_down)
213 # uuid is a regular Arvados UUID
214 rsc = ArvadosModel::resource_class_for_uuid uuid
216 Job.readable_by(*@read_users).where(uuid: uuid).each do |job|
217 visited[uuid] = job.as_api_response
218 if direction == :search_up
219 # Follow upstream collections referenced in the script parameters
220 find_collections(visited, job) do |hash, col_uuid|
221 search_edges(visited, hash, :search_up) if hash
222 search_edges(visited, col_uuid, :search_up) if col_uuid
224 elsif direction == :search_down
225 # Follow downstream job output
226 search_edges(visited, job.output, direction)
229 elsif rsc == Container
230 c = Container.readable_by(*@read_users).where(uuid: uuid).limit(1).first
232 visited[uuid] = c.as_api_response
233 if direction == :search_up
234 # Follow upstream collections referenced in the script parameters
235 find_collections(visited, c, ignore_columns=["log", "output"]) do |hash, col_uuid|
236 search_edges(visited, hash, :search_up) if hash
237 search_edges(visited, col_uuid, :search_up) if col_uuid
239 elsif direction == :search_down
240 # Follow downstream job output
241 search_edges(visited, c.output, :search_down)
244 elsif rsc == ContainerRequest
245 c = ContainerRequest.readable_by(*@read_users).where(uuid: uuid).limit(1).first
247 visited[uuid] = c.as_api_response
248 if direction == :search_up
249 # Follow upstream collections
250 find_collections(visited, c, ignore_columns=["log_uuid", "output_uuid"]) do |hash, col_uuid|
251 search_edges(visited, hash, :search_up) if hash
252 search_edges(visited, col_uuid, :search_up) if col_uuid
254 elsif direction == :search_down
255 # Follow downstream job output
256 search_edges(visited, c.output_uuid, :search_down)
259 elsif rsc == Collection
260 c = Collection.readable_by(*@read_users).where(uuid: uuid).limit(1).first
262 if direction == :search_up
263 visited[c.uuid] = c.as_api_response
265 if !Rails.configuration.API.DisabledAPIs["jobs.list"]
266 Job.readable_by(*@read_users).where(output: c.portable_data_hash).each do |job|
267 search_edges(visited, job.uuid, :search_up)
270 Job.readable_by(*@read_users).where(log: c.portable_data_hash).each do |job|
271 search_edges(visited, job.uuid, :search_up)
275 ContainerRequest.readable_by(*@read_users).where(output_uuid: uuid).each do |cr|
276 search_edges(visited, cr.uuid, :search_up)
279 ContainerRequest.readable_by(*@read_users).where(log_uuid: uuid).each do |cr|
280 search_edges(visited, cr.uuid, :search_up)
282 elsif direction == :search_down
283 search_edges(visited, c.portable_data_hash, :search_down)
287 rsc.where(uuid: uuid).each do |r|
288 visited[uuid] = r.as_api_response
293 if direction == :search_up
294 # Search for provenance links pointing to the current uuid
295 Link.readable_by(*@read_users).
296 where(head_uuid: uuid, link_class: "provenance").
298 visited[link.uuid] = link.as_api_response
299 search_edges(visited, link.tail_uuid, direction)
301 elsif direction == :search_down
302 # Search for provenance links emanating from the current uuid
303 Link.readable_by(current_user).
304 where(tail_uuid: uuid, link_class: "provenance").
306 visited[link.uuid] = link.as_api_response
307 search_edges(visited, link.head_uuid, direction)
315 search_edges(visited, @object[:uuid], :search_up)
317 search_edges(visited, @object[:portable_data_hash], :search_up)
325 search_edges(visited, @object[:uuid], :search_down)
327 search_edges(visited, @object[:portable_data_hash], :search_down)
334 def load_select_param *args
336 if action_name == 'index'
337 # Omit manifest_text and unsigned_manifest_text from index results unless expressly selected.
338 @select ||= model_class.selectable_attributes - ["manifest_text", "unsigned_manifest_text"]