Skip to content

Commit

Permalink
Merge pull request #280 from lsa-mis/skylight-analyze-queries
Browse files Browse the repository at this point in the history
Skylight analyze queries
  • Loading branch information
britaumich authored Nov 21, 2024
2 parents e7b09b6 + f4b826b commit e597b75
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 50 deletions.
4 changes: 2 additions & 2 deletions app/controllers/rover_navigations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ class RoverNavigationsController < ApplicationController

def zones
authorize :rover_navigation, :zones?
@zones = Zone.includes(:buildings).order(:name)
@zones = Zone.all.order(:name)
end

def buildings
authorize :rover_navigation, :buildings?

@zone = Zone.find(params[:zone_id])
@buildings = @zone.buildings.includes(:floors).order(:name)
@buildings = @zone.buildings.order(:name)

if params[:search].present?
@buildings = @buildings.where("name ILIKE :search OR address ILIKE :search", search: "%#{params[:search]}%")
Expand Down
37 changes: 19 additions & 18 deletions app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,44 @@ def welcome_rovers

def dashboard
authorize :static_page
@room_update_log = RoomUpdateLog.last

@selected_date = params[:dashboard_date].present? ? Date.parse(params[:dashboard_date]) : Date.today

last_room_states = RoomState.all.order(updated_at: :desc).limit(5)
@last_checked_rooms = []
@last_checked_rooms = last_room_states.map { |room_state| [Room.find(room_state.room_id), room_state.updated_at] }

@zones = Zone.all.order(:name)

active_rooms = Room.active_in_zones

@room_access_data = {
"Not accessed 3 times": rooms_not_accessed_for_number_of_times(3).count,
"Not accessed 5 times": rooms_not_accessed_for_number_of_times(5).count,
"Not accessed 7 times": rooms_not_accessed_for_number_of_times(7).count
"Not accessed 3 times": rooms_not_accessed_for_number_of_times(active_rooms, 3).count,
"Not accessed 5 times": rooms_not_accessed_for_number_of_times(active_rooms, 5).count,
"Not accessed 7 times": rooms_not_accessed_for_number_of_times(active_rooms, 7).count
}

@rooms_not_checked_in_3_days = Room.active.where('DATE(last_time_checked) < ?', 3.days.ago.to_date)
@rooms_not_checked_4_to_7_days = Room.active.where('DATE(last_time_checked) >= ? AND DATE(last_time_checked) < ?', 7.days.ago.to_date, 3.days.ago.to_date)
@rooms_not_checked_7_plus_days = Room.active.where('DATE(last_time_checked) < ?', 7.days.ago.to_date)
@rooms_never_checked = Room.active.where(last_time_checked: nil)
@rooms_not_checked_in_3_days = active_rooms.where('DATE(last_time_checked) < ?', 3.days.ago.to_date)
@rooms_not_checked_4_to_7_days = active_rooms.where('DATE(last_time_checked) >= ? AND DATE(last_time_checked) < ?', 7.days.ago.to_date, 3.days.ago.to_date)
@rooms_not_checked_7_plus_days = active_rooms.where('DATE(last_time_checked) < ?', 7.days.ago.to_date)
@rooms_never_checked = active_rooms.where(last_time_checked: nil)

@room_check_in_data = {
"Not checked for 3 days" => @rooms_not_checked_in_3_days.count,
"Not checked for 4 to 7 days" => @rooms_not_checked_4_to_7_days.count,
"Not checked for over 7 days" => @rooms_not_checked_7_plus_days.count,
"Never checked" => @rooms_never_checked.count
}

last_room_states = RoomState.all.order(updated_at: :desc).limit(5)
@last_checked_rooms = last_room_states.map { |room_state| [Room.find(room_state.room_id), room_state.updated_at] }

@latest_room_tickets = RoomTicket.latest

@room_update_log = RoomUpdateLog.last
@room_update_log_status = @room_update_log.status

end

private

def rooms_not_accessed_for_number_of_times(number)
def rooms_not_accessed_for_number_of_times(active_rooms, number)
result_rooms = []
rooms = Room.active.joins(floor: :building)
.where.not(buildings: { zone_id: nil })
rooms.each do |room|
active_rooms.each do |room|
states = room.room_states.order('updated_at DESC').limit(number).pluck(:is_accessed)
if states.length == number && (states.all? false)
result_rooms << room
Expand Down
23 changes: 0 additions & 23 deletions app/helpers/dashboard_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,6 @@ def completion_percentage(checked, total)
((checked.to_f / total) * 100).round
end

# getter for the number of latest tickets to be displayed on the dashboard, or makes it 5 by default
def recent_tickets_quantity
value = AppPreference.find_by(name: "tdx_tickets_quantity_on_dashboard")&.value
value.presence&.to_i || 5
end


