Merge branch 'master' into 1971-show-image-thumbnails
[arvados.git] / apps / workbench / app / controllers / collections_controller.rb
1 class CollectionsController < ApplicationController
2   skip_before_filter :find_object_by_uuid, :only => [:provenance]
3   skip_before_filter :check_user_agreements, :only => [:show_file]
4
5   def show_pane_list
6     %w(Files Attributes Metadata Provenance_graph Used_by JSON API)
7   end
8   def index
9     if params[:search].andand.length.andand > 0
10       tags = Link.where(any: ['contains', params[:search]])
11       @collections = (Collection.where(uuid: tags.collect(&:head_uuid)) |
12                       Collection.where(any: ['contains', params[:search]])).
13         uniq { |c| c.uuid }
14     else
15       if params[:limit]
16         limit = params[:limit].to_i
17       else
18         limit = 100
19       end
20
21       if params[:offset]
22         offset = params[:offset].to_i
23       else
24         offset = 0
25       end
26
27       @collections = Collection.limit(limit).offset(offset)
28     end
29     @links = Link.limit(1000).
30       where(head_uuid: @collections.collect(&:uuid))
31     @collection_info = {}
32     @collections.each do |c|
33       @collection_info[c.uuid] = {
34         tag_links: [],
35         wanted: false,
36         wanted_by_me: false,
37         provenance: [],
38         links: []
39       }
40     end
41     @links.each do |link|
42       @collection_info[link.head_uuid] ||= {}
43       info = @collection_info[link.head_uuid]
44       case link.link_class
45       when 'tag'
46         info[:tag_links] << link
47       when 'resources'
48         info[:wanted] = true
49         info[:wanted_by_me] ||= link.tail_uuid == current_user.uuid
50       when 'provenance'
51         info[:provenance] << link.name
52       end
53       info[:links] << link
54     end
55     @request_url = request.url
56   end
57
58   def show_file
59     opts = params.merge(arvados_api_token: Thread.current[:arvados_api_token])
60     if r = params[:file].match(/(\.\w+)/)
61       ext = r[1]
62     end
63     self.response.headers['Content-Type'] =
64       Rack::Mime::MIME_TYPES[ext] || 'application/octet-stream'
65     self.response.headers['Content-Length'] = params[:size] if params[:size]
66     self.response.headers['Content-Disposition'] = params[:disposition] if params[:disposition]
67     self.response_body = FileStreamer.new opts
68   end
69
70
71   def show
72     return super if !@object
73     @provenance = []
74     @output2job = {}
75     @output2colorindex = {}
76     @sourcedata = {params[:uuid] => {uuid: params[:uuid]}}
77     @protected = {}
78
79     colorindex = -1
80     any_hope_left = true
81     while any_hope_left
82       any_hope_left = false
83       Job.where(output: @sourcedata.keys).sort_by { |a| a.finished_at || a.created_at }.reverse.each do |job|
84         if !@output2colorindex[job.output]
85           any_hope_left = true
86           @output2colorindex[job.output] = (colorindex += 1) % 10
87           @provenance << {job: job, output: job.output}
88           @sourcedata.delete job.output
89           @output2job[job.output] = job
90           job.dependencies.each do |new_source_data|
91             unless @output2colorindex[new_source_data]
92               @sourcedata[new_source_data] = {uuid: new_source_data}
93             end
94           end
95         end
96       end
97     end
98
99     Link.where(head_uuid: @sourcedata.keys | @output2job.keys).each do |link|
100       if link.link_class == 'resources' and link.name == 'wants'
101         @protected[link.head_uuid] = true
102       end
103     end
104     Link.where(tail_uuid: @sourcedata.keys).each do |link|
105       if link.link_class == 'data_origin'
106         @sourcedata[link.tail_uuid][:data_origins] ||= []
107         @sourcedata[link.tail_uuid][:data_origins] << [link.name, link.head_uuid]
108       end
109     end
110     Collection.where(uuid: @sourcedata.keys).each do |collection|
111       if @sourcedata[collection.uuid]
112         @sourcedata[collection.uuid][:collection] = collection
113       end
114     end
115
116     Collection.where(uuid: @object.uuid).each do |u|
117       puts request
118       @prov_svg = ProvenanceHelper::create_provenance_graph(u.provenance, "provenance_svg",
119                                                             {:request => request,
120                                                               :direction => :bottom_up,
121                                                               :combine_jobs => :script_only}) rescue nil
122       @used_by_svg = ProvenanceHelper::create_provenance_graph(u.used_by, "used_by_svg",
123                                                                {:request => request,
124                                                                  :direction => :top_down,
125                                                                  :combine_jobs => :script_only,
126                                                                  :pdata_only => true}) rescue nil
127     end
128   end
129
130   protected
131   class FileStreamer
132     def initialize(opts={})
133       @opts = opts
134     end
135     def each
136       return unless @opts[:uuid] && @opts[:file]
137       env = Hash[ENV].
138         merge({
139                 'ARVADOS_API_HOST' =>
140                 $arvados_api_client.arvados_v1_base.
141                 sub(/\/arvados\/v1/, '').
142                 sub(/^https?:\/\//, ''),
143                 'ARVADOS_API_TOKEN' =>
144                 @opts[:arvados_api_token],
145                 'ARVADOS_API_HOST_INSECURE' =>
146                 Rails.configuration.arvados_insecure_https ? 'true' : 'false'
147               })
148       IO.popen([env, 'arv-get', "#{@opts[:uuid]}/#{@opts[:file]}"],
149                'rb') do |io|
150         while buf = io.read(2**20)
151           yield buf
152         end
153       end
154       Rails.logger.warn("#{@opts[:uuid]}/#{@opts[:file]}: #{$?}") if $? != 0
155     end
156   end
157 end