Optimize the conflicts table and summary models

This commit is contained in:
Petko Bordjukov 2016-10-13 07:24:53 +03:00
parent ea8cb4b077
commit d17bc19b23
2 changed files with 36 additions and 2 deletions

View File

@ -11,8 +11,34 @@ class ConflictsSummary
talk_ids.map do |talk_id| talk_ids.map do |talk_id|
{ {
talk_id: talk_id, talk_id: talk_id,
conflicts: ConflictsTable.new(talk_id: talk_id, other_talks_ids: talk_ids.reject { |id| id == talk_id }).conflicts conflicts: conflicts_table[talk_id]
} }
end end
end end
private
def conflicts_table
@conflicts_table ||= blank_conflicts_table.merge(database_conflicts_table)
end
def database_conflicts_table
Conflicts.where(left: talk_ids, right: talk_ids).group_by(&:left).map do |left, conflicts|
conflicts_row = blank_conflicts_row(talk_ids_without(left))
conflicts_row.merge! conflicts.map { |right_conflicts| [right_conflicts.right, right_conflicts.conflicts] }.to_h
[left, conflicts_row]
end.to_h
end
def talk_ids_without(talk_id)
talk_ids.reject { |id| id == talk_id }
end
def blank_conflicts_row(other_talk_ids)
other_talk_ids.map { |talk_id| [talk_id, 0] }.to_h
end
def blank_conflicts_table
talk_ids.map { |talk_id| [talk_id, blank_conflicts_row(talk_ids_without(talk_id))] }.to_h
end
end end

View File

@ -11,8 +11,16 @@ class ConflictsTable
other_talks_ids.map do |right| other_talks_ids.map do |right|
{ {
talk_id: right, talk_id: right,
number_of_conflicts: Conflicts.new(left: talk_id, right: right).conflicts number_of_conflicts: conflicts_hash[right] || 0
} }
end end
end end
private
def conflicts_hash
@conflicts_hash ||= Conflicts.where(left: talk_id, right: other_talks_ids).map do |conflicts|
[conflicts.right, conflicts.conflicts]
end.to_h
end
end end