10223: Merge branch 'master' into 10223-cr-set-output-name
[arvados.git] / services / api / app / controllers / arvados / v1 / collections_controller.rb
1 require "arvados/keep"
2
3 class Arvados::V1::CollectionsController < ApplicationController
4   include DbCurrentTime
5
6   def self.limit_index_columns_read
7     ["manifest_text"]
8   end
9
10   def create
11     if resource_attrs[:uuid] and (loc = Keep::Locator.parse(resource_attrs[:uuid]))
12       resource_attrs[:portable_data_hash] = loc.to_s
13       resource_attrs.delete :uuid
14     end
15     super
16   end
17
18   def find_objects_for_index
19     if params[:include_trash] || ['destroy', 'trash'].include?(action_name)
20       @objects = Collection.unscoped.readable_by(*@read_users)
21     end
22     super
23   end
24
25   def find_object_by_uuid
26     if loc = Keep::Locator.parse(params[:id])
27       loc.strip_hints!
28       if c = Collection.readable_by(*@read_users).where({ portable_data_hash: loc.to_s }).limit(1).first
29         @object = {
30           uuid: c.portable_data_hash,
31           portable_data_hash: c.portable_data_hash,
32           manifest_text: c.signed_manifest_text,
33         }
34       end
35       true
36     else
37       super
38     end
39   end
40
41   def show
42     if @object.is_a? Collection
43       super
44     else
45       send_json @object
46     end
47   end
48
49   def destroy
50     if !@object.is_trashed
51       @object.update_attributes!(trash_at: db_current_time)
52     end
53     earliest_delete = (@object.trash_at +
54                        Rails.configuration.blob_signature_ttl.seconds)
55     if @object.delete_at > earliest_delete
56       @object.update_attributes!(delete_at: earliest_delete)
57     end
58     show
59   end
60
61   def trash
62     if !@object.is_trashed
63       @object.update_attributes!(trash_at: db_current_time)
64     end
65     show
66   end
67
68   def find_collections(visited, sp, &b)
69     case sp
70     when ArvadosModel
71       sp.class.columns.each do |c|
72         find_collections(visited, sp[c.name.to_sym], &b) if c.name != "log"
73       end
74     when Hash
75       sp.each do |k, v|
76         find_collections(visited, v, &b)
77       end
78     when Array
79       sp.each do |v|
80         find_collections(visited, v, &b)
81       end
82     when String
83       if m = /[a-f0-9]{32}\+\d+/.match(sp)
84         yield m[0], nil
85       elsif m = Collection.uuid_regex.match(sp)
86         yield nil, m[0]
87       end
88     end
89   end
90
91   def search_edges(visited, uuid, direction)
92     if uuid.nil? or uuid.empty? or visited[uuid]
93       return
94     end
95
96     if loc = Keep::Locator.parse(uuid)
97       loc.strip_hints!
98       return if visited[loc.to_s]
99     end
100
101     logger.debug "visiting #{uuid}"
102
103     if loc
104       # uuid is a portable_data_hash
105       collections = Collection.readable_by(*@read_users).where(portable_data_hash: loc.to_s)
106       c = collections.limit(2).all
107       if c.size == 1
108         visited[loc.to_s] = c[0]
109       elsif c.size > 1
110         name = collections.limit(1).where("name <> ''").first
111         if name
112           visited[loc.to_s] = {
113             portable_data_hash: c[0].portable_data_hash,
114             name: "#{name.name} + #{collections.count-1} more"
115           }
116         else
117           visited[loc.to_s] = {
118             portable_data_hash: c[0].portable_data_hash,
119             name: loc.to_s
120           }
121         end
122       end
123
124       if direction == :search_up
125         # Search upstream for jobs where this locator is the output of some job
126         Job.readable_by(*@read_users).where(output: loc.to_s).each do |job|
127           search_edges(visited, job.uuid, :search_up)
128         end
129
130         Job.readable_by(*@read_users).where(log: loc.to_s).each do |job|
131           search_edges(visited, job.uuid, :search_up)
132         end
133       elsif direction == :search_down
134         if loc.to_s == "d41d8cd98f00b204e9800998ecf8427e+0"
135           # Special case, don't follow the empty collection.
136           return
137         end
138
139         # Search downstream for jobs where this locator is in script_parameters
140         Job.readable_by(*@read_users).where(["jobs.script_parameters like ?", "%#{loc.to_s}%"]).each do |job|
141           search_edges(visited, job.uuid, :search_down)
142         end
143
144         Job.readable_by(*@read_users).where(["jobs.docker_image_locator = ?", "#{loc.to_s}"]).each do |job|
145           search_edges(visited, job.uuid, :search_down)
146         end
147       end
148     else
149       # uuid is a regular Arvados UUID
150       rsc = ArvadosModel::resource_class_for_uuid uuid
151       if rsc == Job
152         Job.readable_by(*@read_users).where(uuid: uuid).each do |job|
153           visited[uuid] = job.as_api_response
154           if direction == :search_up
155             # Follow upstream collections referenced in the script parameters
156             find_collections(visited, job) do |hash, col_uuid|
157               search_edges(visited, hash, :search_up) if hash
158               search_edges(visited, col_uuid, :search_up) if col_uuid
159             end
160           elsif direction == :search_down
161             # Follow downstream job output
162             search_edges(visited, job.output, direction)
163           end
164         end
165       elsif rsc == Collection
166         if c = Collection.readable_by(*@read_users).where(uuid: uuid).limit(1).first
167           search_edges(visited, c.portable_data_hash, direction)
168           visited[c.portable_data_hash] = c.as_api_response
169         end
170       elsif rsc != nil
171         rsc.where(uuid: uuid).each do |r|
172           visited[uuid] = r.as_api_response
173         end
174       end
175     end
176
177     if direction == :search_up
178       # Search for provenance links pointing to the current uuid
179       Link.readable_by(*@read_users).
180         where(head_uuid: uuid, link_class: "provenance").
181         each do |link|
182         visited[link.uuid] = link.as_api_response
183         search_edges(visited, link.tail_uuid, direction)
184       end
185     elsif direction == :search_down
186       # Search for provenance links emanating from the current uuid
187       Link.readable_by(current_user).
188         where(tail_uuid: uuid, link_class: "provenance").
189         each do |link|
190         visited[link.uuid] = link.as_api_response
191         search_edges(visited, link.head_uuid, direction)
192       end
193     end
194   end
195
196   def provenance
197     visited = {}
198     search_edges(visited, @object[:portable_data_hash], :search_up)
199     search_edges(visited, @object[:uuid], :search_up)
200     send_json visited
201   end
202
203   def used_by
204     visited = {}
205     search_edges(visited, @object[:uuid], :search_down)
206     search_edges(visited, @object[:portable_data_hash], :search_down)
207     send_json visited
208   end
209
210   protected
211
212   def load_limit_offset_order_params *args
213     super
214     if action_name == 'index'
215       # Omit manifest_text from index results unless expressly selected.
216       @select ||= model_class.selectable_attributes - ["manifest_text"]
217     end
218   end
219 end