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