Skip to content

Commit

Permalink
Merge pull request #5705 from nyaruka/new_counts_for_node_and_status_pt3
Browse files Browse the repository at this point in the history
Stop reading node and status counts from old models
  • Loading branch information
rowanseymour authored Nov 27, 2024
2 parents 4005328 + e17993b commit 80cefb6
Show file tree
Hide file tree
Showing 4 changed files with 405 additions and 569 deletions.
49 changes: 0 additions & 49 deletions temba/flows/management/commands/recalc_node_counts.py

This file was deleted.

84 changes: 1 addition & 83 deletions temba/flows/management/commands/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.utils import timezone

from temba.contacts.models import Contact
from temba.flows.models import FlowNodeCount, FlowStart
from temba.flows.models import FlowStart
from temba.tests import TembaTest
from temba.tests.engine import MockSessionWriter

Expand Down Expand Up @@ -73,88 +73,6 @@ def test_command(self):
self.assertFalse(flow3.has_issues)


class RecalcNodeCountsTest(TembaTest):
def test_recalc_node_counts(self):
contact1 = self.create_contact("Ben Haggerty", phone="+12065552020")
contact2 = self.create_contact("Joe", phone="+12065550002")
contact3 = self.create_contact("Frank", phone="+12065550003")

def check_node_count_rebuild(flow, assert_count):
node_counts = FlowNodeCount.get_totals(flow)

call_command("recalc_node_counts", flow_id=flow.id)

new_counts = FlowNodeCount.get_totals(flow)
self.assertEqual(new_counts, node_counts)
self.assertEqual(assert_count, sum(new_counts.values()))

flow = self.get_flow("favorites_v13")
nodes = flow.get_definition()["nodes"]

color_prompt = nodes[0]
color_other = nodes[1]
color_split = nodes[2]
beer_prompt = nodes[3]
beer_split = nodes[5]
name_prompt = nodes[6]
name_split = nodes[7]
name_reply = nodes[8]

session1 = MockSessionWriter(contact1, flow).visit(color_prompt).visit(color_split).wait().save()
session2 = MockSessionWriter(contact2, flow).visit(color_prompt).visit(color_split).wait().save()
session3 = MockSessionWriter(contact3, flow).visit(color_prompt).visit(color_split).wait().save()

# recalculate node counts and check they are the same
check_node_count_rebuild(flow, 3)

(session1.resume(self.create_incoming_msg(contact1, "Blue")).visit(beer_prompt).visit(beer_split).wait().save())
(
session2.resume(self.create_incoming_msg(contact2, "Beige"))
.visit(color_other)
.visit(color_split)
.wait()
.save()
)
(
session3.resume(self.create_incoming_msg(contact3, "Amber"))
.visit(color_other)
.visit(color_split)
.wait()
.save()
)

check_node_count_rebuild(flow, 3)

(
session1.resume(self.create_incoming_msg(contact1, "Primus"))
.visit(name_prompt)
.visit(name_split)
.wait()
.save()
)
(
session2.resume(self.create_incoming_msg(contact2, "Orange"))
.visit(color_other)
.visit(color_split)
.wait()
.save()
)
(
session3.resume(self.create_incoming_msg(contact3, "Amber"))
.visit(color_other)
.visit(color_split)
.wait()
.save()
)

check_node_count_rebuild(flow, 3)

# contact1 replies with name to complete the flow
(session1.resume(self.create_incoming_msg(contact1, "Bob")).visit(name_reply).complete().save())

check_node_count_rebuild(flow, 2)


class UndoFootgunTest(TembaTest):
def test_group_changes(self):
flow = self.create_flow("Test")
Expand Down
29 changes: 6 additions & 23 deletions temba/flows/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,18 +441,14 @@ def lock(self):
lock_key = FLOW_LOCK_KEY % (self.org_id, self.id)
return r.lock(lock_key, FLOW_LOCK_TTL)

def get_activity(self, use_new: bool = False) -> tuple:
def get_activity(self) -> tuple:
"""
Get the activity summary for a flow as a tuple of the number of active runs
at each step and a map of the previous visits
"""

if use_new:
counts = self.counts.prefix("node:").scope_totals()
by_node = {scope[5:]: count for scope, count in counts.items() if count}
else:
by_node = FlowNodeCount.get_totals(self)

counts = self.counts.prefix("node:").scope_totals()
by_node = {scope[5:]: count for scope, count in counts.items() if count}
by_segment = FlowPathCount.get_totals(self)

return by_node, by_segment
Expand Down Expand Up @@ -589,12 +585,9 @@ def update_single_message_flow(self, user, translations: dict, base_language: st

self.save_revision(user, definition)

def get_run_stats(self, use_new: bool = False):
if use_new:
counts = self.counts.prefix("status:").scope_totals()
by_status = {scope[7:]: count for scope, count in counts.items()}
else:
by_status = FlowRunStatusCount.get_totals(self)
def get_run_stats(self):
counts = self.counts.prefix("status:").scope_totals()
by_status = {scope[7:]: count for scope, count in counts.items()}

total_runs = sum(by_status.values())
completed = by_status.get(FlowRun.STATUS_COMPLETED, 0)
Expand Down Expand Up @@ -1603,11 +1596,6 @@ def get_squash_query(cls, distinct_set):

return sql, (distinct_set.node_uuid, distinct_set.flow_id, distinct_set.node_uuid)

@classmethod
def get_totals(cls, flow):
totals = list(cls.objects.filter(flow=flow).values_list("node_uuid").annotate(replies=Sum("count")))
return {str(t[0]): t[1] for t in totals if t[1]}

class Meta:
indexes = [
models.Index(
Expand Down Expand Up @@ -1639,11 +1627,6 @@ def get_squash_query(cls, distinct_set):

return sql, (distinct_set.flow_id, distinct_set.status) * 2

@classmethod
def get_totals(cls, flow):
totals = list(cls.objects.filter(flow=flow).values_list("status").annotate(total=Sum("count")))
return {t[0]: t[1] for t in totals}

class Meta:
indexes = [
models.Index(fields=("flow", "status")),
Expand Down
Loading

0 comments on commit 80cefb6

Please sign in to comment.