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