Skip to content

Commit

Permalink
Merge pull request #724 from rcpch:logging
Browse files Browse the repository at this point in the history
Implements more detailed logging
  • Loading branch information
pacharanero authored Jan 8, 2024
2 parents 8ff7cfb + b1d8166 commit 2ae50bc
Show file tree
Hide file tree
Showing 30 changed files with 305 additions and 191 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ coverage.xml
*.pot

# Django stuff:
*.log
*.log*
local_settings.py
db.sqlite3
db.sqlite3-journal
Expand Down Expand Up @@ -145,4 +145,4 @@ data.csv

# exclude github actions local testing secret file
.secrets
*.csv
*.csv
2 changes: 1 addition & 1 deletion documentation/docs/development/user-groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ A security requirement was the ability to track user activity. This is done in a
```python
@receiver(user_logged_in)
def log_user_login(sender, request, user, **kwargs):
print(f'{user} ({user.email}) logged in from {get_client_ip(request)}.')
logger.info(f'{user} ({user.email}) logged in from {get_client_ip(request)}.')
VisitActivity.objects.create(
activity=1,
ip_address=get_client_ip(request),
Expand Down
36 changes: 19 additions & 17 deletions epilepsy12/common_view_functions/aggregate_by.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Literal, Union
from datetime import date
import logging

# Django imports
from django.apps import apps
Expand All @@ -16,11 +17,12 @@
# E12 imports
from epilepsy12.constants import ETHNICITIES, SEX_TYPE, EnumAbstractionLevel

# Logging setup
logger = logging.getLogger(__name__)

"""
Reporting
"""


def cases_aggregated_by_sex(selected_organisation):
# aggregate queries on trust level cases

Expand Down Expand Up @@ -369,7 +371,7 @@ def update_kpi_aggregation_model(
"open_access": open_access,
},
)
print(f"created {new_obj}")
logger.debug(f"created {new_obj}")

else:
new_obj, created = NationalKPIAggregation.objects.update_or_create(
Expand All @@ -383,9 +385,9 @@ def update_kpi_aggregation_model(
)

if created:
print(f"created {new_obj}")
logger.debug(f"created {new_obj}")
else:
print(f"updated {new_obj}")
logger.debug(f"updated {new_obj}")

return None

Expand All @@ -412,10 +414,10 @@ def update_kpi_aggregation_model(
value_count["open_access"] = open_access
try:
new_obj = AbstractionKPIAggregationModel.objects.create(**value_count)
print(f"created {new_obj}")
logger.debug(f"created {new_obj}")
except Exception as error:
print(
f"PUBLIC VIEW: Can't save new KPIAggregations for {abstraction_level} for {abstraction_relation_instance}: {error}"
logger.exception(
f"Can't save new KPIAggregations for {abstraction_level} for {abstraction_relation_instance}: {error}"
)
return

Expand All @@ -436,15 +438,15 @@ def update_kpi_aggregation_model(
open_access=open_access,
)
except Exception as error:
print(
logger.exception(
f"CLOSED VIEW: Can't update/save KPIAggregations for {abstraction_level} for {abstraction_relation_instance}: {error}"
)
return

if created:
print(f"created {new_obj}")
logger.debug(f"created {new_obj}")
else:
print(f"updated {new_obj}")
logger.debug(f"updated {new_obj}")


def aggregate_kpis_update_models_all_abstractions_for_organisation(
Expand Down Expand Up @@ -688,38 +690,38 @@ def _seed_all_aggregation_models() -> None:
]

if len(all_entities) + 1 != len(all_agg_models):
print(
logger.error(
f"Incorrect lengths for entities. KPIAggregations not seeded. {len(all_entities)+1=}{len(all_agg_models)=}"
)
return

for entities, AggregationModel in zip(all_entities, all_agg_models):
print(f"Creating aggregations for {AggregationModel}")
logger.info(f"Creating aggregations for {AggregationModel}")
for entity in entities:
if AggregationModel.objects.filter(
abstraction_relation=entity,
cohort=current_cohort,
).exists():
print(f"AggregationModel for {entity} already exists. Skipping...")
logger.info(f"AggregationModel for {entity} already exists. Skipping...")
continue

new_agg_model = AggregationModel.objects.create(
abstraction_relation=entity,
cohort=current_cohort,
)

print(f"Created {new_agg_model}")
logger.info(f"Created {new_agg_model}")

# National handled separately as it has no abstraction relation field
if NationalKPIAggregation.objects.filter(
cohort=current_cohort,
).exists():
print(f"NationalKPIAggregation for {entity} already exists. Skipping...")
logger.info(f"NationalKPIAggregation for {entity} already exists. Skipping...")
else:
new_agg_model = NationalKPIAggregation.objects.create(
cohort=current_cohort,
)
print(f"Created {new_agg_model} (Cohort {current_cohort})")
logger.info(f"Created {new_agg_model} (Cohort {current_cohort})")


def ___delete_and_recreate_all_kpi_aggregation_models():
Expand Down
7 changes: 6 additions & 1 deletion epilepsy12/decorator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# python imports
import logging

from django.core.exceptions import PermissionDenied
from django.contrib.auth.decorators import login_required
from .models import (
Expand All @@ -18,6 +21,8 @@
Epilepsy12User,
)

# Logging setup
logger = logging.getLogger(__name__)

model_primary_keys = [
{"id": "case_id", "model": "Case"},
Expand Down Expand Up @@ -404,7 +409,7 @@ def wrapper(request, *args, **kwargs):
user = request.user

if not user.is_verified():
print(f"{user=} is unverified. Tried accessing {view}")
logger.info(f"{user=} is unverified. Tried accessing {view}")
raise PermissionDenied()

return view(request, *args, **kwargs)
Expand Down
6 changes: 5 additions & 1 deletion epilepsy12/forms_folder/epilepsy12_user_form.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Any
import logging

from django import forms
from django.conf import settings
from django.core import validators
Expand All @@ -15,6 +17,8 @@
)
from ..models import Epilepsy12User, Organisation, VisitActivity

# Logging setup
logger = logging.getLogger(__name__)

class Epilepsy12UserUpdatePasswordForm(SetPasswordForm):
# form show when setting or resetting password
Expand Down Expand Up @@ -46,7 +50,7 @@ def save(self, *args, commit=True, **kwargs):
user = super().save(*args, commit=False, **kwargs)
user.password_last_set = timezone.now()
if commit:
print(f"Updating password_last_set to {timezone.now()}")
logger.debug(f"Updating password_last_set to {timezone.now()}")
user.save()
return user

Expand Down
10 changes: 7 additions & 3 deletions epilepsy12/general_functions/fetch_snomed.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# standard imports
import logging
import requests

# third party imports
Expand All @@ -7,6 +8,9 @@
# RCPCH imports
from ..constants import BENZODIAZEPINE_TYPES, ANTIEPILEPSY_MEDICINE_TYPES

# Logging setup
logger = logging.getLogger(__name__)


def fetch_ecl(ecl):
"""
Expand All @@ -16,7 +20,7 @@ def fetch_ecl(ecl):
response = requests.get(search_url)

if response.status_code == 404:
print("Could not get SNOMED data from server...")
logger.warning("Could not get SNOMED data from server...")
return None

serialised = response.json()
Expand All @@ -33,7 +37,7 @@ def fetch_concept(concept_id):
response = requests.get(search_url)

if response.status_code == 404:
print("Could not get SNOMED data from server...")
logger.warning("Could not get SNOMED data from server...")
return None

serialised = response.json()
Expand All @@ -49,7 +53,7 @@ def fetch_paediatric_neurodisability_outpatient_diagnosis_simple_reference_set()
response = requests.get(search_url)

if response.status_code == 404:
print("Could not get SNOMED data from server...")
logger.warning("Could not get SNOMED data from server...")
return None

# filters out Autism-related entries
Expand Down
41 changes: 22 additions & 19 deletions epilepsy12/general_functions/ods_update.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# python imports
import logging
import requests
from requests.exceptions import HTTPError

Expand All @@ -7,6 +8,8 @@
from django.utils import timezone, dateformat
from django.apps import apps

# Logging setup
logger = logging.getLogger(__name__)

def fetch_updated_organisations(time_frame: int = 30):
"""
Expand Down Expand Up @@ -37,7 +40,7 @@ def fetch_updated_organisations(time_frame: int = 30):
)
response.raise_for_status()
except HTTPError as e:
print(e.response.text)
logger.exception(e.response.text)

return response.json()["Organisations"]

Expand All @@ -51,7 +54,7 @@ def get_organisation(org_link):
response = requests.get(url=org_link, timeout=10)
response.raise_for_status()
except HTTPError as e:
print(e.response.text)
logger.exception(e.response.text)

return response.json()["Organisation"]

Expand Down Expand Up @@ -108,37 +111,37 @@ def update_organisation_model_with_ORD_changes():
ods_code = extract_ods_code(org_link=org_link["OrgLink"])
organisation = match_organisation(ods_code=ods_code)
if organisation:
print(
logger.info(
f"{index}. {ods_code} has a match with {organisation} in the E12 database"
)
ord_organisation_update = get_organisation(org_link["OrgLink"])
print(ord_organisation_update["Name"])
print(ord_organisation_update["GeoLoc"]["Location"]["AddrLn1"])
print(ord_organisation_update["GeoLoc"]["Location"]["AddrLn2"])
print(ord_organisation_update["GeoLoc"]["Location"]["Town"])
print(ord_organisation_update["GeoLoc"]["Location"]["PostCode"])
print(ord_organisation_update["GeoLoc"]["Location"]["Country"])
print(ord_organisation_update["Contacts"]["Contact"]["value"])
logger.info(ord_organisation_update["Name"])
logger.info(ord_organisation_update["GeoLoc"]["Location"]["AddrLn1"])
logger.info(ord_organisation_update["GeoLoc"]["Location"]["AddrLn2"])
logger.info(ord_organisation_update["GeoLoc"]["Location"]["Town"])
logger.info(ord_organisation_update["GeoLoc"]["Location"]["PostCode"])
logger.info(ord_organisation_update["GeoLoc"]["Location"]["Country"])
logger.info(ord_organisation_update["Contacts"]["Contact"]["value"])
else:
trust = match_trust(ods_code=ods_code)
if trust:
print(
logger.info(
f"{index}. {ods_code} has a match with {trust} in the E12 database"
)
ord_trust_update = get_organisation(org_link["OrgLink"])
print(ord_trust_update["Name"])
print(ord_trust_update["GeoLoc"]["Location"]["AddrLn1"])
logger.info(ord_trust_update["Name"])
logger.info(ord_trust_update["GeoLoc"]["Location"]["AddrLn1"])
try:
line_two = ord_trust_update["GeoLoc"]["Location"]["AddrLn2"]
print(line_two)
logger.info(line_two)
except Exception:
pass

print(ord_trust_update["GeoLoc"]["Location"]["Town"])
print(ord_trust_update["GeoLoc"]["Location"]["PostCode"])
print(ord_trust_update["GeoLoc"]["Location"]["Country"])
logger.info(ord_trust_update["GeoLoc"]["Location"]["Town"])
logger.info(ord_trust_update["GeoLoc"]["Location"]["PostCode"])
logger.info(ord_trust_update["GeoLoc"]["Location"]["Country"])
for i in ord_trust_update["Contacts"]["Contact"]:
if i["type"] == "http":
print(f'website: {i["value"]}')
logger.info(f'website: {i["value"]}')
else:
print(f'telephone: {i["value"]}')
logger.info(f'telephone: {i["value"]}')
8 changes: 6 additions & 2 deletions epilepsy12/general_functions/postcode.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import requests
import logging

from django.conf import settings
from ..constants import UNKNOWN_POSTCODES_NO_SPACES

# Logging setup
logger = logging.getLogger(__name__)

def is_valid_postcode(postcode: str) -> bool:
"""
Expand All @@ -23,7 +27,7 @@ def is_valid_postcode(postcode: str) -> bool:
return True

# Only other possibility should be 404, but handle any other status code
print(
logger.error(
f"Postcode validation failure. Could not validate postcode at {url}. {response.status_code=}"
)
return False
Expand All @@ -36,7 +40,7 @@ def return_random_postcode(country_boundary_identifier: str):
response = requests.get(url=url)

if response.status_code == 404:
print("Postcode generation failure. Could not get random postcode.")
logger.error("Postcode generation failure. Could not get random postcode.")
return None

return response.json()["data"]["relationships"]["example_postcodes"]["data"][0][
Expand Down
Loading

0 comments on commit 2ae50bc

Please sign in to comment.