Skip to content

Commit 55f2076

Browse files
Implement logger dependency injection for GIAS helper
1 parent 1ba8533 commit 55f2076

5 files changed

Lines changed: 129 additions & 118 deletions

File tree

app/lib/gias.rb

Lines changed: 117 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 = []
@@ -187,6 +192,76 @@ def check_import(input_file:, progress_bar: nil)
187192
}
188193
end
189194

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

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

app/lib/mavis_cli/gias/check_import.rb

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

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

1922
results = ::GIAS.check_import(input_file:, progress_bar:)
2023

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
24+
::GIAS.log_import_check_results(results, logger:)
8825
end
8926

90-
private
91-
92-
def format_successors_with_teams(successor_urns)
93-
annotated_successor_urns = successor_urns
94-
.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
130-
end
13127
end
13228
end
13329

app/lib/mavis_cli/gias/download.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ 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 = proc { |_severity, _datetime, _progname, msg| "#{msg}\n" }
26+
27+
::GIAS.download(output_file:, logger:)
3028
end
3129
end
3230
end

app/lib/mavis_cli/gias/import.rb

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

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

20-
::GIAS.import(input_file:, progress_bar:)
22+
::GIAS.import(input_file:, progress_bar:, logger:)
2123
end
2224
end
2325
end

db/data/dfe-schools.zip

640 KB
Binary file not shown.

0 commit comments

Comments
 (0)