Port c-historico to postgres
This commit is contained in:
parent
b0fafae4ef
commit
02e7594e3d
2
build.sh
2
build.sh
@ -13,6 +13,8 @@ pass iol-pass | faas-cli secret create iol-pass
|
||||
pass iol-user | faas-cli secret create iol-user
|
||||
pass iol-api-secret | faas-cli secret create iol-api-secret
|
||||
|
||||
pass nombres-user | faas-cli secret create nombres-user
|
||||
pass nombres-pass | faas-cli secret create nombres-pass
|
||||
|
||||
faas-cli publish -f functions.yml --platforms linux/arm64 --build-arg 'TEST_ENABLED=false' $*
|
||||
faas-cli deploy -f functions.yml $*
|
||||
|
@ -4,6 +4,11 @@ require "http/request"
|
||||
require "ishi/html"
|
||||
require "json"
|
||||
require "uuid"
|
||||
require "db"
|
||||
require "pg"
|
||||
|
||||
USER = File.read("/var/openfaas/secrets/nombres-user").strip
|
||||
PASS = File.read("/var/openfaas/secrets/nombres-pass").strip
|
||||
|
||||
class Handler
|
||||
def format_buffer(buffer, canvas_name, title = "")
|
||||
@ -37,23 +42,21 @@ class Handler
|
||||
end
|
||||
|
||||
def query(sql)
|
||||
# Runs a SQL query against the Rqlite database.
|
||||
# Runs a SQL query against the database.
|
||||
#
|
||||
# Returns an array of values (which need to be casted)
|
||||
# Returns an array of values [[Year,Count]...]
|
||||
# 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
|
||||
DB.open("postgres://#{USER}:#{PASS}@10.61.0.1:5432/nombres") do |db|
|
||||
db.query sql do |rs|
|
||||
result = [] of Tuple(Int32, Int32)
|
||||
rs.each do
|
||||
year = rs.read(Int32)
|
||||
contador = rs.read(Int32)
|
||||
result.push({year, contador})
|
||||
end
|
||||
return result
|
||||
end
|
||||
end
|
||||
# No result, return nil
|
||||
nil
|
||||
@ -72,7 +75,7 @@ class Handler
|
||||
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 = query["i"].as_s.split(",").map(&.strip)
|
||||
nombres.reject! { |n| n.size == 0 }
|
||||
end
|
||||
|
||||
@ -93,7 +96,7 @@ class Handler
|
||||
show_key(true)
|
||||
xrange(1922..2015)
|
||||
nombres.map { |nombre|
|
||||
sql = "SELECT anio, contador FROM nombres WHERE nombre = '#{nombre}' ORDER BY anio"
|
||||
sql = "SELECT anio::integer, contador::integer FROM nombres WHERE nombre = '#{nombre}' ORDER BY anio"
|
||||
x = Array(Int32).new
|
||||
y = Array(Int32).new
|
||||
results = query(sql)
|
||||
@ -102,19 +105,20 @@ class Handler
|
||||
y = x.map { |_| 0 }
|
||||
else # We got results
|
||||
values = Hash(Int32, Int32).new(default_value: 0)
|
||||
results.map { |r|
|
||||
values[r[0].as_i] = r[1].as_i
|
||||
results.map { |row|
|
||||
values[row[0]] = row[1]
|
||||
}
|
||||
(1922..2015).map { |year|
|
||||
x << year
|
||||
y << values[year] # Defaults to 0, so we have the whole set
|
||||
y << values[year] # Defaults to 0, so we have the whole set
|
||||
}
|
||||
end
|
||||
plot(x, y, title: nombre.titleize, style: :lines, linewidth: 3)
|
||||
}
|
||||
end
|
||||
puts "After Ishi"
|
||||
|
||||
return {
|
||||
{
|
||||
body: format_buffer(buffer, "historico"),
|
||||
status_code: 200,
|
||||
headers: HTTP::Headers{"Content-Type" => "text/html"},
|
||||
|
@ -4,3 +4,5 @@ version: 0.1.0
|
||||
dependencies:
|
||||
ishi:
|
||||
github: toddsundsted/ishi
|
||||
pg:
|
||||
github: will/crystal-pg
|
||||
|
@ -21,12 +21,18 @@ functions:
|
||||
image: ralsina/c-historico:latest
|
||||
build_args:
|
||||
ADDITIONAL_PACKAGE: gnuplot
|
||||
secrets:
|
||||
- nombres-pass
|
||||
- nombres-user
|
||||
c-busqueda:
|
||||
lang: crystal-http
|
||||
handler: ./c-busqueda
|
||||
image: ralsina/c-busqueda:latest
|
||||
build_args:
|
||||
ADDITIONAL_PACKAGE: gnuplot
|
||||
secrets:
|
||||
- nombres-pass
|
||||
- nombres-user
|
||||
iol:
|
||||
lang: python3-fastapi
|
||||
handler: ./iol
|
||||
|
Loading…
Reference in New Issue
Block a user