def latest_room_tickets
RoomTicket.includes(room: { floor: :building })
.order(created_at: :desc)
.limit(recent_tickets_quantity)
end

def rooms_checked_for_date(zone, date)
date = Date.parse(date) if date.is_a? String
RoomState.joins(room: { floor: { building: :zone } } )
Expand All @@ -49,14 +36,4 @@ def rooms_checked_for_date(zone, date)
.count

end

def total_rooms(zone)
Room.active.joins(floor: { building: :zone })
.where(zones: { id: zone.id })
.count
end

def room_update_log_status(log_note)
log_note.split("|").first.strip
end
end
1 change: 1 addition & 0 deletions app/models/room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Room < ApplicationRecord
accepts_nested_attributes_for :specific_attributes

scope :active, -> { where(archived: false).order(:room_number) }
scope :active_in_zones, -> { active.joins(floor: :building).where.not(buildings: { zone_id: nil }) }
scope :archived, -> { where(archived: true).order(:room_number) }

def full_name
Expand Down
4 changes: 4 additions & 0 deletions app/models/room_ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ class RoomTicket < ApplicationRecord
has_rich_text :description

validates :description, :submitted_by, :tdx_email, presence: true

scope :latest, -> { all.order(created_at: :desc)
.limit(AppPreference.find_by(name: "tdx_tickets_quantity_on_dashboard")&.value.presence&.to_i || 5) }

end
4 changes: 4 additions & 0 deletions app/models/room_update_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
# updated_at :datetime not null
#
class RoomUpdateLog < ApplicationRecord

def status
self.note.split("|").first.strip
end
end
6 changes: 6 additions & 0 deletions app/models/zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ class Zone < ApplicationRecord
validates :name, presence: true
has_many :buildings, -> { active }, class_name: 'Building'

def total_rooms
Room.active.joins(floor: { building: :zone })
.where(zones: { id: self.id })
.count
end

end
8 changes: 4 additions & 4 deletions app/views/static_pages/_last_room_update_log.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
<div class="card-text mb-4">
Status:
<span
class="<%= room_update_log_status(last_room_update_log.note) == 'error' ? 'text-danger' : 'text-success' %>">
class="<%= @room_update_log_status == 'error' ? 'text-danger' : 'text-success' %>">
<%= last_room_update_log.note.include?('success') ? 'SUCCESS' : 'ERROR' %>
</span>
</div>

<div class="card-text mb-4">
Note:
<% if room_update_log_status(last_room_update_log.note) == 'success' %>
<% if @room_update_log_status == 'success' %>
Resources updated successfully for rooms.
<% elsif room_update_log_status(last_room_update_log.note) == 'success-partial' %>
<% elsif @room_update_log_status == 'success-partial' %>
Resources updated successfully for rooms.
<a class="text-primary" data-bs-toggle="collapse" href="#nonExistingRooms" role="button" aria-expanded="false" aria-controls="nonExistingRooms">
These rooms
Expand All @@ -28,7 +28,7 @@
<%= last_room_update_log.note.split(':').last.strip %>
</div>
</div>
<% elsif room_update_log_status(last_room_update_log.note) == 'error' %>
<% elsif @room_update_log_status == 'error' %>
An error occurred while updating resources for rooms. Please report an issue.
<% end %>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/views/static_pages/dashboard.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</div>
<div class="card-body">
<div class="progress mb-3">
<% total = total_rooms(zone) %>
<% total = zone.total_rooms %>
<% checked_for_date = rooms_checked_for_date(zone, @selected_date) %>
<% percentage = completion_percentage(checked_for_date, total) %>
<div class="progress-bar <%= room_check_progress_class(checked_for_date, total) %> text-white" role="progressbar"
Expand Down Expand Up @@ -100,7 +100,7 @@

<h2>Recent Room Issues</h2>
<div class="row m-2">
<% latest_room_tickets.each do |ticket| %>
<% @latest_room_tickets.each do |ticket| %>
<div class="mb-2">
<div class="card">
<div class="card-header">
Expand Down
2 changes: 1 addition & 1 deletion app/views/zones/_zone.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</div>
<div class="card-body">
<p><%= zone.buildings.count %> buildings</p>
<p><%= total_rooms(zone) %> rooms</p>
<p><%= zone.total_rooms %> rooms</p>
</div>
<div class="d-flex flex-row justify-content-between align-items-center mb-2 m-2">
<%= link_to "Zone Details", zone_buildings_path(zone), class: "link_to" %>
Expand Down

0 comments on commit e597b75

Please sign in to comment.