1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class Arvados::V1::CollectionsController < ApplicationController
10 def self._index_requires_parameters
14 type: 'boolean', required: false, description: "Include collections whose is_trashed attribute is true."
21 if resource_attrs[:uuid] and (loc = Keep::Locator.parse(resource_attrs[:uuid]))
22 resource_attrs[:portable_data_hash] = loc.to_s
23 resource_attrs.delete :uuid
28 def find_objects_for_index
29 if params[:include_trash] || ['destroy', 'trash', 'untrash'].include?(action_name)
30 @objects = Collection.unscoped.readable_by(*@read_users)
35 def find_object_by_uuid
36 if loc = Keep::Locator.parse(params[:id])
38 if c = Collection.readable_by(*@read_users).where({ portable_data_hash: loc.to_s }).limit(1).first
40 uuid: c.portable_data_hash,
41 portable_data_hash: c.portable_data_hash,
42 manifest_text: c.signed_manifest_text,
52 if @object.is_a? Collection
53 # Omit unsigned_manifest_text
54 @select ||= model_class.selectable_attributes - ["unsigned_manifest_text"]
62 if !@object.is_trashed
63 @object.update_attributes!(trash_at: db_current_time)
65 earliest_delete = (@object.trash_at +
66 Rails.configuration.blob_signature_ttl.seconds)
67 if @object.delete_at > earliest_delete
68 @object.update_attributes!(delete_at: earliest_delete)
74 if !@object.is_trashed
75 @object.update_attributes!(trash_at: db_current_time)
82 @object.trash_at = nil
84 if params[:ensure_unique_name]
85 @object.save_with_unique_name!
90 raise InvalidStateTransitionError
95 def find_collections(visited, sp, &b)
98 sp.class.columns.each do |c|
99 find_collections(visited, sp[c.name.to_sym], &b) if c.name != "log"
103 find_collections(visited, v, &b)
107 find_collections(visited, v, &b)
110 if m = /[a-f0-9]{32}\+\d+/.match(sp)
112 elsif m = Collection.uuid_regex.match(sp)
118 def search_edges(visited, uuid, direction)
119 if uuid.nil? or uuid.empty? or visited[uuid]
123 if loc = Keep::Locator.parse(uuid)
125 return if visited[loc.to_s]
128 logger.debug "visiting #{uuid}"
131 # uuid is a portable_data_hash
132 collections = Collection.readable_by(*@read_users).where(portable_data_hash: loc.to_s)
133 c = collections.limit(2).all
135 visited[loc.to_s] = c[0]
137 name = collections.limit(1).where("name <> ''").first
139 visited[loc.to_s] = {
140 portable_data_hash: c[0].portable_data_hash,
141 name: "#{name.name} + #{collections.count-1} more"
144 visited[loc.to_s] = {
145 portable_data_hash: c[0].portable_data_hash,
151 if direction == :search_up
152 # Search upstream for jobs where this locator is the output of some job
153 Job.readable_by(*@read_users).where(output: loc.to_s).each do |job|
154 search_edges(visited, job.uuid, :search_up)
157 Job.readable_by(*@read_users).where(log: loc.to_s).each do |job|
158 search_edges(visited, job.uuid, :search_up)
160 elsif direction == :search_down
161 if loc.to_s == "d41d8cd98f00b204e9800998ecf8427e+0"
162 # Special case, don't follow the empty collection.
166 # Search downstream for jobs where this locator is in script_parameters
167 Job.readable_by(*@read_users).where(["jobs.script_parameters like ?", "%#{loc.to_s}%"]).each do |job|
168 search_edges(visited, job.uuid, :search_down)
171 Job.readable_by(*@read_users).where(["jobs.docker_image_locator = ?", "#{loc.to_s}"]).each do |job|
172 search_edges(visited, job.uuid, :search_down)
176 # uuid is a regular Arvados UUID
177 rsc = ArvadosModel::resource_class_for_uuid uuid
179 Job.readable_by(*@read_users).where(uuid: uuid).each do |job|
180 visited[uuid] = job.as_api_response
181 if direction == :search_up
182 # Follow upstream collections referenced in the script parameters
183 find_collections(visited, job) do |hash, col_uuid|
184 search_edges(visited, hash, :search_up) if hash
185 search_edges(visited, col_uuid, :search_up) if col_uuid
187 elsif direction == :search_down
188 # Follow downstream job output
189 search_edges(visited, job.output, direction)
192 elsif rsc == Collection
193 if c = Collection.readable_by(*@read_users).where(uuid: uuid).limit(1).first
194 search_edges(visited, c.portable_data_hash, direction)
195 visited[c.portable_data_hash] = c.as_api_response
198 rsc.where(uuid: uuid).each do |r|
199 visited[uuid] = r.as_api_response
204 if direction == :search_up
205 # Search for provenance links pointing to the current uuid
206 Link.readable_by(*@read_users).
207 where(head_uuid: uuid, link_class: "provenance").
209 visited[link.uuid] = link.as_api_response
210 search_edges(visited, link.tail_uuid, direction)
212 elsif direction == :search_down
213 # Search for provenance links emanating from the current uuid
214 Link.readable_by(current_user).
215 where(tail_uuid: uuid, link_class: "provenance").
217 visited[link.uuid] = link.as_api_response
218 search_edges(visited, link.head_uuid, direction)
225 search_edges(visited, @object[:portable_data_hash], :search_up)
226 search_edges(visited, @object[:uuid], :search_up)
232 search_edges(visited, @object[:uuid], :search_down)
233 search_edges(visited, @object[:portable_data_hash], :search_down)
239 def load_limit_offset_order_params *args
241 if action_name == 'index'
242 # Omit manifest_text and unsigned_manifest_text from index results unless expressly selected.
243 @select ||= model_class.selectable_attributes - ["manifest_text", "unsigned_manifest_text"]