Skip to content

Commit 3f09828

Browse files
committed
test_unique_permission.py requires cacher patch
1 parent 23b3a28 commit 3f09828

9 files changed

Lines changed: 274 additions & 167 deletions

File tree

recordprocessor/src/clients.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Initialise s3 and kinesis clients"""
22

3-
# import os
3+
import os
44
import logging
55
from boto3 import client as boto3_client, resource as boto3_resource
66
from botocore.config import Config
7+
from redis_cacher import RedisCacher
78

89
REGION_NAME = "eu-west-2"
910

@@ -22,3 +23,9 @@
2223
logging.basicConfig(level="INFO")
2324
logger = logging.getLogger()
2425
logger.setLevel("INFO")
26+
27+
# TODO Remove defaults for production
28+
REDIS_HOST = os.getenv("REDIS_HOST", "immunisation-redis-cluster.0y9mwl.0001.euw2.cache.amazonaws.com")
29+
REDIS_PORT = os.getenv("REDIS_PORT", 6379)
30+
31+
redis_cacher = RedisCacher(REDIS_HOST, REDIS_PORT, logger)
Lines changed: 61 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,62 @@
11
{
2-
"disease":
3-
[
4-
{
5-
"id": "COVID19",
6-
"code": "840539006",
7-
"display": "Disease caused by severe acute respiratory syndrome coronavirus 2"
8-
},
9-
{
10-
"id": "FLU",
11-
"code": "6142004",
12-
"display": "Influenza"
13-
},
14-
{
15-
"id": "HPV",
16-
"code": "240532009",
17-
"display": "Human Papillomavirus Infection"
18-
},
19-
{
20-
"id": "FLU_B",
21-
"code": "77385005",
22-
"display": "Influenza B"
23-
},
24-
{
25-
"id": "FLU_C",
26-
"code": "77388002",
27-
"display": "Influenza C"
28-
},
29-
{
30-
"id": "MEASLES",
31-
"code": "14189004",
32-
"display": "Measles"
33-
},
34-
{
35-
"id": "MUMPS",
36-
"code": "36989005",
37-
"display": "Mumps"
38-
},
39-
{
40-
"id": "RUBELLA",
41-
"code": "36653000",
42-
"display": "Rubella"
43-
},
44-
{
45-
"id": "RSV",
46-
"code": "55735004",
47-
"display": "Respiratory syncytial virus infection (disorder)"
48-
}
49-
],
50-
"vaccine":
51-
[
52-
{
53-
"id": "COVID19",
54-
"display": "COVID-19 vaccine",
55-
"diseases":
56-
[
57-
"COVID19"
58-
]
59-
},
60-
{
61-
"id": "FLU",
62-
"display": "Influenza vaccine",
63-
"diseases":
64-
[
65-
"FLU"
66-
]
67-
},
68-
{
69-
"id": "HPV",
70-
"display": "Human Papillomavirus vaccine",
71-
"diseases":
72-
[
73-
"HPV"
74-
]
75-
},
76-
{
77-
"id": "MMR",
78-
"display": "Measles, mumps, and rubella vaccine",
79-
"diseases":
80-
[
81-
"MEASLES",
82-
"MUMPS",
83-
"RUBELLA"
84-
]
85-
},
86-
{
87-
"id": "RSV",
88-
"display": "Respiratory syncytial virus vaccine",
89-
"diseases":
90-
[
91-
"RSV"
92-
]
93-
}
94-
]
95-
}
2+
"disease": {
3+
"COVID19": {
4+
"code": "840539006",
5+
"display": "Disease caused by severe acute respiratory syndrome coronavirus 2"
6+
},
7+
"FLU": {
8+
"code": "6142004",
9+
"display": "Influenza"
10+
},
11+
"HPV": {
12+
"code": "240532009",
13+
"display": "Human Papillomavirus Infection"
14+
},
15+
"FLU_B": {
16+
"code": "77385005",
17+
"display": "Influenza B"
18+
},
19+
"FLU_C": {
20+
"code": "77388002",
21+
"display": "Influenza C"
22+
},
23+
"MEASLES": {
24+
"code": "14189004",
25+
"display": "Measles"
26+
},
27+
"MUMPS": {
28+
"code": "36989005",
29+
"display": "Mumps"
30+
},
31+
"RUBELLA": {
32+
"code": "36653000",
33+
"display": "Rubella"
34+
},
35+
"RSV": {
36+
"code": "55735004",
37+
"display": "Respiratory syncytial virus infection (disorder)"
38+
}
39+
},
40+
"vaccine": {
41+
"COVID19": {
42+
"display": "COVID-19 vaccine",
43+
"diseases": ["COVID19"]
44+
},
45+
"FLU": {
46+
"display": "Influenza vaccine",
47+
"diseases": ["FLU"]
48+
},
49+
"HPV": {
50+
"display": "Human Papillomavirus vaccine",
51+
"diseases": ["HPV"]
52+
},
53+
"MMR": {
54+
"display": "Measles, mumps, and rubella vaccine",
55+
"diseases": ["MEASLES", "MUMPS", "RUBELLA"]
56+
},
57+
"RSV": {
58+
"display": "Respiratory syncytial virus vaccine",
59+
"diseases": ["RSV"]
60+
}
61+
}
62+
}

recordprocessor/src/mappings.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Mappings for converting vaccine type into target disease FHIR element"""
22

