app/models/foreman_tasks/task.rb in foreman-tasks-4.1.6 vs app/models/foreman_tasks/task.rb in foreman-tasks-5.0.0
- old
+ new
@@ -3,10 +3,12 @@
module ForemanTasks
class Task < ApplicationRecord
include Authorizable
extend Search
+ graphql_type '::Types::Task'
+
def check_permissions_after_save
# there's no create_tasks permission, tasks are created as a result of internal actions, in such case we
# don't do authorization, that should have been performed on wrapping action level
# the same applies for updating
true
@@ -251,9 +253,35 @@
def action_continuous_output
return unless main_action.is_a?(Actions::Helpers::WithContinuousOutput)
main_action.continuous_output.sort!
main_action.continuous_output.raw_outputs
+ end
+
+ def self.latest_tasks_by_resource_ids(label, resource_type, resource_ids)
+ tasks = arel_table
+ links = ForemanTasks::Link.arel_table
+ started_at = tasks[:started_at]
+ resource_id = links[:resource_id]
+
+ base_combined_table = tasks
+ .join(links).on(tasks[:id].eq(links[:task_id]))
+ .where(tasks[:label].eq(label)
+ .and(links[:resource_type].eq(resource_type))
+ .and(links[:resource_id].in(resource_ids)))
+
+ grouped = base_combined_table.project(
+ started_at.maximum.as('started_at_max'),
+ resource_id
+ ).group(resource_id).order(resource_id).as('grouped')
+
+ max_per_resource_id = tasks
+ .join(links).on(tasks[:id].eq(links[:task_id]))
+ .join(grouped).on(grouped[:started_at_max].eq(started_at).and(grouped[:resource_id].eq(resource_id)))
+ .distinct
+ .project(tasks[Arel.star], grouped[:resource_id])
+
+ find_by_sql(max_per_resource_id.to_sql).index_by(&:resource_id)
end
protected
def generate_id