Skip to content

Commit 507330d

Browse files
committed
Extract succesor school info from GIAS data
GIAS provides a separate "links" file that contains all the successor and predecessor information for schools. This is helpful when we deiscover a school is closed and has migrated to a different URN. Eventually we want to handle these cases automatcially perhaps adding the successor school to the team and having the info ready gets us one step closer to that.
1 parent 4dd42fb commit 507330d

5 files changed

Lines changed: 96 additions & 25 deletions

File tree

app/lib/mavis_cli/gias/check_import.rb

Lines changed: 84 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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 "\nURNs 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 "\nURNs 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 "\nURNs 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

app/lib/mavis_cli/gias/download.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
# To get the latest version of the zip:
44
# 1. Go to https://get-information-schools.service.gov.uk/Downloads
55
# 2. Check "Establishment fields CSV"
6-
# 3. Submit
7-
# 4. Download the zip file
8-
# 5. Place it in db/data/dfe-schools.zip
6+
# 3. Check "Establishment links CSV"
7+
# 4. Submit
8+
# 5. Download the zip file
9+
# 6. Move the downloaded file to db/data/dfe-schools.zip
910
#
1011
# Alternatively, you can run this task.
1112

@@ -34,6 +35,9 @@ def call(output_file:, **)
3435
form = page.form_with(action: "/Downloads/Collate")
3536
form.checkbox_with(id: "establishment-fields-csv-checkbox").check
3637

38+
puts "Checking the establishment links CSV checkbox"
39+
form.checkbox_with(id: "establishment-links-csv-checkbox").check
40+
3741
puts "Submitting the form"
3842
download_page = form.submit
3943

spec/features/cli_gias_check_import_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def and_there_are_schools_with_future_sessions
3232
team: @team
3333
)
3434

35+
@successor_school =
36+
create(:school, name: "The Aldgate Academy", urn: "100004", team: @team)
37+
3538
@school2_with_future_session =
3639
create(
3740
:school,
@@ -90,7 +93,7 @@ def then_i_should_see_the_correct_counts
9093
expect(@output).to include("That have year group changes: 1")
9194

9295
expect(@output).to include(
93-
"URNs of closed schools with future sessions:\n 100000"
96+
"URNs of closed schools with future sessions:\n 100000 -> successor(s): 100004 (Team: #{@team.name})"
9497
)
9598
expect(@output).to include(
9699
"URNs of schools that will be closing, with future sessions:\n 100002"

spec/features/cli_gias_import_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def when_i_run_the_import_command
4545
end
4646

4747
def then_schools_are_imported_correctly
48-
expect(Location.count).to eq(6)
48+
expect(Location.count).to eq(7)
4949
expect(Location.find_by_urn_and_site("100000").name).to eq(
5050
"The Aldgate School"
5151
)
1.57 KB
Binary file not shown.

0 commit comments

Comments
 (0)