Skip to content

Commit

Permalink
Merge pull request #19 from TogetherCrew/feat/4-analytics-batch-save
Browse files Browse the repository at this point in the history
feat: updated heatmap analytics to be generator!
  • Loading branch information
amindadgar authored Aug 26, 2024
2 parents 3595ec4 + a613cc3 commit 34cda0e
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 107 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name="tc-analyzer-lib",
version="1.4.3",
version="1.4.4",
author="Mohammad Amin Dadgar, TogetherCrew",
maintainer="Mohammad Amin Dadgar",
maintainer_email="dadgaramin96@gmail.com",
Expand Down
19 changes: 16 additions & 3 deletions tc_analyzer_lib/metrics/heatmaps/heatmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ def __init__(
self.analyzer_config = analyzer_config
self.utils = HeatmapsUtils(platform_id)

async def start(self, from_start: bool = False) -> list[dict]:
async def start(
self,
from_start: bool = False,
batch_return: int = 5,
):
"""
Based on the rawdata creates and stores the heatmap data
Expand Down Expand Up @@ -75,13 +79,14 @@ async def start(self, from_start: bool = False) -> list[dict]:
async for bot in cursor:
bot_ids.append(bot["id"])

index = 0
while analytics_date.date() < datetime.now().date():
start_day = analytics_date.replace(
hour=0, minute=0, second=0, microsecond=0
)
end_day = start_day + timedelta(days=1)
logging.info(
f"{log_prefix} ANALYZING HEATMAPS {start_day.date()} - {end_day.date()}!"
f"{log_prefix} ANALYZING HEATMAPS {start_day.date()} - {end_day.date()}! | index: {index}"
)

# getting the active resource_ids (activities being done there by users)
Expand Down Expand Up @@ -150,10 +155,18 @@ async def start(self, from_start: bool = False) -> list[dict]:

heatmaps_results.extend(day_results)

if index % batch_return == 0:
yield heatmaps_results
# emptying it
heatmaps_results = []

index += 1

# analyze next day
analytics_date += timedelta(days=1)

return heatmaps_results
# returning any other values
yield heatmaps_results

async def _prepare_heatmaps_document(
self, date: datetime, resource_id: str, author_id: str
Expand Down
48 changes: 30 additions & 18 deletions tc_analyzer_lib/tc_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,19 @@ async def run_once(self):
resources=self.resources,
analyzer_config=self.analyzer_config,
)
heatmaps_data = await heatmaps_analysis.start(from_start=False)

# storing heatmaps since memberactivities use them
analytics_data = {}
analytics_data["heatmaps"] = heatmaps_data
analytics_data["memberactivities"] = (None, None)

self.DB_connections.store_analytics_data(
analytics_data=analytics_data,
platform_id=self.platform_id,
graph_schema=self.graph_schema,
remove_memberactivities=False,
remove_heatmaps=False,
)
async for heatmaps_data in heatmaps_analysis.start(from_start=False):
# storing heatmaps since memberactivities use them
analytics_data = {}
analytics_data["heatmaps"] = heatmaps_data
analytics_data["memberactivities"] = (None, None)

self.DB_connections.store_analytics_data(
analytics_data=analytics_data,
platform_id=self.platform_id,
graph_schema=self.graph_schema,
remove_memberactivities=False,
remove_heatmaps=False,
)

memberactivity_analysis = MemberActivities(
platform_id=self.platform_id,
Expand Down Expand Up @@ -149,13 +148,12 @@ async def recompute(self):
resources=self.resources,
analyzer_config=self.analyzer_config,
)
heatmaps_data = await heatmaps_analysis.start(from_start=True)

# storing heatmaps since memberactivities use them
# This is to remove heatmaps data
# TODO: in future remove heatmaps better instead of using the lines below
analytics_data = {}
analytics_data["heatmaps"] = heatmaps_data
analytics_data["heatmaps"] = []
analytics_data["memberactivities"] = (None, None)

self.DB_connections.store_analytics_data(
analytics_data=analytics_data,
platform_id=self.platform_id,
Expand All @@ -164,6 +162,20 @@ async def recompute(self):
remove_heatmaps=True,
)

async for heatmaps_data in heatmaps_analysis.start(from_start=True):
# storing heatmaps since memberactivities use them
analytics_data = {}
analytics_data["heatmaps"] = heatmaps_data
analytics_data["memberactivities"] = (None, None)

self.DB_connections.store_analytics_data(
analytics_data=analytics_data,
platform_id=self.platform_id,
graph_schema=self.graph_schema,
remove_memberactivities=False,
remove_heatmaps=False,
)

# run the member_activity analyze
logging.info(
f"Analyzing the MemberActivities data for platform: {self.platform_id}!"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ async def test_analyzer_week_period_run_once_empty_analytics(self):
rawinfo_samples = []

# generating random rawinfo data
for i in range(160):
# for past 7 days (168 / 24 = 7)
for i in range(168):
author = np.random.choice(acc_id)
replied_user = np.random.choice(acc_id)
# not producing any self-interactions
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/test_assess_engagement_mention.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ async def heatmaps_analytics(self):
"""
heatmaps are the input for assess_engagement's interaction matrix
"""
heatmaps_data = await self.heatmaps.start(from_start=True)
heatmaps_data = []
async for results in self.heatmaps.start(from_start=True):
heatmaps_data.extend(results)

analytics_data = {}
analytics_data["heatmaps"] = heatmaps_data
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/test_assess_engagement_reactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ async def heatmaps_analytics(self):
"""
heatmaps are the input for assess_engagement's interaction matrix
"""
heatmaps_data = await self.heatmaps.start(from_start=True)
heatmaps_data = []
async for results in self.heatmaps.start(from_start=True):
heatmaps_data.extend(results)

analytics_data = {}
analytics_data["heatmaps"] = heatmaps_data
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/test_assess_engagement_replies.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ async def heatmaps_analytics(self):
"""
heatmaps are the input for assess_engagement's interaction matrix
"""
heatmaps_data = await self.heatmaps.start(from_start=True)
heatmaps_data = []
async for results in self.heatmaps.start(from_start=True):
heatmaps_data.extend(results)

analytics_data = {}
analytics_data["heatmaps"] = heatmaps_data
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/test_heatmaps_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ async def test_heatmaps_single_day_from_start(self):
sample_raw_data
)

analytics = await self.heatmaps.start(from_start=True)
analytics = []
async for heatmaps_data in self.heatmaps.start(from_start=True):
analytics.extend(heatmaps_data)

self.assertIsInstance(analytics, list)

Expand Down
9 changes: 7 additions & 2 deletions tests/integration/test_heatmaps_analytics_different_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ async def test_heatmaps_single_day_from_start(self):
sample_raw_data
)

analytics = await self.heatmaps.start(from_start=True)
analytics = []
async for heatmaps_data in self.heatmaps.start(from_start=True):
analytics.extend(heatmaps_data)

self.assertIsInstance(analytics, list)

Expand Down Expand Up @@ -181,7 +183,10 @@ async def test_heatmaps_analytics_pre_filled(self):
}
)

analytics = await self.heatmaps.start(from_start=False)
analytics = []
async for heatmaps_data in self.heatmaps.start(from_start=False):
analytics.extend(heatmaps_data)

# the day was pre-filled before
# and the period was exactly yesterday
self.assertEqual(analytics, [])
4 changes: 3 additions & 1 deletion tests/integration/test_heatmaps_hourly_lone_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ async def test_lone_messages(self):
resources=list(channelIds),
analyzer_config=DiscordAnalyzerConfig(),
)
results = await analyzer_heatmaps.start(from_start=True)
results = []
async for heatmaps_data in analyzer_heatmaps.start(from_start=True):
results.extend(heatmaps_data)

assert len(results) == len(acc_names) * DAY_COUNT * len(channelIds)
for document in results:
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/test_heatmaps_hourly_mentions.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ async def test_mentioned_messages(self):
resources=list(channelIds),
analyzer_config=DiscordAnalyzerConfig(),
)
results = await analyzer_heatmaps.start(from_start=True)
results = []
async for heatmaps_data in analyzer_heatmaps.start(from_start=True):
results.extend(heatmaps_data)

assert len(results) == len(acc_names) * DAY_COUNT * len(channelIds)
for document in results:
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/test_heatmaps_reactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ async def test_reacted_messages(self):
resources=list(channelIds),
analyzer_config=DiscordAnalyzerConfig(),
)
results = await analyzer_heatmaps.start(from_start=True)
results = []
async for heatmaps_data in analyzer_heatmaps.start(from_start=True):
results.extend(heatmaps_data)

assert len(results) == len(acc_names) * DAY_COUNT * len(channelIds)
for document in results:
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/test_heatmaps_replier.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ async def test_reply_messages(self):
resources=list(channelIds),
analyzer_config=DiscordAnalyzerConfig(),
)
results = await analyzer_heatmaps.start(from_start=True)
results = []
async for heatmaps_data in analyzer_heatmaps.start(from_start=True):
results.extend(heatmaps_data)

assert len(results) == len(acc_names) * DAY_COUNT * len(channelIds)
for document in results:
Expand Down
Loading

0 comments on commit 34cda0e

Please sign in to comment.