From 26aeb2fddd862a286e192bda55dd4766e62309d9 Mon Sep 17 00:00:00 2001 From: Mohammad Date: Tue, 9 Jul 2024 10:36:50 -0400 Subject: [PATCH 1/6] add archive migration --- app/models/resource.rb | 1 + db/migrate/20240709143456_add_archive_field_to_resource.rb | 5 +++++ db/schema.rb | 4 ++-- spec/factories/resources.rb | 1 + spec/models/resource_spec.rb | 1 + 5 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20240709143456_add_archive_field_to_resource.rb diff --git a/app/models/resource.rb b/app/models/resource.rb index 6b3757f5..1074558d 100644 --- a/app/models/resource.rb +++ b/app/models/resource.rb @@ -8,6 +8,7 @@ # room_id :bigint not null # created_at :datetime not null # updated_at :datetime not null +# archived :boolean default(FALSE) # class Resource < ApplicationRecord belongs_to :room diff --git a/db/migrate/20240709143456_add_archive_field_to_resource.rb b/db/migrate/20240709143456_add_archive_field_to_resource.rb new file mode 100644 index 00000000..3725b8ae --- /dev/null +++ b/db/migrate/20240709143456_add_archive_field_to_resource.rb @@ -0,0 +1,5 @@ +class AddArchiveFieldToResource < ActiveRecord::Migration[7.1] + def change + add_column :resources, :archived, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index c1bfbf1b..2cd81c10 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_07_03_185321) do +ActiveRecord::Schema[7.1].define(version: 2024_07_09_143456) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -99,7 +99,6 @@ t.boolean "need_quantity_box" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.boolean "archived", default: false end create_table "floors", force: :cascade do |t| @@ -135,6 +134,7 @@ t.bigint "room_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "archived", default: false t.index ["room_id"], name: "index_resources_on_room_id" end diff --git a/spec/factories/resources.rb b/spec/factories/resources.rb index e6510a58..3b7d393d 100644 --- a/spec/factories/resources.rb +++ b/spec/factories/resources.rb @@ -8,6 +8,7 @@ # room_id :bigint not null # created_at :datetime not null # updated_at :datetime not null +# archived :boolean default(FALSE) # FactoryBot.define do factory :resource do diff --git a/spec/models/resource_spec.rb b/spec/models/resource_spec.rb index 1d10a76e..e42b20b0 100644 --- a/spec/models/resource_spec.rb +++ b/spec/models/resource_spec.rb @@ -8,6 +8,7 @@ # room_id :bigint not null # created_at :datetime not null # updated_at :datetime not null +# archived :boolean default(FALSE) # require 'rails_helper' From 6502cf3f09792dc31028dd476bfbf50da02a7502 Mon Sep 17 00:00:00 2001 From: Mohammad Date: Tue, 9 Jul 2024 10:46:50 -0400 Subject: [PATCH 2/6] define scopes & relations --- app/models/resource.rb | 3 +++ app/models/room.rb | 2 ++ 2 files changed, 5 insertions(+) diff --git a/app/models/resource.rb b/app/models/resource.rb index 1074558d..4d8baa36 100644 --- a/app/models/resource.rb +++ b/app/models/resource.rb @@ -18,6 +18,9 @@ class Resource < ApplicationRecord validates :resource_type, presence: true validates :room_id, presence: true + scope :active, -> { where(archived: false) } + scope :archived, -> { where(archived: true) } + def display_name "#{self.name} - #{self.resource_type}" end diff --git a/app/models/room.rb b/app/models/room.rb index 529c05fa..539ac70d 100644 --- a/app/models/room.rb +++ b/app/models/room.rb @@ -19,6 +19,8 @@ class Room < ApplicationRecord has_many :specific_attributes has_many :active_specific_attributes, -> { active }, class_name: 'SpecificAttribute' has_many :archived_specific_attributes, -> { archived }, class_name: 'SpecificAttribute' + has_many :active_resources, -> { active }, class_name: 'Resource' + has_many :archived_resources, -> { archived }, class_name: 'Resource' has_many :room_states has_many :notes From 0141c9da2d227347e4c57380968472d0b12642fa Mon Sep 17 00:00:00 2001 From: Mohammad Date: Tue, 9 Jul 2024 13:24:51 -0400 Subject: [PATCH 3/6] updated update_resources.rake to archive/delete --- db/schema.rb | 2 ++ lib/tasks/update_resources.rake | 32 ++++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index 2cd81c10..1188f98b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -99,6 +99,7 @@ t.boolean "need_quantity_box" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "archived", default: false end create_table "floors", force: :cascade do |t| @@ -204,6 +205,7 @@ t.bigint "room_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "archived", default: false t.index ["room_id"], name: "index_specific_attributes_on_room_id" end diff --git a/lib/tasks/update_resources.rake b/lib/tasks/update_resources.rake index 8c0d8a3c..a1a55446 100644 --- a/lib/tasks/update_resources.rake +++ b/lib/tasks/update_resources.rake @@ -80,24 +80,36 @@ def update_resources_in_db(resources, room_location, type_names, rooms_to_update room_rmrecnbr = room_location[oid] room = Room.find_by(rmrecnbr: room_rmrecnbr) if room.present? - # hash of resources for the room that exist in db {name => id} - resources_in_db = room.resources.pluck(:name, :id).to_h + # hashes of resources for the room that exist in db {name => id} + active_resources_in_db = room.active_resources.pluck(:name, :id).to_h + archived_resources_in_db = room.archived_resources.pluck(:name, :id).to_h room_resources.each do |resource_name, resource_type| - if resources_in_db.key?(resource_name) + next unless type_names.include?(resource_type) + + if active_resources_in_db.key?(resource_name) # wco resource exist in db, delete from array - resources_in_db.delete(resource_name) + active_resources_in_db.delete(resource_name) + elsif archived_resources_in_db.key?(resource_name) + # wco resource is archived in db, so unarchive it + id = archived_resources_in_db[resource_name] + room.resources.find(id).update(archived: false) else # create a wco resource that was not present in db - if type_names.include?(resource_type) - room.resources.create(name: resource_name, resource_type: resource_type) - end + room.resources.create(name: resource_name, resource_type: resource_type) end end - if resources_in_db.present? - # these resoursces are not present in sco any more - delete from db - Resource.where(id: resources_in_db.values).delete_all + + active_resources_in_db.each do |_, id| + # these resoursces are not present in sco any more, so archive or delete appropriately + resource = room.resources.find(id) + if resource.resource_states.any? + resource.update(archived: true) + else + resource.destroy + end end + # room updated by wco - delete from list rooms_to_update.delete(room_rmrecnbr) end From b738a769d6f18051e2e59c38eeb37ffbc857373f Mon Sep 17 00:00:00 2001 From: Mohammad Date: Tue, 9 Jul 2024 13:38:20 -0400 Subject: [PATCH 4/6] filtered by .active in different places --- app/controllers/application_controller.rb | 2 +- app/controllers/resource_states_controller.rb | 2 +- app/models/room_status.rb | 2 +- app/views/rooms/show.html.erb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d61f9fdf..055710c1 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -81,7 +81,7 @@ def redirect_rover_to_correct_state(room:, room_state:, step:, mode: "new") CommonAttribute.active.present? ? redirect["common_attributes"] = true : redirect["common_attributes"] = false room.active_specific_attributes.present? ? redirect["specific_attributes"] = true : redirect["specific_attributes"] = false - room.resources.present? ? redirect["resources"] = true : redirect["resources"] = false + room.active_resources.present? ? redirect["resources"] = true : redirect["resources"] = false new_path_to_redirect = { "common_attributes" => new_common_attribute_state_path(room_state_id: room_state.id), diff --git a/app/controllers/resource_states_controller.rb b/app/controllers/resource_states_controller.rb index 903c62b1..6a352c94 100644 --- a/app/controllers/resource_states_controller.rb +++ b/app/controllers/resource_states_controller.rb @@ -6,7 +6,7 @@ class ResourceStatesController < ApplicationController def new authorize ResourceState - @resource_states = @room.resources.all.map do |resource| + @resource_states = @room.active_resources.all.map do |resource| resource.resource_states.new end end diff --git a/app/models/room_status.rb b/app/models/room_status.rb index 3a8b06c5..79d6d132 100644 --- a/app/models/room_status.rb +++ b/app/models/room_status.rb @@ -15,7 +15,7 @@ def specific_attributes_exist? end def resources_exist? - @room.resources.count > 0 + @room.active_resources.count > 0 end def status_weight diff --git a/app/views/rooms/show.html.erb b/app/views/rooms/show.html.erb index 8292c316..e0e04fed 100644 --- a/app/views/rooms/show.html.erb +++ b/app/views/rooms/show.html.erb @@ -32,7 +32,7 @@

