Skip to content

Commit

Permalink
Merge pull request #274 from lsa-mis/improve-reports
Browse files Browse the repository at this point in the history
Improve reports loading time
  • Loading branch information
britaumich authored Nov 16, 2024
2 parents c3d895d + 2611779 commit 65df27c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
35 changes: 13 additions & 22 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ def common_attribute_states_report
.where(buildings: { id: building_id, zone_id: zone_id })
.where(room_states: { updated_at: start_time..end_time })
.select('rooms.*')
.select('buildings.name AS building')
.select('zones.name AS zone')
.select('room_states.updated_at')
.select('common_attributes.description AS common_attribute_description')
.select('common_attributes.need_checkbox as need_checkbox')
Expand All @@ -333,18 +335,13 @@ def common_attribute_states_report
@room_link = true
@title = 'Common Attribute States Report'

earliest_date = rooms.flat_map { |room| room.room_states.pluck(:updated_at) }.min
latest_date = rooms.flat_map { |room| room.room_states.pluck(:updated_at) }.max
header_start = start_time == Date.new(0) ? earliest_date : start_time
header_end = end_time == Date::Infinity.new ? latest_date : end_time

@date_headers = (header_start.to_date..header_end.to_date).to_a
@headers = [ 'Room', 'Building', 'Zone'] + @date_headers
@date_headers = (start_time.to_date..end_time.to_date).to_a
@headers = [ 'Room', 'Building', 'zone'] + @date_headers

grouped_rooms = rooms.group_by { |room| room.common_attribute_description }
@data = grouped_rooms.transform_values do |room_group|
room_group.each_with_object(Hash.new { |hash, key| hash[key] = {} }) do |room, pivot_table|
key = [[room.room_number, room.id], room.floor.building.name, show_zone(room.floor.building)]
key = [[room.room_number, room.id], room.building, room.zone]
value = room.need_checkbox ? (room.checkbox_value ? 'Yes' : 'No') : room.quantity_box_value
pivot_table[key][room.updated_at.to_date] = value
end
Expand All @@ -370,6 +367,8 @@ def specific_attribute_states_report
.where(buildings: { id: building_id, zone_id: zone_id })
.where(room_states: { updated_at: start_time..end_time })
.select('rooms.*')
.select('buildings.name AS building')
.select('zones.name AS zone')
.select('specific_attributes.description AS specific_attribute_description')
.select('room_states.updated_at')
.select('specific_attributes.need_checkbox as need_checkbox')
Expand All @@ -381,15 +380,10 @@ def specific_attribute_states_report
@grouped = true
@title = 'Specific Attribute States Report'

earliest_date = rooms.flat_map { |room| room.room_states.pluck(:updated_at) }.min
latest_date = rooms.flat_map { |room| room.room_states.pluck(:updated_at) }.max
header_start = start_time == Date.new(0) ? earliest_date : start_time
header_end = end_time == Date::Infinity.new ? latest_date : end_time

@date_headers = (header_start.to_date..header_end.to_date).to_a
@date_headers = (start_time.to_date..end_time.to_date).to_a
@headers = ['Specific Attribute'] + @date_headers

grouped_rooms = rooms.group_by { |room| ["#{show_zone(room.floor.building)} | #{room.floor.building.name} |", room] }
grouped_rooms = rooms.group_by { |room| ["#{room.zone} | #{room.building} |", room] }
@group_link = true
@data = grouped_rooms.transform_values do |room_group|
room_group.each_with_object(Hash.new { |hash, key| hash[key] = {} }) do |room, pivot_table|
Expand Down Expand Up @@ -422,6 +416,8 @@ def resource_states_report
.where(buildings: { id: building_id, zone_id: zone_id })
.where(room_states: { updated_at: start_time..end_time })
.select('rooms.*')
.select('buildings.name AS building')
.select('zones.name AS zone')
.select('resources.name AS resource_name')
.select("resources.resource_type as resource_type")
.select('room_states.updated_at')
Expand All @@ -433,15 +429,10 @@ def resource_states_report
@grouped = true
@title = 'Resource States Report'

earliest_date = rooms.flat_map { |room| room.room_states.pluck(:updated_at) }.min
latest_date = rooms.flat_map { |room| room.room_states.pluck(:updated_at) }.max
header_start = start_time == Date.new(0) ? earliest_date : start_time
header_end = end_time == Date::Infinity.new ? latest_date : end_time

@date_headers = (header_start.to_date..header_end.to_date).to_a
@date_headers = (start_time.to_date..end_time.to_date).to_a
@headers = ['Resource'] + @date_headers

grouped_rooms = rooms.group_by { |room| ["#{show_zone(room.floor.building)} | #{room.floor.building.name} |", room] }
grouped_rooms = rooms.group_by { |room| ["#{room.zone} | #{room.building} |", room] }
@group_link = true
@data = grouped_rooms.transform_values do |room_group|
room_group.each_with_object(Hash.new { |hash, key| hash[key] = {} }) do |room, pivot_table|
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20241116051111_add_index_on_room_states.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddIndexOnRoomStates < ActiveRecord::Migration[7.1]
disable_ddl_transaction!
def change
add_index :room_states, :updated_at, algorithm: :concurrently
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 65df27c

Please sign in to comment.