Skip to content

Commit

Permalink
move id logic to models
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderGi committed Jul 15, 2024
1 parent e707c80 commit e1c9bb3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
34 changes: 28 additions & 6 deletions bots/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,7 @@ class ConvoState(models.IntegerChoices):
class ConversationQuerySet(models.QuerySet):
def get_unique_users(self) -> "ConversationQuerySet":
"""Get unique conversations"""
return self.distinct(
"fb_page_id", "ig_account_id", "wa_phone_number", "slack_user_id"
)
return self.distinct(*Conversation.ID_COLUMNS())

def to_df(self, tz=pytz.timezone(settings.TIME_ZONE)) -> "pd.DataFrame":
import pandas as pd
Expand Down Expand Up @@ -1013,6 +1011,24 @@ class Meta:
def __str__(self):
return f"{self.get_display_name()} <> {self.bot_integration}"

@classmethod
def ID_COLUMNS(cls):
return [
"fb_page_id",
"ig_account_id",
"wa_phone_number",
"slack_user_id",
"web_user_id",
]

def get_id(self):
self_id = None
for col in self.ID_COLUMNS():
if getattr(self, col):
self_id = getattr(self, col)
break
return self_id

def get_display_name(self):
return (
(self.wa_phone_number and self.wa_phone_number.as_international)
Expand All @@ -1021,9 +1037,7 @@ def get_display_name(self):
or " in #".join(
filter(None, [self.slack_user_name, self.slack_channel_name])
)
or self.fb_page_id
or self.slack_user_id
or self.web_user_id
or self.get_id()
)

get_display_name.short_description = "User"
Expand Down Expand Up @@ -1056,6 +1070,10 @@ def msgs_for_llm_context(self):


class MessageQuerySet(models.QuerySet):
def get_unique_users(self) -> "MessageQuerySet":
"""Get unique users"""
return self.distinct(*Message.CONVO_ID_COLUMNS())

def to_df(self, tz=pytz.timezone(settings.TIME_ZONE)) -> "pd.DataFrame":
import pandas as pd

Expand Down Expand Up @@ -1288,6 +1306,10 @@ def __str__(self):
def local_lang(self):
return Truncator(self.display_content).words(30)

@classmethod
def CONVO_ID_COLUMNS(cls):
return [f"conversation__{col}" for col in Conversation.ID_COLUMNS()]


class MessageAttachment(models.Model):
message = models.ForeignKey(
Expand Down
17 changes: 3 additions & 14 deletions recipes/VideoBotsStats.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@
from gooey_ui import RedirectException
from recipes.VideoBots import VideoBotsPage

ID_COLUMNS = [
"conversation__fb_page_id",
"conversation__ig_account_id",
"conversation__wa_phone_number",
"conversation__slack_user_id",
]


class VideoBotsStatsPage(BasePage):
title = "Copilot Analytics" # "Create Interactive Video Bots"
Expand Down Expand Up @@ -418,19 +411,15 @@ def calculate_overall_stats(*, bi, run_title, run_url):
conversation__in=users,
created_at__gte=timezone.now() - timedelta(days=7),
)
.distinct(
*ID_COLUMNS,
)
.get_unique_users()
.count()
)
num_active_users_last_30_days = (
user_messages.filter(
conversation__in=users,
created_at__gte=timezone.now() - timedelta(days=30),
)
.distinct(
*ID_COLUMNS,
)
.get_unique_users()
.count()
)
positive_feedbacks = Feedback.objects.filter(
Expand Down Expand Up @@ -488,7 +477,7 @@ def calculate_stats_binned_by_time(*, bi, start_date, end_date, factor, trunc_fn
.annotate(
Senders=Count(
Concat(
*ID_COLUMNS,
*Message.CONVO_ID_COLUMNS(),
),
distinct=True,
)
Expand Down

0 comments on commit e1c9bb3

Please sign in to comment.