Spaces:
Running
Running
File size: 1,276 Bytes
1ab5bfc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | from src.database.models import SessionLocal, VerifiedNews
from sqlalchemy import or_
import json
def debug_query(q_str="", interests_str="Defense & Security"):
db = SessionLocal()
try:
query = db.query(VerifiedNews)
if q_str:
query = query.filter(
or_(
VerifiedNews.title.ilike(f"%{q_str}%"),
VerifiedNews.why_it_matters.ilike(f"%{q_str}%")
)
)
if interests_str:
interest_list = [i.strip() for i in interests_str.split(',')]
interest_filters = [VerifiedNews.category.ilike(f"%{i}%") for i in interest_list]
query = query.filter(or_(*interest_filters))
# Print the SQL
print("SQL Query:")
print(query.statement.compile(compile_kwargs={"literal_binds": True}))
articles = query.order_by(VerifiedNews.impact_score.desc(), VerifiedNews.created_at.desc()).limit(12).all()
print(f"\nFound {len(articles)} articles.")
for a in articles:
print(f"- [{a.category}] {a.title} (Impact: {a.impact_score}, Created: {a.created_at})")
finally:
db.close()
if __name__ == "__main__":
debug_query()
|