diff --git a/discord_analyzer/analysis/compute_member_activity.py b/discord_analyzer/analysis/compute_member_activity.py index 70d55d3..bf8fac0 100644 --- a/discord_analyzer/analysis/compute_member_activity.py +++ b/discord_analyzer/analysis/compute_member_activity.py @@ -264,6 +264,7 @@ def compute_member_activity( # updating account names for past 7 days acc_names = get_users_past_window( window_start_date=window_start.strftime("%Y-%m-%d"), + window_end_date=last_date.strftime("%Y-%m-%d"), collection=db_access.db_mongo_client[db_name]["heatmaps"], ) diff --git a/discord_analyzer/analysis/utils/member_activity_utils.py b/discord_analyzer/analysis/utils/member_activity_utils.py index 57f0014..97ae499 100644 --- a/discord_analyzer/analysis/utils/member_activity_utils.py +++ b/discord_analyzer/analysis/utils/member_activity_utils.py @@ -175,7 +175,9 @@ def convert_to_dict(data: list[Any], dict_keys: list[str]) -> dict[str, dict]: def get_users_past_window( - window_start_date: str, collection: pymongo.collection.Collection + window_start_date: str, + window_end_date: str, + collection: pymongo.collection.Collection, ) -> list[str]: """ get all users in the past date window from specific collection @@ -183,8 +185,11 @@ def get_users_past_window( Parameters: ------------ window_start_date : str - the starting point of the window until today + the starting point of the window must be in format of the database which for now is %Y-%m-%d + window_end_date : str + the ending point of the window + must be in format of the database which for now is %Y-%m-%d collection : pymongo.collection.Collection the mongodb collection to do the aggregation @@ -195,7 +200,7 @@ def get_users_past_window( """ pipeline = [ # Filter documents based on date - {"$match": {"date": {"$gte": window_start_date}}}, + {"$match": {"date": {"$gte": window_start_date, "$lte": window_end_date}}}, {"$group": {"_id": "$account_name"}}, { "$group": { diff --git a/tests/integration/test_get_past_7_days_heatmaps.py b/tests/integration/test_get_past_7_days_heatmaps.py index 4fe5d2c..2e35b46 100644 --- a/tests/integration/test_get_past_7_days_heatmaps.py +++ b/tests/integration/test_get_past_7_days_heatmaps.py @@ -50,15 +50,128 @@ def test_get_past_7_days_heatmap_users_available_users(): start_date = datetime(2023, 1, 1) + timedelta(days=243) user_names = get_users_past_window( - start_date.strftime("%Y-%m-%d"), db_access.db_mongo_client[guildId]["heatmaps"] + start_date.strftime("%Y-%m-%d"), + (start_date + timedelta(days=250)).strftime("%Y-%m-%d"), + db_access.db_mongo_client[guildId]["heatmaps"], ) - print(set(user_names)) - print(set(acc_names[-6:])) - assert set(user_names) == set(acc_names[-7:]) +def test_get_all_days_heatmap_users_available_users(): + """ + test if we're getting the right heatmap users + """ + # first create the collections + guildId = "1234" + db_access = launch_db_access(guildId) + + start_date = datetime(2023, 1, 1) + + db_access.db_mongo_client[guildId].drop_collection("heatmaps") + + db_access.db_mongo_client[guildId].create_collection("heatmaps") + + heatmaps_data = [] + acc_names = [] + for i in range(250): + date = start_date + timedelta(days=i) + account = f"9739932992810762{i}" + document = { + "date": date.strftime("%Y-%m-%d"), + "channelId": "1020707129214111827", + "thr_messages": list(np.zeros(24)), + "lone_messages": list(np.zeros(24)), + "replier": list(np.zeros(24)), + "replied": list(np.zeros(24)), + "mentioner": list(np.zeros(24)), + "mentioned": list(np.zeros(24)), + "reacter": list(np.zeros(24)), + "reacted": list(np.zeros(24)), + "reacted_per_acc": [], + "mentioner_per_acc": [], + "replied_per_acc": [], + "account_name": account, + } + + heatmaps_data.append(document) + acc_names.append(account) + + db_access.db_mongo_client[guildId]["heatmaps"].insert_many(heatmaps_data) + + user_names = get_users_past_window( + window_start_date=datetime(2023, 1, 1).strftime("%Y-%m-%d"), + window_end_date=(start_date + timedelta(days=250)).strftime("%Y-%m-%d"), + collection=db_access.db_mongo_client[guildId]["heatmaps"], + ) + + assert set(user_names) == set(acc_names) + + +def test_get_just_7_days_heatmap_users_available_users(): + """ + test if we're getting the right heatmap users + """ + # first create the collections + guildId = "1234" + db_access = launch_db_access(guildId) + + start_date = datetime(2023, 1, 1) + + db_access.db_mongo_client[guildId].drop_collection("heatmaps") + + db_access.db_mongo_client[guildId].create_collection("heatmaps") + + heatmaps_data = [] + acc_names = [] + for i in range(250): + date = start_date + timedelta(days=i) + account = f"9739932992810762{i}" + document = { + "date": date.strftime("%Y-%m-%d"), + "channelId": "1020707129214111827", + "thr_messages": list(np.zeros(24)), + "lone_messages": list(np.zeros(24)), + "replier": list(np.zeros(24)), + "replied": list(np.zeros(24)), + "mentioner": list(np.zeros(24)), + "mentioned": list(np.zeros(24)), + "reacter": list(np.zeros(24)), + "reacted": list(np.zeros(24)), + "reacted_per_acc": [], + "mentioner_per_acc": [], + "replied_per_acc": [], + "account_name": account, + } + + heatmaps_data.append(document) + acc_names.append(account) + + db_access.db_mongo_client[guildId]["heatmaps"].insert_many(heatmaps_data) + + start_date = datetime(2023, 1, 1) + end_date = start_date + timedelta(days=7) + + user_names = get_users_past_window( + start_date.strftime("%Y-%m-%d"), + end_date.strftime("%Y-%m-%d"), + db_access.db_mongo_client[guildId]["heatmaps"], + ) + + assert set(user_names) == set( + [ + "97399329928107620", + "97399329928107621", + "97399329928107622", + "97399329928107623", + "97399329928107624", + "97399329928107625", + "97399329928107626", + "97399329928107627", + ] + ) + + def test_get_past_7_days_heatmap_users_no_users(): """ test if we're getting the right heatmap users @@ -73,10 +186,13 @@ def test_get_past_7_days_heatmap_users_no_users(): db_access.db_mongo_client[guildId].create_collection("heatmaps") - start_date = datetime(2023, 1, 1) + timedelta(days=243) + start_date = datetime(2023, 1, 1) + end_date = start_date + timedelta(days=243) user_names = get_users_past_window( - start_date.strftime("%Y-%m-%d"), db_access.db_mongo_client[guildId]["heatmaps"] + window_start_date=start_date.strftime("%Y-%m-%d"), + window_end_date=end_date.strftime("%Y-%m-%d"), + collection=db_access.db_mongo_client[guildId]["heatmaps"], ) assert user_names == []