Make split by gender faster

This commit is contained in:
Roberto Alsina 2024-05-15 20:48:29 -03:00
parent e4c44dcae1
commit cb9a24f4aa

View File

@ -42,27 +42,6 @@ class Handler
html.gsub("gnuplot_canvas", canvas_name)
end
def query(sql)
# Runs a SQL query against the database.
#
# Returns an array of values [[Year,Count]...]
# Or nil if there are no results
DB.open("postgres://#{USER}:#{PASS}@10.61.0.1:5432/nombres") do |cursor|
cursor.query sql do |result_set|
result = [] of Tuple(Int32, Int32)
result_set.each do
year = result_set.read(Int32)
contador = result_set.read(Int32)
result.push({year, contador})
end
return result
end
end
# No result, return nil
nil
end
def normalize_name(s)
# Remove diacritics, turn lowercase
normalized = s.unicode_normalize(:nfkd).chars
@ -71,13 +50,12 @@ class Handler
}.join("").downcase
end
def feminidad(nombre)
def feminidad(cursor, nombre)
# Yes this database is upper case
nombre = nombre.to_s.upcase
sql1 = %(
SELECT COALESCE(frecuencia,0)
FROM mujeres WHERE nombre='#{nombre}'
)
sql2 = %(
SELECT COALESCE(frecuencia,0)
@ -85,13 +63,11 @@ class Handler
)
hombres = mujeres = 0
DB.open("postgres://#{USER}:#{PASS}@10.61.0.1:5432/nombres") do |cursor|
cursor.query sql1 do |result|
mujeres = result.read(Int32)
end
cursor.query sql2 do |result|
hombres = result.read(Int32)
end
cursor.query sql1 do |result|
mujeres = result.read(Int32)
end
cursor.query sql2 do |result|
hombres = result.read(Int32)
end
if hombres == mujeres == 0
return 0.5
@ -99,7 +75,7 @@ class Handler
mujeres / (hombres + mujeres)
end
def split_por_genero(nombres)
def split_por_genero(cursor, nombres)
femeninos = Array(Tuple(Int32, String)).new
masculinos = Array(Tuple(Int32, String)).new
nombres.map { |nombre|
@ -200,8 +176,8 @@ class Handler
datos = [] of Tuple(Int32, String)
DB.open("postgres://#{USER}:#{PASS}@10.61.0.1:5432/nombres") do |cursor|
cursor.query sql do |result_set|
puts "loop"
result_set.each do
puts "loop"
valor = result_set.read(Int32)
nombre = result_set.read(String)
datos.push({valor, nombre})
@ -225,10 +201,11 @@ class Handler
row[1].to_s.includes? " "
}
if genero
datos = split_por_genero(datos)[genero]
puts "Data split by gender"
DB.open("postgres://#{USER}:#{PASS}@10.61.0.1:5432/nombres") do |cursor|
datos = split_por_genero(cursor, datos)[genero]
puts "Data split by gender"
end
end
datos = datos[..10]