Skip to content

Commit d6ee775

Browse files
authored
Log queries in dev reset endpoint (#3365)
Previously the dev reset endpoint wouldn't show any output and it was hard to understand what was happening. This changes it to output the query being performed and the time it took as a log message to give more feedback. ![image](https://github.com/user-attachments/assets/2430f720-6b62-4fab-b81f-3c8241c116aa)
2 parents 6cb6ff7 + 2f6e554 commit d6ee775

File tree

9 files changed

+235
-211
lines changed

9 files changed

+235
-211
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
module DevConcern
4+
extend ActiveSupport::Concern
5+
6+
included do
7+
skip_before_action :authenticate_user!
8+
skip_before_action :store_user_location!
9+
skip_after_action :verify_policy_scoped
10+
11+
before_action :ensure_dev_env_or_dev_tools_enabled
12+
end
13+
14+
private
15+
16+
def ensure_dev_env_or_dev_tools_enabled
17+
unless Rails.env.local? || Flipper.enabled?(:dev_tools)
18+
raise "Not in development environment"
19+
end
20+
end
21+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
class Dev::RandomConsentFormController < ApplicationController
4+
include DevConcern
5+
6+
def call
7+
Faker::Config.locale = "en-GB"
8+
9+
session =
10+
if params[:slug].present?
11+
Session.includes(programmes: :vaccines).find_by(slug: params[:slug])
12+
else
13+
Session.includes(programmes: :vaccines).find(params[:session_id])
14+
end
15+
16+
attributes =
17+
if ActiveModel::Type::Boolean.new.cast(params[:parent_phone])
18+
{}
19+
else
20+
{ parent_phone: nil }
21+
end
22+
23+
consent_form =
24+
FactoryBot.create(:consent_form, :draft, session:, **attributes)
25+
26+
consent_form.seed_health_questions
27+
consent_form.each_health_answer do |health_answer|
28+
health_answer.response = "no"
29+
end
30+
consent_form.save!
31+
32+
request.session[:consent_form_id] = consent_form.id
33+
redirect_to confirm_parent_interface_consent_form_path(consent_form)
34+
end
35+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
class Dev::ResetController < ApplicationController
4+
include DevConcern
5+
6+
def call
7+
session.delete :user_return_to
8+
Rake::Task.clear
9+
Rails.application.load_tasks
10+
11+
Organisation.with_advisory_lock("reset") do
12+
Rake::Task["db:seed:replant"].invoke
13+
end
14+
15+
redirect_to root_path
16+
end
17+
end
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# frozen_string_literal: true
2+
3+
class Dev::ResetOrganisationController < ApplicationController
4+
include ActionController::Live
5+
include DevConcern
6+
7+
def call
8+
response.headers["Content-Type"] = "text/event-stream"
9+
response.headers["Cache-Control"] = "no-cache"
10+
11+
organisation =
12+
Organisation.find_by!(ods_code: params[:organisation_ods_code])
13+
14+
@start_time = Time.zone.now
15+
16+
Organisation.with_advisory_lock("reset-organisation-#{organisation.id}") do
17+
log_destroy(CohortImport.where(organisation:))
18+
log_destroy(ImmunisationImport.where(organisation:))
19+
20+
sessions = Session.where(organisation:)
21+
22+
log_destroy(ClassImport.where(session: sessions))
23+
log_destroy(SessionDate.where(session: sessions))
24+
25+
log_destroy(ConsentNotification.where(session: sessions))
26+
log_destroy(SessionNotification.where(session: sessions))
27+
log_destroy(VaccinationRecord.where(session: sessions))
28+
29+
patient_sessions = PatientSession.where(session: sessions)
30+
log_destroy(GillickAssessment.where(patient_session: patient_sessions))
31+
log_destroy(PreScreening.where(patient_session: patient_sessions))
32+
patient_sessions.in_batches { log_destroy(it) }
33+
34+
log_destroy(sessions)
35+
36+
patients = organisation.patients
37+
38+
log_destroy(SchoolMove.where(patient: patients))
39+
log_destroy(SchoolMove.where(organisation:))
40+
log_destroy(SchoolMoveLogEntry.where(patient: patients))
41+
log_destroy(AccessLogEntry.where(patient: patients))
42+
log_destroy(NotifyLogEntry.where(patient: patients))
43+
# In local dev we can end up with NotifyLogEntries without a patient
44+
log_destroy(NotifyLogEntry.where(patient_id: nil))
45+
log_destroy(VaccinationRecord.where(patient: patients))
46+
47+
log_destroy(ConsentForm.where(organisation:))
48+
log_destroy(Consent.where(organisation:))
49+
log_destroy(Triage.where(organisation:))
50+
51+
patients.includes(:parents).in_batches { log_destroy(it) }
52+
53+
batches = Batch.where(organisation:)
54+
log_destroy(VaccinationRecord.where(batch: batches))
55+
log_destroy(batches)
56+
57+
log_destroy(
58+
VaccinationRecord.where(performed_ods_code: organisation.ods_code)
59+
)
60+
61+
UnscheduledSessionsFactory.new.call
62+
end
63+
64+
response.stream.write "Done"
65+
rescue StandardError => e
66+
response.stream.write "Error: #{e.message}\n"
67+
ensure
68+
response.stream.close
69+
end
70+
71+
private
72+
73+
def log_destroy(query)
74+
where_clause = query.where_clause
75+
@log_time ||= Time.zone.now
76+
query.destroy_all
77+
response.stream.write(
78+
"#{query.model.name}.where(#{where_clause.to_h}) reset: #{Time.zone.now - @log_time}s\n"
79+
)
80+
@log_time = Time.zone.now
81+
end
82+
end

app/controllers/dev_controller.rb

Lines changed: 0 additions & 106 deletions
This file was deleted.

config/routes.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@
6060
mount flipper_app, at: "/flipper"
6161

6262
unless Rails.env.production?
63-
get "/reset", to: "dev#reset"
63+
get "/reset", to: "dev/reset#call"
6464
get "/reset/:organisation_ods_code",
65-
to: "dev#reset_organisation",
65+
to: "dev/reset_organisation#call",
6666
as: :reset_organisation
67-
get "/random-consent-form", to: "dev#random_consent_form"
67+
get "/random-consent-form(/:slug)", to: "dev/random_consent_form#call"
6868
end
6969

7070
get "/csrf", to: "csrf#new"
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# frozen_string_literal: true
2+
3+
describe Dev::ResetOrganisationController do
4+
before { Flipper.enable(:dev_tools) }
5+
after { Flipper.disable(:dev_tools) }
6+
7+
describe "GET" do
8+
let(:programmes) { [create(:programme, :hpv_all_vaccines)] }
9+
10+
let(:organisation) { create(:organisation, ods_code: "R1L", programmes:) }
11+
12+
let(:cohort_import) do
13+
create(
14+
:cohort_import,
15+
csv: fixture_file_upload("spec/fixtures/cohort_import/valid.csv"),
16+
organisation:
17+
)
18+
end
19+
20+
let(:immunisation_import) do
21+
create(
22+
:immunisation_import,
23+
csv:
24+
fixture_file_upload(
25+
"spec/fixtures/immunisation_import/valid_hpv.csv"
26+
),
27+
organisation:
28+
)
29+
end
30+
31+
before do
32+
programmes.each do |programme|
33+
programme.vaccines.each do |vaccine|
34+
create_list(:batch, 4, organisation:, vaccine:)
35+
end
36+
end
37+
38+
create(:school, urn: "123456", organisation:) # to match cohort_import/valid.csv
39+
create(:school, urn: "110158", organisation:) # to match valid_hpv.csv
40+
41+
cohort_import.process!
42+
immunisation_import.process!
43+
44+
Patient.find_each do |patient|
45+
create(:notify_log_entry, :email, patient:, consent_form: nil)
46+
47+
consent_form = create(:consent_form, session: Session.first)
48+
parent =
49+
patient.parents.first || create(:parent_relationship, patient:).parent
50+
create(
51+
:consent,
52+
:given,
53+
patient:,
54+
parent:,
55+
consent_form:,
56+
programme: programmes.first
57+
)
58+
end
59+
60+
create(:school_move, :to_school, patient: Patient.first)
61+
end
62+
63+
it "deletes associated data" do
64+
expect { get :call, params: { organisation_ods_code: "r1l" } }.to(
65+
change(CohortImport, :count)
66+
.by(-1)
67+
.and(change(ImmunisationImport, :count).by(-1))
68+
.and(change(NotifyLogEntry, :count).by(-3))
69+
.and(change(Parent, :count).by(-4))
70+
.and(change(Patient, :count).by(-3))
71+
.and(change(PatientSession, :count).by(-3))
72+
.and(change(VaccinationRecord, :count).by(-11))
73+
)
74+
end
75+
end
76+
end

0 commit comments

Comments
 (0)