Skip to content

Commit a1aa470

Browse files
Implement logger dependency injection for GIAS helper
1 parent 3955d3b commit a1aa470

5 files changed

Lines changed: 127 additions & 119 deletions

File tree

app/lib/gias.rb

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
module GIAS
44
class << self
5-
def download(output_file:)
5+
def download(output_file:, logger: Rails.logger)
66
# 1. Go to https://get-information-schools.service.gov.uk/Downloads
77
# 2. Check "Establishment fields CSV"
88
# 3. Check "Establishment links CSV"
99
# 4. Submit
1010
# 5. Download the zip file
1111
# 6. Move the downloaded file to db/data/dfe-schools.zip
1212

13+
logger.info "Starting schools data download process..."
14+
1315
require "mechanize"
1416

1517
agent = Mechanize.new
@@ -36,13 +38,16 @@ def download(output_file:)
3638
download_button = download_form.button_with(value: "Results.zip")
3739
download_file = agent.click(download_button)
3840
download_file.save!(output_file)
41+
logger.info "File downloaded successfully to #{output_file}"
3942
true
4043
else
44+
logger.info "Download button never appeared, aborting"
4145
false
4246
end
4347
end
4448

45-
def import(input_file:, progress_bar: nil)
49+
def import(input_file:, progress_bar: nil, logger: Rails.logger)
50+
logger.info "Starting import of #{row_count(input_file) - 1} schools."
4651
open_csv(input_file) do |rows|
4752
batch_size = 1000
4853
schools = []
@@ -191,6 +196,71 @@ def check_import(input_file:, progress_bar: nil)
191196
}
192197
end
193198

199+
def log_import_check_results(results, logger: Rails.logger)
200+
new_schools = results[:new_schools]
201+
schools_with_future_sessions = results[:schools_with_future_sessions]
202+
schools_without_future_sessions =
203+
results[:schools_without_future_sessions]
204+
205+
closed_schools_count =
206+
schools_without_future_sessions[:closed].count +
207+
schools_with_future_sessions[:closed].count
208+
closing_schools_count =
209+
schools_without_future_sessions[:closing].count +
210+
schools_with_future_sessions[:closing].count
211+
212+
closed_schools_with_future_sessions_pct =
213+
calculate_percentage(schools_with_future_sessions, :closed)
214+
closing_schools_with_future_sessions_pct =
215+
calculate_percentage(schools_with_future_sessions, :closing)
216+
schools_with_changed_year_groups_pct =
217+
calculate_percentage(schools_with_future_sessions, :year_group_changes)
218+
219+
logger.info <<~OUTPUT
220+
New schools (total): #{new_schools.count}
221+
Closed schools (total): #{closed_schools_count}
222+
Proposed to be closed schools (total): #{closing_schools_count}
223+
224+
Existing schools with future sessions: #{schools_with_future_sessions[:existing].count}
225+
That are closed in import: #{schools_with_future_sessions[:closed].count} (#{closed_schools_with_future_sessions_pct * 100}%)
226+
That are proposed to be closed in import: #{schools_with_future_sessions[:closing].count} (#{closing_schools_with_future_sessions_pct * 100}%)
227+
That have year group changes: #{schools_with_future_sessions[:year_group_changes].count} (#{schools_with_changed_year_groups_pct * 100}%)
228+
OUTPUT
229+
230+
if schools_with_future_sessions[:closed].any?
231+
logger.info "\nURNs of closed schools with future sessions:"
232+
schools_with_future_sessions[:closed].sort.each do |urn, successors|
233+
if successors.any?
234+
successor_info = format_successors_with_teams(successors)
235+
logger.info " #{urn} -> successor(s): #{successor_info}"
236+
else
237+
logger.info " #{urn}"
238+
end
239+
end
240+
end
241+
242+
if schools_with_future_sessions[:closing].any?
243+
logger.info "\nURNs of schools that will be closing, with future sessions:"
244+
schools_with_future_sessions[:closing].sort.each do |urn, successors|
245+
if successors.any?
246+
successor_info = format_successors_with_teams(successors)
247+
logger.info " #{urn} -> successor(s): #{successor_info}"
248+
else
249+
logger.info " #{urn}"
250+
end
251+
end
252+
end
253+
254+
if schools_with_future_sessions[:year_group_changes].any?
255+
logger.info "\nURNs of schools with year group changes, with future sessions:"
256+
schools_with_future_sessions[:year_group_changes].each do |urn, change|
257+
logger.info " #{urn}:"
258+
logger.info " Current: #{change[:current]}"
259+
logger.info " New: #{change[:new]}"
260+
end
261+
end
262+
end
263+
194264
def process_url(url)
195265
return nil if url.blank?
196266

