Skip to content

Commit 917fb88

Browse files
committed
feat(Batch): Added Batch Processing
1 parent acc30fb commit 917fb88

File tree

3 files changed

+62
-19
lines changed

3 files changed

+62
-19
lines changed

management/core/constants.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,40 @@ class Reaction(models.TextChoices):
5555
SAD = "sad", "😢"
5656
ANGRY = "angry", "😡"
5757
ROCKET = "rocket", "🚀"
58+
59+
60+
class AlertType(models.TextChoices):
61+
"""Possible alert types for notifications or system messages.
62+
63+
* ASSIGNED: Task or ticket has been assigned.
64+
* WAITING: Task or ticket is waiting to be processed.
65+
* CLOSED: Task or ticket has been closed.
66+
* OPEN: Task or ticket has been opened.
67+
* COMPLETED: Task or ticket has been completed.
68+
* IN_PROGRESS: Task or ticket is currently in progress.
69+
"""
70+
71+
ASSIGNED = "assigned", "Assigned"
72+
WAITING = "waiting", "Waiting"
73+
CLOSED = "closed", "Closed"
74+
OPEN = "open", "Open"
75+
COMPLETED = "completed", "Completed"
76+
IN_PROGRESS = "in_progress", "In Progress"
77+
78+
79+
class ChannelType(models.TextChoices):
80+
"""Possible channel types for communication.
81+
82+
* EMAIL: Communication via email.
83+
* SLACK: Communication via Slack.
84+
* DISCORD: Communication via Discord.
85+
* SMS: Communication via SMS.
86+
* WHATSAPP: Communication via WhatsApp.
87+
* SMS: Communication via sms
88+
"""
89+
90+
EMAIL = "email", "Email"
91+
SLACK = "slack", "Slack"
92+
DISCORD = "discord", "Discord"
93+
SMS = "sms", "SMS"
94+
WHATSAPP = "whatsapp", "WhatsApp"

management/core/dumps.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
Copyright (c) Supportix. All rights reserved.
66
Written in 2025 by Dorna Raj Gyawali <dronarajgyawali@gmail.com>
77
"""
8-
8+
# context that is used while building Restapi
99
SUCESS_SIGNUP = {"User created Successfully"}
1010
CONTEXT_400 = {"Invalid credentials"}
1111
CONTEXT_405 = {"Request method not allowed."}
1212
CONTEXT_403 = "Unauthorized access."
13+
14+
15+
# image configuration for this app: Can be changed acccording to the need.
1316
ImageAttachmentExt = [
1417
".jpeg",
1518
".jpg",
@@ -24,7 +27,8 @@
2427
".ico",
2528
]
2629
FileAttachmentExt = [".pdf"]
27-
FileAttachmentSize = 20 * 1024 * 1024
28-
ImageAttachmentSize = 5 * 1024 * 1024
30+
FileAttachmentSize = 20 * 1024 * 1024 #20 mb
31+
ImageAttachmentSize = 5 * 1024 * 1024 # 5 mb
2932
OVERLOAD_THRESHOLD = 50
3033
UNDERUTILIZED_THRESHOLD = 10
34+
BATCH_SIZE = 100

management/core/tasks.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from core.automation.rule_runner import RuleEngine
1010
from core.constants import Status
1111
from core.models import Agent, Ticket
12-
12+
from core.dumps import BATCH_SIZE
1313
logger = logging.getLogger(__name__)
1414

1515

@@ -96,24 +96,26 @@ def delete_completed_tickets(self):
9696
logger.info(f"Deleted {deleted_count} completed tickets older than 60 days.")
9797

9898

99-
# TODO: implement the batch processing[!important][Memmory Issue]
10099
@shared_task(bind=True)
101-
def apply_rules_to_all_tickets():
100+
def apply_rules_to_all_tickets(self):
102101
"""
103-
Celery task to apply RuleEngine to all tickets in the database.
102+
Celery task to apply RuleEngine to all tickets in the database in batches.
104103
"""
105-
tickets = Ticket.objects.all()
104+
total_tickets = Ticket.objects.count()
106105
results = []
107106

108-
for ticket in tickets:
109-
try:
110-
engine = RuleEngine(ticket.ticket_id)
111-
result = engine.run()
112-
results.append({"ticket_id": ticket.ticket_id, "result": result})
113-
logger.info(f"Rules applied to ticket {ticket.ticket_id}: {result}")
114-
except Exception as e:
115-
logger.exception(
116-
f"Failed to apply rules to ticket {ticket.ticket_id}: {str(e)}"
117-
)
118-
107+
for start in range(0, total_tickets, BATCH_SIZE):
108+
end = start + BATCH_SIZE
109+
ticket_batch = Ticket.objects.all()[start:end]
110+
111+
for ticket in ticket_batch:
112+
try:
113+
engine = RuleEngine(ticket.ticket_id)
114+
result = engine.run()
115+
results.append({"ticket_id": ticket.ticket_id, "result": result})
116+
logger.info(f"Rules applied to ticket {ticket.ticket_id}: {result}")
117+
except Exception as e:
118+
logger.exception(
119+
f"Failed to apply rules to ticket {ticket.ticket_id}: {str(e)}"
120+
)
119121
return results

0 commit comments

Comments
 (0)