3381: address the situation where sum percentages calculated exceeded 100%
[arvados.git] / apps / workbench / app / views / application / _job_progress.html.erb
1 <%
2    failed = j[:tasks_summary][:failed] || 0 rescue 0
3    done = j[:tasks_summary][:done] || 0 rescue 0
4    running = j[:tasks_summary][:running] || 0 rescue 0
5    todo = j[:tasks_summary][:todo] || 0 rescue 0
6
7    if j[:success] == false and done + running + failed == 0
8      # The job failed but no tasks were ever started (i.e. crunch-dispatch
9      # was unable to start the job). Display a full 100% failed progress bar.
10      failed_percent = 100
11      success_percent = 0
12      running_percent = 0
13    elsif done + running + failed + todo == 0
14      # No tasks were ever created for this job;
15      # render an empty progress bar.
16      failed_percent = 0
17      success_percent = 0
18      running_percent = 0
19    else
20      percent_total_tasks = 100.0 / (done + running + failed + todo)
21      if defined? scaleby
22        percent_total_tasks *= scaleby
23      end
24      failed_percent = (failed * percent_total_tasks).ceil
25      success_percent = (done * percent_total_tasks).ceil
26      running_percent = (running * percent_total_tasks).ceil
27
28      # We used ceil instead of round to compute percents to handle the situation
29      # where one of them is much larger than the other(s). However, this may
30      # have resulted in total percent > 100, hence we need to account for that.
31      if (failed_percent + success_percent + running_percent) > 100
32         overflow = failed_percent + success_percent + running_percent - 100
33         # decrease the biggest by the overlow amount
34         if (failed_percent > success_percent) && (failed_percent > running_percent)
35           failed_percent -= overflow
36         elsif (success_percent > failed_percent) && (success_percent > running_percent)
37           success_percent -= overflow
38         else
39           running_percent -= overflow
40         end
41      end
42    end
43 %>
44
45 <% if not defined? scaleby %>
46   <div class="progress">
47 <% end %>
48
49 <span class="progress-bar progress-bar-success" style="width: <%= success_percent %>%;">
50 </span>
51 <span class="progress-bar progress-bar-danger" style="width: <%= failed_percent %>%;">
52 </span>
53 <span class="progress-bar" style="width: <%= running_percent %>%;">
54 </span>
55
56 <% if not defined? scaleby %>
57 </div>
58 <% end %>