feat(nexxa): taxonomía cerrada de estados y registro completo de eventos (motivo, hasta, quién), /resumen y /usos filtrable; 2.0.0 (P337/F1)
Tests ADN / test (push) Canceled after 0s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Co-Authored-By: GEMMA <noreply@gemma.dtic>
This commit is contained in:
Ricardo Monla
2026-10-05 03:29:40 -03:00
co-authored by Claude Opus 5.5 GEMMA
parent 08aa45117e
commit 82e56aad0e
11 changed files with 233 additions and 81 deletions
+31 -14
View File
@@ -103,24 +103,36 @@ module Nexxa
def usos_registrar_pg(linea)
sincronizado do
con.exec_params('INSERT INTO usos (provider, identificador, individuo, evento, en) VALUES ($1, $2, $3, $4, $5)',
linea.values_at('provider', 'identificador', 'individuo', 'evento', 'en'))
con.exec_params('INSERT INTO usos (provider, identificador, individuo, evento, en, motivo, hasta) ' \
'VALUES ($1, $2, $3, $4, $5, $6, $7)',
linea.values_at('provider', 'identificador', 'individuo', 'evento', 'en', 'motivo', 'hasta'))
end
end
# Más reciente primero
def usos_historial(provider: nil, identificador: nil, limit: 50)
return usos_archivo(provider, identificador, limit) unless pg?
FILTROS_USOS = %w[provider identificador individuo evento].freeze
# Más reciente primero. Filtros exactos por campo y 'desde' (Time) para una ventana (P337/Fase 1)
def usos_historial(provider: nil, identificador: nil, individuo: nil, evento: nil, desde: nil, limit: 100)
f = { 'provider' => provider, 'identificador' => identificador, 'individuo' => individuo, 'evento' => evento }
return usos_archivo(f, desde, limit) unless pg?
sincronizado do
con.exec_params(<<~SQL, [provider, identificador, limit]).map { |f| f.merge('en' => Time.parse(f['en']).iso8601) }
SELECT provider, identificador, individuo, evento, en FROM usos
con.exec_params(<<~SQL, f.values_at(*FILTROS_USOS) + [desde&.iso8601, limit]).map { |u| normalizar_uso(u) }
SELECT provider, identificador, individuo, evento, en, motivo, hasta FROM usos
WHERE ($1::text IS NULL OR provider = $1) AND ($2::text IS NULL OR identificador = $2)
ORDER BY id DESC LIMIT $3
AND ($3::text IS NULL OR individuo = $3) AND ($4::text IS NULL OR evento = $4)
AND ($5::timestamptz IS NULL OR en >= $5)
ORDER BY id DESC LIMIT $6
SQL
end
end
# Fechas en ISO 8601 con zona, igual en los dos motores
def normalizar_uso(u)
u.merge('en' => Time.parse(u['en'].to_s).iso8601, 'hasta' => u['hasta'] && Time.parse(u['hasta'].to_s).iso8601(3),
'motivo' => u['motivo'])
end
# ---------- motor archivo ----------
def archivo_leer(archivo, defecto)
@@ -137,17 +149,17 @@ module Nexxa
File.rename(tmp, archivo)
end
def usos_archivo(provider, identificador, limit)
def usos_archivo(filtros, desde, limit)
return [] unless File.exist?(USOS_LOG)
lineas = File.readlines(USOS_LOG).filter_map do |l|
lineas = File.readlines(USOS_LOG, encoding: 'UTF-8').filter_map do |l| # locale C del servicio (ME-1216)
JSON.parse(l)
rescue JSON::ParserError
nil
end
lineas.select! { |l| l['provider'] == provider } if provider
lineas.select! { |l| l['identificador'] == identificador } if identificador
lineas.last(limit).reverse
filtros.each { |campo, valor| lineas.select! { |l| l[campo] == valor } if valor }
lineas.select! { |l| l['en'] && Time.parse(l['en']) >= desde } if desde
lineas.last(limit).reverse.map { |u| normalizar_uso(u) }
end
# ---------- motor pg ----------
@@ -180,6 +192,9 @@ module Nexxa
en TIMESTAMPTZ NOT NULL
);
CREATE INDEX IF NOT EXISTS usos_key ON usos (provider, identificador);
ALTER TABLE usos ADD COLUMN IF NOT EXISTS motivo TEXT;
ALTER TABLE usos ADD COLUMN IF NOT EXISTS hasta TIMESTAMPTZ;
CREATE INDEX IF NOT EXISTS usos_en ON usos (en);
SQL
def crear_esquema!
@@ -191,7 +206,9 @@ module Nexxa
def con
@con ||= begin
require 'pg'
PG.connect(dbname: ENV.fetch('NEXXA_PG_DB', 'nexxa'))
c = PG.connect(dbname: ENV.fetch('NEXXA_PG_DB', 'nexxa'))
c.exec(ESQUEMA) # idempotente: un despliegue que amplía el esquema se aplica solo al arrancar (P337/Fase 1)
c
end
end