anderson-ufrj
commited on
Commit
·
10cc683
1
Parent(s):
c1e6a7a
feat: add demo mode for Portal da Transparência when API key is not available
Browse files- Add fallback demo contracts for testing without API key
- Add debug endpoint to check Portal configuration status
- Return realistic demo data for Ministério da Saúde contracts
- Include demo_mode flag in response metadata
src/api/routes/chat_stable.py
CHANGED
|
@@ -354,4 +354,30 @@ async def test_portal_integration(query: str):
|
|
| 354 |
"success": False,
|
| 355 |
"query": query,
|
| 356 |
"error": str(e)
|
| 357 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
"success": False,
|
| 355 |
"query": query,
|
| 356 |
"error": str(e)
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
@router.get("/debug/portal-status")
|
| 360 |
+
async def debug_portal_status():
|
| 361 |
+
"""Debug endpoint to check Portal da Transparência configuration"""
|
| 362 |
+
import os
|
| 363 |
+
from src.core.config import settings
|
| 364 |
+
|
| 365 |
+
# Check environment variable
|
| 366 |
+
env_key = os.getenv("TRANSPARENCY_API_KEY")
|
| 367 |
+
|
| 368 |
+
# Check settings
|
| 369 |
+
settings_key = None
|
| 370 |
+
if hasattr(settings, 'transparency_api_key') and settings.transparency_api_key:
|
| 371 |
+
settings_key = "Configured"
|
| 372 |
+
|
| 373 |
+
# Check service
|
| 374 |
+
service_key = None
|
| 375 |
+
if hasattr(chat_data_integration, 'portal') and chat_data_integration.portal.api_key:
|
| 376 |
+
service_key = "Loaded"
|
| 377 |
+
|
| 378 |
+
return {
|
| 379 |
+
"env_variable": "Found" if env_key else "Not Found",
|
| 380 |
+
"settings_config": settings_key or "Not Configured",
|
| 381 |
+
"service_loaded": service_key or "Not Loaded",
|
| 382 |
+
"portal_base_url": chat_data_integration.portal.BASE_URL if hasattr(chat_data_integration, 'portal') else "Not initialized"
|
| 383 |
+
}
|
src/services/portal_transparencia_service.py
CHANGED
|
@@ -124,6 +124,11 @@ class PortalTransparenciaService:
|
|
| 124 |
logger.info("Returning cached contracts data")
|
| 125 |
return cached
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
try:
|
| 128 |
response = await self.client.get(
|
| 129 |
self.ENDPOINTS["contratos"],
|
|
@@ -455,6 +460,83 @@ class PortalTransparenciaService:
|
|
| 455 |
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
| 456 |
"""Async context manager exit."""
|
| 457 |
await self.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
|
| 459 |
|
| 460 |
# Singleton instance
|
|
|
|
| 124 |
logger.info("Returning cached contracts data")
|
| 125 |
return cached
|
| 126 |
|
| 127 |
+
# Demo mode if no API key
|
| 128 |
+
if not self.api_key:
|
| 129 |
+
logger.warning("No API key configured - returning demo data")
|
| 130 |
+
return self._get_demo_contracts(params)
|
| 131 |
+
|
| 132 |
try:
|
| 133 |
response = await self.client.get(
|
| 134 |
self.ENDPOINTS["contratos"],
|
|
|
|
| 460 |
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
| 461 |
"""Async context manager exit."""
|
| 462 |
await self.close()
|
| 463 |
+
|
| 464 |
+
def _get_demo_contracts(self, params: Dict[str, Any]) -> Dict[str, Any]:
|
| 465 |
+
"""Get demo contracts when no API key is available."""
|
| 466 |
+
from datetime import datetime
|
| 467 |
+
|
| 468 |
+
demo_contracts = [
|
| 469 |
+
{
|
| 470 |
+
"id": "CTR-2024-001",
|
| 471 |
+
"numero": "001/2024",
|
| 472 |
+
"objeto": "Aquisição de medicamentos para tratamento de COVID-19 e outras doenças respiratórias",
|
| 473 |
+
"valorTotal": 2500000.00,
|
| 474 |
+
"dataAssinatura": "2024-01-15",
|
| 475 |
+
"dataPublicacao": "2024-01-16",
|
| 476 |
+
"vigenciaInicio": "2024-01-20",
|
| 477 |
+
"vigenciaFim": "2025-01-20",
|
| 478 |
+
"situacao": "Ativo",
|
| 479 |
+
"modalidadeCompra": "Pregão Eletrônico",
|
| 480 |
+
"cnpjFornecedor": "12345678000190",
|
| 481 |
+
"nomeFantasiaFornecedor": "Farmacêutica Nacional S.A.",
|
| 482 |
+
"orgaoContratante": {
|
| 483 |
+
"codigo": "26000",
|
| 484 |
+
"nome": "Ministério da Saúde",
|
| 485 |
+
"sigla": "MS"
|
| 486 |
+
}
|
| 487 |
+
},
|
| 488 |
+
{
|
| 489 |
+
"id": "CTR-2024-002",
|
| 490 |
+
"numero": "002/2024",
|
| 491 |
+
"objeto": "Contratação de serviços de manutenção hospitalar para unidades de saúde",
|
| 492 |
+
"valorTotal": 8750000.00,
|
| 493 |
+
"dataAssinatura": "2024-02-01",
|
| 494 |
+
"dataPublicacao": "2024-02-02",
|
| 495 |
+
"vigenciaInicio": "2024-02-05",
|
| 496 |
+
"vigenciaFim": "2025-02-05",
|
| 497 |
+
"situacao": "Ativo",
|
| 498 |
+
"modalidadeCompra": "Concorrência",
|
| 499 |
+
"cnpjFornecedor": "98765432000123",
|
| 500 |
+
"nomeFantasiaFornecedor": "Engenharia e Manutenção LTDA",
|
| 501 |
+
"orgaoContratante": {
|
| 502 |
+
"codigo": "26000",
|
| 503 |
+
"nome": "Ministério da Saúde",
|
| 504 |
+
"sigla": "MS"
|
| 505 |
+
}
|
| 506 |
+
},
|
| 507 |
+
{
|
| 508 |
+
"id": "CTR-2024-003",
|
| 509 |
+
"numero": "003/2024",
|
| 510 |
+
"objeto": "Fornecimento de equipamentos de proteção individual (EPIs) para profissionais de saúde",
|
| 511 |
+
"valorTotal": 3200000.00,
|
| 512 |
+
"dataAssinatura": "2024-03-10",
|
| 513 |
+
"dataPublicacao": "2024-03-11",
|
| 514 |
+
"vigenciaInicio": "2024-03-15",
|
| 515 |
+
"vigenciaFim": "2025-03-15",
|
| 516 |
+
"situacao": "Ativo",
|
| 517 |
+
"modalidadeCompra": "Pregão Eletrônico",
|
| 518 |
+
"cnpjFornecedor": "11223344000155",
|
| 519 |
+
"nomeFantasiaFornecedor": "Proteção Médica Distribuidora",
|
| 520 |
+
"orgaoContratante": {
|
| 521 |
+
"codigo": "26000",
|
| 522 |
+
"nome": "Ministério da Saúde",
|
| 523 |
+
"sigla": "MS"
|
| 524 |
+
}
|
| 525 |
+
}
|
| 526 |
+
]
|
| 527 |
+
|
| 528 |
+
# Filter by organization if specified
|
| 529 |
+
if params.get("codigoOrgao"):
|
| 530 |
+
demo_contracts = [c for c in demo_contracts if c["orgaoContratante"]["codigo"] == params["codigoOrgao"]]
|
| 531 |
+
|
| 532 |
+
return {
|
| 533 |
+
"contratos": demo_contracts[:params.get("tamanhoPagina", 100)],
|
| 534 |
+
"total": len(demo_contracts),
|
| 535 |
+
"pagina": params.get("pagina", 1),
|
| 536 |
+
"tamanho_pagina": params.get("tamanhoPagina", 100),
|
| 537 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 538 |
+
"demo_mode": True
|
| 539 |
+
}
|
| 540 |
|
| 541 |
|
| 542 |
# Singleton instance
|