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/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 = "")
@ -35,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

View File

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