persistence for recurring api calls
All checks were successful
Build and push docker image / Build (pull_request) Successful in 1m48s

This commit is contained in:
2024-07-31 12:54:25 +02:00
parent db82495f11
commit 07dde5ab58
11 changed files with 118 additions and 81 deletions

View File

@@ -1,4 +1,3 @@
import uuid
from .landmark import Landmark
from utils.get_time_separation import get_time
@@ -9,8 +8,7 @@ class LinkedLandmarks:
"""
_landmarks = list[Landmark]
total_time = int
uuid = str
total_time: int = 0
def __init__(self, data: list[Landmark] = None) -> None:
"""
@@ -19,7 +17,6 @@ class LinkedLandmarks:
Args:
data (list[Landmark], optional): The list of landmarks that are linked together. Defaults to None.
"""
self.uuid = uuid.uuid4()
self._landmarks = data if data else []
self._link_landmarks()
@@ -28,7 +25,6 @@ class LinkedLandmarks:
"""
Create the links between the landmarks in the list by setting their .next_uuid and the .time_to_next attributes.
"""
self.total_time = 0
for i, landmark in enumerate(self._landmarks[:-1]):
landmark.next_uuid = self._landmarks[i + 1].uuid
time_to_next = get_time(landmark.location, self._landmarks[i + 1].location)
@@ -44,18 +40,4 @@ class LinkedLandmarks:
def __str__(self) -> str:
return f"LinkedLandmarks, total time: {self.total_time} minutes, {len(self._landmarks)} stops: [{','.join([str(landmark) for landmark in self._landmarks])}]"
def asdict(self) -> dict:
"""
Convert the linked landmarks to a json serializable dictionary.
Returns:
dict: A dictionary representation of the linked landmarks.
"""
return {
'uuid': self.uuid,
'total_time': self.total_time,
'landmarks': [landmark.dict() for landmark in self._landmarks]
}
return f"LinkedLandmarks [{' ->'.join([str(landmark) for landmark in self._landmarks])}]"

View File

@@ -2,7 +2,6 @@ from pydantic import BaseModel
from typing import Optional, Literal
class Preference(BaseModel) :
name: str
type: Literal['sightseeing', 'nature', 'shopping', 'start', 'finish']
score: int # score could be from 1 to 5
@@ -17,5 +16,5 @@ class Preferences(BaseModel) :
# Shopping (diriger plutôt vers des zones / rues commerçantes)
shopping : Preference
max_time_minute: Optional[int] = 6*60
max_time_minute: Optional[int] = 6*60
detour_tolerance_minute: Optional[int] = 0

View File

@@ -0,0 +1,28 @@
from pydantic import BaseModel, Field
from .landmark import Landmark
from .linked_landmarks import LinkedLandmarks
import uuid
class Trip(BaseModel):
uuid: str = Field(default_factory=uuid.uuid4)
total_time: int
first_landmark_uuid: str
@classmethod
def from_linked_landmarks(self, landmarks: LinkedLandmarks, cache_client) -> "Trip":
"""
Initialize a new Trip object and ensure it is stored in the cache.
"""
trip = Trip(
total_time = landmarks.total_time,
first_landmark_uuid = str(landmarks[0].uuid)
)
# Store the trip in the cache
cache_client.set(f"trip_{trip.uuid}", trip)
for landmark in landmarks:
cache_client.set(f"landmark_{landmark.uuid}", landmark)
return trip