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 we use it to get signed_manifest_text,
73 # the value of which is affected by the value of trash_at.
75 # From postgres doc: "By default, null values sort as if larger than any non-null
76 # value; that is, NULLS FIRST is the default for DESC order, and
77 # 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 if c = Collection.readable_by(*@read_users, opts).where({ portable_data_hash: loc.to_s }).order("trash_at desc").limit(1).first
85 uuid: c.portable_data_hash,
86 portable_data_hash: c.portable_data_hash,
87 manifest_text: c.signed_manifest_text,
96 if @object.is_a? Collection
97 # Omit unsigned_manifest_text
98 @select ||= model_class.selectable_attributes - ["unsigned_manifest_text"]
106 def find_collections(visited, sp, ignore_columns=[], &b)
109 sp.class.columns.each do |c|
110 find_collections(visited, sp[c.name.to_sym], &b) if !ignore_columns.include?(c.name)
114 find_collections(visited, v, &b)
118 find_collections(visited, v, &b)
121 if m = /[a-f0-9]{32}\+\d+/.match(sp)
123 elsif m = Collection.uuid_regex.match(sp)
129 def search_edges(visited, uuid, direction)
130 if uuid.nil? or uuid.empty? or visited[uuid]
134 if loc = Keep::Locator.parse(uuid)
136 return if visited[loc.to_s]
140 # uuid is a portable_data_hash
141 collections = Collection.readable_by(*@read_users).where(portable_data_hash: loc.to_s)
142 c = collections.limit(2).all
144 visited[loc.to_s] = c[0]
146 name = collections.limit(1).where("name <> ''").first
148 visited[loc.to_s] = {
149 portable_data_hash: c[0].portable_data_hash,
150 name: "#{name.name} + #{collections.count-1} more"
153 visited[loc.to_s] = {
154 portable_data_hash: c[0].portable_data_hash,
160 if direction == :search_up
161 # Search upstream for jobs where this locator is the output of some job
162 if !Rails.configuration.API.DisabledAPIs["jobs.list"]
163 Job.readable_by(*@read_users).where(output: loc.to_s).each do |job|
164 search_edges(visited, job.uuid, :search_up)
167 Job.readable_by(*@read_users).where(log: loc.to_s).each do |job|
168 search_edges(visited, job.uuid, :search_up)
172 Container.readable_by(*@read_users).where(output: loc.to_s).each do |c|
173 search_edges(visited, c.uuid, :search_up)
176 Container.readable_by(*@read_users).where(log: loc.to_s).each do |c|
177 search_edges(visited, c.uuid, :search_up)
179 elsif direction == :search_down
180 if loc.to_s == "d41d8cd98f00b204e9800998ecf8427e+0"
181 # Special case, don't follow the empty collection.
185 # Search downstream for jobs where this locator is in script_parameters
186 if !Rails.configuration.API.DisabledAPIs["jobs.list"]
187 Job.readable_by(*@read_users).where(["jobs.script_parameters like ?", "%#{loc.to_s}%"]).each do |job|
188 search_edges(visited, job.uuid, :search_down)
191 Job.readable_by(*@read_users).where(["jobs.docker_image_locator = ?", "#{loc.to_s}"]).each do |job|
192 search_edges(visited, job.uuid, :search_down)
196 Container.readable_by(*@read_users).where([Container.full_text_trgm + " like ?", "%#{loc.to_s}%"]).each do |c|
197 if c.output != loc.to_s && c.log != loc.to_s
198 search_edges(visited, c.uuid, :search_down)
203 # uuid is a regular Arvados UUID
204 rsc = ArvadosModel::resource_class_for_uuid uuid
206 Job.readable_by(*@read_users).where(uuid: uuid).each do |job|
207 visited[uuid] = job.as_api_response
208 if direction == :search_up
209 # Follow upstream collections referenced in the script parameters
210 find_collections(visited, job) do |hash, col_uuid|
211 search_edges(visited, hash, :search_up) if hash
212 search_edges(visited, col_uuid, :search_up) if col_uuid
214 elsif direction == :search_down
215 # Follow downstream job output
216 search_edges(visited, job.output, direction)
219 elsif rsc == Container
220 c = Container.readable_by(*@read_users).where(uuid: uuid).limit(1).first
222 visited[uuid] = c.as_api_response
223 if direction == :search_up
224 # Follow upstream collections referenced in the script parameters
225 find_collections(visited, c, ignore_columns=["log", "output"]) do |hash, col_uuid|
226 search_edges(visited, hash, :search_up) if hash
227 search_edges(visited, col_uuid, :search_up) if col_uuid
229 elsif direction == :search_down
230 # Follow downstream job output
231 search_edges(visited, c.output, :search_down)
234 elsif rsc == ContainerRequest
235 c = ContainerRequest.readable_by(*@read_users).where(uuid: uuid).limit(1).first
237 visited[uuid] = c.as_api_response
238 if direction == :search_up
239 # Follow upstream collections
240 find_collections(visited, c, ignore_columns=["log_uuid", "output_uuid"]) do |hash, col_uuid|
241 search_edges(visited, hash, :search_up) if hash
242 search_edges(visited, col_uuid, :search_up) if col_uuid
244 elsif direction == :search_down
245 # Follow downstream job output
246 search_edges(visited, c.output_uuid, :search_down)
249 elsif rsc == Collection
250 c = Collection.readable_by(*@read_users).where(uuid: uuid).limit(1).first
252 if direction == :search_up
253 visited[c.uuid] = c.as_api_response
255 if !Rails.configuration.API.DisabledAPIs["jobs.list"]
256 Job.readable_by(*@read_users).where(output: c.portable_data_hash).each do |job|
257 search_edges(visited, job.uuid, :search_up)
260 Job.readable_by(*@read_users).where(log: c.portable_data_hash).each do |job|
261 search_edges(visited, job.uuid, :search_up)
265 ContainerRequest.readable_by(*@read_users).where(output_uuid: uuid).each do |cr|
266 search_edges(visited, cr.uuid, :search_up)
269 ContainerRequest.readable_by(*@read_users).where(log_uuid: uuid).each do |cr|
270 search_edges(visited, cr.uuid, :search_up)
272 elsif direction == :search_down
273 search_edges(visited, c.portable_data_hash, :search_down)
277 rsc.where(uuid: uuid).each do |r|
278 visited[uuid] = r.as_api_response
283 if direction == :search_up
284 # Search for provenance links pointing to the current uuid
285 Link.readable_by(*@read_users).
286 where(head_uuid: uuid, link_class: "provenance").
288 visited[link.uuid] = link.as_api_response
289 search_edges(visited, link.tail_uuid, direction)
291 elsif direction == :search_down
292 # Search for provenance links emanating from the current uuid
293 Link.readable_by(current_user).
294 where(tail_uuid: uuid, link_class: "provenance").
296 visited[link.uuid] = link.as_api_response
297 search_edges(visited, link.head_uuid, direction)
305 search_edges(visited, @object[:uuid], :search_up)
307 search_edges(visited, @object[:portable_data_hash], :search_up)
315 search_edges(visited, @object[:uuid], :search_down)
317 search_edges(visited, @object[:portable_data_hash], :search_down)
324 def load_limit_offset_order_params *args
326 if action_name == 'index'
327 # Omit manifest_text and unsigned_manifest_text from index results unless expressly selected.
328 @select ||= model_class.selectable_attributes - ["manifest_text", "unsigned_manifest_text"]