@@ -316,5 +386,45 @@ def check_for_year_group_changes(row, school_set, existing_schools)
316386
}
317387
end
318388
end
389+
390+
def calculate_percentage(schools_set, metric)
391+
if schools_set[:existing].count.positive?
392+
schools_set[metric].count.to_f / schools_set[:existing].count
393+
else
394+
0.0
395+
end
396+
end
397+
398+
def format_successors_with_teams(successor_urns)
399+
annotated_successor_urns =
400+
successor_urns.map do |successor_urn|
401+
locations = Location.school.where(urn: successor_urn)
402+
403+
if locations.count == 1
404+
teams = locations.sole.teams.uniq
405+
if teams.any?
406+
team_names = teams.map(&:name).join(", ")
407+
"#{successor_urn} (Team: #{team_names})"
408+
else
409+
"#{successor_urn} (no team)"
410+
end
411+
elsif locations.count > 1
412+
site_urns =
413+
locations.where.not(site: nil).map(&:urn_and_site).join(", ")
414+
team_names =
415+
locations
416+
.where.not(site: nil)
417+
.flat_map(&:teams)
418+
.uniq
419+
.map(&:name)
420+
.join(", ")
421+
"#{site_urns} (Teams: #{team_names})"
422+
else
423+
"#{successor_urn} (not found)"
424+
end
425+
end
426+
427+
annotated_successor_urns.join(", ")
428+
end
319429
end
320430
end

app/lib/mavis_cli/gias/check_import.rb

Lines changed: 5 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -13,120 +13,16 @@ class CheckImport < Dry::CLI::Command
1313
def call(input_file:, **)
1414
MavisCLI.load_rails
1515

16+
logger = Logger.new($stdout)
17+
logger.formatter =
18+
proc { |_severity, _datetime, _progname, msg| "#{msg}\n" }
19+
1620
row_count = ::GIAS.row_count(input_file)
1721
progress_bar = MavisCLI.progress_bar(row_count)
1822

1923
results = ::GIAS.check_import(input_file:, progress_bar:)
2024

