Merge branch 'master' of git.clinicalfuture.com:arvados into 1692-redesign-dashboard
[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     @collections = Collection.limit(100).to_hash
10     @links = Link.eager.limit(100).where(head_kind: 'arvados#collection', link_class: 'resources', name: 'wants') |
11       Link.eager.limit(100).where(tail_kind: 'arvados#collection', link_class: 'data_origin')
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 == 'data_origin'
38           c[:origin] = l
39         end
40       end
41     end
42   end
43
44   def show
45     return super if !@object
46     @provenance = []
47     @output2job = {}
48     @output2colorindex = {}
49     @sourcedata = {params[:uuid] => {uuid: params[:uuid]}}
50     @protected = {}
51
52     colorindex = -1
53     any_hope_left = true
54     while any_hope_left
55       any_hope_left = false
56       Job.where(output: @sourcedata.keys).sort_by { |a| a.finished_at || a.created_at }.reverse.each do |job|
57         if !@output2colorindex[job.output]
58           any_hope_left = true
59           @output2colorindex[job.output] = (colorindex += 1) % 10
60           @provenance << {job: job, output: job.output}
61           @sourcedata.delete job.output
62           @output2job[job.output] = job
63           job.dependencies.each do |new_source_data|
64             unless @output2colorindex[new_source_data]
65               @sourcedata[new_source_data] = {uuid: new_source_data}
66             end
67           end
68         end
69       end
70     end
71
72     Link.where(head_uuid: @sourcedata.keys | @output2job.keys).each do |link|
73       if link.link_class == 'resources' and link.name == 'wants'
74         @protected[link.head_uuid] = true
75       end
76     end
77     Link.where(tail_uuid: @sourcedata.keys).each do |link|
78       if link.link_class == 'data_origin'
79         @sourcedata[link.tail_uuid][:data_origins] ||= []
80         @sourcedata[link.tail_uuid][:data_origins] << [link.name, link.head_kind, link.head_uuid]
81       end
82     end
83     Collection.where(uuid: @sourcedata.keys).each do |collection|
84       if @sourcedata[collection.uuid]
85         @sourcedata[collection.uuid][:collection] = collection
86       end
87     end
88   end
89 end