-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpull_retention_rates.rb
2260 lines (2005 loc) · 95.3 KB
/
pull_retention_rates.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'base64'
require 'colorize'
require 'csv'
require 'date'
require 'yaml'
require_relative 'lib/data_mapper/mapper'
require_relative './lib/transparent_classroom/client'
require_relative './lib/name_matcher/matcher'
PREVIOUS_YEAR = '2020-21'
CURRENT_YEAR = '2021-22'
# Dates assume latest possible start and earliest possible finish
# Min session length is meant to filter out unexpectedly short sessions, i.e. a winter or JTerm session
SCHOOL_YEAR_LATEST_START = '09/15'
SCHOOL_YEAR_EARLIEST_STOP = '05/01'
MIN_SESSION_LENGTH_DAYS = 60
CONFIG = begin
YAML.load_file('config.yml')
rescue
{
'rejectSchools' => [],
'ignoreSchools' => [],
'ignoreClassrooms' => [],
'ignoreChildren' => [],
'graduatedChildren' => [],
'groupSchools' => [],
}
end
def date_from_year_and_month_day(year, month_day)
Date.parse("#{year}/#{month_day}")
end
def is_school_ignored(school_id, school_name=nil)
isSchoolIgnored = CONFIG['ignoreSchools'].any? do |ignore|
school_id == ignore['id'] || (school_name != nil && school_name == ignore['name'])
end
isSchoolIgnored
end
def is_classroom_ignored(school_id, classroom_id, school_name=nil, classroom_name=nil)
isClassroomIgnored = CONFIG['ignoreClassrooms'].any? do |ignore|
if ignore['schoolId'] == school_id || (school_name != nil && ignore['schoolName'] == school_name)
ignore['classrooms'].any? do |ignoreClassroom|
ignoreClassroom['id'] == classroom_id || (classroom_name != nil && ignoreClassroom['name'] == classroom_name)
end
end
end
isClassroomIgnored
end
def is_child_ignored(child)
classroom_ids=child['classroom_ids']
school = child['school']
is_child_school_ignored = is_school_ignored(school['id'], school['name'])
are_all_child_classrooms_ignored = classroom_ids.all? do |classroom_id|
is_classroom_ignored(school['id'], classroom_id, school['name'])
end
is_child_explicitly_ignored = CONFIG['ignoreChildren'].any? do |ignore|
if ignore['schoolId'] == school['id'] or ignore['schoolName'] == school['name']
ignore['children'].any? do |ignoreChildren|
ignoreChildren['id'] == child['id'] || ignoreChildren['name'] == name(child)
end
end
end
is_child_age_ignored = age_on(child, Date.parse(school['current_year']['start_date'])) >= to_age(years: 21, months: 0)
is_child_school_ignored || are_all_child_classrooms_ignored || is_child_explicitly_ignored || is_child_age_ignored
end
def is_child_included_in_graduated_override_config(school_id, child_id, school_name=nil, child_name=nil)
is_child_in_graduated_config = CONFIG['graduatedChildren'].any? do |graduated|
if graduated['schoolId'] == school_id or graduated['schoolName'] == school_name
graduated['children'].any? do |ignoreChildren|
ignoreChildren['id'] == child_id || ignoreChildren['name'] == child_name
end
end
end
end
def load_schools(tc)
schools = tc.get 'schools.json'
schools.reject! do |school|
CONFIG['rejectSchools'].any? do |ignore|
school['name'] == ignore['name'] || school['id'] == ignore['id']
end
end
schools.each do |school|
school['ignore'] = is_school_ignored(school['id'], school['name'])
end
schools
end
def load_classrooms_by_id(tc, school)
tc.school_id = school['id']
classrooms = tc.get('classrooms.json', params: {show_inactive: true})
classrooms.each do |c|
c['ignore'] = is_classroom_ignored(school['id'], c['id'], school['name'], c['name'])
end
classrooms.index_by { |c| c['id'] }
end
def load_children(tc, school, session)
tc.school_id = school['id']
children = tc.get 'children.json', params: { session_id: session['id'] }
children.each do |child|
child['school'] = school
child['ignore'] = is_child_ignored(child=child)
child['graduated_teacher'] = child.has_key?('exit_reason') && child['exit_reason'].downcase == "graduated"
child['graduated_parent'] = nil
child['exit_survey'] = nil
child['parent_exit_reason'] = nil
child['ethnicity_original'] = child['ethnicity'].clone
child['ethnicity_survey_original'] = nil
if child.has_key?('exit_survey_id') && child['exit_survey_id'] != nil
exit_survey_response = tc.get("forms/#{child['exit_survey_id']}.json")
child['exit_survey'] = exit_survey_response
child['parent_exit_reason'] = nil
if exit_survey_response['state'] == "submitted" && exit_survey_response.has_key?('fields') && exit_survey_response['fields'].has_key?('Reason for Leaving')
child['parent_exit_reason'] = exit_survey_response['fields']["Reason for Leaving"]
child['graduated_parent'] = exit_survey_response['fields']["Reason for Leaving"].downcase == "graduated"
end
end
child['graduated_override'] = is_child_included_in_graduated_override_config(
school_id=school['id'],
child_id=child['id'],
school_name=school['name'],
child_name=name(child)
)
child['fingerprint'] = fingerprint(child)
end
children
end
def load_network_family_survey_form_templates(tc)
tc.school_id = nil
forms = tc.get("form_templates.json")
network_forms = forms.map do |f|
network_template_id = f['id']
network_template_name = f['name']
if network_template_name =~ /family survey/i
{
network_template_id: network_template_id,
network_template_name: network_template_name,
is_family_survey: true
}
else
nil
end
end
network_forms.compact
end
def load_school_family_survey_form_templates(tc, network_family_survey_forms, school)
tc.school_id = school['id']
network_family_survey_ids = network_family_survey_forms.map{|f| f[:network_template_id]}
forms = tc.get("form_templates.json")
school_forms = forms.map do |f|
school_template_id = f['id']
school_template_name = f['name']
is_family_survey_school_template = false
f['widgets'].each do |w|
if w['type'] == 'EmbeddedForm' && network_family_survey_ids.include?(w['embedded_form_id'].to_i)
is_family_survey_school_template = true
break
end
end
if is_family_survey_school_template
{
school_template_id: school_template_id,
school_template_name: school_template_name,
is_family_survey: is_family_survey_school_template
}
else
nil
end
end
school_forms.compact
end
def load_school_family_survey_form_data(tc, school_family_survey_form_templates, school)
tc.school_id = school['id']
school_family_survey_ids = school_family_survey_form_templates.map{|f| f[:school_template_id]}
form_data_by_child = {}
school_family_survey_ids.each do |f_id|
form_responses = tc.get("forms.json", params: { form_template_id: f_id })
form_responses.each do |fr|
unless form_data_by_child.has_key?(fr['child_id'])
form_data_by_child[fr['child_id']] = []
end
form_data = {
'form_id': fr['id'],
'state': fr['state'],
'created_at_text': fr['created_at'],
'updated_at_text': fr['updated_at'],
'student_id': fr['child_id'],
'school_id': school['id']
}
fields = fr['fields']
fields.each do | key, value |
if !value.nil? && DataMapper.field_names_has_key?(key)
form_data[DataMapper.field_names_value_for(key).to_sym] = value
end
end
form_data_by_child[fr['child_id']].append(form_data)
end
end
form_data_by_child
end
# TC's classrooms endpoint doesn't return 'inactive' classrooms, but the child endpoint will return classroom_ids that point to these 'inactive' classrooms
# This means we can't load classroom objects for all the classrooms a child might be linked to
# This function returns a synthetic classroom, it includes an 'ignore' attribute which checks against the classrooms that have been ignored in the config file
def unknown_classroom_object(school_id, classroom_id)
level = 'unknown'
{'id'=>classroom_id, 'name'=>"UNKNOWN (#{classroom_id})", 'level'=>level, 'ignore'=>is_classroom_ignored(school_id, classroom_id)}
end
def fingerprint(child)
Base64.encode64("#{child['first_name'].strip.downcase} #{child['last_name'].strip.downcase}, #{child['birth_date']}")
end
def age_on(child, date)
return nil if child['birth_date'].blank?
birth_date = Date.parse(child['birth_date'])
months = date.month + date.year * 12 - (birth_date.year * 12 + birth_date.month)
date.day >= birth_date.day ? months : months - 1
end
def format_age(months)
str = "#{months / 12}y"
str << " #{months % 12}m" unless months % 12 == 0
str
end
def to_age(years:, months: 0)
years * 12 + months
end
# Check if child will turn 5 at some point between start of previous school year through start of next school
def is_child_kindergarten_eligible?(child, previous_year_start, current_year_start)
age_on(child, previous_year_start) < to_age(years: 5, months: 0) && age_on(child, current_year_start) >= to_age(years: 5, months: 0)
end
def age_appropriate_for_school?(school, age)
classrooms_by_id = school['classrooms_by_id']
age_appropriate = false
classrooms_by_id.each do |id, classroom|
# skip this classroom if it can't be found or if the classroom is in the ignored list
if classroom == nil || is_classroom_ignored(school['id'], id, school['name'], classroom['name'])
next
end
age_appropriate = age_appropriate?(classroom, age)
break if age_appropriate
end
age_appropriate
end
def age_appropriate?(classroom, age)
if classroom.nil?
return true
end
case classroom['level']
when '0-1.5'
age >= to_age(years: 0, months: 0) && age < to_age(years: 1, months: 6)
when '1.5-3', '0-3' # Cutoff age for aging out of toddler is 33 months
age >= to_age(years: 1, months: 6) && age < to_age(years: 2, months: 9)
when '3-6' # Entry age for primary is 33 months
age >= to_age(years: 2, months: 9) && age < to_age(years: 6)
when '6-9'
age >= to_age(years: 6, months: 0) && age < to_age(years: 9)
when '6-12'
age >= to_age(years: 6, months: 0) && age < to_age(years: 12)
when '9-12'
age >= to_age(years: 9, months: 0) && age < to_age(years: 12)
when '12-15'
age >= to_age(years: 12, months: 0) && age < to_age(years: 15)
else
puts "don't know how to handle level #{classroom['level']}".red
true
end
end
def infant_toddler_classroom?(classroom)
if classroom.nil?
return false
end
case classroom['level']
when '0-1.5', '1.5-3', '0-3'
true
when '3-6', '6-9', '6-12', '9-12', '12-15'
false
else
puts "infant_toddler_classroom? - don't know how to handle level #{classroom['level']}".red
false
end
end
def too_old?(classroom, age)
if classroom.nil?
return false
end
case classroom['level']
when '0-1.5'
age >= to_age(years: 1, months: 6)
when '1.5-3', '0-3' # Cutoff age for aging out of toddler is 33 months
age >= to_age(years: 2, months: 9)
when '3-6' # Entry age for primary is 33 months
age >= to_age(years: 6)
when '6-9'
age >= to_age(years: 9)
when '6-12'
age >= to_age(years: 12)
when '9-12'
age >= to_age(years: 12)
when '12-15'
age >= to_age(years: 15)
else
puts "too_old? - don't know how to handle level #{classroom['level']}".red
false
end
end
def name(child)
"#{child['first_name']} #{child['last_name']}"
end
def new_school_year
school_year_template = {'children'=>{}, 'sessions'=>[], 'classrooms'=>[], 'start_date'=>nil, 'stop_date'=>nil}
Marshal.load(Marshal.dump(school_year_template))
end
def get_child_recognized_classrooms(child)
classrooms = child['classroom_ids'].map do |id|
child['school']['classrooms_by_id'][id] || unknown_classroom_object(child['school']['id'], id)
end.compact
classrooms.reject {|c| c['ignore']}
end
def get_child_ignored_classrooms(child)
classrooms = child['classroom_ids'].map do |id|
child['school']['classrooms_by_id'][id] || unknown_classroom_object(child['school']['id'], id)
end.compact
classrooms.select {|c| c['ignore']}
end
def get_child_active_classroom(child)
classrooms_recognized = get_child_recognized_classrooms(child)
if classrooms_recognized.count > 1
# notes << "Child in multiple classrooms, defaulting to first in list: #{classrooms_recognized}"
puts "#{name child} is in multiple recognized classrooms: #{classrooms_recognized}".red
end
classrooms_recognized.first
end
def child_ignored_list(child)
classrooms_ignored = get_child_ignored_classrooms(child)
classroom = get_child_active_classroom(child)
{
ignoredChild: child['ignore'],
ignoredClassroom: classroom.nil? && classrooms_ignored.count > 0,
ignoredSchool: child['school']['ignore'],
}
end
def is_child_in_infant_toddler_classroom?(child)
classroom = get_child_active_classroom(child)
infant_toddler_classroom?(classroom)
end
def reasons_child_ignored_details(child)
details = []
ignored_list = child_ignored_list(child)
if ignored_list[:ignoredClassroom]
details << "Ignored - child only present in ignored classroom list ('#{get_child_ignored_classrooms(child)}')"
end
if ignored_list[:ignoredSchool]
details << "Ignored - in ignored school list ('#{child['school']['name']}')"
end
if ignored_list[:ignoredChild]
details << "Ignored - in ignored child list"
end
details
end
def does_children_collection_include(children, child)
# if children.has_key?(child['fingerprint']) && not(is_child_ignored(children[child['fingerprint']]))
if children.has_key?(child['fingerprint']) && not(children[child['fingerprint']]['ignore'])
return [true, children[child['fingerprint']]]
end
children_with_birthdate = children.values.select { |c| child['birth_date'] == c['birth_date'] && not(c['ignore']) }
if children_with_birthdate.empty?
return [false, nil]
end
puts "Performing fuzzy match on #{name(child)}"
nearest = children_with_birthdate.map{ |c| {c => NamesMatcher.distance(name(child), name(c))}}.min_by{|r| r.values}
match = nearest.values.first <= 0.8
if match
puts "Found #{name(nearest.keys.first)} w/ distance #{nearest.values.first}"
return [true, nearest.keys.first]
else
puts "No match, nearest candidate #{name(nearest.keys.first)} w/ distance #{nearest.values.first}"
return [false, nil]
end
end
def get_school_from_name(school_name, schools)
match = schools.detect do |school|
break school if school['name'].strip.downcase == school_name.strip.downcase
end
match
end
def get_classroom_from_name(classroom_name, schools)
classroom = schools.detect do |school|
c = school['classrooms_by_id'].detect do |_, classroom|
break classroom if classroom['name'].strip.downcase == classroom_name.strip.downcase
end
break c if c
end
classroom
end
def load_missing_children_from_csv(schools)
return [] unless File.file?("missing_children.csv")
missing = CSV.parse(File.read("missing_children.csv"), headers: true)
children = missing.map do |raw|
child = {
'id' => raw['child_id'].to_i,
'school' => get_school_from_name(raw['school_name'], schools),
'first_name' => raw['first_name'],
'last_name' => raw['last_name'],
'birth_date' => raw['birth_date'],
'ethnicity' => (raw['ethnicity'] || '').split(","),
'household_income' => raw['household_income'],
'dominant_language' => raw['dominant_language'],
'classroom_ids' => raw['classroom_names'].split(",").map{|name| c = get_classroom_from_name(name, schools); c ? c['id'].to_i : nil }.compact,
'was_missing' => true
}
child['fingerprint'] = fingerprint(child)
child
end
# Scrub records that could not be associated with a school
children.reject!{|child| child['school'].nil?}
children.each do |child|
child['ignore'] = is_child_ignored(child=child)
end
children
end
dir = File.expand_path("output", File.dirname(__FILE__))
FileUtils.mkdir_p(dir) unless File.directory?(dir)
tc = TransparentClassroom::Client.new
tc.masquerade_id = ENV['TC_MASQUERADE_ID']
schools = load_schools(tc)
network_family_survey_forms = load_network_family_survey_form_templates(tc)
#schools = schools[4..8]
#schools.reject! {|s| s['name'] != 'Cosmos Montessori'}
stats = {}
puts '=' * 100
puts "Loading schools".bold
puts '=' * 100
schools.each do |school|
puts
puts "Loading #{school['name'].bold}"
puts '=' * 100
tc.school_id = school['id']
# Helper for loading school_year sessions and latest start and earliest stop dates
# school_year formatting expected to match: e.g. '2018-19'
load_sessions_for_school_year = lambda do |school_year|
results = new_school_year.slice('sessions', 'start_date', 'stop_date')
latest_start = date_from_year_and_month_day(school_year.split('-')[0], SCHOOL_YEAR_LATEST_START)
earliest_stop = date_from_year_and_month_day(school_year.split('-')[1], SCHOOL_YEAR_EARLIEST_STOP)
sessions = tc.find_sessions_by_school_year(
latest_start: latest_start,
earliest_stop: earliest_stop,
min_school_days: MIN_SESSION_LENGTH_DAYS)
sessions.each {|s| puts "#{school_year} - '#{s['name']}' (start: #{s['start_date']}, stop: #{s['stop_date']})"}
results['sessions'] = sessions
sessions.each do |session|
session_start_date = Date.parse(session['start_date'])
session_stop_date = Date.parse(session['stop_date'])
if results['start_date'].nil? or Date.parse(results['start_date']) < session_start_date
results['start_date'] = session['start_date']
end
if results['stop_date'].nil? or Date.parse(results['stop_date']) > session_stop_date
results['stop_date'] = session['stop_date']
end
end
results
end
load_children_for_sessions = lambda do |sessions|
children = sessions.map do |session|
load_children(tc, school, session)
end.flatten
children.each do |child|
if school['forms_by_child_id'].keys.include?(child['id'])
child_forms = school['forms_by_child_id'][child['id']]
child_latest_form = child_forms.max_by { |f| Date.strptime(f[:created_at_text]) }
if child_latest_form.has_key?(:ethnicity_response)
child['ethnicity_survey_original'] = child_latest_form[:ethnicity_response].clone
unless child_latest_form[:ethnicity_response].nil? ||
child_latest_form[:ethnicity_response] == "" ||
child_latest_form[:ethnicity_response] == []
child['ethnicity'] = child_latest_form[:ethnicity_response]
end
end
if child_latest_form.has_key?(:household_income_response)
child['household_income'] = child_latest_form[:household_income_response]
end
if child_latest_form.has_key?(:language_response)
child['dominant_language'] = child_latest_form[:language_response]
end
end
if child['ethnicity'].nil?
child['ethnicity'] = []
else
child['ethnicity'] = DataMapper.ethnicities_value_for(child['ethnicity'])
end
if child['household_income'].nil?
child['household_income'] = []
else
child['household_income'] = DataMapper.income_value_for(child['household_income'])
end
if child['dominant_language'].nil?
child['dominant_language'] = []
else
child['dominant_language'] = DataMapper.languages_value_for(child['dominant_language'])
end
child['is_afam'] = child['ethnicity'].include?('African-American, Afro-Caribbean or Black')
child['is_asam'] = child['ethnicity'].include?('Asian-American')
child['is_latinx'] = child['ethnicity'].include?('Hispanic, Latinx, or Spanish Origin')
child['is_me'] = child['ethnicity'].include?('Middle Eastern or North African')
child['is_natam'] = child['ethnicity'].include?('Native American or Alaska Native')
child['is_pi'] = child['ethnicity'].include?('Native Hawaiian or Other Pacific Islander')
child['is_white'] = child['ethnicity'].include?('White')
child['is_white_only'] = child['is_white'] && child['ethnicity'].length == 1
child['is_other_nonwhite'] = child['ethnicity'].include?('Other (non-white)')
child['is_mixed'] = child['ethnicity'].include?('Unspecified multiple ethnicities')
is_gom = child['is_afam'] || child['is_asam'] || child['is_latinx'] || child['is_me'] || child['is_natam'] || child['is_pi'] || child['is_other_nonwhite'] || child['is_mixed']
child['is_gom'] = (is_gom == 1 || is_gom == true)
is_afam_latinx = child['is_afam'] || child['is_latinx']
child['is_afam_latinx'] = (is_afam_latinx == 1 || is_afam_latinx == true)
child['is_low_income'] = child['household_income'] == 'Low'
child['is_medium_income'] = child['household_income'] == 'Medium'
child['is_high_income'] = child['household_income'] == 'High'
end
children.uniq { |c| c['id'] }
end
puts
puts "Loading Family Survey Forms"
puts '-' * 100
school_form_templates = load_school_family_survey_form_templates(tc, network_family_survey_forms, school)
school['forms_by_child_id'] = load_school_family_survey_form_data(tc, school_form_templates, school)
puts
puts "Loading Sessions"
puts '-' * 100
school['previous_year'] = new_school_year.merge(load_sessions_for_school_year.call(PREVIOUS_YEAR))
school['current_year'] = new_school_year.merge(load_sessions_for_school_year.call(CURRENT_YEAR))
puts
puts "Loading Classrooms"
puts '-' * 100
school['classrooms_by_id'] = load_classrooms_by_id(tc, school)
puts "Loaded #{school['classrooms_by_id'].count} classrooms"
school['classrooms_by_id'].each do |_, classroom|
puts "(#{classroom['id']}) #{classroom['name']} - #{classroom['level']}, active: #{classroom['active']}"
end
puts
puts "Loading Children for #{PREVIOUS_YEAR}"
puts '-' * 100
school['previous_year']['children'] = load_children_for_sessions.call(school['previous_year']['sessions'])
puts "Loaded #{school['previous_year']['children'].count} children"
puts
puts "Loading Children for #{CURRENT_YEAR}"
puts '-' * 100
school['current_year']['children'] = load_children_for_sessions.call(school['current_year']['sessions'])
puts "Loaded #{school['current_year']['children'].count} children"
puts
end
puts
puts "Adding Missing Children to #{PREVIOUS_YEAR}".bold
puts '=' * 100
missing_children = load_missing_children_from_csv(schools)
puts
puts "Organizing Collection of All Children for #{PREVIOUS_YEAR}".bold
puts '=' * 100
previous_years_children = {}
schools.each do |school|
fids = school['previous_year']['children'].map do |child|
child['fingerprint']
end
child_ids = school['previous_year']['children'].map do |child|
child['id']
end
missing_children_for_school = missing_children.find_all do |missing|
school['id'] == missing['school']['id']
end
missing_children_for_school.each do |missing|
unless fids.include?(missing['fingerprint']) || child_ids.include?(missing['id'])
puts "Adding child from missing_children.csv: #{name(missing)} (#{missing['id']}) - #{missing['school']['name']} - #{missing['classroom_ids']}".yellow
school['previous_year']['children'] << missing
end
end
school['previous_year']['children'].each do |child|
fid = child['fingerprint']
if previous_years_children.has_key?(fid)
puts "Child #{name(child)} (#{child['id']}) is in multiple places in #{PREVIOUS_YEAR}".red
else
previous_years_children[fid] = child
end
end
end
puts
puts "Organizing Collection of All Children for #{CURRENT_YEAR}".bold
puts '=' * 100
current_years_children = {}
schools.each do |school|
school['current_year']['children'].each do |child|
fid = child['fingerprint']
if current_years_children.has_key?(fid)
puts "Child #{name(child)} (#{child['id']}) is in multiple places in #{CURRENT_YEAR}".red
else
current_years_children[fid] = child
end
end
end
CSV.open("#{dir}/children_#{CURRENT_YEAR}.csv", 'wb') do |csv|
csv << [
'ID',
'School',
'First',
'Last',
'Birthdate',
'Ethnicity Original',
'Ethnicity Survey',
'Ethnicity Normalized',
'Is AFAM',
'Is ASAM',
'Is Latinx',
'Is ME',
'Is NATAM',
'Is PI',
'Is White',
'Is Mixed',
'Is GOM',
'Is AFAM Latinx',
'Household Income',
'Is Low Income',
'Is Medium Income',
'Is High Income',
'Dominant Language',
"Classroom in #{CURRENT_YEAR}",
"Level in #{CURRENT_YEAR}",
"In Infant/Toddler Classroom in #{CURRENT_YEAR}",
"Start of #{CURRENT_YEAR}",
"End of #{CURRENT_YEAR}",
"Age at Start of #{CURRENT_YEAR}",
"Age in Months at Start of #{CURRENT_YEAR}",
"Enrolled in #{CURRENT_YEAR}",
"Enrolled in #{PREVIOUS_YEAR}",
"Enrolled at Different School in #{PREVIOUS_YEAR}",
"Matched Child ID",
"Aging out Midyear",
"Ignored",
'Notes',
]
schools.each do |school|
current_year = school['current_year']
next if current_year['children'].empty?
puts school['name'].bold
start_date = Date.parse(current_year['start_date'])
stop_date = Date.parse(current_year['stop_date'])
current_year['children'].each do |child|
notes = []
ignored = child['ignore']
if ignored
notes.concat(reasons_child_ignored_details(child))
end
classroom = get_child_active_classroom(child)
is_currently_enrolled_in_network, _ = does_children_collection_include(current_years_children, child)
was_previously_enrolled_in_network, previous_year_child_match = does_children_collection_include(previous_years_children, child)
previous_year_child_match_id = nil
if was_previously_enrolled_in_network
previous_year_child_match_id = previous_year_child_match['id']
notes << "Matched with previous year child - id: #{previous_year_child_match_id} name: #{name(previous_year_child_match)}"
end
# was_previously_at_current_school = previous_year['children'].any? {|c| c['fingerprint'] == child['fingerprint'] && not(c['ignore'])}
was_previously_at_current_school = was_previously_enrolled_in_network && child['school']['id'] == previous_year_child_match['school']['id']
was_previously_at_different_school = was_previously_enrolled_in_network && not(was_previously_at_current_school)
csv << [
child['id'],
school['name'],
child['first_name'],
child['last_name'],
child['birth_date'],
child['ethnicity_original'],
child['ethnicity_survey_original'],
"[#{child['ethnicity']&.map{|v| "\"#{v}\""}.join(", ")}]",
child['is_afam'],
child['is_asam'],
child['is_latinx'],
child['is_me'],
child['is_natam'],
child['is_pi'],
child['is_white'],
child['is_mixed'],
child['is_gom'],
child['is_afam_latinx'],
child['household_income'],
child['is_low_income'],
child['is_medium_income'],
child['is_high_income'],
child['dominant_language'],
classroom ? classroom['name'] : nil,
classroom ? classroom['level'] : nil,
is_child_in_infant_toddler_classroom?(child),
start_date,
stop_date,
format_age(age_on(child, start_date)),
age_on(child, start_date),
is_currently_enrolled_in_network,
was_previously_enrolled_in_network,
was_previously_at_different_school,
previous_year_child_match_id,
too_old?(classroom, age_on(child, stop_date)),
ignored,
notes.join("\n"),
]
end
end
end
CSV.open("#{dir}/children_#{PREVIOUS_YEAR}.csv", 'wb') do |csv|
csv << [
'ID',
'School',
'First',
'Last',
'Birthdate',
'Ethnicity Original',
'Ethnicity Survey',
'Ethnicity Normalized',
'Is AFAM',
'Is ASAM',
'Is Latinx',
'Is ME',
'Is NATAM',
'Is PI',
'Is White',
'Is Mixed',
'Is GOM',
'Is AFAM Latinx',
'Household Income',
'Is Low Income',
'Is Medium Income',
'Is High Income',
'Dominant Language',
"Classroom in #{PREVIOUS_YEAR}",
"Level in #{PREVIOUS_YEAR}",
"In Infant/Toddler Classroom in #{PREVIOUS_YEAR}",
"Start of #{CURRENT_YEAR}",
"Age at Start of #{CURRENT_YEAR}",
"Age in Months at Start of #{CURRENT_YEAR}",
"Enrolled in #{PREVIOUS_YEAR}",
"Enrolled in #{CURRENT_YEAR}",
"Enrolled at Different School in #{CURRENT_YEAR}",
"Matched Child ID",
"Aging out of Level",
"Age Appropriate for School in #{CURRENT_YEAR}",
"Graduated School",
"Continued School",
"Continued Network",
"Dropped School",
'Kindergarten Eligible Year',
'Exit Reason (teacher)',
'Graduated According to Teacher',
'Exit Reason (parent)',
'Graduated According to Parent',
'Ignored',
'Added Manually (previously deleted from TC)',
'Notes',
]
puts
puts '=' * 100
puts "Seeing which children from #{PREVIOUS_YEAR} are continued".bold
puts '=' * 100
schools.each do |school|
#next if (current_year = school['current_year'])['sessions'].empty? || (previous_year = school['previous_year'])['sessions'].empty?
current_year = school['current_year']
previous_year = school['previous_year']
stats[school['id']] = school_stats = {
graduated_school: [],
graduated_school_gom: [],
graduated_school_white: [],
graduated_school_low_income: [],
graduated_school_medium_income: [],
graduated_school_high_income: [],
graduated_school_and_continued_in_network: [],
graduated_school_and_continued_in_network_gom: [],
graduated_school_and_continued_in_network_white: [],
graduated_school_and_continued_in_network_low_income: [],
graduated_school_and_continued_in_network_medium_income: [],
graduated_school_and_continued_in_network_high_income: [],
continued: [],
continued_gom: [],
continued_white: [],
continued_low_income: [],
continued_medium_income: [],
continued_high_income: [],
continued_at_school: [],
continued_at_school_gom: [],
continued_at_school_white: [],
continued_at_school_low_income: [],
continued_at_school_medium_income: [],
continued_at_school_high_income: [],
continued_in_network: [],
continued_in_network_gom: [],
continued_in_network_white: [],
continued_in_network_low_income: [],
continued_in_network_medium_income: [],
continued_in_network_high_income: [],
continued_at_school_kindergarten: [],
continued_in_network_kindergarten: [],
continued_at_school_infant_toddler: [],
continued_in_network_infant_toddler: [],
continued_at_school_kindergarten_or_infant_toddler: [],
continued_in_network_kindergarten_or_infant_toddler: [],
dropped_school: [],
dropped_school_gom: [],
dropped_school_white: [],
dropped_school_low_income: [],
dropped_school_medium_income: [],
dropped_school_high_income: [],
dropped_school_but_continued_in_network: [],
dropped_school_but_continued_in_network_gom: [],
dropped_school_but_continued_in_network_white: [],
dropped_school_but_continued_in_network_low_income: [],
dropped_school_but_continued_in_network_medium_income: [],
dropped_school_but_continued_in_network_high_income: [],
dropped_school_kindergarten: [],
dropped_school_but_continued_in_network_kindergarten: [],
dropped_school_infant_toddler: [],
dropped_school_but_continued_in_network_infant_toddler: [],
dropped_school_kindergarten_or_infant_toddler: [],
dropped_school_but_continued_in_network_kindergarten_or_infant_toddler: [],
enrolled_current_year: current_year['children'].reject{ |c| c['ignore'] },
enrolled_current_year_gom: [],
enrolled_current_year_white: [],
enrolled_current_year_low_income: [],
enrolled_current_year_medium_income: [],
enrolled_current_year_high_income: [],
enrolled_previous_year: previous_year['children'].reject{ |c| c['ignore'] },
enrolled_previous_year_gom: [],
enrolled_previous_year_white: [],
enrolled_previous_year_low_income: [],
enrolled_previous_year_medium_income: [],
enrolled_previous_year_high_income: [],
exit_reason_teacher_graduated: [],
exit_reason_teacher_relocated: [],
exit_reason_teacher_expense: [],
exit_reason_teacher_hours_offered: [],
exit_reason_teacher_location: [],
exit_reason_teacher_eligible_for_kindergarten: [],
exit_reason_teacher_asked_to_leave: [],
exit_reason_teacher_joined_sibling: [],
exit_reason_teacher_no_lottery_spot: [],
exit_reason_teacher_bad_fit: [],
exit_reason_teacher_equity: [],
exit_reason_teacher_family_dissatisfied: [],
exit_reason_teacher_natural_disaster: [],
exit_reason_teacher_entered_public_system: [],
exit_reason_teacher_transferred_multi_year: [],
exit_reason_parent_graduated: [],
exit_reason_parent_relocated: [],
exit_reason_parent_expense: [],
exit_reason_parent_hours_offered: [],
exit_reason_parent_location: [],
exit_reason_parent_eligible_for_kindergarten: [],
exit_reason_parent_asked_to_leave: [],
exit_reason_parent_joined_sibling: [],
exit_reason_parent_no_lottery_spot: [],
exit_reason_parent_bad_fit: [],
exit_reason_parent_equity: [],
exit_reason_parent_family_dissatisfied: [],
exit_reason_parent_natural_disaster: [],
exit_reason_parent_entered_public_system: [],
exit_reason_parent_transferred_multi_year: []