Merge branch 'master' into 3605-improved-dashboard
[arvados.git] / apps / workbench / app / helpers / pipeline_instances_helper.rb
index 56e85dbd792269362c880f85626d14a50e48c996..995b1c3e642fc34fd1452e2366ac67e382fe2eb6 100644 (file)
@@ -22,25 +22,54 @@ module PipelineInstancesHelper
     pj
   end
 
-  def pipieline_log_history(job_uuids)
-    results = []
-
-    log_history = Log.where(event_type: 'stderr',
-                            object_uuid: job_uuids).order('id DESC')
-    if !log_history.results.empty?
-      reversed_results = log_history.results.reverse
-      reversed_results.each do |entry|
-        if entry.andand.properties
-          properties = entry.properties
-          text = properties[:text]
-          if text
-            results = results.concat text.split("\n")
-          end
+  def merge_range timestamps, started_at, finished_at
+    timestamps.each_index do |i|
+      if started_at
+        if started_at >= timestamps[i][0] and finished_at <= timestamps[i][1]
+          # 'j' started and ended during 'i'
+          return timestamps
+        end
+
+        if started_at < timestamps[i][0] and finished_at >= timestamps[i][0] and finished_at <= timestamps[i][1]
+          # 'j' started before 'i' and finished during 'i'
+          # re-merge range between when 'j' started and 'i' finished
+          finished_at = timestamps[i][1]
+          timestamps.delete_at i
+          return merge_range timestamps, started_at, finished_at
+        end
+
+        if started_at >= timestamps[i][0] and started_at <= timestamps[i][1]
+          # 'j' started during 'i' and finished sometime after
+          # move end time of 'i' back
+          # re-merge range between when 'i' started and 'j' finished
+          started_at = timestamps[i][0]
+          timestamps.delete_at i
+          return merge_range timestamps, started_at, finished_at
+        end
+
+        if finished_at < timestamps[i][0]
+          # 'j' finished before 'i' started, so insert before 'i'
+          timestamps.insert i, [started_at, finished_at]
+          return timestamps
         end
       end
     end
 
-    return results
+    timestamps << [started_at, finished_at]
+  end
+
+  def determine_wallclock_runtime jobs
+    puts "Begin #{jobs}"
+    timestamps = []
+    jobs.each do |j|
+      insert_at = 0
+      started_at = j[:started_at]
+      finished_at = (if j[:finished_at] then j[:finished_at] else Time.now end)
+      if started_at
+        timestamps = merge_range timestamps, started_at, finished_at
+      end
+    end
+    timestamps.map { |t| t[1] - t[0] }.reduce(:+) || 0
   end
 
   protected
@@ -49,10 +78,26 @@ module PipelineInstancesHelper
     ret = []
     i = -1
 
+    jobuuids = object.components.values.map { |c|
+      c[:job][:uuid] if c.is_a?(Hash) and c[:job].is_a?(Hash)
+    }.compact
+    job = {}
+    Job.where(uuid: jobuuids).each do |j|
+      job[j[:uuid]] = j
+    end
+
     object.components.each do |cname, c|
       i += 1
       pj = {index: i, name: cname}
-      pj[:job] = c[:job].is_a?(Hash) ? c[:job] : {}
+      if not c.is_a?(Hash)
+        ret << pj
+        next
+      end
+      if c[:job] and c[:job][:uuid] and job[c[:job][:uuid]]
+        pj[:job] = job[c[:job][:uuid]]
+      else
+        pj[:job] = c[:job].is_a?(Hash) ? c[:job] : {}
+      end
       pj[:percent_done] = 0
       pj[:percent_running] = 0
       if pj[:job][:success]
@@ -82,23 +127,32 @@ module PipelineInstancesHelper
       end
       if pj[:job][:success]
         pj[:result] = 'complete'
+        pj[:labeltype] = 'success'
         pj[:complete] = true
         pj[:progress] = 1.0
       elsif pj[:job][:finished_at]
         pj[:result] = 'failed'
+        pj[:labeltype] = 'danger'
         pj[:failed] = true
       elsif pj[:job][:started_at]
         pj[:result] = 'running'
+        pj[:labeltype] = 'primary'
       elsif pj[:job][:uuid]
         pj[:result] = 'queued'
+        pj[:labeltype] = 'default'
       else
         pj[:result] = 'none'
+        pj[:labeltype] = 'default'
       end
+
       pj[:job_id] = pj[:job][:uuid]
       pj[:script] = pj[:job][:script] || c[:script]
+      pj[:repository] = pj[:job][:script] || c[:repository]
       pj[:script_parameters] = pj[:job][:script_parameters] || c[:script_parameters]
       pj[:script_version] = pj[:job][:script_version] || c[:script_version]
+      pj[:nondeterministic] = pj[:job][:nondeterministic] || c[:nondeterministic]
       pj[:output] = pj[:job][:output]
+      pj[:output_uuid] = c[:output_uuid]
       pj[:finished_at] = (Time.parse(pj[:job][:finished_at]) rescue nil)
       ret << pj
     end
@@ -152,4 +206,34 @@ module PipelineInstancesHelper
     end
     ret
   end
+
+  def runtime duration, long
+    hours = 0
+    minutes = 0
+    seconds = 0
+    if duration >= 3600
+      hours = (duration / 3600).floor
+      duration -= hours * 3600
+    end
+    if duration >= 60
+      minutes = (duration / 60).floor
+      duration -= minutes * 60
+    end
+    duration = duration.floor
+
+    if long
+      s = ""
+      if hours > 0 then
+        s += "#{hours} hour#{'s' if hours != 1} "
+      end
+      if minutes > 0 then
+        s += "#{minutes} minute#{'s' if minutes != 1} "
+      end
+      s += "#{duration} second#{'s' if duration != 1}"
+    else
+      s = "#{hours}:#{minutes.to_s.rjust(2, '0')}:#{duration.to_s.rjust(2, '0')}"
+    end
+    s
+  end
+
 end