Skip to content

Commit 1ee7cd6

Browse files
Add data filler
1 parent 76c05bc commit 1ee7cd6

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

lib/tasks/chapter.rake

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,14 @@ namespace :chapter do
248248
logger.debug response.code
249249
logger.debug response.body
250250
json_list = JSON.parse(response.body)
251+
252+
# Guard against data issues
253+
if json_list.empty?
254+
logger.error '[Error] Chapter list is empty.'
255+
logger.error "```\n#{response.body}\n```"
256+
exit(1)
257+
end
258+
251259
# Removes the "N/A" records
252260
json_list.delete_if { |c| c['chapterdesignation'].empty? }
253261

@@ -281,6 +289,76 @@ namespace :chapter do
281289
Chapter.inactive.update_all(manpower: 0, expansion: 0)
282290
end
283291

292+
desc 'Fill gaps in manpower survey data for all chapters'
293+
task fill_manpower_gaps: :environment do
294+
logger.info 'Starting manpower gaps fill process for all chapters...'
295+
296+
# Statistics tracking
297+
chapters_processed = 0
298+
chapters_skipped = 0
299+
total_gaps_filled = 0
300+
chapters_with_gaps = 0
301+
302+
Chapter.all.each do |chapter|
303+
# Get all existing survey dates for this chapter, ordered by date
304+
existing_surveys = chapter.manpower_surveys.order(:survey_date)
305+
306+
if existing_surveys.empty?
307+
logger.debug "Skipping #{chapter.name} (#{chapter.institution_name}): No survey data"
308+
chapters_skipped += 1
309+
next
310+
end
311+
312+
chapters_processed += 1
313+
earliest_date = existing_surveys.first.survey_date
314+
latest_date = existing_surveys.last.survey_date
315+
316+
logger.info "Processing #{chapter.name} (#{chapter.institution_name})..."
317+
logger.info " Date range: #{earliest_date} to #{latest_date}"
318+
319+
# Create a hash of existing surveys for quick lookup
320+
existing_dates = existing_surveys.index_by(&:survey_date)
321+
322+
gaps_filled_for_chapter = 0
323+
last_known_manpower = existing_surveys.first.manpower
324+
325+
# Iterate through each date in the range
326+
(earliest_date..latest_date).each do |date|
327+
if existing_dates[date]
328+
# Survey exists, update our last known manpower value
329+
last_known_manpower = existing_dates[date].manpower
330+
else
331+
# Gap found, create missing survey entry
332+
ManpowerSurvey.create!(
333+
chapter: chapter,
334+
survey_date: date,
335+
manpower: last_known_manpower
336+
)
337+
logger.debug " Filled gap on #{date} with manpower: #{last_known_manpower}"
338+
gaps_filled_for_chapter += 1
339+
total_gaps_filled += 1
340+
end
341+
end
342+
343+
if gaps_filled_for_chapter.positive?
344+
chapters_with_gaps += 1
345+
logger.info " Filled #{gaps_filled_for_chapter} gaps for #{chapter.name}"
346+
else
347+
logger.info " No gaps found for #{chapter.name}"
348+
end
349+
end
350+
351+
# Summary report
352+
logger.info ''
353+
logger.info 'Manpower gaps fill process completed!'
354+
logger.info '===================================='
355+
logger.info "Chapters processed: #{chapters_processed}"
356+
logger.info "Chapters skipped (no data): #{chapters_skipped}"
357+
logger.info "Total gaps filled: #{total_gaps_filled}"
358+
logger.info "Chapters with gaps: #{chapters_with_gaps}"
359+
logger.info ''
360+
end
361+
284362
desc 'Output to Wikipedia table'
285363
task wiki: :environment do
286364
chapters = Chapter.order('chapter_roll ASC')

0 commit comments

Comments
 (0)