@@ -24,14 +24,18 @@ def call(input_file:, **)
2424 . merge ( Session . scheduled )
2525 . pluck ( :urn )
2626 ) ,
27- closed : Set . new ,
28- closing : Set . new ,
27+ closed : {
28+ } ,
29+ closing : {
30+ } ,
2931 year_group_changes : {
3032 }
3133 }
3234 schools_without_future_sessions = {
33- closed : Set . new ,
34- closing : Set . new ,
35+ closed : {
36+ } ,
37+ closing : {
38+ } ,
3539 year_group_changes : {
3640 }
3741 }
@@ -48,13 +52,29 @@ def call(input_file:, **)
4852 new_schools = Set . new
4953
5054 Zip ::File . open ( input_file ) do |zip |
51- csv_entry = zip . glob ( "edubasealldata *.csv" ) . first
52- csv_content = csv_entry . get_input_stream . read
55+ links_csv = zip . glob ( "links_edubasealldata *.csv" ) . first
56+ links_csv_content = links_csv . get_input_stream . read
5357
54- progress_bar = MavisCLI . progress_bar ( csv_content . lines . count + 1 )
58+ @school_successors = { }
59+ CSV . parse (
60+ links_csv_content ,
61+ headers : true ,
62+ encoding : "ISO-8859-1:UTF-8"
63+ ) do |row |
64+ next unless row [ "LinkType" ] &.include? ( "Successor" )
65+
66+ @school_successors [ row [ "URN" ] ] ||= [ ]
67+ @school_successors [ row [ "URN" ] ] << row [ "LinkURN" ]
68+ end
69+
70+ school_data_csv = zip . glob ( "edubasealldata*.csv" ) . first
71+ school_csv_content = school_data_csv . get_input_stream . read
72+
73+ progress_bar =
74+ MavisCLI . progress_bar ( school_csv_content . lines . count + 1 )
5575
5676 CSV . parse (
57- csv_content ,
77+ school_csv_content ,
5878 headers : true ,
5979 encoding : "ISO-8859-1:UTF-8"
6080 ) do |row |
@@ -115,17 +135,29 @@ def call(input_file:, **)
115135 That have year group changes: #{ schools_with_future_sessions [ :year_group_changes ] . count } (#{ schools_with_changed_year_groups_pct * 100 } %)
116136 OUTPUT
117137
118- puts <<~OUTPUT if schools_with_future_sessions [ :closed ] . any?
119-
120- URNs of closed schools with future sessions:
121- #{ schools_with_future_sessions [ :closed ] . to_a . sort . join ( "\n " ) }
122- OUTPUT
123-
124- puts <<~OUTPUT if schools_with_future_sessions [ :closing ] . any?
138+ if schools_with_future_sessions [ :closed ] . any?
139+ puts "\n URNs of closed schools with future sessions:"
140+ schools_with_future_sessions [ :closed ] . sort . each do |urn , successors |
141+ if successors . any?
142+ successor_info = format_successors_with_teams ( successors )
143+ puts " #{ urn } -> successor(s): #{ successor_info } "
144+ else
145+ puts " #{ urn } "
146+ end
147+ end
148+ end
125149
126- URNs of schools that will be closing, with future sessions:
127- #{ schools_with_future_sessions [ :closing ] . to_a . sort . join ( "\n " ) }
128- OUTPUT
150+ if schools_with_future_sessions [ :closing ] . any?
151+ puts "\n URNs of schools that will be closing, with future sessions:"
152+ schools_with_future_sessions [ :closing ] . sort . each do |urn , successors |
153+ if successors . any?
154+ successor_info = format_successors_with_teams ( successors )
155+ puts " #{ urn } -> successor(s): #{ successor_info } "
156+ else
157+ puts " #{ urn } "
158+ end
159+ end
160+ end
129161
130162 if schools_with_future_sessions [ :year_group_changes ] . any?
131163 puts "\n URNs of schools with year group changes, with future sessions:"
@@ -141,6 +173,37 @@ def call(input_file:, **)
141173
142174 private
143175
176+ def format_successors_with_teams ( successor_urns )
177+ successor_urns
178+ . map do |successor_urn |
179+ locations = Location . school . where ( urn : successor_urn )
180+
181+ if locations . count == 1
182+ teams = locations . sole . teams . uniq
183+ if teams . any?
184+ team_names = teams . map ( &:name ) . join ( ", " )
185+ "#{ successor_urn } (Team: #{ team_names } )"
186+ else
187+ "#{ successor_urn } (no team)"
188+ end
189+ elsif locations . count > 1
190+ site_urns =
191+ locations . where . not ( site : nil ) . map ( &:urn_and_site ) . join ( ", " )
192+ team_names =
193+ locations
194+ . where . not ( site : nil )
195+ . flat_map ( &:teams )
196+ . uniq
197+ . map ( &:name )
198+ . join ( ", " )
199+ "#{ site_urns } (Teams: #{ team_names } )"
200+ else
201+ "#{ successor_urn } (not found)"
202+ end
203+ end
204+ . join ( ", " )
205+ end
206+
144207 def calculate_percentage ( schools_set , metric )
145208 if schools_set [ :existing ] . count . positive?
146209 schools_set [ metric ] . count . to_f / schools_set [ :existing ] . count
@@ -152,11 +215,12 @@ def calculate_percentage(schools_set, metric)
152215 def check_for_school_closure ( row , school_set )
153216 urn = row [ "URN" ]
154217 new_status = row [ "EstablishmentStatus (name)" ]
218+ successors = @school_successors [ urn ] || [ ]
155219
156220 if new_status == "Closed"
157- school_set [ :closed ] << urn
221+ school_set [ :closed ] [ urn ] = successors
158222 elsif new_status == "Open, but proposed to close"
159- school_set [ :closing ] << urn
223+ school_set [ :closing ] [ urn ] = successors
160224 end
161225 end
162226
0 commit comments