Working on recent pipelines/collections tables
[arvados.git] / apps / workbench / app / controllers / actions_controller.rb
1 class ActionsController < ApplicationController
2
3   skip_filter :require_thread_api_token, only: [:report_issue_popup, :report_issue]
4   skip_filter :check_user_agreements, only: [:report_issue_popup, :report_issue]
5
6   @@exposed_actions = {}
7   def self.expose_action method, &block
8     @@exposed_actions[method] = true
9     define_method method, block
10   end
11
12   def model_class
13     ArvadosBase::resource_class_for_uuid(params[:uuid])
14   end
15
16   def show
17     @object = model_class.andand.find(params[:uuid])
18     if @object.is_a? Link and
19         @object.link_class == 'name' and
20         ArvadosBase::resource_class_for_uuid(@object.head_uuid) == Collection
21       redirect_to collection_path(id: @object.uuid)
22     elsif @object
23       redirect_to @object
24     else
25       raise ActiveRecord::RecordNotFound
26     end
27   end
28
29   def post
30     params.keys.collect(&:to_sym).each do |param|
31       if @@exposed_actions[param]
32         return self.send(param)
33       end
34     end
35     redirect_to :back
36   end
37
38   expose_action :copy_selections_into_project do
39     move_or_copy :copy
40   end
41
42   expose_action :move_selections_into_project do
43     move_or_copy :move
44   end
45
46   def move_or_copy action
47     uuids_to_add = params["selection"]
48     uuids_to_add = [ uuids_to_add ] unless uuids_to_add.is_a? Array
49     uuids_to_add.
50       collect { |x| ArvadosBase::resource_class_for_uuid(x) }.
51       uniq.
52       each do |resource_class|
53       resource_class.filter([['uuid','in',uuids_to_add]]).each do |src|
54         if resource_class == Collection and not Collection.attribute_info.include?(:name)
55           dst = Link.new(owner_uuid: @object.uuid,
56                          tail_uuid: @object.uuid,
57                          head_uuid: src.uuid,
58                          link_class: 'name',
59                          name: src.uuid)
60         else
61           case action
62           when :copy
63             dst = src.dup
64             if dst.respond_to? :'name='
65               if dst.name
66                 dst.name = "Copy of #{dst.name}"
67               else
68                 dst.name = "Copy of unnamed #{dst.class_for_display.downcase}"
69               end
70             end
71             if resource_class == Collection
72               dst.manifest_text = Collection.select([:manifest_text]).where(uuid: src.uuid).first.manifest_text
73             end
74           when :move
75             dst = src
76           else
77             raise ArgumentError.new "Unsupported action #{action}"
78           end
79           dst.owner_uuid = @object.uuid
80           dst.tail_uuid = @object.uuid if dst.class == Link
81         end
82         begin
83           dst.save!
84         rescue
85           dst.name += " (#{Time.now.localtime})" if dst.respond_to? :name=
86           dst.save!
87         end
88       end
89     end
90     redirect_to @object
91   end
92
93   def arv_normalize mt, *opts
94     r = ""
95     env = Hash[ENV].
96       merge({'ARVADOS_API_HOST' =>
97               arvados_api_client.arvados_v1_base.
98               sub(/\/arvados\/v1/, '').
99               sub(/^https?:\/\//, ''),
100               'ARVADOS_API_TOKEN' => 'x',
101               'ARVADOS_API_HOST_INSECURE' =>
102               Rails.configuration.arvados_insecure_https ? 'true' : 'false'
103             })
104     IO.popen([env, 'arv-normalize'] + opts, 'w+b') do |io|
105       io.write mt
106       io.close_write
107       while buf = io.read(2**16)
108         r += buf
109       end
110     end
111     r
112   end
113
114   expose_action :combine_selected_files_into_collection do
115     uuids = []
116     pdhs = []
117     files = []
118     params["selection"].each do |s|
119       a = ArvadosBase::resource_class_for_uuid s
120       if a == Link
121         begin
122           if (m = CollectionsHelper.match(Link.find(s).head_uuid))
123             pdhs.append(m[1] + m[2])
124             files.append(m)
125           end
126         rescue
127         end
128       elsif (m = CollectionsHelper.match(s))
129         pdhs.append(m[1] + m[2])
130         files.append(m)
131       elsif (m = CollectionsHelper.match_uuid_with_optional_filepath(s))
132         uuids.append(m[1])
133         files.append(m)
134       end
135     end
136
137     pdhs = pdhs.uniq
138     uuids = uuids.uniq
139     chash = {}
140
141     Collection.select([:uuid, :manifest_text]).where(uuid: uuids).each do |c|
142       chash[c.uuid] = c
143     end
144
145     Collection.select([:portable_data_hash, :manifest_text]).where(portable_data_hash: pdhs).each do |c|
146       chash[c.portable_data_hash] = c
147     end
148
149     combined = ""
150     files.each do |m|
151       mt = chash[m[1]+m[2]].andand.manifest_text
152       if not m[4].nil? and m[4].size > 1
153         combined += arv_normalize mt, '--extract', m[4][1..-1]
154       else
155         combined += mt
156       end
157     end
158
159     normalized = arv_normalize combined
160     newc = Collection.new({:manifest_text => normalized})
161     newc.save!
162
163     chash.each do |k,v|
164       l = Link.new({
165                      tail_uuid: k,
166                      head_uuid: newc.uuid,
167                      link_class: "provenance",
168                      name: "provided"
169                    })
170       l.save!
171     end
172
173     redirect_to controller: 'collections', action: :show, id: newc.uuid
174   end
175
176   def report_issue_popup
177     respond_to do |format|
178       format.js
179       format.html
180     end
181   end
182
183   def report_issue
184     logger.warn "report_issue: #{params.inspect}"
185
186     respond_to do |format|
187       IssueReporter.send_report(current_user, params).deliver
188       format.js {render nothing: true}
189     end
190   end
191
192 end