https://github.com/ivanm4rtin5/backend_consultas_ans
Back-end de consultas
https://github.com/ivanm4rtin5/backend_consultas_ans
api-rest postgresql postman python
Last synced: 9 months ago
JSON representation
Back-end de consultas
- Host: GitHub
- URL: https://github.com/ivanm4rtin5/backend_consultas_ans
- Owner: IvanM4rtin5
- Created: 2025-04-03T04:28:20.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-05-25T22:47:10.000Z (10 months ago)
- Last Synced: 2025-05-25T23:29:04.042Z (10 months ago)
- Topics: api-rest, postgresql, postman, python
- Language: Python
- Homepage:
- Size: 271 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# πΌ Expense Query API - ANS (National Supplementary Health Agency)
[](https://www.python.org/)
[](https://flask.palletsprojects.com/)
[](https://www.postgresql.org/)
[](https://www.sqlalchemy.org/)
Backend API for querying healthcare provider expenses, integrated into a relational database with data processed from ANS.
---
## π§© Main Features
### **RESTful API**
- Consult the biggest expenses per quarter
- Health check endpoint
- Filtering by operator and period
- Pagination of results
- **Postman queries**

### **Database**
- Optimized modeling for financial queries
- Indexes for performance on large datasets
- Relationship between operators and financial statements
- Data import via CSV
- **Queries in SQL**

---
## π How to Execute the Project
### Prerequisites
- Python 3.10+
- PostgreSQL 17+
- Postman (for testing endpoints)
### Download csv files from the ans website
1. the user will have to download the csv files from the websites (selecting the year of interest):
- https://dadosabertos.ans.gov.br/FTP/PDA/demonstracoes_contabeis/
- https://dadosabertos.ans.gov.br/FTP/PDA/operadoras_de_plano_de_saude_ativas/
2. insert in the **Uploads** folder
### Initial Setup
1. Create a PostgreSQL database:
```sql
CREATE DATABASE my_base;
```
2. Configure the environment variables in .env:
```ini Copy
DB_USER=postgres
DB_PASSWORD=secret_password
DB_HOST=localhost
DB_PORT=5432
DB_NAME=my_base
```
**Installation**
```bash Copy
git clone https://github.com/IvanM4rtin5/backend_Consultas_Ans.git
```
```
cd api-ans
```
```
python -m venv venv
```
```
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows
```
```
pip install -r requirements.txt
```
**Bank Startup**
```bash Copy
python create_db.py # Create tables and indexes
python import_csv.py # Import data from CSVs
```
**Run the API**
```bash Copy
python run.py # Start server at http://localhost:5000
```
## π‘ Main Endpoints
**Health Check**
```http Copy
GET /api/health
```
**Top 10 Expenses from the Last Quarter**
```http Copy
GET /api/expenses/quarter
```
**Sample Answer:**
```json Copy
{
"status": "success",
"data": [
{
"razao_social": "SUL AMERICA COMPANHIA DE SEGURO SAΓDE",
"registro_ans": "6246",
"modalidade": "Segquadora Especializada em SaΓΊde",
"total_despesas": "42249880884.69"
}
]
}
```
### ποΈ Project Structure
```Copy
api-ans/
βββ app/
β βββ routes/
β β βββ expenses.py # Query routes
β β βββ health.py # Health check
β βββ __init__.py # Application factory
β βββ config.py # Flask Settings
β βββ create_db.py # DB creation script
β βββ database.py # Connection to the database
β βββ models.py # SQLAlchemy Models
β βββ import_csv.py # Data importer
βββ .env # Environment variables
βββ requirements.txt # Dependencies
βββ run.py # Entrypoint of the application
```
### π Example SQL Queries
Query used in the /expenses/quarter endpoint:
```sql Copy
WITH UltimoTrimestre AS (
SELECT MAX(data) AS data_max FROM demonstracoes_contabeis
)
SELECT
o.razao_social,
o.registro_ans,
o.modalidade,
ABS(SUM(d.vl_saldo_final)) AS total_despesas
FROM demonstracoes_contabeis d
JOIN operadoras_ativas o ON d.reg_ans = o.registro_ans
JOIN UltimoTrimestre ut ON d.data = ut.data_max
WHERE d.descricao LIKE '%ASSISTΓNCIA A SAΓDE MEDICO HOSPITALAR%'
GROUP BY o.razao_social, o.registro_ans, o.modalidade
ORDER BY total_despesas DESC
LIMIT 10;
```
### π Usage Tips
Test with Postman: Import the collection available in docs/postman_collection.json
Optimization: Pre-built indexes speed up queries on reg_ans and data
Example Data: CSVs must follow the structure of the accounting_demonstrations and active_operators tables
---
### π§ Contact
**Developer:** Ivan Martins
***Email:** ivanmarti.alves@gmail.com
**LinkedIn:** https://www.linkedin.com/in/ivan-martins-alves/