Skip to content

Commit

Permalink
change counting method for new attendances
Browse files Browse the repository at this point in the history
  • Loading branch information
AlisoSouza committed Nov 24, 2023
1 parent 4a906f2 commit f27172f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
29 changes: 29 additions & 0 deletions connect/billing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Tuple
from connect.billing.models import Contact
import pendulum
from itertools import groupby


def day_period(day: DateTime) -> Tuple[DateTime, ...]:
Expand Down Expand Up @@ -34,3 +35,31 @@ def get_attendances(project: Project, start: str, end: str) -> int:
total_per_project += day_attendances

return total_per_project


def count_attendances(contact_group: list):
attendances = 1
base_contact = contact_group[0]

for contact in contact_group:
date_period = pendulum.instance(base_contact.last_seen_on) - pendulum.instance(contact.last_seen_on)
if abs(date_period.days) >= 1:
attendances += 1
base_contact = contact

return attendances


def custom_get_attendances(project: Project, start: str, end: str):
start = pendulum.parse(start)
end = pendulum.parse(end)
total_attendances = 0

contacts = Contact.objects.filter(last_seen_on__range=(start, end), project=project).order_by("contact_flow_uuid", "-last_seen_on")
grouped_contacts = groupby(contacts, lambda contact: contact.contact_flow_uuid)

for _, group in grouped_contacts:
contact_group = list(group)
total_attendances += count_attendances(contact_group)

return total_attendances
9 changes: 5 additions & 4 deletions connect/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,17 +1006,18 @@ def send_email_invite_project(self, email):

def get_contacts(self, before: str, after: str, counting_method: str = None):
from connect.billing.models import Contact
from connect.billing.utils import get_attendances
from connect.billing.utils import get_attendances, custom_get_attendances

if not counting_method:
counting_method = self.organization.organization_billing.plan_method

if counting_method == BillingPlan.ACTIVE_CONTACTS:
return Contact.objects.filter(project=self).filter(last_seen_on__range=(after, before)).distinct("contact_flow_uuid").count()

if pendulum.parse(after) < pendulum.parse(settings.NEW_ATTENDANCE_DATE).end_of("day"):
return get_attendances(self, str(after), str(before))

total = get_attendances(self, str(after), str(before))

return total
return custom_get_attendances(self, str(after), str(before))


class OpenedProject(models.Model):
Expand Down
2 changes: 2 additions & 0 deletions connect/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,5 @@
EDA_BROKER_PASSWORD = env.str("EDA_BROKER_PASSWORD", default="guest")
EDA_VIRTUAL_HOST = env.str("EDA_VIRTUAL_HOST", default="/")
EDA_WAIT_TIME_RETRY = env.int("EDA_WAIT_TIME_RETRY", default=5)

NEW_ATTENDANCE_DATE = env.str("NEW_ATTENDANCE_DATE")

0 comments on commit f27172f

Please sign in to comment.