-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FEAT: Unified Notifications Feature Implementation #1089
Open
tahierhussain
wants to merge
12
commits into
main
Choose a base branch
from
feat/unified-notifications-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cb45777
Backend changes to support logs storage in redis
tahierhussain 0f253ef
Implemented logs_helper plugin
tahierhussain b9738e8
UI changes to support unified notifications
tahierhussain 0ffd1c4
Merge branch 'main' of github.com:Zipstack/unstract into feat/unified…
tahierhussain 993a133
Merge branch 'main' into feat/unified-notifications-v2
tahierhussain 53b95bd
Fixed sonar issues
tahierhussain 0266676
Merge branch 'feat/unified-notifications-v2' of github.com:Zipstack/u…
tahierhussain 06fa051
Merge branch 'main' into feat/unified-notifications-v2
tahierhussain 7666cb6
Clear the logs message on logout
tahierhussain 051ffe4
UX Improvements
tahierhussain 97b7009
Merge branch 'feat/unified-notifications-v2' of github.com:Zipstack/u…
tahierhussain 1c90ae3
Merge branch 'main' into feat/unified-notifications-v2
tahierhussain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,66 @@ | ||
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) | ||
|
||
@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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tahierhussain if we are already storing logs in redis before publishing the message - why do we need this API?