Skip to content

Commit

Permalink
Merge branch 'main' into new_segment_counts_pt2
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Nov 28, 2024
2 parents 5133a4a + 1a4e755 commit 69ec073
Show file tree
Hide file tree
Showing 8 changed files with 608 additions and 33 deletions.
603 changes: 575 additions & 28 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ boto3 = "^1.35.54"
cryptography = "^43.0.3"
vonage = "3.17.4"
pyotp = "2.4.1"
twilio = "6.24.0"
twilio = "9.3.7"
twython = "3.5.0"
geojson = "^2.5.0"
Markdown = "^3.3.4"
Expand Down
2 changes: 1 addition & 1 deletion temba/api/v2/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3187,7 +3187,7 @@ def test_flows(self):
"modified_on": format_datetime(survey.modified_on),
},
],
num_queries=NUM_BASE_SESSION_QUERIES + 5,
num_queries=NUM_BASE_SESSION_QUERIES + 3,
)

self.assertGet(endpoint_url, [self.admin2], results=[other_org])
Expand Down
3 changes: 3 additions & 0 deletions temba/api/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,9 @@ def filter_queryset(self, queryset):

return self.filter_before_after(queryset, "modified_on")

def prepare_for_serialization(self, object_list, using: str):
Flow.prefetch_run_stats(object_list, using=using)

@classmethod
def get_read_explorer(cls):
return {
Expand Down
2 changes: 1 addition & 1 deletion temba/contacts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ def _full_release(self):
broadcast.contacts.remove(self)

@classmethod
def bulk_urn_cache_initialize(cls, contacts, *, using="default"):
def bulk_urn_cache_initialize(cls, contacts, *, using: str = "default"):
"""
Initializes the URN caches on the given contacts.
"""
Expand Down
26 changes: 25 additions & 1 deletion temba/flows/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,16 @@ def update_single_message_flow(self, user, translations: dict, base_language: st

self.save_revision(user, definition)

@classmethod
def prefetch_run_stats(cls, flows, *, using="default"):
FlowActivityCount.prefetch_by_scope(flows, prefix="status:", to_attr="_status_counts", using=using)

def get_run_stats(self):
counts = self.counts.prefix("status:").scope_totals()
if hasattr(self, "_status_counts"):
counts = self._status_counts
else:
counts = self.counts.prefix("status:").scope_totals()

by_status = {scope[7:]: count for scope, count in counts.items()}

total_runs = sum(by_status.values())
Expand Down Expand Up @@ -1449,6 +1457,22 @@ def get_squash_query(cls, distinct_set) -> tuple:

return sql, params

@classmethod
def prefetch_by_scope(cls, flows, *, prefix: str, to_attr: str, using: str):
counts = (
FlowActivityCount.objects.using(using)
.filter(flow__in=flows)
.prefix(prefix)
.values_list("flow_id", "scope")
.annotate(total=Sum("count"))
)
by_flow = defaultdict(dict)
for count in counts:
by_flow[count[0]][count[1]] = count[2]

for flow in flows:
setattr(flow, to_attr, by_flow[flow.id])

class Meta:
indexes = [
models.Index("flow", OpClass("scope", name="varchar_pattern_ops"), name="flowactivitycount_flow_scope"),
Expand Down
2 changes: 2 additions & 0 deletions temba/flows/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,8 @@ class BaseList(SpaMixin, BulkActionMixin, ContextMenuMixin, BaseListView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)

Flow.prefetch_run_stats(context["object_list"])

# decorate flow objects with their run activity stats
for flow in context["object_list"]:
flow.run_stats = flow.get_run_stats()
Expand Down
1 change: 0 additions & 1 deletion temba/tests/twilio.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def __init__(self, sid, token, org=None, base=None):
self.org = org
self.base = base
self.auth = ["", "FakeRequestToken"]
self.events = []

@property
def api(self):
Expand Down

0 comments on commit 69ec073

Please sign in to comment.