Don't use pg pool, too flaky for now

This commit is contained in:
Roberto Alsina 2024-08-21 18:18:35 -03:00
parent dbbf7224b3
commit 9190060c52
5 changed files with 70 additions and 86 deletions

View File

@ -1,5 +1,6 @@
require "json"
require "kemal"
require "db"
require "pg"
require "pool/connection"
@ -11,11 +12,6 @@ DBHOST = File.read("/secrets/dbhost").strip
DBURL = "postgres://#{USER}:#{PASS}@#{DBHOST}:5432/nombres"
puts "Connnecting to #{DBURL}"
# Create a connection pool to the database
pg = ConnectionPool.new(capacity: 25, timeout: 1.seconds) do
PG.connect(DBURL)
end
def normalize(s : String) : String
s.unicode_normalize(:nfkd)
.chars.reject! { |character|
@ -25,6 +21,7 @@ end
# A basic hello world get endpoint
post "/" do |env|
db = DB.open DBURL
prefijo = env.params.json["p"].as(String)
genero = env.params.json["g"].as(String)
year = env.params.json["a"].as(String)
@ -35,34 +32,33 @@ post "/" do |env|
end
datos = [] of Tuple(String, Int32 | String)
# Connect using credentials provided
pg.connection do |cursor|
if prefijo.empty? && year.empty?
result_set = cursor.query("
if prefijo.empty? && year.empty?
result_set = db.query("
SELECT nombre, total::integer
FROM totales
ORDER BY total DESC
LIMIT 50")
elsif prefijo.empty? && !year.empty?
# Per-year totals
result_set = cursor.query("
elsif prefijo.empty? && !year.empty?
# Per-year totals
result_set = db.query("
SELECT nombre, contador::integer
FROM nombres
WHERE
anio = $1
ORDER BY contador DESC
LIMIT 50", year)
elsif !prefijo.empty? && year.empty?
# Filter only by prefix
result_set = cursor.query("
elsif !prefijo.empty? && year.empty?
# Filter only by prefix
result_set = db.query("
SELECT nombre, total::integer
FROM totales
WHERE
nombre LIKE $1
ORDER BY total DESC
LIMIT 50", prefijo + "%")
elsif !prefijo.empty? && !year.empty?
# We have both
result_set = cursor.query("
elsif !prefijo.empty? && !year.empty?
# We have both
result_set = db.query("
SELECT nombre, contador::integer
FROM nombres
WHERE
@ -70,20 +66,19 @@ post "/" do |env|
nombre LIKE $2
ORDER BY contador DESC
LIMIT 50", year, prefijo + "%")
end
end
if !result_set.nil?
result_set.each do
nombre = result_set.read(String)
valor = result_set.read(Int32)
datos.push({nombre, valor})
end
result_set.close
if !result_set.nil?
result_set.each do
nombre = result_set.read(String)
valor = result_set.read(Int32)
datos.push({nombre, valor})
end
result_set.close
end
if datos.empty?
raise "No data found"
end
if datos.empty?
raise "No data found"
end
# In this context, remove all composite names
datos.reject! { |row|
@ -92,39 +87,37 @@ post "/" do |env|
datos.insert(0, {"Nombre", "Cuantos?"})
if genero
pg.connection do |cursor|
datos.reject! { |row|
# How feminine is this name?
# Yes this database is upper case
puts "Checking #{row[1]} #{row[0]}"
feminidad = 0
sql = %(
datos.reject! { |row|
# How feminine is this name?
# Yes this database is upper case
puts "Checking #{row[1]} #{row[0]}"
feminidad = 0
sql = %(
SELECT COALESCE((SELECT frecuencia FROM mujeres WHERE nombre='#{row[0]?.to_s.upcase}'), 0) AS mujeres,
COALESCE((SELECT frecuencia FROM hombres WHERE nombre='#{row[0]?.to_s.upcase}'), 0) AS hombres
)
puts "SQL: #{sql}"
cursor.query sql do |result_set|
result_set.each do
mujeres = result_set.read(Int32)
hombres = result_set.read(Int32)
puts "frecuencias: #{mujeres} #{hombres}"
if hombres == mujeres == 0
feminidad = 0.5
else
feminidad = mujeres / (hombres + mujeres)
end
puts "SQL: #{sql}"
db.query sql do |result_set|
result_set.each do
mujeres = result_set.read(Int32)
hombres = result_set.read(Int32)
puts "frecuencias: #{mujeres} #{hombres}"
if hombres == mujeres == 0
feminidad = 0.5
else
feminidad = mujeres / (hombres + mujeres)
end
end
# El overlap en 0.5 es intencional!
if (feminidad >= 0.5 && genero == "f") ||
(feminidad <= 0.5 && genero == "m")
false
else
true
end
}
puts "Data split by gender"
end
end
# El overlap en 0.5 es intencional!
if (feminidad >= 0.5 && genero == "f") ||
(feminidad <= 0.5 && genero == "m")
false
else
true
end
}
puts "Data split by gender"
end
datos = datos[..10].map { |row|
[row[0].capitalize, row[1]]
@ -141,9 +134,14 @@ post "/" do |env|
"title" => title,
"data" => datos,
}.to_json
ensure
db.try &.close
end
get "/ping/" do
pg.connection.exec("SELECT 42")
db = DB.open DBURL
db.exec("SELECT 42")
"OK"
ensure
db.try &.close
end

View File

@ -1,14 +1,14 @@
#!/bin/sh -x
set -e
#export OPENFAAS_URL=http://pinky:8082
#pass faas.ralsina.me | faas-cli login -u admin --password-stdin
export OPENFAAS_URL=http://pinky:8082
pass faas.ralsina.me | faas-cli login -u admin --password-stdin
#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 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 deploy -f functions.yml $*
faas-cli deploy -f functions.yml $*
export FAASO_SERVER=http://rocky:8888/admin
pass faaso-rocky | faaso login

View File

@ -3,10 +3,6 @@ provider:
name: openfaas
gateway: http://pinky:8082
functions:
tapas:
lang: python3-flask
handler: ./tapas
image: ralsina/tapas:latest
iol:
lang: python3-fastapi
handler: ./iol

View File

@ -11,11 +11,6 @@ DBHOST = File.read("/secrets/dbhost").strip
DBURL = "postgres://#{USER}:#{PASS}@#{DBHOST}:5432/nombres"
puts "Connnecting to #{DBURL}"
# Create a connection pool to the database
pg = ConnectionPool.new(capacity: 25, timeout: 1.seconds) do
PG.connect(DBURL)
end
# Connect to the database and get information about
# the requested names
get "/" do |env|
@ -31,7 +26,7 @@ get "/" do |env|
results << [anio.to_s]
end
# Connect using credentials provided
pg.connection do |cursor|
db = DB.open DBURL
# Get the information for each name
names.map do |name|
# Normalize: remove diacritics etc.
@ -41,7 +36,7 @@ get "/" do |env|
}.join("").downcase
counter_per_year = {} of Int32 => Int32
cursor.query("
db.query("
SELECT anio::integer, contador::integer
FROM nombres WHERE nombre = $1", name) do |result_set|
result_set.each do
@ -52,15 +47,15 @@ get "/" do |env|
results[anio - 1921] << counter_per_year.fetch(anio, 0).to_s
end
end
end
results.to_json
ensure
db.try &.close
end
# The `/ping/` endpoint is configured in the container as a healthcheck
# You can make it better by checking that your database is responding
# or whatever checks you think are important
#
get "/ping/" do
pg.connection.exec("SELECT 42")
db = DB.open DBURL
db.exec("SELECT 42")
"OK"
ensure
db.try &.close
end

View File

@ -306,12 +306,7 @@
id="nombres"
placeholder="Nombres separados con comas"
aria-label="Search"
<<<<<<< Updated upstream
value="Juan, María"
||||||| Stash base
=======
value="juan,maria"
>>>>>>> Stashed changes
value="juan,maria"
/>
<input type="submit" value="Buscar" onCLick="drawChart2();" />
</form>