From 66137bb1d5bb528d2c9a487b6495df46d466134a Mon Sep 17 00:00:00 2001 From: ramazanoacar Date: Sat, 12 Oct 2024 18:48:24 +0300 Subject: [PATCH 1/6] init logic for updating repositories --- github_tracker_bot/mongo_data_handler.py | 4 ++++ leader_bot/sheet_functions.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/github_tracker_bot/mongo_data_handler.py b/github_tracker_bot/mongo_data_handler.py index 300fb75..67c40ca 100644 --- a/github_tracker_bot/mongo_data_handler.py +++ b/github_tracker_bot/mongo_data_handler.py @@ -329,6 +329,10 @@ def update_all_contribution_datas_from_ai_decisions( user.qualified_daily_contribution_dates ) + repositories = user.repositories.copy() + repositories.extend([decision.repository for decision in ai_decisions[0]]) + user.repositories = list(set(repositories)) + updated_user = self.update_user(user_handle, user) return updated_user diff --git a/leader_bot/sheet_functions.py b/leader_bot/sheet_functions.py index b66a960..0e016ff 100644 --- a/leader_bot/sheet_functions.py +++ b/leader_bot/sheet_functions.py @@ -509,3 +509,17 @@ def write_all_data_of_user_to_csv_by_month(file_path: str, username: str, date: except Exception as e: logger.error(f"Failed to write to CSV: {e}") return f"Failed to write to CSV: {e}" + + +def get_repositories_from_user(username: str): + data = read_sheet(config.SPREADSHEET_ID) + if not data: + logger.error("No data found in the spreadsheet.") + return + + for row in data: + if row[0] == username: + return row[2].split(", ") + + logger.error(f"User with Discord handle {username} not found.") + return None From d7e82537e6bd907098b2a9b4354f966641134390 Mon Sep 17 00:00:00 2001 From: ramazanoacar Date: Sat, 12 Oct 2024 21:20:40 +0300 Subject: [PATCH 2/6] delete unused function --- leader_bot/sheet_functions.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/leader_bot/sheet_functions.py b/leader_bot/sheet_functions.py index 0e016ff..b66a960 100644 --- a/leader_bot/sheet_functions.py +++ b/leader_bot/sheet_functions.py @@ -509,17 +509,3 @@ def write_all_data_of_user_to_csv_by_month(file_path: str, username: str, date: except Exception as e: logger.error(f"Failed to write to CSV: {e}") return f"Failed to write to CSV: {e}" - - -def get_repositories_from_user(username: str): - data = read_sheet(config.SPREADSHEET_ID) - if not data: - logger.error("No data found in the spreadsheet.") - return - - for row in data: - if row[0] == username: - return row[2].split(", ") - - logger.error(f"User with Discord handle {username} not found.") - return None From e61fce6587ff90422a575ffdae057fb76ae9681d Mon Sep 17 00:00:00 2001 From: ramazanoacar Date: Sat, 12 Oct 2024 21:24:24 +0300 Subject: [PATCH 3/6] use the previously deleted unused function for gathering data from sheets --- github_tracker_bot/mongo_data_handler.py | 4 ++-- leader_bot/sheet_functions.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/github_tracker_bot/mongo_data_handler.py b/github_tracker_bot/mongo_data_handler.py index 67c40ca..f19b46b 100644 --- a/github_tracker_bot/mongo_data_handler.py +++ b/github_tracker_bot/mongo_data_handler.py @@ -2,7 +2,7 @@ import os import copy import config - +from leader_bot.sheet_functions import get_repositories_from_user from datetime import datetime from dataclasses import dataclass, field, asdict from typing import List, Dict, Any, Optional, Union @@ -330,7 +330,7 @@ def update_all_contribution_datas_from_ai_decisions( ) repositories = user.repositories.copy() - repositories.extend([decision.repository for decision in ai_decisions[0]]) + repositories.extend(get_repositories_from_user(user_handle)) user.repositories = list(set(repositories)) updated_user = self.update_user(user_handle, user) diff --git a/leader_bot/sheet_functions.py b/leader_bot/sheet_functions.py index b66a960..0e016ff 100644 --- a/leader_bot/sheet_functions.py +++ b/leader_bot/sheet_functions.py @@ -509,3 +509,17 @@ def write_all_data_of_user_to_csv_by_month(file_path: str, username: str, date: except Exception as e: logger.error(f"Failed to write to CSV: {e}") return f"Failed to write to CSV: {e}" + + +def get_repositories_from_user(username: str): + data = read_sheet(config.SPREADSHEET_ID) + if not data: + logger.error("No data found in the spreadsheet.") + return + + for row in data: + if row[0] == username: + return row[2].split(", ") + + logger.error(f"User with Discord handle {username} not found.") + return None From 46d49b6f080550669098905c887114d1619ba3b0 Mon Sep 17 00:00:00 2001 From: ramazanoacar Date: Tue, 15 Oct 2024 12:08:32 +0300 Subject: [PATCH 4/6] bypass circular import --- github_tracker_bot/mongo_data_handler.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/github_tracker_bot/mongo_data_handler.py b/github_tracker_bot/mongo_data_handler.py index f19b46b..0bff24a 100644 --- a/github_tracker_bot/mongo_data_handler.py +++ b/github_tracker_bot/mongo_data_handler.py @@ -2,7 +2,6 @@ import os import copy import config -from leader_bot.sheet_functions import get_repositories_from_user from datetime import datetime from dataclasses import dataclass, field, asdict from typing import List, Dict, Any, Optional, Union @@ -303,6 +302,8 @@ def update_all_contribution_datas_from_ai_decisions( self, user_handle ) -> Optional[User]: """Updates all contribution data by calculating ai decisions with helper functions""" + from leader_bot.sheet_functions import get_repositories_from_user + try: user = self.get_user(user_handle) ai_decisions = user.ai_decisions From 5443abe2630e670057fe5f157002b54c81513423 Mon Sep 17 00:00:00 2001 From: ramazanoacar Date: Tue, 15 Oct 2024 13:07:08 +0300 Subject: [PATCH 5/6] fix import for db_functions --- leader_bot/sheet_functions.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/leader_bot/sheet_functions.py b/leader_bot/sheet_functions.py index 0e016ff..8304410 100644 --- a/leader_bot/sheet_functions.py +++ b/leader_bot/sheet_functions.py @@ -7,7 +7,6 @@ from log_config import get_logger from google.oauth2.service_account import Credentials from googleapiclient.discovery import build -from db_functions import fetch_db_get_users, get_ai_decisions_by_user_and_timeframe from github_tracker_bot.mongo_data_handler import AIDecision from helpers import get_monthly_user_data_from_ai_decisions, get_since_until_y_m_d from config import GOOGLE_CREDENTIALS @@ -129,6 +128,8 @@ def create_leaderboard_sheet( def fill_created_spreadsheet_with_users_except_ai_decisions(spreadsheed_id): + from db_functions import fetch_db_get_users + try: column_names = [ [ @@ -169,6 +170,8 @@ def fill_created_spreadsheet_with_users_except_ai_decisions(spreadsheed_id): def write_users_to_csv(file_path): + from db_functions import fetch_db_get_users + try: column_names = [ "User Handle", @@ -210,6 +213,8 @@ def write_users_to_csv(file_path): def write_users_to_csv_monthly(file_path, month): + from db_functions import fetch_db_get_users + try: users = fetch_db_get_users() filtered_users = [] @@ -274,6 +279,8 @@ def write_ai_decisions_to_csv( def update_created_spreadsheet_with_users_except_ai_decisions(spreadsheed_id): + from db_functions import fetch_db_get_users + try: users = fetch_db_get_users() data = [] @@ -479,6 +486,8 @@ def delete_user(discord_handle: str): def write_all_data_of_user_to_csv_by_month(file_path: str, username: str, date: str): + from db_functions import get_ai_decisions_by_user_and_timeframe + try: since, until = get_since_until_y_m_d(date) ai_decisions = get_ai_decisions_by_user_and_timeframe(username, since, until) From 237a052787bab6d765df9fcba8dc567768e8c2b7 Mon Sep 17 00:00:00 2001 From: ramazanoacar Date: Tue, 15 Oct 2024 13:37:31 +0300 Subject: [PATCH 6/6] fix empty ai decisions --- github_tracker_bot/mongo_data_handler.py | 40 ++++++++++++++---------- leader_bot/sheet_functions.py | 2 +- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/github_tracker_bot/mongo_data_handler.py b/github_tracker_bot/mongo_data_handler.py index 0bff24a..2f80bfc 100644 --- a/github_tracker_bot/mongo_data_handler.py +++ b/github_tracker_bot/mongo_data_handler.py @@ -554,20 +554,26 @@ def set_qualified_daily_contribution_streak( raise def update_ai_decisions(self, user: User, new_decisions: List[AIDecision]) -> None: - for new_decision in new_decisions: - for user_ai_decision in user.ai_decisions[0]: - if ( - user_ai_decision.repository == new_decision.repository - and user_ai_decision.date == new_decision.date - ): - user_ai_decision.response = new_decision.response - for commit in new_decision.commit_hashes: - if commit not in user_ai_decision.commit_hashes: - user_commit_hashes = user_ai_decision.commit_hashes - if type(user_commit_hashes) != list: - user_commit_hashes = user_commit_hashes.split(",") - user_commit_hashes.extend(commit) - user_ai_decision.commit_hashes = user_commit_hashes - break - else: - user.ai_decisions[0].extend([new_decision]) + logger.info(f"Updating AI decisions for user {user.user_handle}") + logger.info(f"New decisions: {new_decisions}") + logger.info(f"Old decisions: {user.ai_decisions}") + if len(user.ai_decisions) == 0: + user.ai_decisions = [new_decisions] + else: + for new_decision in new_decisions: + for user_ai_decision in user.ai_decisions[0]: + if ( + user_ai_decision.repository == new_decision.repository + and user_ai_decision.date == new_decision.date + ): + user_ai_decision.response = new_decision.response + for commit in new_decision.commit_hashes: + if commit not in user_ai_decision.commit_hashes: + user_commit_hashes = user_ai_decision.commit_hashes + if type(user_commit_hashes) != list: + user_commit_hashes = user_commit_hashes.split(",") + user_commit_hashes.extend(commit) + user_ai_decision.commit_hashes = user_commit_hashes + break + else: + user.ai_decisions[0].extend([new_decision]) diff --git a/leader_bot/sheet_functions.py b/leader_bot/sheet_functions.py index 8304410..5148909 100644 --- a/leader_bot/sheet_functions.py +++ b/leader_bot/sheet_functions.py @@ -8,7 +8,6 @@ from google.oauth2.service_account import Credentials from googleapiclient.discovery import build from github_tracker_bot.mongo_data_handler import AIDecision -from helpers import get_monthly_user_data_from_ai_decisions, get_since_until_y_m_d from config import GOOGLE_CREDENTIALS sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) @@ -487,6 +486,7 @@ def delete_user(discord_handle: str): def write_all_data_of_user_to_csv_by_month(file_path: str, username: str, date: str): from db_functions import get_ai_decisions_by_user_and_timeframe + from helpers import get_monthly_user_data_from_ai_decisions, get_since_until_y_m_d try: since, until = get_since_until_y_m_d(date)