Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #41

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions discord_analyzer/analyzer/analyzer_memberactivities.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ def analysis_member_activity(self, guildId, mongo_connection_str, from_start=Fal
) * window[0]
date_range[0] = date_range[1] - timedelta(days=num_days_to_load)

if date_range[0] < period:
date_range[0] = period + timedelta(days=window[0])
# if the date range goes back more than the "7 days `period` forward"
if date_range[0] < period + timedelta(days=window[0]):
date_range[0] = period + timedelta(days=window[0])

# get all users during date_range
all_users = self.utils.get_all_users(guildId)
Expand Down
108 changes: 108 additions & 0 deletions tests/integration/test_analyzer_period_35days_run_once.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# test analyzing memberactivities
from datetime import datetime, timedelta

import numpy as np

from .utils.analyzer_setup import launch_db_access, setup_analyzer
from .utils.mock_heatmaps import create_empty_heatmaps_data
from .utils.mock_memberactivities import create_empty_memberactivities_data
from .utils.remove_and_setup_guild import setup_db_guild


def test_analyzer_40days_period_run_once_available_analytics_overlapping_period():
"""
test the whole analyzer pipeline for a guild with a 40 days period
and use run_once method with empty analytics available
This test is utilized for the use case of overlapping period and 40 days ago
"""
# first create the collections
guildId = "1234"
db_access = launch_db_access(guildId)

acc_id = [
"user1",
"user2",
]

setup_db_guild(db_access, guildId, discordId_list=acc_id, days_ago_period=40)

db_access.db_mongo_client[guildId].drop_collection("heatmaps")
db_access.db_mongo_client[guildId].drop_collection("memberactivities")

db_access.db_mongo_client[guildId].create_collection("heatmaps")
db_access.db_mongo_client[guildId].create_collection("memberactivities")

# filling memberactivities with some data
# filling heatmaps with some data
# filling up to 4 days ago with 83 documents
memberactivity_data = create_empty_memberactivities_data(
datetime.now() - timedelta(days=33), count=29
)
db_access.db_mongo_client[guildId]["memberactivities"].insert_many(
memberactivity_data
)

# filling heatmaps with some data
# filling up to 4 days ago with 89 documents
# just yesterday is left to be analyzed
heatmaps_data = create_empty_heatmaps_data(
datetime.now() - timedelta(days=40), count=36
)
db_access.db_mongo_client[guildId]["heatmaps"].insert_many(heatmaps_data)

# generating rawinfo samples
rawinfo_samples = []

# generating random rawinfo data
# 24 hours, 40 days
for i in range(24 * 40):
sample = {
"type": 19,
"author": np.random.choice(acc_id),
"content": f"test{i}",
"user_mentions": [],
"role_mentions": [],
"reactions": [],
"replied_user": np.random.choice(acc_id),
"createdDate": (datetime.now() - timedelta(hours=i)),
"messageId": f"11188143219343360{i}",
"channelId": "1020707129214111827",
"channelName": "general",
"threadId": None,
"threadName": None,
"isGeneratedByWebhook": False,
}
rawinfo_samples.append(sample)

db_access.db_mongo_client[guildId]["rawinfos"].insert_many(rawinfo_samples)

analyzer = setup_analyzer()
analyzer.run_once(guildId=guildId)

memberactivities_cursor = db_access.query_db_find(
"memberactivities", {}, sorting=("date", -1)
)
memberactivities_data = list(memberactivities_cursor)

yesterday = (datetime.now() - timedelta(days=1)).replace(
hour=0, minute=0, second=0, microsecond=0
)

assert len(memberactivities_data) == 33
assert memberactivities_data[0]["date"] == yesterday.isoformat()
# yesterday is `-1` day and so
# we would use 32 days ago rather than 40
document_start_date = yesterday - timedelta(days=32)
assert memberactivities_data[-1]["date"] == (document_start_date).isoformat()

heatmaps_cursor = db_access.query_db_find("heatmaps", {}, sorting=("date", -1))
heatmaps_data = list(heatmaps_cursor)

# 39 days of 1 document plus the last day as 2 documents
# as we have 2 accounts
# (accounts are: "user1", "user2")
assert len(heatmaps_data) == 44
# checking first and last document
assert heatmaps_data[0]["date"] == yesterday.strftime("%Y-%m-%d")
month_ago = yesterday - timedelta(39)
assert heatmaps_data[-1]["date"] == month_ago.strftime("%Y-%m-%d")