Skip to content

Commit

Permalink
Merge pull request #61 from TogetherCrew/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
cyri113 authored Jan 22, 2024
2 parents ce8c038 + f8e00de commit 35819a1
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 9 deletions.
1 change: 1 addition & 0 deletions discord_analyzer/analysis/compute_member_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)

Expand Down
11 changes: 8 additions & 3 deletions discord_analyzer/analysis/utils/member_activity_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,21 @@ 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
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
Expand All @@ -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": {
Expand Down
128 changes: 122 additions & 6 deletions tests/integration/test_get_past_7_days_heatmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 == []

0 comments on commit 35819a1

Please sign in to comment.