Skip to content

Commit

Permalink
Merge pull request #126 from lsa-mis/LRA-963-room-ready-resource-stat…
Browse files Browse the repository at this point in the history
…es-report

Lra 963 room ready resource states report
  • Loading branch information
britaumich authored Jul 3, 2024
2 parents 62b80aa + aa4b004 commit 0b228a4
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
53 changes: 53 additions & 0 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def index
{title: "Inspection Rate", url: inspection_rate_report_reports_path },
{title: "No Access", url: no_access_report_reports_path },
{title: "Common Attribute States", url: common_attribute_states_report_reports_path },
{title: "Resource States", url: resource_states_report_reports_path },
]
end

Expand Down Expand Up @@ -228,6 +229,58 @@ def common_attribute_states_report
end
end

def resource_states_report
authorize :report, :resource_states_report?

@zones = Zone.all.order(:name).map { |z| [z.name, z.id] }
@resource_types = AppPreference.find_by(name: "resource_types").value.split(",").each(&:strip!)

if params[:commit]
zone_id = params[:zone_id].present? ? params[:zone_id] : Zone.all.pluck(:id).push(nil)
start_time = params[:from].present? ? Date.parse(params[:from]).beginning_of_day : Date.new(0)
end_time = params[:to].present? ? Date.parse(params[:to]).end_of_day : Date::Infinity.new
resource_type = params[:resource_type].presence

rooms = Room.joins(floor: { building: :zone }).joins(room_states: { resource_states: :resource })
.where(buildings: { zone_id: zone_id })
.where(room_states: { updated_at: start_time..end_time })
.select('rooms.*')
.select('resources.name AS resource_name')
.select("resources.resource_type as resource_type")
.select('room_states.updated_at')
.select('resource_states.is_checked as check_value')
.where("resources.resource_type ILIKE ?", "%#{resource_type}%") # need to do a manual query for this because of circular definition of resources
.order('zones.name ASC, buildings.name ASC, rooms.room_number ASC, resources.name ASC')

grouped_rooms = rooms.group_by { |room| "#{room.floor.building.zone.name} | #{room.floor.building.name} | #{room.room_number}" }

@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.to_date : start_time.to_date
header_end = end_time == Date::Infinity.new ? latest_date.to_date : end_time.to_date
@date_headers = (header_start..header_end).to_a

@headers = ['Resource'] + @date_headers

@data = grouped_rooms.transform_values do |rooms|
rooms.each_with_object(Hash.new { |hash, key| hash[key] = {} }) do |room, pivot_table|
key = ["#{room.resource_name} (#{room.resource_type})"]
value = room.check_value ? 'Yes' : 'No'
pivot_table[key][room.updated_at.to_date] = value
end
end
end

respond_to do |format|
format.html
format.csv { send_data csv_data, filename: 'resource_states_report.csv', type: 'text/csv' }
end
end

private

def csv_data
Expand Down
4 changes: 4 additions & 0 deletions app/policies/report_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ def no_access_report?
def common_attribute_states_report?
is_admin?
end

def resource_states_report?
is_admin?
end
end
32 changes: 32 additions & 0 deletions app/views/reports/resource_states_report.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<h1>Resource States Report</h1>
<h6>This report shows the repsonses to the Resource Questions in the Rover form.</h6>

<div class="py-3">
<!-- turning off turbo for the form is necessary - otherwise the browser doesn't download the csv properly -->
<%= form_with url: resource_states_report_reports_path, method: :get, data: { turbo: false }, class: "mb-3" do |form| %>
<div>
<label for="zone_id" class="">Select a Zone</label>
<%= form.select :zone_id, options_for_select(@zones, selected: params[:zone_id]), {include_blank: "All Zones"}, class: "form-select" %>
<div class="d-flex flex-row justify-content-start align-items-center gap-3">
<%= form.label :from, "From: "%>
<%= form.date_field :from, value: params[:from], class: "form-control" %>
<%= form.label :to, "To: "%>
<%= form.date_field :to, value: params[:to], class: "form-control" %>
</div>
<%= form.label :resource_type, 'Select a Resource Type', class: 'form-label' %>
<%= form.select :resource_type, options_for_select(@resource_types, selected: params[:resource_type]), {include_blank: "All Resource Types"}, class: "form-select" %>
<div class="d-flex flex-row justify-content-start align-items-center">
<%= form.label :format, "Report Type: ", class: "form-label" %>
<%= form.select :format, options_for_select([["Display in Browser", "html"], ["Export to CSV", "csv"]],
selected: params[:format]), {}, class: "form-select" %>
</div>
</div>
<div class="d-flex flex-row justify-content-start align-items-center gap-3">
<%= form.submit "Run Report" %>
<%= link_to "Clear filters", resource_states_report_reports_path, class: "text-primary cancel-button" %>
</div>
<% end %>
<% if params[:commit] && params[:format] == 'html' %>
<%= render 'reports_table' %>
<% end %>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
get 'inspection_rate_report', to: 'reports#inspection_rate_report'
get 'no_access_report', to: 'reports#no_access_report'
get 'common_attribute_states_report', to: 'reports#common_attribute_states_report'
get 'resource_states_report', to: 'reports#resource_states_report'
end
end

Expand Down

0 comments on commit 0b228a4

Please sign in to comment.