diff --git a/c-busqueda/handler.cr b/c-busqueda/handler.cr index 005610c..c6241fe 100644 --- a/c-busqueda/handler.cr +++ b/c-busqueda/handler.cr @@ -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 diff --git a/c-busqueda/shard.yml b/c-busqueda/shard.yml index 87fc635..88cf5e0 100644 --- a/c-busqueda/shard.yml +++ b/c-busqueda/shard.yml @@ -4,3 +4,5 @@ version: 0.1.0 dependencies: ishi: github: toddsundsted/ishi + pg: + github: will/crystal-pg