2023-06-04 14:53:24 +00:00
|
|
|
require "http/request"
|
|
|
|
require "http/headers"
|
|
|
|
require "ishi/html"
|
|
|
|
require "json"
|
|
|
|
|
|
|
|
class Handler
|
2023-06-04 21:16:21 +00:00
|
|
|
def format_buffer(buffer, canvas_name, title = "")
|
2023-06-04 14:53:24 +00:00
|
|
|
# Process the gnuplot output so it works in the page
|
|
|
|
#
|
|
|
|
# buffer is the Ishi output
|
|
|
|
# name is a string to replace for gnuplot_canvas so
|
2023-06-04 18:30:30 +00:00
|
|
|
# we can have multiple charts in a page
|
|
|
|
# title is added on top of the chart
|
2023-06-04 14:53:24 +00:00
|
|
|
|
|
|
|
html = buffer.to_s.split("\n")
|
|
|
|
html = html[html.index("<script type=\"text/javascript\">")..html.index("</script>")]
|
2023-06-04 18:30:30 +00:00
|
|
|
html = "<b>#{title}</b>" + html.join("\n") + %(
|
2023-06-04 14:53:24 +00:00
|
|
|
<div class="gnuplot">
|
|
|
|
<canvas id="Tile" width="32" height="32" hidden></canvas>
|
|
|
|
<table class="plot">
|
|
|
|
<tr><td>
|
|
|
|
<canvas id="gnuplot_canvas" width="800" height="300" tabindex="0">
|
|
|
|
Sorry, your browser seems not to support the HTML 5 canvas element
|
|
|
|
</canvas>
|
|
|
|
</td></tr>
|
|
|
|
</table>
|
|
|
|
<script type="text/javascript" defer>
|
|
|
|
gnuplot.init(); gnuplot_canvas();
|
|
|
|
</script>
|
|
|
|
</div>
|
|
|
|
)
|
2023-06-04 18:30:30 +00:00
|
|
|
# This ID needs to be unique in case
|
2023-06-04 14:53:24 +00:00
|
|
|
# we have 2 charts in the same page
|
|
|
|
html.gsub("gnuplot_canvas", canvas_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def query(sql)
|
|
|
|
# Runs a SQL query against the Rqlite database.
|
|
|
|
#
|
|
|
|
# Returns an array of values (which need to be casted)
|
|
|
|
# Or nil if there are no results
|
|
|
|
|
|
|
|
params = URI::Params.encode({"q": sql})
|
|
|
|
response = HTTP::Client.get URI.new(
|
|
|
|
"http",
|
|
|
|
"10.61.0.1",
|
|
|
|
4001,
|
|
|
|
"/db/query",
|
|
|
|
params)
|
|
|
|
|
|
|
|
# This API only has a values key when there are actual results
|
|
|
|
results = JSON.parse(response.body)["results"][0].as_h
|
|
|
|
if results.has_key?("values")
|
|
|
|
return results["values"].as_a
|
|
|
|
end
|
|
|
|
# No result, return nil
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def normalize_name(s)
|
|
|
|
# Remove diacritics, turn lowercase
|
|
|
|
normalized = s.unicode_normalize(:nfkd).chars
|
|
|
|
normalized.reject! { |c|
|
|
|
|
!c.ascii_letter?
|
|
|
|
}.join("").downcase
|
|
|
|
end
|
|
|
|
|
|
|
|
def feminidad(nombre)
|
|
|
|
nombre = nombre.to_s.upcase
|
|
|
|
sql1 = %(
|
|
|
|
SELECT COALESCE(frecuencia,0)
|
|
|
|
FROM mujeres WHERE nombre='#{nombre}'
|
|
|
|
|
|
|
|
)
|
|
|
|
sql2 = %(
|
|
|
|
SELECT COALESCE(frecuencia,0)
|
|
|
|
FROM hombres WHERE nombre='#{nombre}'
|
|
|
|
)
|
|
|
|
|
|
|
|
# Yes this database is upper case
|
|
|
|
mujeres = query(sql1)
|
|
|
|
mujeres = mujeres.nil? ? 0 : mujeres[0][0].as_i
|
|
|
|
hombres = query(sql2)
|
|
|
|
hombres = hombres.nil? ? 0 : hombres[0][0].as_i
|
|
|
|
if hombres == mujeres == 0
|
|
|
|
return 0.5
|
|
|
|
end
|
|
|
|
return mujeres / (hombres + mujeres)
|
|
|
|
end
|
|
|
|
|
|
|
|
def split_por_genero(nombres)
|
|
|
|
femeninos = Array(Array(String | Int32)).new
|
|
|
|
masculinos = Array(Array(String | Int32)).new
|
|
|
|
nombres.map { |n|
|
|
|
|
fem = feminidad(n[1])
|
|
|
|
# El overlap en 0.5 es intencional!
|
|
|
|
if fem >= 0.5
|
|
|
|
femeninos << n
|
|
|
|
end
|
|
|
|
if fem <= 0.5
|
|
|
|
masculinos << n
|
|
|
|
end
|
|
|
|
}
|
|
|
|
{
|
|
|
|
"f": femeninos,
|
|
|
|
"m": masculinos,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(request : HTTP::Request)
|
2023-06-04 22:08:36 +00:00
|
|
|
# Try to find most popular names based on a prefix, year and gender.
|
|
|
|
#
|
|
|
|
# Request body is JSON in this form:
|
|
|
|
#
|
|
|
|
# {
|
|
|
|
# p: prefijo del nombre,
|
|
|
|
# g: genero del nombre,
|
|
|
|
# y: año de nacimiento
|
|
|
|
# }
|
|
|
|
|
2023-06-04 14:53:24 +00:00
|
|
|
unless (body = request.body).nil?
|
|
|
|
query = Hash(String, String).from_json(body)
|
|
|
|
else
|
|
|
|
query = {"p": "", "g": "", a: ""}
|
|
|
|
end
|
|
|
|
|
|
|
|
p! query
|
|
|
|
|
|
|
|
# Sanitize input.
|
|
|
|
# Each one either a valid string or nil
|
|
|
|
prefijo = query.fetch("p", "")
|
|
|
|
genero = query.fetch("g", "")
|
2023-06-04 22:08:36 +00:00
|
|
|
año = query.fetch("y", "")
|
2023-06-04 14:53:24 +00:00
|
|
|
|
|
|
|
if !prefijo.empty?
|
|
|
|
prefijo = normalize_name(prefijo)
|
|
|
|
else
|
|
|
|
prefijo = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if !["f", "m"].includes?(genero)
|
|
|
|
genero = nil
|
|
|
|
end
|
|
|
|
|
2023-06-04 22:08:36 +00:00
|
|
|
año = año.to_i?
|
2023-06-04 14:53:24 +00:00
|
|
|
|
|
|
|
if prefijo.nil? && año.nil?
|
|
|
|
# Global totals
|
|
|
|
sql = %(
|
|
|
|
SELECT total, nombre
|
|
|
|
FROM totales
|
|
|
|
ORDER BY total DESC
|
|
|
|
LIMIT 50
|
|
|
|
)
|
|
|
|
elsif prefijo.nil? && !año.nil?
|
|
|
|
# Per-year totals
|
|
|
|
sql = %(
|
|
|
|
SELECT contador, nombre
|
|
|
|
FROM nombres
|
|
|
|
WHERE
|
|
|
|
anio = '#{año}'
|
|
|
|
ORDER BY contador DESC
|
|
|
|
LIMIT 50
|
|
|
|
)
|
|
|
|
elsif !prefijo.nil? && año.nil?
|
|
|
|
# Filter only by prefix
|
|
|
|
sql = %(
|
|
|
|
SELECT total, nombre
|
|
|
|
FROM totales
|
|
|
|
WHERE
|
|
|
|
nombre LIKE '#{prefijo}%'
|
|
|
|
ORDER BY total DESC
|
|
|
|
LIMIT 50
|
|
|
|
)
|
|
|
|
else
|
2023-06-04 22:08:36 +00:00
|
|
|
# We have both
|
2023-06-04 14:53:24 +00:00
|
|
|
sql = %(
|
|
|
|
SELECT contador, nombre
|
|
|
|
FROM nombres
|
|
|
|
WHERE
|
|
|
|
anio = '#{año}' AND
|
|
|
|
nombre LIKE '#{prefijo}%'
|
|
|
|
ORDER BY contador DESC
|
|
|
|
LIMIT 50
|
|
|
|
)
|
|
|
|
end
|
|
|
|
results = query(sql)
|
|
|
|
|
|
|
|
if results.nil?
|
|
|
|
# This is bad 😀
|
|
|
|
return {
|
|
|
|
body: "Que raro, no tengo *idea*!",
|
|
|
|
status_code: 200,
|
|
|
|
headers: HTTP::Headers{"Content-Type" => "text/html"},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
datos = results.map { |r|
|
|
|
|
[r[0].as_i, r[1].as_s]
|
|
|
|
}
|
|
|
|
|
2023-06-04 18:30:30 +00:00
|
|
|
# In this context, remove all composite names
|
|
|
|
datos.reject! { |r|
|
|
|
|
r[1].to_s.includes? " "
|
|
|
|
}
|
|
|
|
|
2023-06-04 14:53:24 +00:00
|
|
|
if genero
|
|
|
|
datos = split_por_genero(datos)[genero]
|
|
|
|
end
|
|
|
|
datos = datos[..10]
|
|
|
|
|
2023-06-04 18:30:30 +00:00
|
|
|
if datos.size > 1
|
2023-06-04 20:39:03 +00:00
|
|
|
title = "¿Puede ser ... #{datos[0][1].to_s.titleize}? ¿O capaz que #{datos[1][1].to_s.titleize}? ¡Contame más!"
|
2023-06-04 18:30:30 +00:00
|
|
|
elsif datos.size == 1
|
|
|
|
title = "Me parece que ... #{datos[0][1].to_s.titleize}!"
|
|
|
|
else
|
|
|
|
title = "No tengo idea!"
|
|
|
|
end
|
|
|
|
|
2023-06-04 14:53:24 +00:00
|
|
|
buffer = IO::Memory.new
|
|
|
|
Ishi.new(buffer) do
|
2023-06-04 18:30:30 +00:00
|
|
|
x = (0..datos.size - 1).to_a
|
|
|
|
y = datos.map { |r|
|
2023-06-04 20:39:03 +00:00
|
|
|
r[0].to_f / 1000
|
2023-06-04 18:30:30 +00:00
|
|
|
}
|
2023-06-05 19:02:54 +00:00
|
|
|
yrange(0..(y.max*1.1).to_i + 1)
|
2023-06-04 18:30:30 +00:00
|
|
|
|
|
|
|
xtics = Hash(Float64, String).new
|
|
|
|
datos.each_with_index { |r, i|
|
|
|
|
xtics[i.to_f] = r[1].to_s.titleize
|
|
|
|
}
|
|
|
|
|
2023-06-04 14:53:24 +00:00
|
|
|
canvas_size(800, 300)
|
2023-06-04 18:30:30 +00:00
|
|
|
plot(x, y, style: :boxes, fs: 0.25)
|
|
|
|
.boxwidth(0.5)
|
|
|
|
.show_key(false)
|
|
|
|
.ylabel("Popularidad (miles)")
|
|
|
|
.xtics(xtics)
|
2023-06-04 14:53:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
{
|
2023-06-04 18:30:30 +00:00
|
|
|
body: format_buffer(buffer, "busqueda", title),
|
2023-06-04 14:53:24 +00:00
|
|
|
status_code: 200,
|
|
|
|
headers: HTTP::Headers{"Content-Type" => "text/html"},
|
|
|
|
}
|
|
|
|
end
|
2023-06-04 18:30:30 +00:00
|
|
|
end
|