33
from enum import Enum
4-
from typing import Dict, List
4+
# from typing import Dict, List
55
from constants import Urls
6+
from redis_disease_mapping import DiseaseMapping
7+
from clients import redis_cacher
68

79

810
class Vaccine(Enum):
@@ -47,12 +49,14 @@ class DiseaseDisplayTerm(Enum):
4749
RSV: str = "Respiratory syncytial virus infection (disorder)"
4850

4951

50-
VACCINE_DISEASE_MAPPING: Dict[Vaccine, List[Disease]] = {
51-
Vaccine.COVID_19: [Disease.COVID_19],
52-
Vaccine.FLU: [Disease.FLU],
53-
Vaccine.MMR: [Disease.MEASLES, Disease.MUMPS, Disease.RUBELLA],
54-
Vaccine.RSV: [Disease.RSV],
55-
}
52+
# VACCINE_DISEASE_MAPPING: Dict[Vaccine, List[Disease]] = {
53+
# Vaccine.COVID_19: [Disease.COVID_19],
54+
# Vaccine.FLU: [Disease.FLU],
55+
# Vaccine.MMR: [Disease.MEASLES, Disease.MUMPS, Disease.RUBELLA],
56+
# Vaccine.RSV: [Disease.RSV],
57+
# }
58+
59+
VACCINE_DISEASE_MAPPING = (DiseaseMapping(redis_cacher)).get_vaccine_map()
5660

5761

5862
def map_target_disease(vaccine: Vaccine) -> list:

recordprocessor/src/mappings_rdis.py

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
"Upload the content from a config file in S3 to ElastiCache (Redis)"
22
import json
33
import redis
4-
from clients import logger
54

65

76
class RedisCacher():
8-
def __init__(cls, redis_host, redis_port):
7+
8+
def __init__(self, redis_host, redis_port, logger):
99
try:
1010
# Attempt to connect to Redis
11-
cls.redis_client = redis.StrictRedis(redis_host, redis_port, decode_responses=True)
11+
self.redis_client = redis.StrictRedis(redis_host, redis_port, decode_responses=True)
1212
# Check the connection with a PING command
13-
if cls.redis_client.ping():
13+
if self.redis_client.ping():
1414
logger.info("Successfully connected to Redis.")
1515
else:
1616
logger.error("Failed to connect to Redis.")
1717
except Exception as e:
1818
logger.exception(f"Connection to Redis failed: {e}")
1919

20-
def get_cache(cls, key: str) -> dict:
20+
def get_cache(self, key: str) -> dict:
2121
"""Gets the value from Redis cache for the given key."""
22-
value = cls.redis_client.get(key)
22+
value = self.redis_client.get(key)
2323
if value is not None:
2424
return json.loads(value)
2525
return {}
26-
27-
28-
# redis_client = RedisCacher(host=os.getenv("REDIS_HOST"), port=os.getenv("REDIS_PORT"))
29-
host = "immunisation-redis-cluster.0y9mwl.0001.euw2.cache.amazonaws.com"
30-
redis_client = RedisCacher(host, 6379)
Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,62 @@
11
"Upload the content from a config file in S3 to ElastiCache (Redis)"
2-
32
from redis_cacher import RedisCacher
43

54
from constants import DISEASE_MAPPING_FILE_KEY
65

76

