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