Skip to content

Commit

Permalink
chat history data fetch changes (#1254)
Browse files Browse the repository at this point in the history
* changed conversation fetching for chat history and chat client to use flattened data properly

* added live_agent_enabled flag to chat client config

* changes

---------

Co-authored-by: spandan.mondal <[email protected]>
  • Loading branch information
hasinaxp and spandan.mondal authored Jun 26, 2024
1 parent 18dd402 commit 834e1bf
Show file tree
Hide file tree
Showing 9 changed files with 29,287 additions and 45 deletions.
24 changes: 8 additions & 16 deletions kairon/chat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,36 +206,28 @@ def get_last_session_conversation(bot: Text, sender_id: Text):
{
"$match": {
"sender_id": sender_id,
"event.timestamp": {
"timestamp": {
"$gt": last_session["event"]["timestamp"]
},
"event.event": {
"$in": ["session_started", "user", "bot"]
"type": {
"$in": ["flattened"]
},
}
},
{
"$project": {
"sender_id": 1,
"event.event": 1,
"event.timestamp": 1,
"event.text": 1,
"event.data": 1,
"tabname": "$event.metadata.tabname",
"event.metadata": "$event.metadata",
}
"$addFields": {"_id": {"$toString": "$_id"}}
},
{"$sort": {"event.timestamp": 1}},
{"$sort": {"timestamp": 1}},
{
"$group": {
"_id": {"tabname": "$tabname"},
"events": {"$push": "$event"},
"_id": "$metadata.tabname",
"events": {"$push": "$$ROOT"}
}
},
{
"$project": {
"_id": 0,
"tabname": "$_id.tabname",
"tabname": "$_id",
"events": "$events",
}
},
Expand Down
30 changes: 12 additions & 18 deletions kairon/history/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,25 +789,19 @@ def flatten_conversations(collection: Text, from_date: date = (datetime.utcnow()
with client as client:
db = client.get_database()
conversations = db.get_collection(collection)
user_data = list(
conversations.aggregate(
[{"$match": {"type": {"$in": ["flattened", "broadcast"]},
"timestamp": {"$gte": Utility.get_timestamp_from_date(from_date),
"$lte": Utility.get_timestamp_from_date(to_date)}}},
{"$project": {"user_input": "$data.user_input", "intent": "$data.intent",
"confidence": "$data.confidence", "action": "$data.action",
"timestamp": {"$toDate": {"$multiply": ["$timestamp", 1000]}},
"bot_response": "$data.bot_response", "sender_id": "$sender_id",
"template_name": "$data.name", "template": "$data.template",
"template_params": "$data.template_params"
}},
{"$sort": {"timestamp": -1}},
{"$project": {"_id": "$sender_id", "user_input": 1, "intent": 1, "confidence": 1,
"timestamp": {'$dateToString': {'format': "%d-%m-%Y %H:%M:%S", 'date': '$timestamp'}},
"action": 1, "bot_response": 1, "template_name": 1, "template": 1,
"template_params": 1}}
], allowDiskUse=True))
search_query = {
"type": {"$in": ["flattened", "broadcast"]},
"timestamp": {
"$gte": Utility.get_timestamp_from_date(from_date),
"$lte": Utility.get_timestamp_from_date(to_date)
}
}
values = list(conversations.find(search_query).sort("timestamp", -1))

if values:
for v in values:
v.pop("_id")
user_data = values
except Exception as e:
logger.error(e)
message = str(e)
Expand Down
3 changes: 2 additions & 1 deletion kairon/shared/data/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
from ..constants import KaironSystemSlots, PluginTypes, EventClass
from ..custom_widgets.data_objects import CustomWidgets
from ..importer.data_objects import ValidationLogs
from ..live_agent.live_agent import LiveAgentHandler
from ..multilingual.data_objects import BotReplicationLogs
from ..test.data_objects import ModelTestingLogs

Expand Down Expand Up @@ -5585,7 +5586,7 @@ def get_chat_client_config(
client_config.config['api_server_host_url'] = Utility.environment['app']['server_url']
client_config.config['nudge_server_url'] = Utility.environment['nudge']['server_url']
client_config.config['live_agent_socket_url'] = Utility.environment['live_agent']['live_agent_socket_url']

client_config.config['live_agent_enabled'] = LiveAgentHandler.is_live_agent_service_available(bot)
token, refresh_token = Authentication.generate_integration_token(
bot,
user,
Expand Down
1 change: 1 addition & 0 deletions template/chat-client/default-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"userType": "custom",
"userStorage": "ls",
"whitelist": ["*"],
"live_agent_enabled": false,
"styles": {
"headerStyle": {
"backgroundColor": "#2b3595",
Expand Down
1 change: 1 addition & 0 deletions tests/testing_data/all/chat_client_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ config:
name: kairon_testing
openButtonClassName: ''
live_agent_socket_url: ws://localhost:8000/ws/client
live_agent_enabled: true
styles:
botStyle:
backgroundColor: '#e0e0e0'
Expand Down
Loading

0 comments on commit 834e1bf

Please sign in to comment.