Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
zMardone committed Nov 21, 2024
2 parents 9b4e391 + 365b61e commit 2661926
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
2 changes: 2 additions & 0 deletions nexus/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@

# Redis Config

week_duration = 60 * 60 * 24 * 7
REDIS_MESSAGE_CACHE_KEY_DURATION = env.int("REDIS_MESSAGE_CACHE_KEY_DURATION", default=week_duration)
REDIS_URL = env.str("CELERY_BROKER_URL", default="redis://localhost:6379/1")

CACHES = {
Expand Down
14 changes: 10 additions & 4 deletions nexus/usecases/logs/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
from nexus.projects.signals import send_message_to_websocket

from django.core.cache import cache
from django.conf import settings


# TODO refactor this class methods to recieve MessageLog object as parameter not using class attributes
class CreateLogUsecase: # TODO: rename method

def __init__(self, message=None) -> None:
self.message = message
if self.message:
self.log = self.message.messagelog

def _create_redis_cache(
self,
message_log: MessageLog,
Expand Down Expand Up @@ -43,10 +50,9 @@ def _create_redis_cache(
cache.set(cache_key, last_5_messages)
print("new cache: ", cache.get(cache_key))

def __init__(self, message = None) -> None:
self.message = message
if self.message:
self.log = self.message.messagelog
key_expiration = settings.REDIS_MESSAGE_CACHE_KEY_DURATION

cache.set(cache_key, last_5_messages, key_expiration)

def create_message(self, text: str, contact_urn: str, status: str = "P") -> Message:
self.message = Message.objects.create(
Expand Down
36 changes: 36 additions & 0 deletions nexus/usecases/logs/tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class TestCreateLogUsecase(TestCase):
def setUp(self) -> None:
self.msg_log: MessageLog = MessageLogFactory()

self.msg_log1: MessageLog = MessageLogFactory()
self.msg_log2: MessageLog = MessageLogFactory()
self.project_uuid1 = self.msg_log1.project.uuid
self.project_uuid2 = self.msg_log2.project.uuid

def test_redis_cache(self):
project_uuid = self.msg_log.project.uuid

Expand All @@ -22,3 +27,34 @@ def test_redis_cache(self):
cache_data = cache_data[0]

self.assertEqual(cache_data['text'], self.msg_log.message.text)

def test_redis_cache_multiple_conversations(self):
usecase = CreateLogUsecase()

for i in range(6):
msg_log = MessageLogFactory(
project=self.msg_log1.project,
message__contact_urn=self.msg_log1.message.contact_urn,
message__text=f'Text {i}'
)
usecase._create_redis_cache(msg_log, self.project_uuid1)

for i in range(6):
msg_log = MessageLogFactory(
project=self.msg_log2.project,
message__contact_urn=self.msg_log2.message.contact_urn,
message__text=f'Text {i}'
)
usecase._create_redis_cache(msg_log, self.project_uuid2)

cache_key1 = f"last_5_messages_{self.project_uuid1}_{self.msg_log1.message.contact_urn}"
cache_data1 = cache.get(cache_key1)
self.assertEqual(len(cache_data1), 5)
self.assertEqual(cache_data1[0]['text'], 'Text 5')

cache_key2 = f"last_5_messages_{self.project_uuid2}_{self.msg_log2.message.contact_urn}"
cache_data2 = cache.get(cache_key2)
self.assertEqual(len(cache_data2), 5)
self.assertEqual(cache_data2[0]['text'], 'Text 5')

self.assertNotEqual(cache_data1, cache_data2)
10 changes: 7 additions & 3 deletions router/tests/test_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from nexus.usecases.intelligences.create import create_llm
from nexus.usecases.intelligences.retrieve import get_file_info
from nexus.usecases.logs.create import CreateLogUsecase
from nexus.usecases.logs.tests.logs_factory import MessageLogFactory

from nexus.intelligences.llms.chatgpt import ChatGPTClient
from nexus.intelligences.llms.wenigpt import WeniGPTClient
Expand Down Expand Up @@ -437,12 +438,15 @@ def test_update_status_with_exception(self):

self.assertEquals(self.log_usecase.message.status, "F")

@skip("LogUsecase needs refactor")
def test_update_log_field(self):
self.assertIsNone(self.log_usecase.log.chunks)
message_log = MessageLogFactory()
self.assertIsNone(message_log.chunks)

chunks = ["Chunk 1", "Chunk 2", "Chunk 3"]

self.log_usecase.update_log_field(
chunks=chunks, project_id=self.project.uuid
msg_log_usecase = CreateLogUsecase()
msg_log_usecase.update_log_field(
chunks=chunks, project_id=message_log.project.uuid
)
self.assertEquals(self.log_usecase.log.chunks, chunks)

0 comments on commit 2661926

Please sign in to comment.