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