Skip to content

Commit

Permalink
Reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
ferblape committed Feb 9, 2021
1 parent 384d240 commit e8d1640
Show file tree
Hide file tree
Showing 6 changed files with 11,019 additions and 4 deletions.
5 changes: 1 addition & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ gem "pg", "~> 0.19"
gem "rails", "~> 5.2.0"
gem "bootsnap"
gem 'listen'
gem "ine-places"

group :development, :test do
gem 'byebug'
end

group :development do
gem 'spring'
end
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ GEM
activesupport (>= 4.2.0)
i18n (1.8.5)
concurrent-ruby (~> 1.0)
ine-places (0.6.1)
activesupport (>= 4.2)
zeitwerk (>= 2.1.2)
listen (3.4.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
Expand Down Expand Up @@ -123,13 +126,15 @@ GEM
websocket-driver (0.7.3)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.4.2)

PLATFORMS
ruby

DEPENDENCIES
bootsnap
byebug
ine-places
listen
pg (~> 0.19)
rails (~> 5.2.0)
Expand Down
54 changes: 54 additions & 0 deletions app/models/reporter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'ine'
require 'csv'

class Reporter
def initialize(database)
raise 'Missing database name' if database.blank?

@database = database
population = CSV.read("docs/population.csv", headers: true)
@population = Hash[population.map{ |r| [r["place_id"].to_i, r["population"].to_i]}]
end

def report
places_ids = INE::Places::Place.all.map(&:id).map(&:to_i).sort
(2020..2020).each do |year|
puts "Reporting #{year}"
found = []
results = connection.execute("select * from tb_inventario_#{year} where codente like '%AA000'")
results.each do |result|
begin
found << result["codente"].gsub(/(\d+)AA000/, "#{$1}").to_i
rescue
puts result["codente"]
end
end
generate_report(year, places_ids, found)
end
end

private

def connection
@connection ||= ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
database: @database,
username: ENV["PG_USERNAME"],
password: ENV["PG_PASSWORD"],
host: ENV["PG_HOST"]
).connection
end

def generate_report(year, places_ids, found)
FileUtils.mkdir_p("docs")
places = (places_ids - found).map{ |missing_place_id| INE::Places::Place.find(missing_place_id) }
s = []
places.group_by(&:province).each do |province, places|
s << "## #{province.name}"
places.sort{ |p2, p1| @population[p1.id.to_i] <=> @population[p2.id.to_i] }.each do |place|
s << "- #{place.name}"
end
end
File.write("docs/#{year}.md", s.join("\n"))
end
end
Loading

0 comments on commit e8d1640

Please sign in to comment.