-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
219 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ downloads/ | |
eggs/ | ||
.eggs/ | ||
lib/ | ||
!src/lib | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import json | ||
import boto3 | ||
from typing import Optional, Dict, Any | ||
from aws_lambda_powertools import Logger, Tracer | ||
from .constants import CACHE_VERSION | ||
|
||
logger = Logger() | ||
tracer = Tracer() | ||
|
||
class CacheService: | ||
def __init__(self, bucket_name: str): | ||
self.s3_client = boto3.client('s3') | ||
self.bucket_name = bucket_name | ||
logger.info(f"Initialized CacheService with bucket: {bucket_name}") | ||
|
||
@staticmethod | ||
def create_versioned_key(hash_key: str) -> str: | ||
"""Create a versioned lib key.""" | ||
return f"{CACHE_VERSION}/{hash_key}" | ||
|
||
@tracer.capture_method | ||
def get(self, hash_key: str) -> Optional[Dict[str, Any]]: | ||
"""Try to get cached response from S3.""" | ||
versioned_key = self.create_versioned_key(hash_key) | ||
logger.debug(f"Attempting to retrieve cached response for versioned key: {versioned_key}") | ||
try: | ||
response = self.s3_client.get_object(Bucket=self.bucket_name, Key=versioned_key) | ||
data = json.loads(response['Body'].read().decode('utf-8')) | ||
logger.info(f"Successfully retrieved cached response for versioned key: {versioned_key}") | ||
logger.debug(f"Cache data: {json.dumps(data)}") | ||
return data | ||
except self.s3_client.exceptions.NoSuchKey: | ||
logger.info(f"No cached response found for versioned key: {versioned_key}") | ||
return None | ||
except Exception as e: | ||
logger.error(f"Error retrieving from lib: {str(e)}", exc_info=True) | ||
logger.error(f"Versioned key: {versioned_key}, Bucket: {self.bucket_name}") | ||
return None | ||
|
||
@tracer.capture_method | ||
def save(self, hash_key: str, data: Dict[str, Any]) -> None: | ||
"""Save response to S3 lib.""" | ||
versioned_key = self.create_versioned_key(hash_key) | ||
logger.debug(f"Attempting to lib response for versioned key: {versioned_key}") | ||
try: | ||
self.s3_client.put_object( | ||
Bucket=self.bucket_name, | ||
Key=versioned_key, | ||
Body=json.dumps(data), | ||
ContentType='application/json' | ||
) | ||
logger.info(f"Successfully cached response for versioned key: {versioned_key}") | ||
logger.debug(f"Cached data: {json.dumps(data)}") | ||
except Exception as e: | ||
logger.error(f"Error saving to lib: {str(e)}", exc_info=True) | ||
logger.error(f"Versioned key: {versioned_key}, Bucket: {self.bucket_name}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
BUCKET_NAME = 'path-summarize-data' | ||
CACHE_VERSION = '1.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from pydantic import BaseModel, Field | ||
|
||
class AlertSummary(BaseModel): | ||
text: str | ||
is_delay: bool = Field(description="indicating if the alert is about a delay on lines, true is yes, false if it is a general announcement") | ||
is_relevant: bool = Field(description="indicating if the alert affects the rider's experience or not") |
Oops, something went wrong.