* Misma funcion query que la otra funcion
* Mejor soporte de nombres que no estan en la base de datos
This commit is contained in:
parent
a55784a4cd
commit
9263815727
@ -6,7 +6,7 @@ require "json"
|
||||
require "uuid"
|
||||
|
||||
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
|
||||
#
|
||||
# buffer is the Ishi output
|
||||
@ -37,6 +37,11 @@ class Handler
|
||||
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",
|
||||
@ -48,12 +53,10 @@ class Handler
|
||||
# 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.map { |r|
|
||||
[r[0].as_i, r[1].as_i]
|
||||
}
|
||||
return results["values"].as_a
|
||||
end
|
||||
# Return a dummy result
|
||||
[[1922, 0]]
|
||||
# No result, return nil
|
||||
nil
|
||||
end
|
||||
|
||||
nombres = [] of String
|
||||
@ -93,15 +96,22 @@ class Handler
|
||||
sql = "SELECT anio, contador FROM nombres WHERE nombre = '#{nombre}' ORDER BY anio"
|
||||
x = Array(Int32).new
|
||||
y = Array(Int32).new
|
||||
query(sql).map { |r|
|
||||
x << r[0]
|
||||
y << r[1]
|
||||
}
|
||||
results = query(sql)
|
||||
if results.nil? # No results, all 0s
|
||||
x = (1922..2015).to_a
|
||||
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)
|
||||
}
|
||||
end
|
||||
|
||||
{
|
||||
return {
|
||||
body: format_buffer(buffer, "historico"),
|
||||
status_code: 200,
|
||||
headers: HTTP::Headers{"Content-Type" => "text/html"},
|
||||
|
Loading…
Reference in New Issue
Block a user