|
1 | 1 | "Upload the content from a config file in S3 to ElastiCache (Redis)" |
2 | | - |
3 | 2 | from redis_cacher import RedisCacher |
4 | 3 |
|
5 | 4 | from constants import DISEASE_MAPPING_FILE_KEY |
6 | 5 |
|
7 | 6 |
|
8 | 7 | class DiseaseMapping: |
9 | 8 | """Class to handle disease mapping operations.""" |
10 | | - |
| 9 | + # redis_cache instance is found in clients.py |
11 | 10 | 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", []) |
13 | 59 |
|
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 |
0 commit comments