From 18e5c802417001e3ee59b5da0e849e1a1a56e9c2 Mon Sep 17 00:00:00 2001 From: Khai Tran Date: Sat, 4 May 2024 11:42:38 +0700 Subject: [PATCH] chore: Update log in user reward --- locker_server/api/v1_0/user_rewards/views.py | 8 ++++++++ locker_server/shared/log/config.py | 12 ++++++++++++ locker_server/shared/log/cylog.py | 16 ++++++++++++++++ locker_server/shared/log/handlers.py | 14 ++++++++++++++ server_config/settings/dev.py | 1 + 5 files changed, 51 insertions(+) diff --git a/locker_server/api/v1_0/user_rewards/views.py b/locker_server/api/v1_0/user_rewards/views.py index 6358fac4..205510d4 100644 --- a/locker_server/api/v1_0/user_rewards/views.py +++ b/locker_server/api/v1_0/user_rewards/views.py @@ -18,6 +18,7 @@ USER_MISSION_STATUS_UNDER_VERIFICATION, USER_MISSION_STATUS_REWARD_SENT, REWARD_TYPE_PREMIUM from locker_server.shared.constants.transactions import PLAN_TYPE_PM_FREE, PLAN_TYPE_PM_PREMIUM from locker_server.shared.error_responses.error import gen_error +from locker_server.shared.log.cylog import CyLog from locker_server.shared.utils.app import now from locker_server.shared.utils.factory import factory @@ -119,6 +120,13 @@ def completed(self, request, *args, **kwargs): if not mission_factory: return Response(status=status.HTTP_200_OK, data={"claim": False}) input_data = {"user": user, "user_identifier": user_identifier} + + if kwargs.get("pk") in ["capterra_rating_and_review", "clutch_rating_and_review", "g2_rating_and_review"]: + CyLog.info(**{ + "message": f"[+] Checking user reward: #{kwargs.get('pk')} - {input_data} by user #{user.user_id}", + "output": ["slack_reward_checking"] + }) + mission_check = mission_factory.check_mission_completion(input_data) answer = json.dumps(answer) diff --git a/locker_server/shared/log/config.py b/locker_server/shared/log/config.py index ce794a53..89e5d83d 100644 --- a/locker_server/shared/log/config.py +++ b/locker_server/shared/log/config.py @@ -45,6 +45,12 @@ def filter(self, record): 'level': logging.DEBUG, 'filters': ['site_filter'] }, + 'slack_reward_checking': { + 'class': 'locker_server.shared.log.handlers.SlackRewardCheckingHandler', + 'formatter': 'medium', + 'level': logging.DEBUG, + 'filters': ['site_filter'] + }, 'console': { 'level': logging.DEBUG, 'class': 'logging.StreamHandler', @@ -76,6 +82,12 @@ def filter(self, record): 'propagate': True, 'filters': ['site_filter'] }, + 'slack_reward_checking_service': { + 'handlers': ['slack_reward_checking', ], + 'level': 'DEBUG', + 'propagate': True, + 'filters': ['site_filter'] + }, 'stdout_service': { 'level': 'DEBUG', 'handlers': ['console', ], diff --git a/locker_server/shared/log/cylog.py b/locker_server/shared/log/cylog.py index 8eebe35d..c3adb9f1 100644 --- a/locker_server/shared/log/cylog.py +++ b/locker_server/shared/log/cylog.py @@ -18,6 +18,8 @@ def create(cls, data): cls.log_to_slack(message, severity) elif output_type == "slack_new_users": cls.log_to_slack_new_users(message, severity) + elif output_type == "slack_reward_checking": + cls.log_to_slack_reward_checking(message, severity) elif output_type == "stdout": cls.log_to_screen(message, severity) @@ -50,6 +52,20 @@ def log_to_slack_new_users(message, severity): elif severity == "critical": logger.critical(message) + @staticmethod + def log_to_slack_reward_checking(message, severity): + logger = logging.getLogger('slack_reward_checking_service') + if severity == "debug": + logger.debug(message) + elif severity == "info": + logger.info(message) + elif severity == "warning": + logger.warning(message) + elif severity == "error": + logger.error(message) + elif severity == "critical": + logger.critical(message) + # Log to Screen @staticmethod def log_to_screen(message, severity): diff --git a/locker_server/shared/log/handlers.py b/locker_server/shared/log/handlers.py index 11c25143..6c84f69b 100644 --- a/locker_server/shared/log/handlers.py +++ b/locker_server/shared/log/handlers.py @@ -36,3 +36,17 @@ def emit(self, record): ) except: print("Slack handler failed") + + +class SlackRewardCheckingHandler(logging.Handler): + def emit(self, record): + try: + record.exc_info = record.exc_text = None + content = {'text': self.format(record)} + requests.post( + url=settings.SLACK_WEBHOOK_REWARD_CHECKING, + json={"text": "```{}```".format(content['text'])}, + timeout=10 + ) + except Exception: + print("SlackRewardCheckingHandler failed") diff --git a/server_config/settings/dev.py b/server_config/settings/dev.py index 74af1f53..fbdd9da2 100644 --- a/server_config/settings/dev.py +++ b/server_config/settings/dev.py @@ -248,6 +248,7 @@ SLACK_WEBHOOK_API_LOG = os.getenv("SLACK_WEBHOOK_API_LOG") SLACK_WEBHOOK_CRON_LOG = os.getenv("SLACK_WEBHOOK_CRON_LOG") SLACK_WEBHOOK_NEW_USERS = os.getenv("SLACK_WEBHOOK_NEW_USERS") + SLACK_WEBHOOK_REWARD_CHECKING = os.getenv("SLACK_WEBHOOK_REWARD_CHECKING") # Google sheet GOOGLE_SHEET_FEEDBACK_URL = os.getenv("GOOGLE_SHEET_FEEDBACK_URL")