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 = [ ]
@@ -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 "\n URNs 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 "\n URNs 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 "\n URNs 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
320430end
0 commit comments