Skip to content

Commit

Permalink
user activity bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jaanbaaz committed Nov 7, 2024
1 parent 86a973a commit 4c9ea48
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
2 changes: 2 additions & 0 deletions handlers/issue_comment_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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}")

Expand Down
21 changes: 12 additions & 9 deletions handlers/issues_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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'

Expand Down Expand Up @@ -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
Expand Down
11 changes: 2 additions & 9 deletions handlers/pull_request_handler.py
Original file line number Diff line number Diff line change
@@ -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):

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions utils/user_activity.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4c9ea48

Please sign in to comment.