ranking y tags

This commit is contained in:
jlimolina 2025-10-17 04:10:09 +02:00
parent 0bfeb610a9
commit d29152a0f6
9 changed files with 344 additions and 48 deletions

33
app.py
View file

@ -236,6 +236,7 @@ def home():
total_results = 0
total_pages = 0
tags_por_trad = {}
try:
with get_conn() as conn:
@ -265,6 +266,19 @@ def home():
cursor.execute(sql_page, tuple(params_page))
noticias = cursor.fetchall()
# Cargar tags por traducción (si aplica)
tr_ids = [row['traduccion_id'] for row in noticias if row.get('traduccion_id')]
if tr_ids:
cursor.execute("""
SELECT tn.traduccion_id, tg.valor, tg.tipo
FROM tags_noticia tn
JOIN tags tg ON tg.id = tn.tag_id
WHERE tn.traduccion_id = ANY(%s)
ORDER BY tg.tipo, tg.valor
""", (tr_ids,))
for trid, valor, tipo in cursor.fetchall():
tags_por_trad.setdefault(trid, []).append((valor, tipo))
except psycopg2.Error as db_err:
app.logger.error(f"[DB ERROR] Al leer noticias: {db_err}", exc_info=True)
flash("Error de base de datos al cargar las noticias.", "error")
@ -274,7 +288,8 @@ def home():
cat_id=int(cat_id) if cat_id else None, cont_id=int(cont_id) if cont_id else None,
pais_id=int(pais_id) if pais_id else None, fecha_filtro=fecha_filtro, q=q,
page=page, per_page=per_page, total_pages=total_pages, total_results=total_results,
lang=lang, use_tr=use_tr
lang=lang, use_tr=use_tr,
tags_por_trad=tags_por_trad
)
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
@ -326,19 +341,31 @@ def noticia(tr_id):
@app.route("/dashboard")
def dashboard():
stats = {'feeds_totales': 0, 'noticias_totales': 0, 'feeds_caidos': 0}
top_tags = []
try:
with get_conn() as conn:
with conn.cursor() as cursor:
# Usamos DictCursor aquí para poder usar t.valor / t.tipo / t.apariciones en Jinja
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cursor:
cursor.execute("SELECT COUNT(*) FROM feeds")
stats['feeds_totales'] = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM noticias")
stats['noticias_totales'] = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM feeds WHERE activo = FALSE")
stats['feeds_caidos'] = cursor.fetchone()[0]
cursor.execute("""
SELECT valor, tipo, apariciones
FROM v_tag_counts_24h
ORDER BY apariciones DESC, valor
LIMIT 20
""")
top_tags = cursor.fetchall()
except psycopg2.Error as db_err:
app.logger.error(f"[DB ERROR] Al calcular estadísticas: {db_err}")
flash("Error al conectar con la base de datos.", "error")
return render_template("dashboard.html", stats=stats)
return render_template("dashboard.html", stats=stats, top_tags=top_tags)
@app.route("/feeds/manage")
def manage_feeds():