98b5494461a102d14859be8ee3e5e3b41932900f
[arvados.git] / app / models / pipeline_invocation.rb
1 class PipelineInvocation < ActiveRecord::Base
2   include AssignUuid
3   serialize :components, Hash
4   belongs_to :pipeline, :foreign_key => :pipeline_uuid, :primary_key => :uuid
5
6   before_validation :bootstrap_components
7   before_validation :update_success
8
9   def progress_table
10     begin
11       # v0 pipeline format
12       nrow = -1
13       components['steps'].collect do |step|
14         nrow += 1
15         row = [nrow, step['name']]
16         if step['output_data_locator']
17           row << 1.0
18         else
19           row << 0.0
20         end
21         row << (step['warehousejob']['id'] rescue nil)
22         row << (step['warehousejob']['revision'] rescue nil)
23         row << step['output_data_locator']
24         row << (Time.parse(step['warehousejob']['finishtime']) rescue nil)
25         row
26       end
27     rescue
28       []
29     end
30   end
31
32   def progress_ratio
33     t = progress_table
34     return 0 if t.size < 1
35     t.collect { |r| r[2] }.inject(0.0) { |sum,a| sum += a } / t.size
36   end
37
38   protected
39   def bootstrap_components
40     if pipeline and (!components or components.empty?)
41       self.components = pipeline.components
42     end
43   end
44
45   def update_success
46     if components and progress_ratio == 1.0
47       self.success = true
48     end
49   end
50 end