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