From 8e7fde52938ad41943263a15a2e27778e2539940 Mon Sep 17 00:00:00 2001 From: Margarita Barvinok Date: Thu, 21 Nov 2024 11:24:33 -0500 Subject: [PATCH 1/4] move some code into models --- app/models/room.rb | 1 + app/models/room_ticket.rb | 4 ++++ app/models/room_update_log.rb | 4 ++++ app/models/zone.rb | 6 ++++++ 4 files changed, 15 insertions(+) diff --git a/app/models/room.rb b/app/models/room.rb index 9a5569a4..039e590f 100644 --- a/app/models/room.rb +++ b/app/models/room.rb @@ -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 diff --git a/app/models/room_ticket.rb b/app/models/room_ticket.rb index 54c38edf..66784640 100644 --- a/app/models/room_ticket.rb +++ b/app/models/room_ticket.rb @@ -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 diff --git a/app/models/room_update_log.rb b/app/models/room_update_log.rb index a8472e88..90f7df49 100644 --- a/app/models/room_update_log.rb +++ b/app/models/room_update_log.rb @@ -9,4 +9,8 @@ # updated_at :datetime not null # class RoomUpdateLog < ApplicationRecord + + def status + self.note.split("|").first.strip + end end diff --git a/app/models/zone.rb b/app/models/zone.rb index f7c9253c..013115d3 100644 --- a/app/models/zone.rb +++ b/app/models/zone.rb @@ -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 From 415d855deec1b9879ca0e9caa5861ed6f1efa065 Mon Sep 17 00:00:00 2001 From: Margarita Barvinok Date: Thu, 21 Nov 2024 11:25:39 -0500 Subject: [PATCH 2/4] remove unneeded querie --- app/controllers/rover_navigations_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/rover_navigations_controller.rb b/app/controllers/rover_navigations_controller.rb index 2a044d70..318b6dbc 100644 --- a/app/controllers/rover_navigations_controller.rb +++ b/app/controllers/rover_navigations_controller.rb @@ -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]}%") From 8ef5f080d470c6d2d88b8b457b91ba448f3c3210 Mon Sep 17 00:00:00 2001 From: Margarita Barvinok Date: Thu, 21 Nov 2024 11:26:44 -0500 Subject: [PATCH 3/4] refactor dashboard --- app/controllers/static_pages_controller.rb | 36 +++++++++---------- app/helpers/dashboard_helper.rb | 23 ------------ .../_last_room_update_log.html.erb | 8 ++--- app/views/static_pages/dashboard.html.erb | 4 +-- app/views/zones/_zone.html.erb | 2 +- 5 files changed, 25 insertions(+), 48 deletions(-) diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index 14cd7519..d2ef18bd 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -15,26 +15,21 @@ 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, @@ -42,16 +37,21 @@ def dashboard "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 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 diff --git a/app/helpers/dashboard_helper.rb b/app/helpers/dashboard_helper.rb index dcc9b30d..08a7762e 100644 --- a/app/helpers/dashboard_helper.rb +++ b/app/helpers/dashboard_helper.rb @@ -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 } } ) @@ -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 diff --git a/app/views/static_pages/_last_room_update_log.html.erb b/app/views/static_pages/_last_room_update_log.html.erb index 58f7fd2c..cdfe76d5 100644 --- a/app/views/static_pages/_last_room_update_log.html.erb +++ b/app/views/static_pages/_last_room_update_log.html.erb @@ -8,16 +8,16 @@
Status: + class="<%= @room_update_log.status == 'error' ? 'text-danger' : 'text-success' %>"> <%= last_room_update_log.note.include?('success') ? 'SUCCESS' : 'ERROR' %>
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.