diff --git a/handlers/issue_comment_handler.py b/handlers/issue_comment_handler.py index 2239a78..488ce7c 100644 --- a/handlers/issue_comment_handler.py +++ b/handlers/issue_comment_handler.py @@ -2,6 +2,7 @@ from datetime import datetime from utils.logging_file import logger import logging +from utils.user_activity import UserActivity class Issue_commentHandler(EventHandler): @@ -16,6 +17,7 @@ async def handle_event(self, data, postgres_client): handler_method = getattr(self, f'handle_issue_comment_{module_name}', None) if handler_method: await handler_method(data, postgres_client) + await UserActivity.log_user_activity(data, 'comment') else: logging.info(f"No handler found for module: {module_name}") diff --git a/handlers/issues_handler.py b/handlers/issues_handler.py index e90ac11..93b89c0 100644 --- a/handlers/issues_handler.py +++ b/handlers/issues_handler.py @@ -3,6 +3,7 @@ from handlers.EventHandler import EventHandler from events.ticketEventHandler import TicketEventHandler from utils.db import PostgresORM +from utils.user_activity import UserActivity class IssuesHandler(EventHandler): async def handle_event(self, data, postgres_client): @@ -17,14 +18,16 @@ async def handle_event(self, data, postgres_client): handler_method = getattr(self, f'handle_issue_{module_name}', None) if handler_method: await handler_method(data) - await self.log_user_activity(data) + # await self.log_user_activity(data) + await UserActivity.log_user_activity(data, 'issue') else: logging.info(f"No handler found for module: {module_name}") elif module_name == 'unlabeled': handler_method = getattr(self, f'handle_issue_{module_name}', None) if handler_method: await handler_method(data) - await self.log_user_activity(data) + # await self.log_user_activity(data) + await UserActivity.log_user_activity(data, 'issue') return 'success' @@ -177,16 +180,16 @@ async def log_user_activity(self, data): user_id = data['issue']['user']['id'] - contributor = await self.postgres_client.get_data('github_id', 'contributors_registration', user_id, '*') - contributor_id = contributor["id"] - + contributor = await postgres_client.get_data('github_id', 'contributors_registration', user_id, '*') + contributor_id = contributor[0]["id"] + mentor = await postgres_client.get_data('issue_id', 'issue_mentors',issue[0]["id"]) activity_data = { - "issue_id": issue["id"], + "issue_id": issue[0]["id"], "activity": f"issue_{data['action']}", - "created_at": issue['created_at'], - "updated_at": issue['updated_at'], + "created_at": issue[0]['created_at'], + "updated_at": issue[0]['updated_at'], "contributor_id": contributor_id, - "mentor_id": "" + "mentor_id": mentor[0]["angel_mentor_id"] if mentor else None } saved_activity_data = await postgres_client.add_data(activity_data,"user_activity") return saved_activity_data diff --git a/handlers/pull_request_handler.py b/handlers/pull_request_handler.py index efd4913..a33959b 100644 --- a/handlers/pull_request_handler.py +++ b/handlers/pull_request_handler.py @@ -1,6 +1,7 @@ import logging from handlers.EventHandler import EventHandler from datetime import datetime +from utils.user_activity import UserActivity class Pull_requestHandler(EventHandler): @@ -59,15 +60,7 @@ async def handle_event(self, data, postgres_client): issue = await postgres_client.get_data('link', 'issues', issue_url, '*') #save activity to user_activity - activity_data = { - "issue_id": issue["id"], - "activity": f"pull_request_{data['action']}", - "created_at": data['pull_request']['created_at'], - "updated_at": data['pull_request']['updated_at'], - "contributor_id": contributor_id, - "mentor_id": "" - } - saved_activity_data = await postgres_client.add_data(activity_data,"user_activity") + await UserActivity.log_user_activity(data, 'pull_request') except Exception as e: print('exception in pr ', e) logging.info(e) diff --git a/models/models.py b/models/models.py index 6fc37b3..506b205 100644 --- a/models/models.py +++ b/models/models.py @@ -1311,7 +1311,7 @@ def to_dict(self): class UserActivity(Base): __tablename__ = 'user_activity' - id = Column(UUID(as_uuid=True), primary_key=True) + id = Column(BigInteger, primary_key=True, autoincrement=True) contributor_id = Column(BigInteger, ForeignKey('contributors_registration.id'), nullable=False) # Assumes 'ContributorsRegistration' model issue_id = Column(BigInteger, ForeignKey('issues.id'), nullable=False) # Assumes 'Issues' model activity = Column(Text, nullable=True) diff --git a/utils/user_activity.py b/utils/user_activity.py new file mode 100644 index 0000000..bdc5907 --- /dev/null +++ b/utils/user_activity.py @@ -0,0 +1,30 @@ +import logging +from utils.db import PostgresORM + +class UserActivity: + async def log_user_activity(data, activity): + try: + postgres_client = PostgresORM.get_instance() + issue = data["issue"] + print('inside user activity', issue) + issue = await postgres_client.get_data('issue_id', 'issues', issue["id"]) + + user_id = data['issue']['user']['id'] + + contributor = await postgres_client.get_data('github_id', 'contributors_registration', user_id, '*') + contributor_id = contributor[0]["id"] + mentor = await postgres_client.get_data('issue_id', 'issue_mentors',issue[0]["id"]) + activity_data = { + "issue_id": issue[0]["id"], + "activity": f"{activity}_{data['action']}", + "created_at": issue[0]['created_at'], + "updated_at": issue[0]['updated_at'], + "contributor_id": contributor_id, + "mentor_id": mentor[0]["angel_mentor_id"] if mentor else None + } + saved_activity_data = await postgres_client.add_data(activity_data,"user_activity") + return saved_activity_data + + except Exception as e: + logging.info(e) + raise Exception \ No newline at end of file