Merge branch '13933-dispatch-batch-size'
[arvados.git] / services / api / app / controllers / arvados / v1 / groups_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require "trashable"
6
7 class Arvados::V1::GroupsController < ApplicationController
8   include TrashableController
9
10   def self._index_requires_parameters
11     (super rescue {}).
12       merge({
13         include_trash: {
14           type: 'boolean', required: false, description: "Include items whose is_trashed attribute is true."
15         },
16       })
17   end
18
19   def self._contents_requires_parameters
20     params = _index_requires_parameters.
21       merge({
22               uuid: {
23                 type: 'string', required: false, default: nil
24               },
25               recursive: {
26                 type: 'boolean', required: false, description: 'Include contents from child groups recursively.'
27               },
28             })
29     params.delete(:select)
30     params
31   end
32
33   def render_404_if_no_object
34     if params[:action] == 'contents'
35       if !params[:uuid]
36         # OK!
37         @object = nil
38         true
39       elsif @object
40         # Project group
41         true
42       elsif (@object = User.where(uuid: params[:uuid]).first)
43         # "Home" pseudo-project
44         true
45       else
46         super
47       end
48     else
49       super
50     end
51   end
52
53   def contents
54     load_searchable_objects
55     send_json({
56       :kind => "arvados#objectList",
57       :etag => "",
58       :self_link => "",
59       :offset => @offset,
60       :limit => @limit,
61       :items_available => @items_available,
62       :items => @objects.as_api_response(nil)
63     })
64   end
65
66   protected
67
68   def load_searchable_objects
69     all_objects = []
70     @items_available = 0
71
72     # Reload the orders param, this time without prefixing unqualified
73     # columns ("name" => "groups.name"). Here, unqualified orders
74     # apply to each table being searched, not "groups".
75     load_limit_offset_order_params(fill_table_names: false)
76
77     # Trick apply_where_limit_order_params into applying suitable
78     # per-table values. *_all are the real ones we'll apply to the
79     # aggregate set.
80     limit_all = @limit
81     offset_all = @offset
82     # save the orders from the current request as determined by load_param,
83     # but otherwise discard them because we're going to be getting objects
84     # from many models
85     request_orders = @orders.clone
86     @orders = []
87
88     request_filters = @filters
89
90     klasses = [Group,
91      Job, PipelineInstance, PipelineTemplate, ContainerRequest, Workflow,
92      Collection,
93      Human, Specimen, Trait]
94
95     table_names = Hash[klasses.collect { |k| [k, k.table_name] }]
96
97     disabled_methods = Rails.configuration.disable_api_methods
98     avail_klasses = table_names.select{|k, t| !disabled_methods.include?(t+'.index')}
99     klasses = avail_klasses.keys
100
101     request_filters.each do |col, op, val|
102       if col.index('.') && !table_names.values.include?(col.split('.', 2)[0])
103         raise ArgumentError.new("Invalid attribute '#{col}' in filter")
104       end
105     end
106
107     wanted_klasses = []
108     request_filters.each do |col,op,val|
109       if op == 'is_a'
110         (val.is_a?(Array) ? val : [val]).each do |type|
111           type = type.split('#')[-1]
112           type[0] = type[0].capitalize
113           wanted_klasses << type
114         end
115       end
116     end
117
118     filter_by_owner = {}
119     if @object
120       if params['recursive']
121         filter_by_owner[:owner_uuid] = [@object.uuid] + @object.descendant_project_uuids
122       else
123         filter_by_owner[:owner_uuid] = @object.uuid
124       end
125     end
126
127     seen_last_class = false
128     klasses.each do |klass|
129       @offset = 0 if seen_last_class  # reset offset for the new next type being processed
130
131       # if current klass is same as params['last_object_class'], mark that fact
132       seen_last_class = true if((params['count'].andand.==('none')) and
133                                 (params['last_object_class'].nil? or
134                                  params['last_object_class'].empty? or
135                                  params['last_object_class'] == klass.to_s))
136
137       # if klasses are specified, skip all other klass types
138       next if wanted_klasses.any? and !wanted_klasses.include?(klass.to_s)
139
140       # don't reprocess klass types that were already seen
141       next if params['count'] == 'none' and !seen_last_class
142
143       # don't process rest of object types if we already have needed number of objects
144       break if params['count'] == 'none' and all_objects.size >= limit_all
145
146       # If the currently requested orders specifically match the
147       # table_name for the current klass, apply that order.
148       # Otherwise, order by recency.
149       request_order =
150         request_orders.andand.find { |r| r =~ /^#{klass.table_name}\./i || r !~ /\./ } ||
151         klass.default_orders.join(", ")
152
153       @select = nil
154       where_conds = filter_by_owner
155       if klass == Collection
156         @select = klass.selectable_attributes - ["manifest_text"]
157       elsif klass == Group
158         where_conds = where_conds.merge(group_class: "project")
159       end
160
161       @filters = request_filters.map do |col, op, val|
162         if !col.index('.')
163           [col, op, val]
164         elsif (col = col.split('.', 2))[0] == klass.table_name
165           [col[1], op, val]
166         else
167           nil
168         end
169       end.compact
170
171       @objects = klass.readable_by(*@read_users, {:include_trash => params[:include_trash]}).
172                  order(request_order).where(where_conds)
173
174       klass_limit = limit_all - all_objects.count
175       @limit = klass_limit
176       apply_where_limit_order_params klass
177       klass_object_list = object_list(model_class: klass)
178       klass_items_available = klass_object_list[:items_available] || 0
179       @items_available += klass_items_available
180       @offset = [@offset - klass_items_available, 0].max
181       all_objects += klass_object_list[:items]
182
183       if klass_object_list[:limit] < klass_limit
184         # object_list() had to reduce @limit to comply with
185         # max_index_database_read. From now on, we'll do all queries
186         # with limit=0 and just accumulate items_available.
187         limit_all = all_objects.count
188       end
189     end
190
191     @objects = all_objects
192     @limit = limit_all
193     @offset = offset_all
194   end
195
196 end