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-user | faas-cli secret create iol-user
|
||||||
pass iol-api-secret | faas-cli secret create iol-api-secret
|
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 publish -f functions.yml --platforms linux/arm64 --build-arg 'TEST_ENABLED=false' $*
|
||||||
faas-cli deploy -f functions.yml $*
|
faas-cli deploy -f functions.yml $*
|
||||||
|
@ -4,6 +4,11 @@ require "http/request"
|
|||||||
require "ishi/html"
|
require "ishi/html"
|
||||||
require "json"
|
require "json"
|
||||||
require "uuid"
|
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
|
class Handler
|
||||||
def format_buffer(buffer, canvas_name, title = "")
|
def format_buffer(buffer, canvas_name, title = "")
|
||||||
@ -37,23 +42,21 @@ class Handler
|
|||||||
end
|
end
|
||||||
|
|
||||||
def query(sql)
|
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
|
# Or nil if there are no results
|
||||||
|
|
||||||
params = URI::Params.encode({"q": sql})
|
DB.open("postgres://#{USER}:#{PASS}@10.61.0.1:5432/nombres") do |db|
|
||||||
response = HTTP::Client.get URI.new(
|
db.query sql do |rs|
|
||||||
"http",
|
result = [] of Tuple(Int32, Int32)
|
||||||
"10.61.0.1",
|
rs.each do
|
||||||
4001,
|
year = rs.read(Int32)
|
||||||
"/db/query",
|
contador = rs.read(Int32)
|
||||||
params)
|
result.push({year, contador})
|
||||||
|
end
|
||||||
# This API only has a values key when there are actual results
|
return result
|
||||||
results = JSON.parse(response.body)["results"][0].as_h
|
end
|
||||||
if results.has_key?("values")
|
|
||||||
return results["values"].as_a
|
|
||||||
end
|
end
|
||||||
# No result, return nil
|
# No result, return nil
|
||||||
nil
|
nil
|
||||||
@ -72,7 +75,7 @@ class Handler
|
|||||||
def run(request : HTTP::Request)
|
def run(request : HTTP::Request)
|
||||||
unless (body = request.body).nil?
|
unless (body = request.body).nil?
|
||||||
query = JSON.parse(body)
|
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 }
|
nombres.reject! { |n| n.size == 0 }
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -93,7 +96,7 @@ class Handler
|
|||||||
show_key(true)
|
show_key(true)
|
||||||
xrange(1922..2015)
|
xrange(1922..2015)
|
||||||
nombres.map { |nombre|
|
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
|
x = Array(Int32).new
|
||||||
y = Array(Int32).new
|
y = Array(Int32).new
|
||||||
results = query(sql)
|
results = query(sql)
|
||||||
@ -102,19 +105,20 @@ class Handler
|
|||||||
y = x.map { |_| 0 }
|
y = x.map { |_| 0 }
|
||||||
else # We got results
|
else # We got results
|
||||||
values = Hash(Int32, Int32).new(default_value: 0)
|
values = Hash(Int32, Int32).new(default_value: 0)
|
||||||
results.map { |r|
|
results.map { |row|
|
||||||
values[r[0].as_i] = r[1].as_i
|
values[row[0]] = row[1]
|
||||||
}
|
}
|
||||||
(1922..2015).map { |year|
|
(1922..2015).map { |year|
|
||||||
x << 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
|
end
|
||||||
plot(x, y, title: nombre.titleize, style: :lines, linewidth: 3)
|
plot(x, y, title: nombre.titleize, style: :lines, linewidth: 3)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
puts "After Ishi"
|
||||||
|
|
||||||
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"},
|
||||||
|
@ -4,3 +4,5 @@ version: 0.1.0
|
|||||||
dependencies:
|
dependencies:
|
||||||
ishi:
|
ishi:
|
||||||
github: toddsundsted/ishi
|
github: toddsundsted/ishi
|
||||||
|
pg:
|
||||||
|
github: will/crystal-pg
|
||||||
|
@ -21,12 +21,18 @@ functions:
|
|||||||
image: ralsina/c-historico:latest
|
image: ralsina/c-historico:latest
|
||||||
build_args:
|
build_args:
|
||||||
ADDITIONAL_PACKAGE: gnuplot
|
ADDITIONAL_PACKAGE: gnuplot
|
||||||
|
secrets:
|
||||||
|
- nombres-pass
|
||||||
|
- nombres-user
|
||||||
c-busqueda:
|
c-busqueda:
|
||||||
lang: crystal-http
|
lang: crystal-http
|
||||||
handler: ./c-busqueda
|
handler: ./c-busqueda
|
||||||
image: ralsina/c-busqueda:latest
|
image: ralsina/c-busqueda:latest
|
||||||
build_args:
|
build_args:
|
||||||
ADDITIONAL_PACKAGE: gnuplot
|
ADDITIONAL_PACKAGE: gnuplot
|
||||||
|
secrets:
|
||||||
|
- nombres-pass
|
||||||
|
- nombres-user
|
||||||
iol:
|
iol:
|
||||||
lang: python3-fastapi
|
lang: python3-fastapi
|
||||||
handler: ./iol
|
handler: ./iol
|
||||||
|
Loading…
Reference in New Issue
Block a user