9319: /all_processes index page.
[arvados.git] / apps / workbench / app / models / pipeline_instance.rb
1 require "arvados/keep"
2
3 class PipelineInstance < ArvadosBase
4   attr_accessor :pipeline_template
5
6   def self.goes_in_projects?
7     true
8   end
9
10   def friendly_link_name lookup=nil
11     pipeline_name = self.name
12     if pipeline_name.nil? or pipeline_name.empty?
13       template = if lookup and lookup[self.pipeline_template_uuid]
14                    lookup[self.pipeline_template_uuid]
15                  else
16                    PipelineTemplate.find(self.pipeline_template_uuid) if self.pipeline_template_uuid
17                  end
18       if template
19         template.name
20       else
21         self.uuid
22       end
23     else
24       pipeline_name
25     end
26   end
27
28   def content_summary
29     begin
30       PipelineTemplate.find(pipeline_template_uuid).name
31     rescue
32       super
33     end
34   end
35
36   def update_job_parameters(new_params)
37     self.components[:steps].each_with_index do |step, i|
38       step[:params].each do |param|
39         if new_params.has_key?(new_param_name = "#{i}/#{param[:name]}") or
40             new_params.has_key?(new_param_name = "#{step[:name]}/#{param[:name]}") or
41             new_params.has_key?(new_param_name = param[:name])
42           param_type = :value
43           %w(hash data_locator).collect(&:to_sym).each do |ptype|
44             param_type = ptype if param.has_key? ptype
45           end
46           param[param_type] = new_params[new_param_name]
47         end
48       end
49     end
50   end
51
52   def editable_attributes
53     %w(name description components)
54   end
55
56   def attribute_editable?(name, ever=nil)
57     if name.to_s == "components"
58       (ever or %w(New Ready).include?(state)) and super
59     else
60       super
61     end
62   end
63
64   def attributes_for_display
65     super.reject { |k,v| k == 'components' }
66   end
67
68   def self.creatable?
69     false
70   end
71
72   def component_input_title(component_name, input_name)
73     component = components[component_name]
74     return nil if component.nil?
75     param_info = component[:script_parameters].andand[input_name.to_sym]
76     if param_info.is_a?(Hash) and param_info[:title]
77       param_info[:title]
78     else
79       "\"#{input_name.to_s}\" parameter for #{component[:script]} script in #{component_name} component"
80     end
81   end
82
83   def textile_attributes
84     [ 'description' ]
85   end
86
87   def job_uuids
88     components_map { |cspec| cspec[:job][:uuid] rescue nil }
89   end
90
91   def job_log_ids
92     components_map { |cspec| cspec[:job][:log] rescue nil }
93   end
94
95   def job_ids
96     components_map { |cspec| cspec[:job][:uuid] rescue nil }
97   end
98
99   def stderr_log_object_uuids
100     result = job_uuids.values.compact
101     result << uuid
102   end
103
104   def stderr_log_query(limit=nil)
105     query = Log.
106       where(event_type: "stderr",
107             object_uuid: stderr_log_object_uuids).
108       order("id DESC")
109     unless limit.nil?
110       query = query.limit(limit)
111     end
112     query
113   end
114
115   def stderr_log_lines(limit=2000)
116     stderr_log_query(limit).results.reverse.
117       flat_map { |log| log.properties[:text].split("\n") rescue [] }
118   end
119
120   def has_readable_logs?
121     log_pdhs, log_uuids = job_log_ids.values.compact.partition do |loc_s|
122       Keep::Locator.parse(loc_s)
123     end
124     if log_pdhs.any? and
125         Collection.where(portable_data_hash: log_pdhs).limit(1).results.any?
126       true
127     elsif log_uuids.any? and
128         Collection.where(uuid: log_uuids).limit(1).results.any?
129       true
130     else
131       stderr_log_query(1).results.any?
132     end
133   end
134
135   def work_unit(label=nil)
136     PipelineInstanceWorkUnit.new(self, label || self.name)
137   end
138
139   private
140
141   def components_map
142     Hash[components.map { |cname, cspec| [cname, yield(cspec)] }]
143   end
144 end