Skip to content

Commit

Permalink
refactored code
Browse files Browse the repository at this point in the history
Co-authored-by: udit-pandey <[email protected]>
Co-authored-by: nupur-khare <[email protected]>
  • Loading branch information
udit-pandey and nupur-khare committed Dec 14, 2023
1 parent dcd15e1 commit 1d1a281
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 45 deletions.
15 changes: 6 additions & 9 deletions kairon/api/app/routers/bot/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from kairon.shared.chat.broadcast.processor import MessageBroadcastProcessor
from kairon.shared.chat.models import ChannelRequest, MessageBroadcastRequest
from kairon.shared.chat.processor import ChatDataProcessor
from kairon.shared.constants import TESTER_ACCESS, DESIGNER_ACCESS, WhatsappBSPTypes, EventRequestType
from kairon.shared.constants import TESTER_ACCESS, DESIGNER_ACCESS, WhatsappBSPTypes, EventRequestType, ChannelTypes
from kairon.shared.data.processor import MongoProcessor
from kairon.shared.models import User

Expand Down Expand Up @@ -237,15 +237,12 @@ async def retrieve_scheduled_message_broadcast_logs(
return Response(data={"logs": logs, "total_count": total_count})


@router.get("/whatsapp/logs", response_model=Response)
async def get_channel_logs_count(
@router.get("/{channel_type}/metrics", response_model=Response)
async def get_channel_metrics(
channel_type: ChannelTypes,
current_user: User = Security(Authentication.get_current_user_and_bot, scopes=TESTER_ACCESS)
):
"""
Get Channel logs count.
Get Channel metrics (Failures/Successes).
"""
channel_logs_count = MessageBroadcastProcessor.get_channel_logs_count(current_user.get_bot())
data = {
'count': channel_logs_count
}
return Response(data=data)
return Response(data=MessageBroadcastProcessor.get_channel_metrics(channel_type, current_user.get_bot()))
2 changes: 1 addition & 1 deletion kairon/chat/handlers/channels/whatsapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async def __handle_meta_payload(self, payload: Dict, metadata: Optional[Dict[Tex
statuses = changes.get("value", {}).get("statuses")
user = metadata.get('display_phone_number')
for status_data in statuses:
ChatDataProcessor.save_whatsapp_audit_log(status_data, bot, user)
ChatDataProcessor.save_whatsapp_audit_log(status_data, bot, user, ChannelTypes.WHATSAPP.value)
for message in messages or []:
await self.message(message, metadata, bot)

Expand Down
33 changes: 8 additions & 25 deletions kairon/shared/chat/broadcast/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from kairon.shared.chat.broadcast.data_objects import MessageBroadcastSettings, SchedulerConfiguration, \
RecipientsConfiguration, TemplateConfiguration, MessageBroadcastLogs
from kairon.shared.chat.data_objects import Channels, ChannelLogs
from kairon.shared.constants import ChannelTypes


class MessageBroadcastProcessor:
Expand Down Expand Up @@ -123,12 +124,13 @@ def extract_message_ids_from_broadcast_logs(reference_id: Text):
@staticmethod
def __add_broadcast_logs_status_and_errors(reference_id: Text, broadcast_logs: Dict[Text, Document]):
message_ids = list(broadcast_logs.keys())
channel_logs = ChannelLogs.objects(message_id__in=message_ids)
channel_logs = ChannelLogs.objects(message_id__in=message_ids, type=ChannelTypes.WHATSAPP.value)
for log in channel_logs:
if log['errors']:
msg_id = log["message_id"]
broadcast_logs[msg_id].update(errors=log['errors'], status="Failed")
log.update(campaign_id=reference_id)

ChannelLogs.objects(message_id__in=message_ids, type=ChannelTypes.WHATSAPP.value).update(campaign_id=reference_id)

@staticmethod
def insert_status_received_on_channel_webhook(reference_id: Text):
Expand All @@ -137,29 +139,10 @@ def insert_status_received_on_channel_webhook(reference_id: Text):
MessageBroadcastProcessor.__add_broadcast_logs_status_and_errors(reference_id, broadcast_logs)

@staticmethod
def get_channel_logs_count(bot: Text):
def get_channel_metrics(channel_type: Text, bot: Text):
result = list(ChannelLogs.objects.aggregate([
{
'$match': {
'bot': bot
}
}, {
'$group': {
'_id': {
'campaign_id': '$campaign_id',
'status': '$status'
},
'count': {
'$sum': 1
}
}
}, {
'$project': {
'status': '$_id.status',
'campaign_id': '$_id.campaign_id',
'cnt': '$count',
'_id': 0
}
}
{'$match': {'bot': bot, "type": channel_type}},
{'$group': {'_id': {'campaign_id': '$campaign_id', 'status': '$status'}, 'count': {'$sum': 1}}},
{'$project': {'status': '$_id.status', 'campaign_id': '$_id.campaign_id', 'count': '$count', '_id': 0}}
]))
return result
6 changes: 5 additions & 1 deletion kairon/shared/chat/data_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ def validate(self, clean=True):
@auditlogger.log
@push_notification.apply
class ChannelLogs(DynamicDocument):
data = DictField(default=None)
type = StringField()
data = DictField()
campaign_id = StringField()
status = StringField(required=True)
message_id = StringField(required=True)
errors = ListField(DictField(default=[]))
initiator = StringField(default=None)
bot = StringField(required=True)
user = StringField(required=True)
timestamp = DateTimeField(default=datetime.utcnow)

meta = {"indexes": [{"fields": ["bot", ("bot", "type", "campaign_id")]}]}
4 changes: 3 additions & 1 deletion kairon/shared/chat/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,17 @@ def get_channel_endpoint(connector_type: Text, bot: Text):
raise AppException('Channel not configured')

@staticmethod
def save_whatsapp_audit_log(status_data: Dict, bot: Text, user: Text):
def save_whatsapp_audit_log(status_data: Dict, bot: Text, user: Text, channel_type: Text):
"""
save or updates channel configuration
:param status_data: status_data dict
:param bot: bot id
:param user: user id
:param channel_type: channel type
:return: None
"""
ChannelLogs(
type=channel_type,
status=status_data.get('status'),
data=status_data.get('conversation'),
initiator=status_data.get('conversation', {}).get('origin', {}).get('type'),
Expand Down
11 changes: 7 additions & 4 deletions tests/integration_test/services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14057,6 +14057,7 @@ def test_list_whatsapp_templates_error():
def test_get_channel_logs():
from kairon.shared.chat.data_objects import ChannelLogs
ChannelLogs(
type=ChannelTypes.WHATSAPP.value,
status='read',
data={'id': 'CONVERSATION_ID', 'expiration_timestamp': '1691598412',
'origin': {'type': 'business_initated'}},
Expand All @@ -14067,6 +14068,7 @@ def test_get_channel_logs():
user='[email protected]'
).save()
ChannelLogs(
type=ChannelTypes.WHATSAPP.value,
status='delivered',
data={'id': 'CONVERSATION_ID', 'expiration_timestamp': '1621598412',
'origin': {'type': 'business_initated'}},
Expand All @@ -14077,6 +14079,7 @@ def test_get_channel_logs():
user='[email protected]'
).save()
ChannelLogs(
type=ChannelTypes.WHATSAPP.value,
status='sent',
data={'id': 'CONVERSATION_ID', 'expiration_timestamp': '1631598412',
'origin': {'type': 'business_initated'}},
Expand All @@ -14087,16 +14090,16 @@ def test_get_channel_logs():
user='[email protected]'
).save()
response = client.get(
f"/api/bot/{pytest.bot}/channels/whatsapp/logs",
f"/api/bot/{pytest.bot}/channels/whatsapp/metrics",
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
print(actual)
assert actual["success"]
assert actual["error_code"] == 0
assert actual["data"] == {'count': [{'status': 'delivered', 'campaign_id': '6779002886649302', 'cnt': 1},
{'status': 'read', 'campaign_id': '6779002886649302', 'cnt': 1},
{'status': 'sent', 'campaign_id': '6779002886649302', 'cnt': 1}]}
assert actual["data"] == [{'status': 'delivered', 'campaign_id': '6779002886649302', 'cnt': 1},
{'status': 'read', 'campaign_id': '6779002886649302', 'cnt': 1},
{'status': 'sent', 'campaign_id': '6779002886649302', 'cnt': 1}]


@responses.activate
Expand Down
7 changes: 3 additions & 4 deletions tests/unit_test/events/events_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from kairon.events.definitions.model_testing import ModelTestingEvent
from kairon.events.definitions.model_training import ModelTrainingEvent
from kairon.exceptions import AppException
from kairon.shared.constants import EventClass, EventRequestType
from kairon.shared.constants import EventClass, EventRequestType, ChannelTypes
from kairon.shared.data.constant import EVENT_STATUS, REQUIREMENTS
from kairon.shared.data.data_objects import Configs, BotSettings
from kairon.shared.data.history_log_processor import HistoryDeletionLogProcessor
Expand Down Expand Up @@ -1287,9 +1287,8 @@ def test_execute_message_broadcast_with_logs_modification(self, mock_is_exist, m
'details': "Failed to send message because this user's phone number is part of an experiment"},
'href': 'https://developers.facebook.com/docs/whatsapp/cloud-api/support/error-codes/'}]}
assert ChannelLogs.objects(bot=bot, message_id='wamid.HBgLMTIxMTU1NTc5NDcVAgARGBIyRkQxREUxRDJFQUJGMkQ3NDIZ').get().campaign_id == reference_id
result = MessageBroadcastProcessor.get_channel_logs_count(bot)
print(result)
assert result == [{'status': 'Failed', 'campaign_id': reference_id, 'cnt': 1}]
result = MessageBroadcastProcessor.get_channel_metrics(ChannelTypes.WHATSAPP.value, bot)
assert result == [{'status': 'Failed', 'campaign_id': reference_id, 'count': 1}]

@responses.activate
@mongomock.patch(servers=(('localhost', 27017),))
Expand Down

0 comments on commit 1d1a281

Please sign in to comment.