Skip to content

Commit

Permalink
Remove squashing of old category counts
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Jan 13, 2025
1 parent c00f219 commit 5661164
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 50 deletions.
27 changes: 0 additions & 27 deletions temba/flows/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1496,39 +1496,12 @@ class FlowCategoryCount(BaseSquashableCount):
TODO: replace by FlowResultCount
"""

squash_over = ("flow_id", "node_uuid", "result_key", "result_name", "category_name")

flow = models.ForeignKey(Flow, on_delete=models.PROTECT, related_name="category_counts")
node_uuid = models.UUIDField(db_index=True)
result_key = models.CharField(max_length=128)
result_name = models.CharField(max_length=128)
category_name = models.CharField(max_length=128)

@classmethod
def get_squash_query(cls, distinct_set: dict) -> tuple: # pragma: no cover
sql = """
WITH removed as (
DELETE FROM %(table)s WHERE "id" IN (
SELECT "id" FROM %(table)s
WHERE "flow_id" = %%s AND "node_uuid" = %%s AND "result_key" = %%s AND "result_name" = %%s AND "category_name" = %%s
LIMIT 10000
) RETURNING "count"
)
INSERT INTO %(table)s("flow_id", "node_uuid", "result_key", "result_name", "category_name", "count", "is_squashed")
VALUES (%%s, %%s, %%s, %%s, %%s, GREATEST(0, (SELECT SUM("count") FROM removed)), TRUE);
""" % {
"table": cls._meta.db_table
}

params = (
distinct_set["flow_id"],
distinct_set["node_uuid"],
distinct_set["result_key"],
distinct_set["result_name"],
distinct_set["category_name"],
) * 2
return sql, params

class Meta:
indexes = [
models.Index(
Expand Down
18 changes: 2 additions & 16 deletions temba/flows/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@
from temba.utils.crons import cron_task
from temba.utils.models import delete_in_batches

from .models import (
Flow,
FlowActivityCount,
FlowCategoryCount,
FlowResultCount,
FlowRevision,
FlowRun,
FlowSession,
FlowStartCount,
)
from .models import Flow, FlowActivityCount, FlowResultCount, FlowRevision, FlowRun, FlowSession, FlowStartCount

logger = logging.getLogger(__name__)

Expand All @@ -44,14 +35,9 @@ def update_session_wait_expires(flow_id):


@cron_task(lock_timeout=7200)
def squash_activity_counts():
def squash_flow_counts():
FlowActivityCount.squash()
FlowResultCount.squash()


@cron_task(lock_timeout=7200)
def squash_flow_counts():
FlowCategoryCount.squash()
FlowStartCount.squash()


Expand Down
10 changes: 5 additions & 5 deletions temba/flows/tests/test_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.utils import timezone

from temba.flows.models import FlowActivityCount, FlowRun, FlowSession
from temba.flows.tasks import squash_activity_counts
from temba.flows.tasks import squash_flow_counts
from temba.tests import TembaTest
from temba.utils.uuid import uuid4

Expand Down Expand Up @@ -130,7 +130,7 @@ def create_runs(flow_status_pairs: tuple) -> list:
self.assertEqual({"status:W": 2}, flow2.counts.scope_totals())

# no difference after squashing
squash_activity_counts()
squash_flow_counts()

self.assertEqual({"status:A": 2, "status:W": 1, "status:C": 1}, flow1.counts.scope_totals())
self.assertEqual({"status:W": 2}, flow2.counts.scope_totals())
Expand All @@ -155,7 +155,7 @@ def create_runs(flow_status_pairs: tuple) -> list:
self.assertEqual({"status:W": 0, "status:X": 1, "status:I": 2}, flow2.counts.scope_totals())

# no difference after squashing except zeros gone
squash_activity_counts()
squash_flow_counts()

self.assertEqual({"status:A": 2, "status:I": 4}, flow1.counts.scope_totals())
self.assertEqual({"status:X": 1, "status:I": 2}, flow2.counts.scope_totals())
Expand Down Expand Up @@ -242,7 +242,7 @@ def test_squashing(self):
self.assertEqual(0, flow2.counts.filter(scope="foo:2").sum())
self.assertEqual(5, flow2.counts.filter(scope="foo:3").sum())

squash_activity_counts()
squash_flow_counts()

self.assertEqual({"foo:1", "foo:2", "foo:3"}, set(flow1.counts.values_list("scope", flat=True)))

Expand All @@ -258,7 +258,7 @@ def test_squashing(self):

flow2.counts.create(scope="foo:3", count=-5) # unsquashed zero + squashed zero

squash_activity_counts()
squash_flow_counts()

# flow2/foo:3 should be gone because it squashed to zero
self.assertEqual({"foo:1"}, set(flow2.counts.values_list("scope", flat=True)))
Expand Down
3 changes: 1 addition & 2 deletions temba/settings_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,10 +748,9 @@
"refresh-whatsapp-tokens": {"task": "refresh_whatsapp_tokens", "schedule": crontab(hour=6, minute=0)},
"refresh-templates": {"task": "refresh_templates", "schedule": timedelta(seconds=900)},
"send-notification-emails": {"task": "send_notification_emails", "schedule": timedelta(seconds=60)},
"squash-activity-counts": {"task": "squash_activity_counts", "schedule": timedelta(seconds=30)},
"squash-channel-counts": {"task": "squash_channel_counts", "schedule": timedelta(seconds=60)},
"squash-group-counts": {"task": "squash_group_counts", "schedule": timedelta(seconds=60)},
"squash-flow-counts": {"task": "squash_flow_counts", "schedule": timedelta(seconds=60)},
"squash-flow-counts": {"task": "squash_flow_counts", "schedule": timedelta(seconds=30)},
"squash-item-counts": {"task": "squash_item_counts", "schedule": timedelta(seconds=30)},
"squash-msg-counts": {"task": "squash_msg_counts", "schedule": timedelta(seconds=60)},
"squash-ticket-counts": {"task": "squash_ticket_counts", "schedule": timedelta(seconds=60)},
Expand Down

0 comments on commit 5661164

Please sign in to comment.