-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: Unified Notifications Feature Implementation (#1089)
* Backend changes to support logs storage in redis * Implemented logs_helper plugin * UI changes to support unified notifications * Fixed sonar issues * Clear the logs message on logout * UX Improvements * Add comment to describe the 'store_log' method --------- Signed-off-by: Tahier Hussain <[email protected]>
- Loading branch information
1 parent
fd067c9
commit c7f0ecf
Showing
28 changed files
with
727 additions
and
280 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
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
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,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class LogsHelperConfig(AppConfig): | ||
name = "logs_helper" |
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,3 @@ | ||
class LogsHelperKeys: | ||
LOG = "LOG" | ||
LOG_EVENTS_ID = "log_events_id" |
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,24 @@ | ||
from utils.cache_service import CacheService | ||
|
||
|
||
class LogService: | ||
@staticmethod | ||
def remove_logs_on_logout(session_id: str) -> None: | ||
|
||
if session_id: | ||
key_pattern = f"{LogService.generate_redis_key(session_id=session_id)}*" | ||
|
||
# Delete keys matching the pattern | ||
CacheService.clear_cache(key_pattern=key_pattern) | ||
|
||
@staticmethod | ||
def generate_redis_key(session_id): | ||
"""Generate a Redis key for logs based on the provided session_id. | ||
Parameters: | ||
session_id (str): The session identifier to include in the Redis key. | ||
Returns: | ||
str: The constructed Redis key. | ||
""" | ||
return f"logs:{session_id}" |
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,5 @@ | ||
from rest_framework import serializers | ||
|
||
|
||
class StoreLogMessagesSerializer(serializers.Serializer): | ||
log = serializers.CharField() |
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,16 @@ | ||
from django.urls import path | ||
from rest_framework.urlpatterns import format_suffix_patterns | ||
|
||
from .views import LogsHelperViewSet | ||
|
||
logs_helper = LogsHelperViewSet.as_view({"get": "get_logs", "post": "store_log"}) | ||
|
||
urlpatterns = format_suffix_patterns( | ||
[ | ||
path( | ||
"", | ||
logs_helper, | ||
name="logs-helper", | ||
), | ||
] | ||
) |
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,68 @@ | ||
import json | ||
import logging | ||
from datetime import datetime, timezone | ||
|
||
from django.conf import settings | ||
from django.http import HttpRequest | ||
from rest_framework import status, viewsets | ||
from rest_framework.decorators import action | ||
from rest_framework.response import Response | ||
from utils.cache_service import CacheService | ||
from utils.user_session import UserSessionUtils | ||
|
||
from .log_service import LogService | ||
from .serializers import StoreLogMessagesSerializer | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class LogsHelperViewSet(viewsets.ModelViewSet): | ||
"""Viewset to handle all Tool Studio prompt related API logics.""" | ||
|
||
@action(detail=False, methods=["get"]) | ||
def get_logs(self, request: HttpRequest) -> Response: | ||
# Extract the session ID | ||
session_id: str = UserSessionUtils.get_session_id(request=request) | ||
|
||
# Construct the Redis key pattern to match keys | ||
# associated with the session ID | ||
redis_key = LogService.generate_redis_key(session_id=session_id) | ||
|
||
# Retrieve keys matching the pattern | ||
keys = CacheService.get_all_keys(f"{redis_key}*") | ||
|
||
# Retrieve values corresponding to the keys and sort them by timestamp | ||
logs = [] | ||
for key in keys: | ||
log_data = CacheService.get_key(key) | ||
logs.append(log_data) | ||
|
||
# Sort logs based on timestamp | ||
sorted_logs = sorted(logs, key=lambda x: x["timestamp"]) | ||
|
||
return Response({"data": sorted_logs}, status=status.HTTP_200_OK) | ||
|
||
# This API will be triggered whenever a notification message | ||
# pops up in the UI. | ||
@action(detail=False, methods=["post"]) | ||
def store_log(self, request: HttpRequest) -> Response: | ||
"""Store log message in Redis.""" | ||
# Extract the session ID | ||
logs_expiry = settings.LOGS_EXPIRATION_TIME_IN_SECOND | ||
session_id: str = UserSessionUtils.get_session_id(request=request) | ||
|
||
serializer = StoreLogMessagesSerializer(data=request.data) | ||
serializer.is_valid(raise_exception=True) | ||
|
||
# Extract the log message from the validated data | ||
log: str = serializer.validated_data.get("log") | ||
log_data = json.loads(log) | ||
timestamp = datetime.now(timezone.utc).timestamp() | ||
|
||
redis_key = ( | ||
f"{LogService.generate_redis_key(session_id=session_id)}:{timestamp}" | ||
) | ||
|
||
CacheService.set_key(redis_key, log_data, logs_expiry) | ||
|
||
return Response({"message": "Successfully stored the message in redis"}) |
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
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
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
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
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
Oops, something went wrong.