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(with_lock: false)
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 if with_lock && Rails.configuration.API.LockBeforeUpdate
89 readable_by(*@read_users, opts).
90 where({ portable_data_hash: loc.to_s }).
91 order("trash_at desc").
92 select(select_attrs.join(", ")).
96 uuid: c.portable_data_hash,
97 portable_data_hash: c.portable_data_hash,
100 if select_attrs.index("manifest_text")
101 @object[:manifest_text] = c.manifest_text
105 super(with_lock: with_lock)
110 if @object.is_a? Collection
111 # Omit unsigned_manifest_text
112 @select ||= model_class.selectable_attributes - ["unsigned_manifest_text"]
120 def find_collections(visited, sp, ignore_columns=[], &b)
123 sp.class.columns.each do |c|
124 find_collections(visited, sp[c.name.to_sym], &b) if !ignore_columns.include?(c.name)
128 find_collections(visited, v, &b)
132 find_collections(visited, v, &b)
135 if m = /[a-f0-9]{32}\+\d+/.match(sp)
137 elsif m = Collection.uuid_regex.match(sp)
143 def search_edges(visited, uuid, direction)
144 if uuid.nil? or uuid.empty? or visited[uuid]
148 if loc = Keep::Locator.parse(uuid)
150 return if visited[loc.to_s]
154 # uuid is a portable_data_hash
155 collections = Collection.readable_by(*@read_users).where(portable_data_hash: loc.to_s)
156 c = collections.limit(2).all
158 visited[loc.to_s] = c[0]
160 name = collections.limit(1).where("name <> ''").first
162 visited[loc.to_s] = {
163 portable_data_hash: c[0].portable_data_hash,
164 name: "#{name.name} + #{collections.count-1} more"
167 visited[loc.to_s] = {
168 portable_data_hash: c[0].portable_data_hash,
174 if direction == :search_up
175 # Search upstream for jobs where this locator is the output of some container
176 Container.readable_by(*@read_users).where(output: loc.to_s).pluck(:uuid).each do |c_uuid|
177 search_edges(visited, c_uuid, :search_up)
180 Container.readable_by(*@read_users).where(log: loc.to_s).pluck(:uuid).each do |c_uuid|
181 search_edges(visited, c_uuid, :search_up)
183 elsif direction == :search_down
184 if loc.to_s == "d41d8cd98f00b204e9800998ecf8427e+0"
185 # Special case, don't follow the empty collection.
189 # Search downstream for jobs where this locator is in mounts
190 Container.readable_by(*@read_users).where([Container.full_text_trgm + " like ?", "%#{loc.to_s}%"]).select("output, log, uuid").each do |c|
191 if c.output != loc.to_s && c.log != loc.to_s
192 search_edges(visited, c.uuid, :search_down)
197 # uuid is a regular Arvados UUID
198 rsc = ArvadosModel::resource_class_for_uuid uuid
200 c = Container.readable_by(*@read_users).where(uuid: uuid).limit(1).first
202 visited[uuid] = c.as_api_response
203 if direction == :search_up
204 # Follow upstream collections referenced in the script parameters
205 find_collections(visited, c, ignore_columns=["log", "output"]) do |hash, col_uuid|
206 search_edges(visited, hash, :search_up) if hash
207 search_edges(visited, col_uuid, :search_up) if col_uuid
209 elsif direction == :search_down
210 # Follow downstream job output
211 search_edges(visited, c.output, :search_down)
214 elsif rsc == ContainerRequest
215 c = ContainerRequest.readable_by(*@read_users).where(uuid: uuid).limit(1).first
217 visited[uuid] = c.as_api_response
218 if direction == :search_up
219 # Follow upstream collections
220 find_collections(visited, c, ignore_columns=["log_uuid", "output_uuid"]) 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, c.output_uuid, :search_down)
229 elsif rsc == Collection
230 c = Collection.readable_by(*@read_users).where(uuid: uuid).limit(1).first
232 if direction == :search_up
233 visited[c.uuid] = c.as_api_response
235 ContainerRequest.readable_by(*@read_users).where(output_uuid: uuid).pluck(:uuid).each do |cr_uuid|
236 search_edges(visited, cr_uuid, :search_up)
239 ContainerRequest.readable_by(*@read_users).where(log_uuid: uuid).pluck(:uuid).each do |cr_uuid|
240 search_edges(visited, cr_uuid, :search_up)
242 elsif direction == :search_down
243 search_edges(visited, c.portable_data_hash, :search_down)
247 rsc.where(uuid: uuid).each do |r|
248 visited[uuid] = r.as_api_response
253 if direction == :search_up
254 # Search for provenance links pointing to the current uuid
255 Link.readable_by(*@read_users).
256 where(head_uuid: uuid, link_class: "provenance").
258 visited[link.uuid] = link.as_api_response
259 search_edges(visited, link.tail_uuid, direction)
261 elsif direction == :search_down
262 # Search for provenance links emanating from the current uuid
263 Link.readable_by(current_user).
264 where(tail_uuid: uuid, link_class: "provenance").
266 visited[link.uuid] = link.as_api_response
267 search_edges(visited, link.head_uuid, direction)
275 search_edges(visited, @object[:uuid], :search_up)
277 search_edges(visited, @object[:portable_data_hash], :search_up)
285 search_edges(visited, @object[:uuid], :search_down)
287 search_edges(visited, @object[:portable_data_hash], :search_down)
294 def load_select_param *args
296 if action_name == 'index'
297 # Omit manifest_text and unsigned_manifest_text from index results unless expressly selected.
298 @select ||= model_class.selectable_attributes - ["manifest_text", "unsigned_manifest_text"]