Skip to content

Commit

Permalink
- Saving ip locations so we don't keep calling the api that we only have
Browse files Browse the repository at this point in the history
  a free 1000 calls a month to -- to public.ip_info
- Fixed hidden vocab bug that was making only one row show up in
  comparison table
  • Loading branch information
Sigfried committed Dec 14, 2023
1 parent 1cf2a4a commit ca2d9e9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
27 changes: 22 additions & 5 deletions backend/api_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from starlette.requests import Request

from backend.config import get_schema_name
from backend.db.utils import get_db_connection, insert_from_dict, run_sql
from backend.db.utils import get_db_connection, insert_from_dict, run_sql, sql_query
from backend.utils import dump


Expand Down Expand Up @@ -106,11 +106,22 @@ async def client_location(request: Request) -> str:

ip = re.sub(':.*', '', ip)

with get_db_connection() as con:
ip_info = sql_query(con, 'SELECT * FROM public.ip_info WHERE ip = :ip', {'ip': ip})
if ip_info:
city = ip_info.get('city', 'no city')
region = ip_info.get('region', 'no region')
location = f"{ip}: {city}, {region}"
return location

# for now we're going to just return the ip, free subscription ran out and need to update key
# return ip

ipstack_key = os.getenv('API_STACK_KEY', None)

if ip != '127.0.0.1' and ipstack_key:
"""
http://api.ipstack.com/134.201.250.155?access_key=7a6f9d6d72d68a1452b643eb58cd8ee7&format=1
http://api.ipstack.com/134.201.250.155?access_key=<key>&format=1
{
"ip": "134.201.250.155",
"type": "ipv4",
Expand Down Expand Up @@ -148,10 +159,16 @@ async def client_location(request: Request) -> str:
async with httpx.AsyncClient() as client:
response = await client.get(loc_url)
if response and response.json:
loc_obj = response.json()
city = loc_obj['city'] if 'city' in loc_obj else 'no city'
region = loc_obj['region'] if 'region' in loc_obj else 'no region'
loc = response.json()
if 'error' in loc:
return f'{ip} (no loc, err {loc.get("error", {}).get("code", "")})'
city = loc.get('city', 'no city')
region = loc.get('region', 'no region')
location = f"{ip}: {city}, {region}"

with get_db_connection() as con:
insert_from_dict(con, 'public.ip_info', loc)

return location

return ip
6 changes: 6 additions & 0 deletions backend/db/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
rpt json
);"""

DDL_IP_INFO = """CREATE TABLE IF NOT EXISTS public.ip_info (
ip text,
info json
);"""

def create_database(con: Connection, schema: str):
"""Create the database"""
print('Current tables: ')
Expand All @@ -79,6 +84,7 @@ def create_database(con: Connection, schema: str):
run_sql(con2, DDL_COUNTS_RUNS)
run_sql(con2, DDL_FETCH_AUDIT)
run_sql(con2, DDL_CSET_COMPARE)
run_sql(con2, DDL_IP_INFO)
run_sql(con, f'CREATE SCHEMA IF NOT EXISTS {schema};')


Expand Down
10 changes: 6 additions & 4 deletions backend/routes/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,16 @@ def get_connected_subgraph(
hide_vocabs: Union[List[int], None] = []
) -> nx.Graph:

csmi = get_cset_members_items(codeset_ids=codeset_ids)
_csmi = get_cset_members_items(codeset_ids=codeset_ids)
# concepts = get_concepts(extra_concept_ids)
# give hidden rxnorm ext count
hidden = {}
csmi = set()
for vocab in hide_vocabs:
hidden[vocab] = set([c['concept_id'] for c in csmi if c['vocabulary_id'] == vocab])

csmi = [c for c in csmi if c['vocabulary_id'] == 'RxNorm Extension']
h = set([c['concept_id'] for c in _csmi if c['vocabulary_id'] == vocab])
if h:
hidden[vocab] = h
csmi.update([c for c in _csmi if c['vocabulary_id'] != vocab])

# Organize Concept IDs
timer = get_timer('')
Expand Down
2 changes: 1 addition & 1 deletion termhub-vocab

0 comments on commit ca2d9e9

Please sign in to comment.