3 class Arvados::V1::CollectionsController < ApplicationController
6 def self._index_requires_parameters
10 type: 'boolean', required: false, description: "Include collections whose is_trashed attribute is true."
17 if resource_attrs[:uuid] and (loc = Keep::Locator.parse(resource_attrs[:uuid]))
18 resource_attrs[:portable_data_hash] = loc.to_s
19 resource_attrs.delete :uuid
24 def find_objects_for_index
25 if params[:include_trash] || ['destroy', 'trash', 'untrash'].include?(action_name)
26 @objects = Collection.unscoped.readable_by(*@read_users)
31 def find_object_by_uuid
32 if loc = Keep::Locator.parse(params[:id])
34 if c = Collection.readable_by(*@read_users).where({ portable_data_hash: loc.to_s }).limit(1).first
36 uuid: c.portable_data_hash,
37 portable_data_hash: c.portable_data_hash,
38 manifest_text: c.signed_manifest_text,
48 if @object.is_a? Collection
49 # Omit unsigned_manifest_text
50 @select ||= model_class.selectable_attributes - ["unsigned_manifest_text"]
58 if !@object.is_trashed
59 @object.update_attributes!(trash_at: db_current_time)
61 earliest_delete = (@object.trash_at +
62 Rails.configuration.blob_signature_ttl.seconds)
63 if @object.delete_at > earliest_delete
64 @object.update_attributes!(delete_at: earliest_delete)
70 if !@object.is_trashed
71 @object.update_attributes!(trash_at: db_current_time)
78 @object.update_attributes!(trash_at: nil)
80 raise InvalidStateTransitionError
85 def find_collections(visited, sp, &b)
88 sp.class.columns.each do |c|
89 find_collections(visited, sp[c.name.to_sym], &b) if c.name != "log"
93 find_collections(visited, v, &b)
97 find_collections(visited, v, &b)
100 if m = /[a-f0-9]{32}\+\d+/.match(sp)
102 elsif m = Collection.uuid_regex.match(sp)
108 def search_edges(visited, uuid, direction)
109 if uuid.nil? or uuid.empty? or visited[uuid]
113 if loc = Keep::Locator.parse(uuid)
115 return if visited[loc.to_s]
118 logger.debug "visiting #{uuid}"
121 # uuid is a portable_data_hash
122 collections = Collection.readable_by(*@read_users).where(portable_data_hash: loc.to_s)
123 c = collections.limit(2).all
125 visited[loc.to_s] = c[0]
127 name = collections.limit(1).where("name <> ''").first
129 visited[loc.to_s] = {
130 portable_data_hash: c[0].portable_data_hash,
131 name: "#{name.name} + #{collections.count-1} more"
134 visited[loc.to_s] = {
135 portable_data_hash: c[0].portable_data_hash,
141 if direction == :search_up
142 # Search upstream for jobs where this locator is the output of some job
143 Job.readable_by(*@read_users).where(output: loc.to_s).each do |job|
144 search_edges(visited, job.uuid, :search_up)
147 Job.readable_by(*@read_users).where(log: loc.to_s).each do |job|
148 search_edges(visited, job.uuid, :search_up)
150 elsif direction == :search_down
151 if loc.to_s == "d41d8cd98f00b204e9800998ecf8427e+0"
152 # Special case, don't follow the empty collection.
156 # Search downstream for jobs where this locator is in script_parameters
157 Job.readable_by(*@read_users).where(["jobs.script_parameters like ?", "%#{loc.to_s}%"]).each do |job|
158 search_edges(visited, job.uuid, :search_down)
161 Job.readable_by(*@read_users).where(["jobs.docker_image_locator = ?", "#{loc.to_s}"]).each do |job|
162 search_edges(visited, job.uuid, :search_down)
166 # uuid is a regular Arvados UUID
167 rsc = ArvadosModel::resource_class_for_uuid uuid
169 Job.readable_by(*@read_users).where(uuid: uuid).each do |job|
170 visited[uuid] = job.as_api_response
171 if direction == :search_up
172 # Follow upstream collections referenced in the script parameters
173 find_collections(visited, job) do |hash, col_uuid|
174 search_edges(visited, hash, :search_up) if hash
175 search_edges(visited, col_uuid, :search_up) if col_uuid
177 elsif direction == :search_down
178 # Follow downstream job output
179 search_edges(visited, job.output, direction)
182 elsif rsc == Collection
183 if c = Collection.readable_by(*@read_users).where(uuid: uuid).limit(1).first
184 search_edges(visited, c.portable_data_hash, direction)
185 visited[c.portable_data_hash] = c.as_api_response
188 rsc.where(uuid: uuid).each do |r|
189 visited[uuid] = r.as_api_response
194 if direction == :search_up
195 # Search for provenance links pointing to the current uuid
196 Link.readable_by(*@read_users).
197 where(head_uuid: uuid, link_class: "provenance").
199 visited[link.uuid] = link.as_api_response
200 search_edges(visited, link.tail_uuid, direction)
202 elsif direction == :search_down
203 # Search for provenance links emanating from the current uuid
204 Link.readable_by(current_user).
205 where(tail_uuid: uuid, link_class: "provenance").
207 visited[link.uuid] = link.as_api_response
208 search_edges(visited, link.head_uuid, direction)
215 search_edges(visited, @object[:portable_data_hash], :search_up)
216 search_edges(visited, @object[:uuid], :search_up)
222 search_edges(visited, @object[:uuid], :search_down)
223 search_edges(visited, @object[:portable_data_hash], :search_down)
229 def load_limit_offset_order_params *args
231 if action_name == 'index'
232 # Omit manifest_text and unsigned_manifest_text from index results unless expressly selected.
233 @select ||= model_class.selectable_attributes - ["manifest_text", "unsigned_manifest_text"]