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