Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save garethrees/a565d32f50638872a3d719f8c2882767 to your computer and use it in GitHub Desktop.
Save garethrees/a565d32f50638872a3d719f8c2882767 to your computer and use it in GitHub Desktop.
Alaveteli Public vs Embargoed Requests per Month / Quarter
# MONTHLY
def month_starts(start_year, start_month, end_year, end_month)
(Date.new(start_year, start_month)..Date.new(end_year, end_month)).
select { |d| d.day == 1 }
end
def embargoed_ever(range)
InfoRequest.
joins(:info_request_events).
where(info_requests: { created_at: range }).
where(info_request_events: { event_type: 'set_embargo' })
end
def embargoed_never(range)
InfoRequest.
where(info_requests: { created_at: range }).
where.not(id: embargoed_ever(range).select(:id))
end
data = [%w(period public_requests embargoed_requests)]
data = month_starts(2010, 1, 2019, 12).each_with_object(data) do |month_start, memo|
month_end = month_start.end_of_month
range = month_start..month_end
period = "#{month_start}-#{month_end}"
public_requests = embargoed_never(range).count
embargoed_requests = embargoed_ever(range).count
memo << [period, public_requests, embargoed_requests]
end
CSV.open("tmp/public_vs_private_requests.csv", "wb") do |csv|
data.each { |d| csv << d }
end
# QUARTERLY
def embargoed_ever(range)
InfoRequest.
joins(:info_request_events).
where(info_requests: { created_at: range }).
where(info_request_events: { event_type: 'set_embargo' })
end
def embargoed_never(range)
InfoRequest.
where(info_requests: { created_at: range }).
where.not(id: embargoed_ever(range).select(:id))
end
quarters = DateQuarter.quarters_between(Date.new(2010, 1), Date.new(2020, 1))
data = [%w(period public_requests embargoed_requests)]
data = quarters.each_with_object(data) do |q, memo|
range = q.first..q.last
period = "#{q.first.strftime('%Y-%m-%d')}-#{q.last.strftime('%Y-%m-%d')}"
public_requests = embargoed_never(range).count
embargoed_requests = embargoed_ever(range).count
memo << [period, public_requests, embargoed_requests]
end
CSV.open("tmp/public_vs_private_requests_quarterly.csv", "wb") do |csv|
data.each { |d| csv << d }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment