Starting port of c-busqueda, not finished

This commit is contained in:
Roberto Alsina 2024-05-15 18:07:25 -03:00
parent 02e7594e3d
commit d186fc3d68
2 changed files with 22 additions and 15 deletions

View File

@ -1,7 +1,14 @@
require "http/request" require "http/client"
require "http/headers" require "http/headers"
require "http/request"
require "ishi/html" require "ishi/html"
require "json" 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 class Handler
def format_buffer(buffer, canvas_name, title = "") def format_buffer(buffer, canvas_name, title = "")
@ -35,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

View File

@ -4,3 +4,5 @@ version: 0.1.0
dependencies: dependencies:
ishi: ishi:
github: toddsundsted/ishi github: toddsundsted/ishi
pg:
github: will/crystal-pg