Compare commits

..

No commits in common. "92638157275bf51dc0076454cfb901f5bbe4f127" and "823cc24e8af3a4896739d866898ea938fac52d05" have entirely different histories.

2 changed files with 15 additions and 26 deletions

View File

@ -4,7 +4,7 @@ require "ishi/html"
require "json" require "json"
class Handler class Handler
def format_buffer(buffer, canvas_name, title = "") def format_buffer(buffer, canvas_name, title)
# Process the gnuplot output so it works in the page # Process the gnuplot output so it works in the page
# #
# buffer is the Ishi output # buffer is the Ishi output

View File

@ -6,17 +6,16 @@ require "json"
require "uuid" require "uuid"
class Handler class Handler
def format_buffer(buffer, canvas_name, title = "") def format_buffer(buffer, canvas_name)
# Process the gnuplot output so it works in the page # Process the gnuplot output so it works in the page
# #
# buffer is the Ishi output # buffer is the Ishi output
# name is a string to replace for gnuplot_canvas so # name is a string to replace for gnuplot_canvas so
# we can have multiple charts in a page # we can have multiple charts in a page
# title is added on top of the chart
html = buffer.to_s.split("\n") html = buffer.to_s.split("\n")
html = html[html.index("<script type=\"text/javascript\">")..html.index("</script>")] html = html[html.index("<script type=\"text/javascript\">")..html.index("</script>")]
html = "<b>#{title}</b>" + html.join("\n") + %( html = html.join("\n") + %(
<div class="gnuplot"> <div class="gnuplot">
<canvas id="Tile" width="32" height="32" hidden></canvas> <canvas id="Tile" width="32" height="32" hidden></canvas>
<table class="plot"> <table class="plot">
@ -37,11 +36,6 @@ class Handler
end end
def query(sql) 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}) params = URI::Params.encode({"q": sql})
response = HTTP::Client.get URI.new( response = HTTP::Client.get URI.new(
"http", "http",
@ -53,10 +47,12 @@ class Handler
# This API only has a values key when there are actual results # This API only has a values key when there are actual results
results = JSON.parse(response.body)["results"][0].as_h results = JSON.parse(response.body)["results"][0].as_h
if results.has_key?("values") if results.has_key?("values")
return results["values"].as_a return results["values"].as_a.map { |r|
[r[0].as_i, r[1].as_i]
}
end end
# No result, return nil # Return a dummy result
nil [[1922, 0]]
end end
nombres = [] of String nombres = [] of String
@ -96,22 +92,15 @@ class Handler
sql = "SELECT anio, contador FROM nombres WHERE nombre = '#{nombre}' ORDER BY anio" sql = "SELECT anio, contador FROM nombres WHERE nombre = '#{nombre}' ORDER BY anio"
x = Array(Int32).new x = Array(Int32).new
y = Array(Int32).new y = Array(Int32).new
results = query(sql) query(sql).map { |r|
if results.nil? # No results, all 0s x << r[0]
x = (1922..2015).to_a y << r[1]
y = x.map {|_| 0} }
else # We got results
displayed = true
results.map { |r|
x << r[0].as_i
y << r[1].as_i
}
end
plot(x, y, title: nombre.titleize, style: :lines, linewidth: 3) plot(x, y, title: nombre.titleize, style: :lines, linewidth: 3)
} }
end end
return { {
body: format_buffer(buffer, "historico"), body: format_buffer(buffer, "historico"),
status_code: 200, status_code: 200,
headers: HTTP::Headers{"Content-Type" => "text/html"}, headers: HTTP::Headers{"Content-Type" => "text/html"},