Compare commits

...

3 Commits

Author SHA1 Message Date
Roberto Alsina 9263815727 * Misma funcion query que la otra funcion
* Mejor soporte de nombres que no estan en la base de datos
2023-06-04 18:17:22 -03:00
Roberto Alsina a55784a4cd Valor default para title 2023-06-04 18:16:21 -03:00
Roberto Alsina 4c46cb2958 Sync format_buffer code 2023-06-04 17:43:51 -03:00
2 changed files with 26 additions and 15 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,16 +6,17 @@ require "json"
require "uuid" require "uuid"
class Handler class Handler
def format_buffer(buffer, canvas_name) 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
# 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 = html.join("\n") + %( html = "<b>#{title}</b>" + 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">
@ -30,12 +31,17 @@ class Handler
</script> </script>
</div> </div>
) )
# This ID needs to be unique in case # This ID needs to be unique in case
# we have 2 charts in the same page # we have 2 charts in the same page
html.gsub("gnuplot_canvas", canvas_name) html.gsub("gnuplot_canvas", canvas_name)
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",
@ -47,12 +53,10 @@ 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.map { |r| return results["values"].as_a
[r[0].as_i, r[1].as_i]
}
end end
# Return a dummy result # No result, return nil
[[1922, 0]] nil
end end
nombres = [] of String nombres = [] of String
@ -92,15 +96,22 @@ 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
query(sql).map { |r| results = query(sql)
x << r[0] if results.nil? # No results, all 0s
y << r[1] 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) 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"},