2023-06-04 00:09:25 +00:00
|
|
|
require "http/client"
|
|
|
|
require "http/headers"
|
|
|
|
require "http/request"
|
|
|
|
require "ishi/html"
|
|
|
|
require "json"
|
2023-06-04 14:52:30 +00:00
|
|
|
require "uuid"
|
2023-06-04 00:09:25 +00:00
|
|
|
|
|
|
|
class Handler
|
2023-06-04 21:17:22 +00:00
|
|
|
def format_buffer(buffer, canvas_name, title = "")
|
2023-06-04 14:52:30 +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 20:43:51 +00:00
|
|
|
# we can have multiple charts in a page
|
|
|
|
# title is added on top of the chart
|
2023-06-04 14:52:30 +00:00
|
|
|
|
|
|
|
html = buffer.to_s.split("\n")
|
|
|
|
html = html[html.index("<script type=\"text/javascript\">")..html.index("</script>")]
|
2023-06-04 20:43:51 +00:00
|
|
|
html = "<b>#{title}</b>" + html.join("\n") + %(
|
2023-06-04 14:52:30 +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 20:43:51 +00:00
|
|
|
# This ID needs to be unique in case
|
2023-06-04 14:52:30 +00:00
|
|
|
# we have 2 charts in the same page
|
|
|
|
html.gsub("gnuplot_canvas", canvas_name)
|
|
|
|
end
|
|
|
|
|
2023-06-04 00:09:25 +00:00
|
|
|
def query(sql)
|
2023-06-04 21:17:22 +00:00
|
|
|
# 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
|
|
|
|
|
2023-06-04 00:09:25 +00:00
|
|
|
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")
|
2023-06-04 21:17:22 +00:00
|
|
|
return results["values"].as_a
|
2023-06-04 00:09:25 +00:00
|
|
|
end
|
2023-06-04 21:17:22 +00:00
|
|
|
# No result, return nil
|
|
|
|
nil
|
2023-06-04 00:09:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
nombres = [] of String
|
|
|
|
|
2023-06-04 00:18:50 +00:00
|
|
|
def normalize_name(s)
|
|
|
|
# Remove diacritics, turn lowercase
|
|
|
|
normalized = s.unicode_normalize(:nfkd).chars
|
|
|
|
normalized.reject! { |c|
|
|
|
|
!c.ascii_letter?
|
|
|
|
}.join("").downcase
|
|
|
|
end
|
|
|
|
|
2023-06-04 00:09:25 +00:00
|
|
|
def run(request : HTTP::Request)
|
|
|
|
unless (body = request.body).nil?
|
|
|
|
query = JSON.parse(body)
|
|
|
|
nombres = query["i"].as_s.split(",").map { |n| n.strip }
|
|
|
|
nombres.reject! { |n| n.size == 0 }
|
|
|
|
end
|
|
|
|
|
|
|
|
if nombres.nil? || nombres.empty?
|
|
|
|
nombres = ["maria", "juan"]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Remove all diacritics and whatnot
|
|
|
|
nombres = nombres.map { |n|
|
2023-06-04 00:18:50 +00:00
|
|
|
normalize_name n
|
2023-06-04 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
puts "Processing #{nombres}"
|
|
|
|
|
|
|
|
buffer = IO::Memory.new
|
|
|
|
Ishi.new(buffer) do
|
|
|
|
canvas_size(800, 300)
|
|
|
|
show_key(true)
|
|
|
|
xrange(1922..2015)
|
|
|
|
nombres.map { |nombre|
|
|
|
|
sql = "SELECT anio, contador FROM nombres WHERE nombre = '#{nombre}' ORDER BY anio"
|
|
|
|
x = Array(Int32).new
|
|
|
|
y = Array(Int32).new
|
2023-06-04 21:17:22 +00:00
|
|
|
results = query(sql)
|
2023-06-04 22:34:45 +00:00
|
|
|
if results.nil? # No results, all 0s
|
2023-06-04 21:17:22 +00:00
|
|
|
x = (1922..2015).to_a
|
2023-06-04 22:34:45 +00:00
|
|
|
y = x.map { |_| 0 }
|
|
|
|
else # We got results
|
|
|
|
values = Hash(Int32, Int32).new(default_value: 0)
|
2023-06-04 21:17:22 +00:00
|
|
|
results.map { |r|
|
2023-06-04 22:34:45 +00:00
|
|
|
values[r[0].as_i] = r[1].as_i
|
|
|
|
}
|
|
|
|
(1922..2015).map { |year|
|
|
|
|
x << year
|
|
|
|
y << values[year] # Defaults to 0, so we have the whole set
|
2023-06-04 21:17:22 +00:00
|
|
|
}
|
|
|
|
end
|
2023-06-04 00:09:25 +00:00
|
|
|
plot(x, y, title: nombre.titleize, style: :lines, linewidth: 3)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-06-04 21:17:22 +00:00
|
|
|
return {
|
2023-06-04 14:52:30 +00:00
|
|
|
body: format_buffer(buffer, "historico"),
|
2023-06-04 00:09:25 +00:00
|
|
|
status_code: 200,
|
|
|
|
headers: HTTP::Headers{"Content-Type" => "text/html"},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|