Skip to content

Commit 5fc4f69

Browse files
committed
pythonpath
zip exist Use Map Cache
1 parent 0b521de commit 5fc4f69

29 files changed

Lines changed: 718 additions & 241 deletions

.github/workflows/sonarcloud.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ jobs:
111111
- name: Run unittest with redis_sync
112112
working-directory: redis_sync
113113
id: redis_sync
114+
env:
115+
PYTHONPATH: redis_sync/src:redis_sync/tests
114116
continue-on-error: true
115117
run: |
116118
poetry env use 3.11

filenameprocessor/src/clients.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import redis
99
from boto3 import client as boto3_client, resource as boto3_resource
1010

11-
# AWS Clients and Resources.
12-
REGION_NAME = "eu-west-2"
11+
REGION_NAME = os.getenv("AWS_REGION", "eu-west-2")
1312

1413
s3_client = boto3_client("s3", region_name=REGION_NAME)
1514
sqs_client = boto3_client("sqs", region_name=REGION_NAME)

filenameprocessor/src/file_name_processor.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from make_and_upload_ack_file import make_and_upload_the_ack_file
1515
from audit_table import upsert_audit_table, get_next_queued_file_details, ensure_file_is_not_a_duplicate
1616
from clients import logger
17-
from elasticache import upload_to_elasticache
1817
from logging_decorator import logging_decorator
1918
from supplier_permissions import validate_vaccine_type_permissions
2019
from errors import (
@@ -34,7 +33,7 @@
3433
@logging_decorator
3534
def handle_record(record) -> dict:
3635
"""
37-
Processes a single record based on whether it came from the 'data-sources' or 'config' bucket.
36+
Processes a single record based on whether it came from the 'data-sources' bucket.
3837
Returns a dictionary containing information to be included in the logs.
3938
"""
4039
try:
@@ -140,17 +139,6 @@ def handle_record(record) -> dict:
140139
"supplier": supplier
141140
}
142141

143-
elif "config" in bucket_name:
144-
try:
145-
upload_to_elasticache(file_key, bucket_name)
146-
logger.info("%s content successfully uploaded to cache", file_key)
147-
message = "File content successfully uploaded to cache"
148-
return {"statusCode": 200, "message": message, "file_key": file_key}
149-
except Exception as error: # pylint: disable=broad-except
150-
logger.error("Error uploading to cache for file '%s': %s", file_key, error)
151-
message = "Failed to upload file content to cache"
152-
return {"statusCode": 500, "message": message, "file_key": file_key, "error": str(error)}
153-
154142
else:
155143
try:
156144
vaccine_type, supplier = validate_file_key(file_key)

recordprocessor/src/clients.py

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

33
import logging
4+
import os
5+
46
from boto3 import client as boto3_client, resource as boto3_resource
57
from botocore.config import Config
8+
from redis_cacher import RedisCacher
69

7-
REGION_NAME = "eu-west-2"
10+
REGION_NAME = os.getenv("AWS_REGION")
811

912
s3_client = boto3_client("s3", region_name=REGION_NAME)
1013
kinesis_client = boto3_client(
@@ -21,3 +24,7 @@
2124
logging.basicConfig(level="INFO")
2225
logger = logging.getLogger()
2326
logger.setLevel("INFO")
27+
28+
REDIS_HOST = os.getenv("REDIS_HOST")
29+
REDIS_PORT = os.getenv("REDIS_PORT")
30+
redis_cacher = RedisCacher(REDIS_HOST, REDIS_PORT, logger)

recordprocessor/src/constants.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,8 @@ class Urls:
8484
NHS_NUMBER = "https://fhir.nhs.uk/Id/nhs-number"
8585
NULL_FLAVOUR_CODES = "http://terminology.hl7.org/CodeSystem/v3-NullFlavor"
8686
VACCINATION_PROCEDURE = "https://fhir.hl7.org.uk/StructureDefinition/Extension-UKCore-VaccinationProcedure"
87+
88+
89+
class RedisCacheKeys:
90+
PERMISSIONS_CONFIG_FILE_KEY = "permissions_config.json"
91+
DISEASE_MAPPING_FILE_KEY = "disease_mapping.json"

recordprocessor/src/mappings.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
from enum import Enum
44
from typing import Dict, List
5-
from constants import Urls
5+
from constants import Urls, RedisCacheKeys
6+
from redis_cacher import redis_cacher
67

78

89
class Vaccine(Enum):
@@ -54,6 +55,8 @@ class DiseaseDisplayTerm(Enum):
5455
Vaccine.RSV: [Disease.RSV],
5556
}
5657

58+
VACCINE_DISEASE_MAPPING = redis_cacher.get(RedisCacheKeys.DISEASE_MAPPING_FILE_KEY)
59+
5760

5861
def map_target_disease(vaccine: Vaccine) -> list:
5962
"""Returns the target disease element for the given vaccine type using the vaccine_disease_mapping"""
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
import redis
3+
4+
5+
class RedisCacher():
6+
""" RedisCacher abstraction class to decouple application code
7+
from direct use of Redis client.
8+
Also centralised error handling & extensibility.
9+
"""
10+
11+
def __init__(self, redis_host, redis_port, logger):
12+
try:
13+
# Attempt to connect to Redis
14+
self.redis_client = redis.StrictRedis(redis_host, redis_port, decode_responses=True)
15+
# Check the connection with a PING command
16+
if self.redis_client.ping():
17+
logger.info("Successfully connected to Redis.")
18+
else:
19+
logger.error("Failed to connect to Redis.")
20+
except Exception as e:
21+
logger.exception(f"Connection to Redis failed: {e}")
22+
23+
def get(self, key: str) -> dict:
24+
"""Gets the value from Redis cache for the given key."""
25+
value = self.redis_client.get(key)
26+
if value is not None:
27+
return json.loads(value)
28+
return {}

redis_sync/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
test:
3+
@PYTHONPATH=src python -m unittest
4+
5+
coverage-run:
6+
coverage run -m unittest discover -v
7+
8+
coverage-report:
9+
coverage report -m
10+
11+
coverage-html:
12+
coverage html
13+
14+
.PHONY: build package

0 commit comments

Comments
 (0)