require "http/request" require "http/headers" require "ishi/html" require "json" class Handler def format_buffer(buffer, canvas_name, title = "") # 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 # we can have multiple charts in a page # title is added on top of the chart html = buffer.to_s.split("\n") html = html[html.index("")] html = "#{title}" + html.join("\n") + %(
Sorry, your browser seems not to support the HTML 5 canvas element
) # This ID needs to be unique in case # 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) # 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 # } 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", "") año = query.fetch("y", "") if !prefijo.empty? prefijo = normalize_name(prefijo) else prefijo = nil end if !["f", "m"].includes?(genero) genero = nil end año = año.to_i? 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 # We have both 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] } # In this context, remove all composite names datos.reject! { |r| r[1].to_s.includes? " " } if genero datos = split_por_genero(datos)[genero] end datos = datos[..10] if datos.size > 1 title = "¿Puede ser ... #{datos[0][1].to_s.titleize}? ¿O capaz que #{datos[1][1].to_s.titleize}? ¡Contame más!" elsif datos.size == 1 title = "Me parece que ... #{datos[0][1].to_s.titleize}!" else title = "No tengo idea!" end buffer = IO::Memory.new Ishi.new(buffer) do x = (0..datos.size - 1).to_a y = datos.map { |r| r[0].to_f / 1000 } yrange(0..(y.max*1.1).to_i + 1) xtics = Hash(Float64, String).new datos.each_with_index { |r, i| xtics[i.to_f] = r[1].to_s.titleize } canvas_size(800, 300) plot(x, y, style: :boxes, fs: 0.25) .boxwidth(0.5) .show_key(false) .ylabel("Popularidad (miles)") .xtics(xtics) end { body: format_buffer(buffer, "busqueda", title), status_code: 200, headers: HTTP::Headers{"Content-Type" => "text/html"}, } end end