87
class DiseaseMapping:
98
"""Class to handle disease mapping operations."""
10-
9+
# redis_cache instance is found in clients.py
1110
def __init__(self, redis_cache: RedisCacher):
12-
self.redis_cache = redis_cache
11+
mapping = redis_cache.get_cache(DISEASE_MAPPING_FILE_KEY)
12+
self.vaccines = mapping["vaccine"]
13+
self.diseases = mapping["disease"]
14+
self.load_vaccines_into_diseases()
15+
self.vaccine_mapp = self.load_simple_vaccine_map()
16+
17+
def load_simple_vaccine_map(self) -> dict:
18+
"""Load a simple vaccine map for quick access."""
19+
vaccine_map = {}
20+
for vaccine, details in self.vaccines.items():
21+
# set key as vaccine name and value as list of diseases
22+
# if details do not contain diseases, set it to an empty list
23+
if not isinstance(details, dict):
24+
continue
25+
diseases = details.get("diseases", [])
26+
if not isinstance(diseases, list):
27+
diseases = []
28+
vaccine_map[vaccine] = diseases
29+
return vaccine_map
30+
31+
def load_vaccines_into_diseases(self):
32+
"""Load vaccines into diseases for easier access."""
33+
# loop through vaccines, identify diseases, and add them to the disease map
34+
for vaccine, details in self.vaccines.items():
35+
diseases = details.get("diseases", [])
36+
for disease in diseases:
37+
if disease not in self.diseases:
38+
self.diseases[disease] = {"vaccines": []}
39+
# if vaccines key does not exist, create it
40+
if "vaccines" not in self.diseases[disease]:
41+
self.diseases[disease]["vaccines"] = []
42+
# if vaccine not in the disease's vaccines, add it
43+
if vaccine not in self.diseases[disease]["vaccines"]:
44+
self.diseases[disease]["vaccines"].append(vaccine)
45+
46+
def get_diseases(self, vaccine: str) -> list:
47+
"""Returns a list of diseases for the given vaccine."""
48+
vaccine = self.vaccines.get(vaccine, {})
49+
if not vaccine:
50+
return []
51+
return vaccine.get("diseases", [])
52+
53+
def get_vaccines(self, disease: str) -> list:
54+
"""Returns a list of vaccines for the given disease."""
55+
disease = self.disease_map.get(disease, {})
56+
if not disease:
57+
return []
58+
return disease.get("vaccines", [])
1359

14-
def get_disease_mapping(self) -> dict:
15-
"""Gets and returns the disease mapping file from ElastiCache (Redis)."""
16-
ret = self.redis_cache.get_cache(DISEASE_MAPPING_FILE_KEY)
17-
return ret
60+
def get_vaccine_map(self) -> dict:
61+
"""Returns the vaccine map."""
62+
return self.vaccine_map
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"disease": {
3+
"COVID19": {
4+
"code": "840539006",
5+
"display": "Disease caused by severe acute respiratory syndrome coronavirus 2"
6+
},
7+
"FLU": {
8+
"code": "6142004",
9+
"display": "Influenza"
10+
},
11+
"HPV": {
12+
"code": "240532009",
13+
"display": "Human Papillomavirus Infection"
14+
},
15+
"FLU_B": {
16+
"code": "77385005",
17+
"display": "Influenza B"
18+
},
19+
"FLU_C": {
20+
"code": "77388002",
21+
"display": "Influenza C"
22+
},
23+
"MEASLES": {
24+
"code": "14189004",
25+
"display": "Measles"
26+
},
27+
"MUMPS": {
28+
"code": "36989005",
29+
"display": "Mumps"
30+
},
31+
"RUBELLA": {
32+
"code": "36653000",
33+
"display": "Rubella"
34+
},
35+
"RSV": {
36+
"code": "55735004",
37+
"display": "Respiratory syncytial virus infection (disorder)"
38+
}
39+
},
40+
"vaccine": {
41+
"COVID19": {
42+
"display": "COVID-19 vaccine",
43+
"diseases": ["COVID19"]
44+
},
45+
"FLU": {
46+
"display": "Influenza vaccine",
47+
"diseases": ["FLU"]
48+
},
49+
"HPV": {
50+
"display": "Human Papillomavirus vaccine",
51+
"diseases": ["HPV"]
52+
},
53+
"MMR": {
54+
"display": "Measles, mumps, and rubella vaccine",
55+
"diseases": ["MEASLES", "MUMPS", "RUBELLA"]
56+
},
57+
"RSV": {
58+
"display": "Respiratory syncytial virus vaccine",
59+
"diseases": ["RSV"]
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)