Resources

- <%= render 'resources_list', resources: @room.resources %> + <%= render 'resources_list', resources: @room.active_resources %> <% if is_admin? %> From 95cfb89c594ea85de556fc6c59286a7d3c9516c9 Mon Sep 17 00:00:00 2001 From: Mohammad Date: Tue, 9 Jul 2024 13:56:13 -0400 Subject: [PATCH 5/6] updated reports with active items for rooms, buildings, common_attributes, specific_attributes & resources --- app/controllers/reports_controller.rb | 103 ++++++++++++++------------ 1 file changed, 56 insertions(+), 47 deletions(-) diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index 0ab86482..0623e909 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -38,13 +38,14 @@ def room_issues_report if params[:commit] zone_id, building_id, start_time, end_time = collect_form_params - rooms = Room.joins(floor: :building).joins(:room_tickets) - .where(buildings: { id: building_id, zone_id: zone_id }) - .where(room_tickets: { created_at: start_time..end_time }) - .group('rooms.id') - .select('rooms.*, COUNT(room_tickets.id) AS tickets_count') - .having('COUNT(room_tickets.id) > 0') - .order('tickets_count DESC') + rooms = Room.active + .joins(floor: :building).joins(:room_tickets) + .where(buildings: { id: building_id, zone_id: zone_id, archived: false }) + .where(room_tickets: { created_at: start_time..end_time }) + .group('rooms.id') + .select('rooms.*, COUNT(room_tickets.id) AS tickets_count') + .having('COUNT(room_tickets.id) > 0') + .order('tickets_count DESC') if rooms.any? @title = 'Room Issues Report' @@ -75,22 +76,24 @@ def inspection_rate_report if params[:commit] zone_id, building_id, start_time, end_time = collect_form_params - rooms = Room.joins(floor: { building: :zone }).joins(:room_states) - .where(buildings: { id: building_id, zone_id: zone_id }) - .where(room_states: { updated_at: start_time..end_time }) - .group('rooms.id') - .select('rooms.*') - .select('COUNT(room_states.id) AS room_check_count') - .order('room_check_count DESC') - - rooms_no_room_state = Room.left_outer_joins(:room_states) - .joins(floor: { building: :zone }) - .where(buildings: { id: building_id, zone_id: zone_id }) - .where(room_states: { id: nil }) - .where.not(id: rooms.map(&:id)) - .select('rooms.*') - .select('0 AS room_check_count') - .order('rooms.room_number') + rooms = Room.active + .joins(floor: { building: :zone }).joins(:room_states) + .where(buildings: { id: building_id, zone_id: zone_id, archived: false }) + .where(room_states: { updated_at: start_time..end_time }) + .group('rooms.id') + .select('rooms.*') + .select('COUNT(room_states.id) AS room_check_count') + .order('room_check_count DESC') + + rooms_no_room_state = Room.active + .left_outer_joins(:room_states) + .joins(floor: { building: :zone }) + .where(buildings: { id: building_id, zone_id: zone_id, archived: false }) + .where(room_states: { id: nil }) + .where.not(id: rooms.map(&:id)) + .select('rooms.*') + .select('0 AS room_check_count') + .order('rooms.room_number') if rooms.any? oldest_record = rooms.min_by { |room| room.room_states.first.updated_at } @@ -130,17 +133,18 @@ def no_access_report if params[:commit] zone_id, building_id, start_time, end_time = collect_form_params - rooms = Room.joins(floor: :building).joins(:room_states) - .where(buildings: { id: building_id, zone_id: zone_id }) - .where(room_states: { updated_at: start_time..end_time }) - .where(room_states: { is_accessed: false }) - .group('rooms.id') - .select('rooms.*') - .select('COUNT(room_states.id) AS na_states_count') - .select('array_agg(room_states.updated_at) as na_states_dates') - .select('array_agg(room_states.no_access_reason) as na_states_reasons') - .having('COUNT(room_states.id) > 0') - .order('na_states_count DESC') + rooms = Room.active + .joins(floor: :building).joins(:room_states) + .where(buildings: { id: building_id, zone_id: zone_id, archived: false }) + .where(room_states: { updated_at: start_time..end_time }) + .where(room_states: { is_accessed: false }) + .group('rooms.id') + .select('rooms.*') + .select('COUNT(room_states.id) AS na_states_count') + .select('array_agg(room_states.updated_at) as na_states_dates') + .select('array_agg(room_states.no_access_reason) as na_states_reasons') + .having('COUNT(room_states.id) > 0') + .order('na_states_count DESC') if rooms.any? @title = 'No Access Report' @@ -177,16 +181,18 @@ def common_attribute_states_report if params[:commit] zone_id, building_id, start_time, end_time = collect_form_params - rooms = Room.joins(floor: { building: :zone }).joins(room_states: { common_attribute_states: :common_attribute }) - .where(buildings: { id: building_id, zone_id: zone_id }) - .where(room_states: { updated_at: start_time..end_time }) - .select('rooms.*') - .select('room_states.updated_at') - .select('common_attributes.description AS common_attribute_description') - .select('common_attributes.need_checkbox as need_checkbox') - .select('common_attribute_states.checkbox_value as checkbox_value') - .select('common_attribute_states.quantity_box_value as quantity_box_value') - .order('zones.name ASC, buildings.name ASC, rooms.room_number ASC') + rooms = Room.active + .joins(floor: { building: :zone }).joins(room_states: { common_attribute_states: :common_attribute }) + .where(buildings: { id: building_id, zone_id: zone_id, archived: false }) + .where(room_states: { updated_at: start_time..end_time }) + .select('rooms.*') + .select('room_states.updated_at') + .select('common_attributes.description AS common_attribute_description') + .select('common_attributes.need_checkbox as need_checkbox') + .select('common_attribute_states.checkbox_value as checkbox_value') + .select('common_attribute_states.quantity_box_value as quantity_box_value') + .where('common_attributes.archived = FALSE') + .order('zones.name ASC, buildings.name ASC, rooms.room_number ASC') if rooms.any? @grouped = true @@ -223,8 +229,9 @@ def specific_attribute_states_report if params[:commit] zone_id, building_id, start_time, end_time = collect_form_params - rooms = Room.joins(floor: { building: :zone }).joins(room_states: { specific_attribute_states: :specific_attribute }) - .where(buildings: { id: building_id, zone_id: zone_id }) + rooms = Room.active + .joins(floor: { building: :zone }).joins(room_states: { specific_attribute_states: :specific_attribute }) + .where(buildings: { id: building_id, zone_id: zone_id, archived: false }) .where(room_states: { updated_at: start_time..end_time }) .select('rooms.*') .select('specific_attributes.description AS specific_attribute_description') @@ -232,6 +239,7 @@ def specific_attribute_states_report .select('specific_attributes.need_checkbox as need_checkbox') .select('specific_attribute_states.checkbox_value as checkbox_value') .select('specific_attribute_states.quantity_box_value as quantity_box_value') + .where('specific_attributes.archived = FALSE') .order('zones.name ASC, buildings.name ASC, rooms.room_number ASC, specific_attributes.description ASC') if rooms.any? @@ -280,6 +288,7 @@ def resource_states_report .select("resources.resource_type as resource_type") .select('room_states.updated_at') .select('resource_states.is_checked as check_value') + .where('resources.archived = FALSE') .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') @@ -316,7 +325,7 @@ def resource_states_report def set_form_values @zones = Zone.all.order(:name).map { |z| [z.name, z.id] } - @buildings = Building.where.not(zone: nil).map { |building| [building.zone_id, building.name, building.id] } + @buildings = Building.active.where.not(zone: nil).map { |building| [building.zone_id, building.name, building.id] } end def collect_form_params From c0d477850e8ae7cbf7b255bc613dfc040af40cdf Mon Sep 17 00:00:00 2001 From: Mohammad Date: Tue, 9 Jul 2024 14:46:19 -0400 Subject: [PATCH 6/6] script only runs for active rooms, removed redundant archived filter for buildings --- app/controllers/reports_controller.rb | 12 ++++++------ lib/tasks/update_resources.rake | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index 0623e909..dd81ffb9 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -40,7 +40,7 @@ def room_issues_report rooms = Room.active .joins(floor: :building).joins(:room_tickets) - .where(buildings: { id: building_id, zone_id: zone_id, archived: false }) + .where(buildings: { id: building_id, zone_id: zone_id }) .where(room_tickets: { created_at: start_time..end_time }) .group('rooms.id') .select('rooms.*, COUNT(room_tickets.id) AS tickets_count') @@ -78,7 +78,7 @@ def inspection_rate_report rooms = Room.active .joins(floor: { building: :zone }).joins(:room_states) - .where(buildings: { id: building_id, zone_id: zone_id, archived: false }) + .where(buildings: { id: building_id, zone_id: zone_id }) .where(room_states: { updated_at: start_time..end_time }) .group('rooms.id') .select('rooms.*') @@ -88,7 +88,7 @@ def inspection_rate_report rooms_no_room_state = Room.active .left_outer_joins(:room_states) .joins(floor: { building: :zone }) - .where(buildings: { id: building_id, zone_id: zone_id, archived: false }) + .where(buildings: { id: building_id, zone_id: zone_id }) .where(room_states: { id: nil }) .where.not(id: rooms.map(&:id)) .select('rooms.*') @@ -135,7 +135,7 @@ def no_access_report rooms = Room.active .joins(floor: :building).joins(:room_states) - .where(buildings: { id: building_id, zone_id: zone_id, archived: false }) + .where(buildings: { id: building_id, zone_id: zone_id }) .where(room_states: { updated_at: start_time..end_time }) .where(room_states: { is_accessed: false }) .group('rooms.id') @@ -183,7 +183,7 @@ def common_attribute_states_report rooms = Room.active .joins(floor: { building: :zone }).joins(room_states: { common_attribute_states: :common_attribute }) - .where(buildings: { id: building_id, zone_id: zone_id, archived: false }) + .where(buildings: { id: building_id, zone_id: zone_id }) .where(room_states: { updated_at: start_time..end_time }) .select('rooms.*') .select('room_states.updated_at') @@ -231,7 +231,7 @@ def specific_attribute_states_report rooms = Room.active .joins(floor: { building: :zone }).joins(room_states: { specific_attribute_states: :specific_attribute }) - .where(buildings: { id: building_id, zone_id: zone_id, archived: false }) + .where(buildings: { id: building_id, zone_id: zone_id }) .where(room_states: { updated_at: start_time..end_time }) .select('rooms.*') .select('specific_attributes.description AS specific_attribute_description') diff --git a/lib/tasks/update_resources.rake b/lib/tasks/update_resources.rake index a1a55446..d8df1d85 100644 --- a/lib/tasks/update_resources.rake +++ b/lib/tasks/update_resources.rake @@ -11,7 +11,7 @@ task update_resources: :environment do end begin - rooms = Room.all.pluck(:rmrecnbr) + rooms = Room.active.pluck(:rmrecnbr) #============================= # get oids from api # @@ -41,7 +41,7 @@ task update_resources: :environment do #============================= # update the database # #============================= - rooms_to_update = Room.all.pluck(:rmrecnbr) + rooms_to_update = Room.active.pluck(:rmrecnbr) # convert wco_resources to hash of arrays: array of resources for every room resources = wco_resources