Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 4 additions & 19 deletions app/controllers/foreman_tasks/api/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,12 @@ def bulk_stop
param :parent_task_id, :identifier, desc: 'UUID of the task'
param_group :search_and_pagination, ::Api::V2::BaseController
def index
if params[:sort_by] || params[:sort_order]
Foreman::Deprecation.api_deprecation_warning(
"The sort params sort_by and sort_order are deprecated.
Please use the order param instead as one string 'order=started_at desc'"
)

ordering_params = {
sort_by: params[:sort_by] || 'started_at',
sort_order: params[:sort_order] || 'DESC',
}
params[:order] = "#{ordering_params[:sort_by]} #{ordering_params[:sort_order]}"
end
params[:order] ||= 'started_at DESC'
@tasks = resource_scope_for_index.order(params[:order].to_s)
@tasks = resource_scope_for_index
end

def search_options
[search_query, {}]
[search_query, { :order => params[:order] }]
end

def_param_group :callback_target do
Expand Down Expand Up @@ -320,19 +308,16 @@ def resource_class
end

def find_task
@task = resource_scope.with_duration.find(params[:id])
@task = resource_scope.select_duration.find(params[:id])
end

def resource_scope(_options = {})
scope = ForemanTasks::Task.authorized("#{action_permission}_foreman_tasks")
scope = scope.where(:parent_task_id => params[:parent_task_id]) if params[:parent_task_id]
scope = scope.select_duration if params[:action] == 'index'
scope
end

def resource_scope_for_index(*args)
super.with_duration.distinct
end

def controller_permission
'foreman_tasks'
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/foreman_tasks/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def resource_class
private

def respond_with_tasks(scope)
@tasks = filter(scope, paginate: false).with_duration
@tasks = filter(scope, paginate: false).select_duration
csv_response(@tasks, [:id, :action, :state, :result, 'started_at.in_time_zone', 'ended_at.in_time_zone', :duration, :username], ['Id', 'Action', 'State', 'Result', 'Started At', 'Ended At', 'Duration', 'User'])
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/foreman_tasks/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def check_permissions_after_save
:"foreman_tasks_links.resource_type" => resource.class.name)
end)
scope :for_action_types, (->(action_types) { where('foreman_tasks_tasks.label IN (?)', Array(action_types)) })
scope :with_duration, -> { select("foreman_tasks_tasks.*, coalesce(ended_at, current_timestamp) - coalesce(coalesce(started_at, ended_at), current_timestamp) as duration") }
virtual_column_scope :select_duration, -> { select("foreman_tasks_tasks.*, coalesce(ended_at, current_timestamp) - coalesce(coalesce(started_at, ended_at), current_timestamp) as duration") }

apipie :class, "A class representing #{model_name.human} object" do
name 'Task'
Expand Down
2 changes: 1 addition & 1 deletion lib/foreman_tasks/tasks/export_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ namespace :foreman_tasks do
format = ENV['TASK_FORMAT'] || 'html'
export_filename = ENV['TASK_FILE'] || generate_filename(format)

task_scope = ForemanTasks::Task.search_for(filter).with_duration.order(:started_at => :desc)
task_scope = ForemanTasks::Task.search_for(filter).select_duration.order(:started_at => :desc)
id_scope = task_scope.group(:id, :started_at)

puts _("Exporting all tasks matching filter #{filter}")
Expand Down
6 changes: 3 additions & 3 deletions test/controllers/api/tasks_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Api::TasksControllerTest < ActionController::TestCase
end

it 'supports ordering by duration' do
get :index, params: { :sort_by => 'duration' }
get :index, params: { :order => 'duration' }
assert_response :success
data = JSON.parse(response.body)
assert_equal 'duration', data.dig('sort', 'by')
Expand Down Expand Up @@ -104,7 +104,7 @@ class Api::TasksControllerTest < ActionController::TestCase
end

it 'shows duration column' do
task = ForemanTasks::Task.with_duration.find(FactoryBot.create(:dynflow_task).id)
task = ForemanTasks::Task.select_duration.find(FactoryBot.create(:dynflow_task).id)
get :show, params: { id: task.id }, session: set_session_user
assert_response :success
data = JSON.parse(response.body)
Expand All @@ -114,7 +114,7 @@ class Api::TasksControllerTest < ActionController::TestCase

describe 'GET /api/tasks/index' do
it 'shows duration column' do
task = ForemanTasks::Task.with_duration.find(FactoryBot.create(:dynflow_task).id)
task = ForemanTasks::Task.select_duration.find(FactoryBot.create(:dynflow_task).id)
get :index, session: set_session_user
assert_response :success
data = JSON.parse(response.body)
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/tasks_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def in_taxonomy_scope(organization, location = nil)

describe 'index' do
it 'shows duration column' do
task = ForemanTasks::Task.with_duration.find(FactoryBot.create(:some_task).id)
task = ForemanTasks::Task.select_duration.find(FactoryBot.create(:some_task).id)
get(:index, params: {}, session: set_session_user)
assert_response :success
row = CSV.parse(response.body, headers: true).first
Expand Down Expand Up @@ -122,7 +122,7 @@ def in_taxonomy_scope(organization, location = nil)

it 'shows duration column' do
parent = ForemanTasks::Task.find(FactoryBot.create(:some_task).id)
child = ForemanTasks::Task.with_duration.find(FactoryBot.create(:some_task).id)
child = ForemanTasks::Task.select_duration.find(FactoryBot.create(:some_task).id)
child.parent_task_id = parent.id
child.save!
get(:sub_tasks, params: { id: parent.id }, session: set_session_user)
Expand Down