8019: Rework partial line throttling. Fix sending flush when buffer is ready
[arvados.git] / services / api / app / controllers / arvados / v1 / schema_controller.rb
1 class Arvados::V1::SchemaController < ApplicationController
2   skip_before_filter :catch_redirect_hint
3   skip_before_filter :find_objects_for_index
4   skip_before_filter :find_object_by_uuid
5   skip_before_filter :load_filters_param
6   skip_before_filter :load_limit_offset_order_params
7   skip_before_filter :load_read_auths
8   skip_before_filter :load_where_param
9   skip_before_filter :render_404_if_no_object
10   skip_before_filter :require_auth_scope
11
12   include DbCurrentTime
13
14   def index
15     expires_in 24.hours, public: true
16     discovery = Rails.cache.fetch 'arvados_v1_rest_discovery' do
17       Rails.application.eager_load!
18       discovery = {
19         kind: "discovery#restDescription",
20         discoveryVersion: "v1",
21         id: "arvados:v1",
22         name: "arvados",
23         version: "v1",
24         revision: "20131114",
25         source_version: AppVersion.hash,
26         generatedAt: db_current_time.iso8601,
27         title: "Arvados API",
28         description: "The API to interact with Arvados.",
29         documentationLink: "http://doc.arvados.org/api/index.html",
30         defaultCollectionReplication: Rails.configuration.default_collection_replication,
31         protocol: "rest",
32         baseUrl: root_url + "arvados/v1/",
33         basePath: "/arvados/v1/",
34         rootUrl: root_url,
35         servicePath: "arvados/v1/",
36         batchPath: "batch",
37         defaultTrashLifetime: Rails.application.config.default_trash_lifetime,
38         blobSignatureTtl: Rails.application.config.blob_signature_ttl,
39         maxRequestSize: Rails.application.config.max_request_size,
40         dockerImageFormats: Rails.application.config.docker_image_formats,
41         crunchLogBytesPerEvent: Rails.application.config.crunch_log_bytes_per_event,
42         crunchLogSecondsBetweenEvents: Rails.application.config.crunch_log_seconds_between_events,
43         crunchLogThrottlePeriod: Rails.application.config.crunch_log_throttle_period,
44         crunchLogThrottleBytes: Rails.application.config.crunch_log_throttle_bytes,
45         crunchLogThrottleLines: Rails.application.config.crunch_log_throttle_lines,
46         crunchLimitLogBytesPerJob: Rails.application.config.crunch_limit_log_bytes_per_job,
47         crunchLogPartialLineThrottlePeriod: Rails.application.config.crunch_log_partial_line_throttle_period,
48         parameters: {
49           alt: {
50             type: "string",
51             description: "Data format for the response.",
52             default: "json",
53             enum: [
54                    "json"
55                   ],
56             enumDescriptions: [
57                                "Responses with Content-Type of application/json"
58                               ],
59             location: "query"
60           },
61           fields: {
62             type: "string",
63             description: "Selector specifying which fields to include in a partial response.",
64             location: "query"
65           },
66           key: {
67             type: "string",
68             description: "API key. Your API key identifies your project and provides you with API access, quota, and reports. Required unless you provide an OAuth 2.0 token.",
69             location: "query"
70           },
71           oauth_token: {
72             type: "string",
73             description: "OAuth 2.0 token for the current user.",
74             location: "query"
75           }
76         },
77         auth: {
78           oauth2: {
79             scopes: {
80               "https://api.curoverse.com/auth/arvados" => {
81                 description: "View and manage objects"
82               },
83               "https://api.curoverse.com/auth/arvados.readonly" => {
84                 description: "View objects"
85               }
86             }
87           }
88         },
89         schemas: {},
90         resources: {}
91       }
92
93       if Rails.application.config.websocket_address
94         discovery[:websocketUrl] = Rails.application.config.websocket_address
95       elsif ENV['ARVADOS_WEBSOCKETS']
96         discovery[:websocketUrl] = root_url.sub(/^http/, 'ws') + "websocket"
97       end
98
99       ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |k|
100         begin
101           ctl_class = "Arvados::V1::#{k.to_s.pluralize}Controller".constantize
102         rescue
103           # No controller -> no discovery.
104           next
105         end
106         object_properties = {}
107         k.columns.
108           select { |col| col.name != 'id' }.
109           collect do |col|
110           if k.serialized_attributes.has_key? col.name
111             object_properties[col.name] = {
112               type: k.serialized_attributes[col.name].object_class.to_s
113             }
114           else
115             object_properties[col.name] = {
116               type: col.type
117             }
118           end
119         end
120         discovery[:schemas][k.to_s + 'List'] = {
121           id: k.to_s + 'List',
122           description: k.to_s + ' list',
123           type: "object",
124           properties: {
125             kind: {
126               type: "string",
127               description: "Object type. Always arvados##{k.to_s.camelcase(:lower)}List.",
128               default: "arvados##{k.to_s.camelcase(:lower)}List"
129             },
130             etag: {
131               type: "string",
132               description: "List version."
133             },
134             items: {
135               type: "array",
136               description: "The list of #{k.to_s.pluralize}.",
137               items: {
138                 "$ref" => k.to_s
139               }
140             },
141             next_link: {
142               type: "string",
143               description: "A link to the next page of #{k.to_s.pluralize}."
144             },
145             next_page_token: {
146               type: "string",
147               description: "The page token for the next page of #{k.to_s.pluralize}."
148             },
149             selfLink: {
150               type: "string",
151               description: "A link back to this list."
152             }
153           }
154         }
155         discovery[:schemas][k.to_s] = {
156           id: k.to_s,
157           description: k.to_s,
158           type: "object",
159           uuidPrefix: (k.respond_to?(:uuid_prefix) ? k.uuid_prefix : nil),
160           properties: {
161             uuid: {
162               type: "string",
163               description: "Object ID."
164             },
165             etag: {
166               type: "string",
167               description: "Object version."
168             }
169           }.merge(object_properties)
170         }
171         discovery[:resources][k.to_s.underscore.pluralize] = {
172           methods: {
173             get: {
174               id: "arvados.#{k.to_s.underscore.pluralize}.get",
175               path: "#{k.to_s.underscore.pluralize}/{uuid}",
176               httpMethod: "GET",
177               description: "Gets a #{k.to_s}'s metadata by UUID.",
178               parameters: {
179                 uuid: {
180                   type: "string",
181                   description: "The UUID of the #{k.to_s} in question.",
182                   required: true,
183                   location: "path"
184                 }
185               },
186               parameterOrder: [
187                                "uuid"
188                               ],
189               response: {
190                 "$ref" => k.to_s
191               },
192               scopes: [
193                        "https://api.curoverse.com/auth/arvados",
194                        "https://api.curoverse.com/auth/arvados.readonly"
195                       ]
196             },
197             list: {
198               id: "arvados.#{k.to_s.underscore.pluralize}.list",
199               path: k.to_s.underscore.pluralize,
200               httpMethod: "GET",
201               description:
202                  %|List #{k.to_s.pluralize}.
203
204                    The <code>list</code> method returns a
205                    <a href="/api/resources.html">resource list</a> of
206                    matching #{k.to_s.pluralize}. For example:
207
208                    <pre>
209                    {
210                     "kind":"arvados##{k.to_s.camelcase(:lower)}List",
211                     "etag":"",
212                     "self_link":"",
213                     "next_page_token":"",
214                     "next_link":"",
215                     "items":[
216                        ...
217                     ],
218                     "items_available":745,
219                     "_profile":{
220                      "request_time":0.157236317
221                     }
222                     </pre>|,
223               parameters: {
224                 limit: {
225                   type: "integer",
226                   description: "Maximum number of #{k.to_s.underscore.pluralize} to return.",
227                   default: "100",
228                   format: "int32",
229                   minimum: "0",
230                   location: "query",
231                 },
232                 offset: {
233                   type: "integer",
234                   description: "Number of #{k.to_s.underscore.pluralize} to skip before first returned record.",
235                   default: "0",
236                   format: "int32",
237                   minimum: "0",
238                   location: "query",
239                   },
240                 filters: {
241                   type: "array",
242                   description: "Conditions for filtering #{k.to_s.underscore.pluralize}.",
243                   location: "query"
244                 },
245                 where: {
246                   type: "object",
247                   description: "Conditions for filtering #{k.to_s.underscore.pluralize}. (Deprecated. Use filters instead.)",
248                   location: "query"
249                 },
250                 order: {
251                   type: "string",
252                   description: "Order in which to return matching #{k.to_s.underscore.pluralize}.",
253                   location: "query"
254                 },
255                 select: {
256                   type: "array",
257                   description: "Select which fields to return.",
258                   location: "query"
259                 },
260                 distinct: {
261                   type: "boolean",
262                   description: "Return each distinct object.",
263                   location: "query"
264                 },
265                 count: {
266                   type: "string",
267                   description: "Type of count to return in items_available ('none' or 'exact').",
268                   default: "exact",
269                   location: "query"
270                 }
271               },
272               response: {
273                 "$ref" => "#{k.to_s}List"
274               },
275               scopes: [
276                        "https://api.curoverse.com/auth/arvados",
277                        "https://api.curoverse.com/auth/arvados.readonly"
278                       ]
279             },
280             create: {
281               id: "arvados.#{k.to_s.underscore.pluralize}.create",
282               path: "#{k.to_s.underscore.pluralize}",
283               httpMethod: "POST",
284               description: "Create a new #{k.to_s}.",
285               parameters: {},
286               request: {
287                 required: true,
288                 properties: {
289                   k.to_s.underscore => {
290                     "$ref" => k.to_s
291                   }
292                 }
293               },
294               response: {
295                 "$ref" => k.to_s
296               },
297               scopes: [
298                        "https://api.curoverse.com/auth/arvados"
299                       ]
300             },
301             update: {
302               id: "arvados.#{k.to_s.underscore.pluralize}.update",
303               path: "#{k.to_s.underscore.pluralize}/{uuid}",
304               httpMethod: "PUT",
305               description: "Update attributes of an existing #{k.to_s}.",
306               parameters: {
307                 uuid: {
308                   type: "string",
309                   description: "The UUID of the #{k.to_s} in question.",
310                   required: true,
311                   location: "path"
312                 }
313               },
314               request: {
315                 required: true,
316                 properties: {
317                   k.to_s.underscore => {
318                     "$ref" => k.to_s
319                   }
320                 }
321               },
322               response: {
323                 "$ref" => k.to_s
324               },
325               scopes: [
326                        "https://api.curoverse.com/auth/arvados"
327                       ]
328             },
329             delete: {
330               id: "arvados.#{k.to_s.underscore.pluralize}.delete",
331               path: "#{k.to_s.underscore.pluralize}/{uuid}",
332               httpMethod: "DELETE",
333               description: "Delete an existing #{k.to_s}.",
334               parameters: {
335                 uuid: {
336                   type: "string",
337                   description: "The UUID of the #{k.to_s} in question.",
338                   required: true,
339                   location: "path"
340                 }
341               },
342               response: {
343                 "$ref" => k.to_s
344               },
345               scopes: [
346                        "https://api.curoverse.com/auth/arvados"
347                       ]
348             }
349           }
350         }
351         # Check for Rails routes that don't match the usual actions
352         # listed above
353         d_methods = discovery[:resources][k.to_s.underscore.pluralize][:methods]
354         Rails.application.routes.routes.each do |route|
355           action = route.defaults[:action]
356           httpMethod = ['GET', 'POST', 'PUT', 'DELETE'].map { |method|
357             method if route.verb.match(method)
358           }.compact.first
359           if httpMethod and
360               route.defaults[:controller] == 'arvados/v1/' + k.to_s.underscore.pluralize and
361               ctl_class.action_methods.include? action
362             if !d_methods[action.to_sym]
363               method = {
364                 id: "arvados.#{k.to_s.underscore.pluralize}.#{action}",
365                 path: route.path.spec.to_s.sub('/arvados/v1/','').sub('(.:format)','').sub(/:(uu)?id/,'{uuid}'),
366                 httpMethod: httpMethod,
367                 description: "#{action} #{k.to_s.underscore.pluralize}",
368                 parameters: {},
369                 response: {
370                   "$ref" => (action == 'index' ? "#{k.to_s}List" : k.to_s)
371                 },
372                 scopes: [
373                          "https://api.curoverse.com/auth/arvados"
374                         ]
375               }
376               route.segment_keys.each do |key|
377                 if key != :format
378                   key = :uuid if key == :id
379                   method[:parameters][key] = {
380                     type: "string",
381                     description: "",
382                     required: true,
383                     location: "path"
384                   }
385                 end
386               end
387             else
388               # We already built a generic method description, but we
389               # might find some more required parameters through
390               # introspection.
391               method = d_methods[action.to_sym]
392             end
393             if ctl_class.respond_to? "_#{action}_requires_parameters".to_sym
394               ctl_class.send("_#{action}_requires_parameters".to_sym).each do |l, v|
395                 if v.is_a? Hash
396                   method[:parameters][l] = v
397                 else
398                   method[:parameters][l] = {}
399                 end
400                 if !method[:parameters][l][:default].nil?
401                   # The JAVA SDK is sensitive to all values being strings
402                   method[:parameters][l][:default] = method[:parameters][l][:default].to_s
403                 end
404                 method[:parameters][l][:type] ||= 'string'
405                 method[:parameters][l][:description] ||= ''
406                 method[:parameters][l][:location] = (route.segment_keys.include?(l) ? 'path' : 'query')
407                 if method[:parameters][l][:required].nil?
408                   method[:parameters][l][:required] = v != false
409                 end
410               end
411             end
412             d_methods[action.to_sym] = method
413           end
414         end
415       end
416       Rails.configuration.disable_api_methods.each do |method|
417         ctrl, action = method.split('.', 2)
418         discovery[:resources][ctrl][:methods].delete(action.to_sym)
419       end
420       discovery
421     end
422     send_json discovery
423   end
424 end