From 02e7594e3d080f91eb50fd8493e590ef60e6de6f Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Wed, 15 May 2024 18:02:50 -0300 Subject: [PATCH] Port c-historico to postgres --- build.sh | 2 ++ c-historico/handler.cr | 44 +++++++++++++++++++++++------------------- c-historico/shard.yml | 2 ++ functions.yml | 6 ++++++ 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/build.sh b/build.sh index 6ac1369..3b9f5a1 100755 --- a/build.sh +++ b/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 $* diff --git a/c-historico/handler.cr b/c-historico/handler.cr index f147eb3..d30150e 100644 --- a/c-historico/handler.cr +++ b/c-historico/handler.cr @@ -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"}, diff --git a/c-historico/shard.yml b/c-historico/shard.yml index c7bc28d..eb1bee6 100644 --- a/c-historico/shard.yml +++ b/c-historico/shard.yml @@ -4,3 +4,5 @@ version: 0.1.0 dependencies: ishi: github: toddsundsted/ishi + pg: + github: will/crystal-pg diff --git a/functions.yml b/functions.yml index 8376a26..d35f3e0 100644 --- a/functions.yml +++ b/functions.yml @@ -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