rename foreign uuid attributes
[arvados.git] / apps / workbench / app / controllers / collections_controller.rb
1 class CollectionsController < ApplicationController
2   skip_before_filter :find_object_by_uuid, :only => [:graph]
3
4   def graph
5     index
6   end
7
8   def index
9     @links = Link.eager.limit(100).where(head_kind: 'arvados#collection') |
10       Link.eager.limit(100).where(tail_kind: 'arvados#collection')
11     @collections = Collection.limit(100).to_hash
12     @collections.merge!(Collection.
13                         limit(100).
14                         where(uuid: @links.select{|x|x.head_kind=='arvados#collection'}.collect(&:head_uuid) |
15                               @links.select{|x|x.tail_kind=='arvados#collection'}.collect(&:tail_uuid)).
16                         to_hash)
17     @collection_info = {}
18     @collections.each do |uuid, c|
19       ci = (@collection_info[uuid] ||= {uuid: uuid})
20       ci[:created_at] = c.created_at
21     end
22     @links.each do |l|
23       if l.head_kind == 'arvados#collection'
24         c = (@collection_info[l.head_uuid] ||= {uuid: l.head_uuid})
25         if l.link_class == 'resources' and l.name == 'wants'
26           if l.head.respond_to? :created_at
27             c[:created_at] = l.head.created_at
28           end
29           c[:wanted] = true
30           if l.owner_uuid == current_user.uuid
31             c[:wanted_by_me] = true
32           end
33         end
34       end
35       if l.tail_kind == 'arvados#collection'
36         c = (@collection_info[l.tail_uuid] ||= {uuid: l.tail_uuid})
37         if l.link_class == 'group' and l.name == 'member_of'
38           c[:groups] ||= {}
39           c[:groups][l.tail_uuid] = true
40         end
41         if l.link_class == 'data_origin'
42           c[:origin] = l
43         end
44       end
45     end
46   end
47
48   def show
49     return super if !@object
50     @provenance = []
51     @output2job = {}
52     @output2colorindex = {}
53     @sourcedata = {params[:uuid] => {uuid: params[:uuid]}}
54     @protected = {}
55     whence = `whence #{params[:uuid]}`
56     colorindex = -1
57     whence.split("\n").each do |line|
58       if line.match /^(\#\d+@\S+)$/
59         job = Job.where(submit_id: line).first
60         @provenance << {job: job, target: line}
61       elsif (re = line.match /^ +output *= *(\S+)/)
62         if !@provenance.empty?
63           @provenance[-1][:output] = re[1]
64           @output2job[re[1]] = @provenance[-1][:job]
65           if !@output2colorindex[re[1]]
66             @output2colorindex[re[1]] = (colorindex += 1) % 10
67           end
68           @sourcedata.delete re[1]
69         end
70       elsif (re = line.match /^([0-9a-f]{32}\b)/)
71         @sourcedata[re[1]] ||= {uuid: re[1]}
72       end
73     end
74     Link.where(head_uuid: @sourcedata.keys | @output2job.keys).each do |link|
75       if link.link_class == 'resources' and link.name == 'wants'
76         @protected[link.head_uuid] = true
77       end
78     end
79     Link.where(tail_uuid: @sourcedata.keys).each do |link|
80       if link.link_class == 'data_origin'
81         @sourcedata[link.tail_uuid][:data_origins] ||= []
82         @sourcedata[link.tail_uuid][:data_origins] << [link.name, link.head_kind, link.head_uuid]
83       end
84     end
85     Collection.where(uuid: @sourcedata.keys).each do |collection|
86       if @sourcedata[collection.uuid]
87         @sourcedata[collection.uuid][:collection] = collection
88       end
89     end
90   end
91 end