22
33module 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 "\n URNs 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 "\n URNs 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 "\n URNs 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
316431end
0 commit comments