|
|
|
@ -41,7 +41,7 @@ class PlanningComparisonService |
|
|
|
|
where #{Journal.table_name}.created_at=latest.latest_date |
|
|
|
|
and #{Journal.table_name}.id=latest.latest_id; |
|
|
|
|
SQL |
|
|
|
|
@@mapped_attributes = Journal::WorkPackageJournal.journaled_attributes.map{|attribute| "#{Journal::WorkPackageJournal.table_name}.#{attribute}"}.join ',' |
|
|
|
|
@@mapped_attributes = Journal::WorkPackageJournal.journaled_attributes.map { |attribute| "#{Journal::WorkPackageJournal.table_name}.#{attribute}" }.join ',' |
|
|
|
|
|
|
|
|
|
@@work_package_select = <<SQL |
|
|
|
|
Select #{Journal.table_name}.journable_id as id, |
|
|
|
@ -58,8 +58,7 @@ SQL |
|
|
|
|
# the comparison always works on the current date, filters the current workpackages |
|
|
|
|
# and returns the state of these work_packages at the given time |
|
|
|
|
# filters are given in the format expected by Query and are just passed through to query |
|
|
|
|
def self.compare(projects, at_time, filter={}) |
|
|
|
|
|
|
|
|
|
def self.compare(projects, at_time, filter = {}) |
|
|
|
|
# The query uses three steps to find the journalized entries for the filtered workpackages |
|
|
|
|
# at the given point in time: |
|
|
|
|
# 1 filter the ids using query |
|
|
|
@ -76,65 +75,62 @@ SQL |
|
|
|
|
|
|
|
|
|
# 2 fetch latest journal-entries for the given time |
|
|
|
|
journal_ids = Journal.find_by_sql([@@journal_sql, at_time, work_package_ids]) |
|
|
|
|
.map(&:id) |
|
|
|
|
.map(&:id) |
|
|
|
|
|
|
|
|
|
# 3&4 fetch the journaled data and make rails think it is actually a work_package |
|
|
|
|
work_packages = WorkPackage.find_by_sql([@@work_package_select,journal_ids]) |
|
|
|
|
work_packages = WorkPackage.find_by_sql([@@work_package_select, journal_ids]) |
|
|
|
|
|
|
|
|
|
restore_references(work_packages) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
protected |
|
|
|
|
|
|
|
|
|
def self.filtered_work_packages(projects, filter) |
|
|
|
|
work_package_scope = WorkPackage.scoped |
|
|
|
|
.joins(:status) |
|
|
|
|
.joins(:project) # query doesn't provide these joins itself... |
|
|
|
|
.for_projects(projects) |
|
|
|
|
|
|
|
|
|
query = Query.new name: 'generated-query' |
|
|
|
|
query.add_filters(filter[:f], filter[:op], filter[:v]) |
|
|
|
|
|
|
|
|
|
work_package_scope.with_query(query) |
|
|
|
|
.pluck(:id) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def self.unfiltered_work_packages(projects) |
|
|
|
|
WorkPackage.for_projects(projects).pluck(:id) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
# This is a very crude way to work around n+1-issues, that are |
|
|
|
|
# introduced by the json/xml-rendering |
|
|
|
|
# the simple .includes does not work the work due to the find_by_sql |
|
|
|
|
def self.restore_references(work_packages) |
|
|
|
|
def self.filtered_work_packages(projects, filter) |
|
|
|
|
work_package_scope = WorkPackage.scoped |
|
|
|
|
.joins(:status) |
|
|
|
|
.joins(:project) # query doesn't provide these joins itself... |
|
|
|
|
.for_projects(projects) |
|
|
|
|
|
|
|
|
|
projects = resolve_projects(work_packages) |
|
|
|
|
types = resolve_types(work_packages) |
|
|
|
|
statuses = resolve_statuses(work_packages) |
|
|
|
|
query = Query.new name: 'generated-query' |
|
|
|
|
query.add_filters(filter[:f], filter[:op], filter[:v]) |
|
|
|
|
|
|
|
|
|
work_packages.each do |wp| |
|
|
|
|
wp.project = projects[wp.project_id] |
|
|
|
|
wp.type = types[wp.type_id] |
|
|
|
|
wp.status = statuses[wp.status_id] |
|
|
|
|
end |
|
|
|
|
work_package_scope.with_query(query) |
|
|
|
|
.pluck(:id) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
work_packages |
|
|
|
|
def self.unfiltered_work_packages(projects) |
|
|
|
|
WorkPackage.for_projects(projects).pluck(:id) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
# This is a very crude way to work around n+1-issues, that are |
|
|
|
|
# introduced by the json/xml-rendering |
|
|
|
|
# the simple .includes does not work the work due to the find_by_sql |
|
|
|
|
def self.restore_references(work_packages) |
|
|
|
|
projects = resolve_projects(work_packages) |
|
|
|
|
types = resolve_types(work_packages) |
|
|
|
|
statuses = resolve_statuses(work_packages) |
|
|
|
|
|
|
|
|
|
work_packages.each do |wp| |
|
|
|
|
wp.project = projects[wp.project_id] |
|
|
|
|
wp.type = types[wp.type_id] |
|
|
|
|
wp.status = statuses[wp.status_id] |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def self.resolve_projects(work_packages) |
|
|
|
|
project_ids = work_packages.map(&:project_id).uniq.compact |
|
|
|
|
projects = Hash[Project.find(project_ids).map {|wp| [wp.id,wp]}] |
|
|
|
|
end |
|
|
|
|
work_packages |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def self.resolve_types(work_packages) |
|
|
|
|
type_ids = work_packages.map(&:type_id).uniq.compact |
|
|
|
|
types = Hash[Type.find(type_ids).map{|type| [type.id,type]}] |
|
|
|
|
end |
|
|
|
|
def self.resolve_projects(work_packages) |
|
|
|
|
project_ids = work_packages.map(&:project_id).uniq.compact |
|
|
|
|
projects = Hash[Project.find(project_ids).map { |wp| [wp.id, wp] }] |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def self.resolve_statuses(work_packages) |
|
|
|
|
status_ids = work_packages.map(&:status_id).uniq.compact |
|
|
|
|
statuses = Hash[Status.find(status_ids).map{|status| [status.id,status]}] |
|
|
|
|
end |
|
|
|
|
def self.resolve_types(work_packages) |
|
|
|
|
type_ids = work_packages.map(&:type_id).uniq.compact |
|
|
|
|
types = Hash[Type.find(type_ids).map { |type| [type.id, type] }] |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def self.resolve_statuses(work_packages) |
|
|
|
|
status_ids = work_packages.map(&:status_id).uniq.compact |
|
|
|
|
statuses = Hash[Status.find(status_ids).map { |status| [status.id, status] }] |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|