Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Using Query Preview from Big Query #213

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def inject_environment_variables(environment: str):
f"Injecting {len(secrets)} environment variables from Infisical:")
for secret in secrets:
logger.info(
f" - {secret.secret_name}: {'*' * len(secret.secret_value)}")
f" - {secret.secret_name}: {len(secret.secret_value)} chars")


environment = getenv_or_action("ENVIRONMENT", action="warn", default="dev")
Expand Down
22 changes: 7 additions & 15 deletions app/routers/frontend.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# -*- coding: utf-8 -*-
import json

from typing import Annotated, List
from fastapi import APIRouter, Depends, HTTPException
from basedosdados import read_sql
from tortoise.exceptions import ValidationError

from app.dependencies import (
Expand All @@ -16,6 +13,7 @@
Encounter,
UserInfo,
)
from app.utils import read_bq
from app.validators import CPFValidator
from app.config import (
BIGQUERY_PROJECT,
Expand Down Expand Up @@ -57,18 +55,14 @@ async def get_patient_header(
except ValidationError:
raise HTTPException(status_code=400, detail="Invalid CPF")

results_json = read_sql(
results = await read_bq(
f"""
SELECT *
FROM `{BIGQUERY_PROJECT}`.{BIGQUERY_PATIENT_HEADER_TABLE_ID}
WHERE cpf = '{cpf}'
""",
from_file="/tmp/credentials.json",
).to_json(orient="records")
try:
results = json.loads(results_json)
except Exception:
results = []
)

if len(results) == 0:
raise HTTPException(status_code=404, detail="Patient not found")
Expand All @@ -90,15 +84,14 @@ async def get_patient_summary(
cpf: str,
) -> PatientSummary:

results_json = read_sql(
results = await read_bq(
f"""
SELECT *
FROM `{BIGQUERY_PROJECT}`.{BIGQUERY_PATIENT_SUMMARY_TABLE_ID}
WHERE cpf = '{cpf}'
""",
from_file="/tmp/credentials.json",
).to_json(orient="records")
results = json.loads(results_json)
)
if len(results) == 0:
raise HTTPException(status_code=404, detail="Patient not found")
else:
Expand Down Expand Up @@ -126,13 +119,12 @@ async def get_patient_encounters(
cpf: str,
) -> List[Encounter]:

results_json = read_sql(
results = await read_bq(
f"""
SELECT *
FROM `{BIGQUERY_PROJECT}`.{BIGQUERY_PATIENT_ENCOUNTERS_TABLE_ID}
WHERE cpf = '{cpf}' and exibicao.indicador = true
""",
from_file="/tmp/credentials.json",
).to_json(orient="records")
results = json.loads(results_json)
)
return results
37 changes: 18 additions & 19 deletions app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import jwt
import hashlib
import json
from typing import Literal
import os

from google.cloud import bigquery
from google.oauth2 import service_account
from asyncer import asyncify
from loguru import logger
from passlib.context import CryptContext

Expand Down Expand Up @@ -124,24 +128,19 @@ async def get_instance(Model, table, slug=None, code=None):
return table[slug]


def read_timestamp(timestamp: int, output_format=Literal['date','datetime']) -> str:
if output_format == 'date':
denominator = 1000
str_format = "%Y-%m-%d"
elif output_format == 'datetime':
denominator = 1
str_format = "%Y-%m-%d %H:%M:%S"
else:
raise ValueError("Invalid format")
async def read_bq(query, from_file="/tmp/credentials.json"):
logger.debug(f"""Reading BigQuery with query (QUERY_PREVIEW_ENABLED={
os.environ['QUERY_PREVIEW_ENABLED']
}): {query}""")

try:
value = datetime(1970, 1, 1) + timedelta(seconds=timestamp/denominator)
except Exception as exc:
logger.error(f"Invalid timestamp: {timestamp} from {exc}")
return None
def execute_job():
credentials = service_account.Credentials.from_service_account_file(
from_file,
)
client = bigquery.Client(credentials=credentials)
row_iterator = client.query_and_wait(query)
return [dict(row) for row in row_iterator]

return value.strftime(str_format)
rows = await asyncify(execute_job)()

def normalize_case(text):
# TODO
return text
return rows
16 changes: 15 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ urllib3 = "2.0.7"
idna = "3.7"
basedosdados = "^2.0.0b16"
nltk = "^3.9.1"
asyncer = "^0.0.8"


[tool.poetry.group.dev.dependencies]
Expand Down
Loading