Skip to content

How to run a report

Bess Sadler edited this page Mar 5, 2019 · 1 revision

Sometimes schools want specific data about all their ETDs. It often doesn't make sense to build this into the application, because what they are asking for can change year-to-year and it's more like a report query that we want to be able to run on demand. To do this, make a file like this, and run it via rails runner:

# How to use this file:
# Put it on a laeviagata server in /home/deploy
# cd /opt/laevigata/current
# RAILS_ENV=production bundle exec rails runner "eval(File.read '/home/deploy/rollins_report.rb')"

require 'csv'

etds = Etd.where(school: "Rollins School of Public Health")

puts etds.count

terms_wanted = ["Summer 2017", "Fall 2017", "Winter 2018", "Spring 2018", "Summer 2018", "Fall 2018", "Winter 2018"]

etds_2018 = etds.select { |m| terms_wanted.include? m.graduation_year }

puts "Rollins had #{etds_2018.count} etds in the requested terms"

    CSV.open("csv_report.csv", "wb", write_headers: true, headers: ["Creator", "Advisor", "Committee Members", "Partnering Agencies", "Date", "Program", "Graduation Year", "Degree"]) do |csv|
      etds_2018.each do |etd|
        creator = etd.creator.to_a
        chair = etd.committee_chair_name.to_a.join("; ")
        members = etd.committee_members_names.to_a.join("; ")
        agencies = etd.partnering_agency.to_a.join("; ")
        date =  etd.date_uploaded.to_date
        program = etd.research_field.first
        graduation_year = etd.graduation_year
        degree = etd.degree.first
        puts etd.title.first
        csv << [creator.first, chair, members, agencies, date, program, graduation_year, degree]
      end
    end
    puts 'export complete.'