21-
new_schools = results[:new_schools]
22-
schools_with_future_sessions = results[:schools_with_future_sessions]
23-
schools_without_future_sessions =
24-
results[:schools_without_future_sessions]
25-
26-
closed_schools_count =
27-
schools_without_future_sessions[:closed].count +
28-
schools_with_future_sessions[:closed].count
29-
closing_schools_count =
30-
schools_without_future_sessions[:closing].count +
31-
schools_with_future_sessions[:closing].count
32-
33-
closed_schools_with_future_sessions_pct =
34-
calculate_percentage(schools_with_future_sessions, :closed)
35-
closing_schools_with_future_sessions_pct =
36-
calculate_percentage(schools_with_future_sessions, :closing)
37-
schools_with_changed_year_groups_pct =
38-
calculate_percentage(
39-
schools_with_future_sessions,
40-
:year_group_changes
41-
)
42-
43-
puts <<~OUTPUT
44-
New schools (total): #{new_schools.count}
45-
Closed schools (total): #{closed_schools_count}
46-
Proposed to be closed schools (total): #{closing_schools_count}
47-
48-
Existing schools with future sessions: #{schools_with_future_sessions[:existing].count}
49-
That are closed in import: #{schools_with_future_sessions[:closed].count} (#{closed_schools_with_future_sessions_pct * 100}%)
50-
That are proposed to be closed in import: #{schools_with_future_sessions[:closing].count} (#{closing_schools_with_future_sessions_pct * 100}%)
51-
That have year group changes: #{schools_with_future_sessions[:year_group_changes].count} (#{schools_with_changed_year_groups_pct * 100}%)
52-
OUTPUT
53-
54-
if schools_with_future_sessions[:closed].any?
55-
puts "\nURNs of closed schools with future sessions:"
56-
schools_with_future_sessions[:closed].sort.each do |urn, successors|
57-
if successors.any?
58-
successor_info = format_successors_with_teams(successors)
59-
puts " #{urn} -> successor(s): #{successor_info}"
60-
else
61-
puts " #{urn}"
62-
end
63-
end
64-
end
65-
66-
if schools_with_future_sessions[:closing].any?
67-
puts "\nURNs of schools that will be closing, with future sessions:"
68-
schools_with_future_sessions[:closing].sort.each do |urn, successors|
69-
if successors.any?
70-
successor_info = format_successors_with_teams(successors)
71-
puts " #{urn} -> successor(s): #{successor_info}"
72-
else
73-
puts " #{urn}"
74-
end
75-
end
76-
end
77-
78-
if schools_with_future_sessions[:year_group_changes].any?
79-
puts "\nURNs of schools with year group changes, with future sessions:"
80-
schools_with_future_sessions[
81-
:year_group_changes
82-
].each do |urn, change|
83-
puts " #{urn}:"
84-
puts " Current: #{change[:current]}"
85-
puts " New: #{change[:new]}"
86-
end
87-
end
88-
end
89-
90-
private
91-
92-
def format_successors_with_teams(successor_urns)
93-
annotated_successor_urns =
94-
successor_urns.map do |successor_urn|
95-
locations = Location.school.where(urn: successor_urn)
96-
97-
if locations.count == 1
98-
teams = locations.sole.teams.uniq
99-
if teams.any?
100-
team_names = teams.map(&:name).join(", ")
101-
"#{successor_urn} (Team: #{team_names})"
102-
else
103-
"#{successor_urn} (no team)"
104-
end
105-
elsif locations.count > 1
106-
site_urns =
107-
locations.where.not(site: nil).map(&:urn_and_site).join(", ")
108-
team_names =
109-
locations
110-
.where.not(site: nil)
111-
.flat_map(&:teams)
112-
.uniq
113-
.map(&:name)
114-
.join(", ")
115-
"#{site_urns} (Teams: #{team_names})"
116-
else
117-
"#{successor_urn} (not found)"
118-
end
119-
end
120-
121-
annotated_successor_urns.join(", ")
122-
end
123-
124-
def calculate_percentage(schools_set, metric)
125-
if schools_set[:existing].count.positive?
126-
schools_set[metric].count.to_f / schools_set[:existing].count
127-
else
128-
0.0
129-
end
25+
::GIAS.log_import_check_results(results, logger:)
13026
end
13127
end
13228
end

app/lib/mavis_cli/gias/download.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ class Download < Dry::CLI::Command
2121
desc: "file path to write GIAS database to"
2222

2323
def call(output_file:, **)
24-
puts "Starting schools data download process..."
25-
if ::GIAS.download(output_file:)
26-
puts "File downloaded successfully to #{output_file}"
27-
else
28-
puts "Download button never appeared, aborting"
29-
end
24+
logger = Logger.new($stdout)
25+
logger.formatter =
26+
proc { |_severity, _datetime, _progname, msg| "#{msg}\n" }
27+
28+
::GIAS.download(output_file:, logger:)
3029
end
3130
end
3231
end

app/lib/mavis_cli/gias/import.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ class Import < Dry::CLI::Command
1313
def call(input_file:, **)
1414
MavisCLI.load_rails
1515

16+
logger = Logger.new($stdout)
17+
logger.formatter =
18+
proc { |_severity, _datetime, _progname, msg| "#{msg}\n" }
19+
1620
row_count = ::GIAS.row_count(input_file)
17-
puts "Starting import of #{row_count - 1} schools."
1821
progress_bar = MavisCLI.progress_bar(row_count)
1922

20-
::GIAS.import(input_file:, progress_bar:)
23+
::GIAS.import(input_file:, progress_bar:, logger:)
2124
end
2225
end
2326
end

db/data/dfe-schools.zip

640 KB
Binary file not shown.

0 commit comments

Comments
 (0)