From 78ceb41a9e7b064be3f73423d600a294b9e3fb75 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Fri, 28 Feb 2020 16:17:32 +0100 Subject: [PATCH] Improve loading of tasks statistics for dashboard [SCI-4356] --- .../javascripts/dashboard/current_tasks.js | 6 ++-- .../dashboard/current_tasks_controller.rb | 17 +++++----- app/models/my_module.rb | 34 +++++-------------- 3 files changed, 22 insertions(+), 35 deletions(-) diff --git a/app/assets/javascripts/dashboard/current_tasks.js b/app/assets/javascripts/dashboard/current_tasks.js index 120028587..1ad0380b9 100644 --- a/app/assets/javascripts/dashboard/current_tasks.js +++ b/app/assets/javascripts/dashboard/current_tasks.js @@ -22,7 +22,7 @@ var DasboardCurrentTasksWidget = (function() { mode: $('.current-tasks-navbar .active').data('mode') }; animateSpinner($currentTasksList, true); - $.get($currentTasksList.attr('data-tasks-list-url'), params, function(data) { + $.get($currentTasksList.data('tasksListUrl'), params, function(data) { // Toggle empty state if (data.tasks_list.length === 0) { $currentTasksList.append(emptyState); @@ -143,7 +143,9 @@ var DasboardCurrentTasksWidget = (function() { } function initNavbar() { - $('.navbar-assigned, .navbar-all').on('click', function() { + $('.navbar-assigned, .navbar-all').on('click', function(e) { + e.stopPropagation(); + e.preventDefault(); $('.current-tasks-navbar').find('a').removeClass('active'); $(this).addClass('active'); loadCurrentTasksList(); diff --git a/app/controllers/dashboard/current_tasks_controller.rb b/app/controllers/dashboard/current_tasks_controller.rb index 965a933b5..340b732c4 100644 --- a/app/controllers/dashboard/current_tasks_controller.rb +++ b/app/controllers/dashboard/current_tasks_controller.rb @@ -34,20 +34,23 @@ module Dashboard tasks end + tasks = tasks.with_step_statistics.preload(experiment: :project) + respond_to do |format| format.json do render json: { tasks_list: tasks.map do |task| - due_date = I18n.l(task.due_date, format: :full_with_comma) if task.due_date.present? { id: task.id, link: protocols_my_module_path(task.id), - experiment: task.experiment.name, - project: task.experiment.project.name, + experiment: escape_input(task.experiment.name), + project: escape_input(task.experiment.project.name), name: escape_input(task.name), - due_date: due_date, + due_date: task.due_date.present? ? I18n.l(task.due_date, format: :full_with_comma) : nil, overdue: task.is_overdue?, state: task.state, - steps_state: task.completed_steps_percentage } + steps_state: { completed_steps: task.steps_completed, + all_steps: task.steps_total, + percentage: task.steps_completed_percentage } } end, status: :ok } @@ -80,9 +83,7 @@ module Dashboard private def task_filters - params.permit( - :project_id, :experiment_id, :mode, :view, :sort - ) + params.permit(:project_id, :experiment_id, :mode, :view, :sort) end def load_project diff --git a/app/models/my_module.rb b/app/models/my_module.rb index 5afcfe473..b1d39c3ef 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -80,6 +80,15 @@ class MyModule < ApplicationRecord end) scope :workflow_ordered, -> { order(workflow_order: :asc) } scope :uncomplete, -> { where(state: 'uncompleted') } + scope :with_step_statistics, (lambda do + joins(protocols: :steps) + .group(:id) + .select('my_modules.*') + .select('COUNT(steps.id) AS steps_total') + .select('COUNT(steps.id) FILTER (where steps.completed = true) AS steps_completed') + .select('((COUNT(steps.id) FILTER (where steps.completed = true)) * 100 / COUNT(steps.id)) '\ + 'AS steps_completed_percentage') + end) # A module takes this much space in canvas (x, y) in database WIDTH = 30 @@ -492,31 +501,6 @@ class MyModule < ApplicationRecord state == 'completed' end - def completed_steps_percentage - if protocol.steps.any? - steps_percentage = - MyModule.joins(protocols: :steps) - .where(id: id) - .group(:id) - .select('my_modules.*') - .select('COUNT(steps.id) AS all') - .select('COUNT(steps.id) FILTER (where steps.completed = true) AS completed') - .select('((COUNT(steps.id) FILTER (where steps.completed = true)) * 100 / COUNT(steps.id)) AS percentage') - .take - { - completed_steps: steps_percentage.completed, - all_steps: steps_percentage.all, - percentage: steps_percentage.percentage - } - else - { - completed_steps: 0, - all_steps: 0, - percentage: 0 - } - end - end - # Check if my_module is ready to become completed def check_completness_status if protocol && protocol.steps.count > 0