Skip to content

Commit

Permalink
feat: Using Query Preview from Big Query
Browse files Browse the repository at this point in the history
  • Loading branch information
TanookiVerde committed Aug 28, 2024
1 parent cfc8262 commit 9e8b171
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 34 deletions.
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
20 changes: 7 additions & 13 deletions app/routers/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

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 +15,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 +57,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 +86,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 +121,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

0 comments on commit 9e8b171

Please sign in to comment.