async def chat_history_users(month: HistoryMonth = 1, current_user: User = Depends(auth.get_current_user)): """ Fetches the list of user who has conversation with the agent """ users, message = ChatHistory.fetch_chat_users(current_user.get_bot(), month) return {"data": {"users": users}, "message": message}
async def count_new_users(month: HistoryMonth = 1, current_user: User = Depends(auth.get_current_user)): """ Fetches the number of new users of the bot """ user_count, message = ChatHistory.new_users(current_user.get_bot(), month) return {"data": user_count, "message": message}
async def count_new_users_range(month: HistoryMonth = 6, current_user: User = Depends( auth.get_current_user)): """ Fetches the counts of new users of the bot for previous months """ range_value = ChatHistory.new_users_range(current_user.get_bot(), month) return {"data": range_value}
async def flat_conversations(month: int = 3, current_user: User = Depends(auth.get_current_user_and_bot)): """ Fetches the flattened conversation data of the bot for previous months """ flat_data, message = ChatHistory.flatten_conversations( current_user.get_bot(), month ) return {"data": flat_data, "message": message}
async def conversation_time(month: HistoryMonth = 1, current_user: User = Depends( auth.get_current_user)): """ Fetches the duration of the chat that took place between the users and the agent""" conversation_time, message = ChatHistory.conversation_time( current_user.get_bot(), month) return {"data": conversation_time, "message": message}
async def visitor_hit_fallback(month: HistoryMonth = 1, current_user: User = Depends(auth.get_current_user)): """ Fetches the number of times the agent hit a fallback (ie. not able to answer) to user queries """ visitor_hit_fallback, message = ChatHistory.visitor_hit_fallback( current_user.get_bot(), month ) return {"data": visitor_hit_fallback, "message": message}
async def fallback_trend(month: HistoryMonth = 6, current_user: User = Depends(auth.get_current_user_and_bot)): """ Fetches the fallback count of the bot for previous months """ range_value, message = ChatHistory.fallback_count_range( current_user.get_bot(), month ) return {"data": range_value, "message": message}
async def retention_trend(month: HistoryMonth = 6, current_user: User = Depends(auth.get_current_user)): """ Fetches the counts of user retention percentages of the bot for previous months """ range_value, message = ChatHistory.user_retention_range( current_user.get_bot(), month ) return {"data": range_value, "message": message}
async def chat_history( sender: Text, month: HistoryMonth = 1,current_user: User = Depends(auth.get_current_user) ): """ Fetches the list of conversation with the agent by particular user """ history, message = ChatHistory.fetch_chat_history(current_user.get_bot(), sender, month) return {"data": {"history": list(history)}, "message": message}
async def calculate_retention(month: HistoryMonth = 1, current_user: User = Depends(auth.get_current_user)): """ Fetches the user retention percentage of the bot """ retention_count, message = ChatHistory.user_retention( current_user.get_bot(), month ) return {"data": retention_count, "message": message}
async def complete_conversation_trend(month: HistoryMonth = 6, current_user: User = Depends(auth.get_current_user)): """ Fetches the counts of successful conversations of the bot for previous months """ range_value, message = ChatHistory.successful_conversation_range( current_user.get_bot(), month ) return {"data": range_value, "message": message}
async def complete_conversations(month: HistoryMonth = 1, current_user: User = Depends(auth.get_current_user)): """ Fetches the number of successful conversations of the bot, which had no fallback """ conversation_count, message = ChatHistory.successful_conversations( current_user.get_bot(), month ) return {"data": conversation_count, "message": message}
async def user_with_metrics( month: HistoryMonth = 1, current_user: User = Depends(auth.get_current_user)): """ Fetches the list of user who has conversation with the agent with steps anf time """ users, message = ChatHistory.user_with_metrics( current_user.get_bot(), month ) return {"data": {"users": users}, "message": message}
async def count_engaged_users(request: ConversationFilter = ConversationFilter(), current_user: User = Depends(auth.get_current_user_and_bot)): """ Fetches the number of engaged users of the bot """ engaged_user_count, message = ChatHistory.engaged_users( current_user.get_bot(), request.month, request.engaged_users_threshold ) return {"data": engaged_user_count, "message": message}
async def engaged_users_trend(month: HistoryMonth = 6, current_user: User = Depends(auth.get_current_user)): """ Fetches the counts of engaged users of the bot for previous months """ range_value, message = ChatHistory.engaged_users_range( current_user.get_bot(), month ) return {"data": range_value, "message": message}
async def conversation_steps(month: HistoryMonth = 1, current_user: User = Depends( auth.get_current_user)): """ Fetches the number of conversation steps that took place in the chat between the users and the agent """ conversation_steps, message = ChatHistory.conversation_steps( current_user.get_bot(), month) return {"data": conversation_steps, "message": message}
async def count_engaged_users(month: HistoryMonth = 1, current_user: User = Depends( auth.get_current_user_and_bot)): """ Fetches the number of engaged users of the bot """ engaged_user_count, message = ChatHistory.engaged_users( current_user.get_bot(), month) return {"data": engaged_user_count, "message": message}
async def engaged_users_trend(request: ConversationFilter = ConversationFilter(), current_user: User = Depends(auth.get_current_user_and_bot)): """ Fetches the counts of engaged users of the bot for previous months """ range_value, message = ChatHistory.engaged_users_range( current_user.get_bot(), request.month, request.engaged_users_threshold ) return {"data": range_value, "message": message}
def test_visitor_hit_fallback_action_not_configured( self, mock_fallback_user_data, monkeypatch): def _mock_load_config(*args, **kwargs): return {"policies": [{"name": "RulePolicy"}]} monkeypatch.setattr(MongoProcessor, 'load_config', _mock_load_config) hit_fall_back, message = ChatHistory.visitor_hit_fallback( "5b029887-bed2-4bbb-aa25-bd12fda26244") assert hit_fall_back["fallback_count"] == 1 assert hit_fall_back["total_count"] == 4 assert message is None
def test_visitor_hit_fallback_nlu_fallback_configured( self, mock_fallback_user_data): steps = [{ "name": "nlu_fallback", "type": "INTENT" }, { "name": "utter_please_rephrase", "type": "BOT" }] rule = {'name': 'fallback_rule', 'steps': steps, 'type': 'RULE'} MongoProcessor().add_complex_story( rule, "5b029887-bed2-4bbb-aa25-bd12fda26244", 'test') hit_fall_back, message = ChatHistory.visitor_hit_fallback( "5b029887-bed2-4bbb-aa25-bd12fda26244") assert hit_fall_back["fallback_count"] == 2 assert hit_fall_back["total_count"] == 4 assert message is None
def test_fetch_chat_history(self, monkeypatch): def events(*args, **kwargs): json_data = json.load( open("tests/testing_data/history/conversation.json")) return json_data['events'], None monkeypatch.setattr(ChatHistory, "fetch_user_history", events) history, message = ChatHistory.fetch_chat_history( sender="5e564fbcdcf0d5fad89e3acd", bot="tests") assert len(history) == 12 assert history[0]["event"] assert history[0]["time"] assert history[0]["date"] assert history[0]["text"] assert history[0]["is_exists"] == False or history[0]["is_exists"] assert history[0]["intent"] assert history[0]["confidence"] assert message is None
def test_fetch_chat_users_empty(self, mock_mongo_client_empty): users = ChatHistory.fetch_chat_users(bot="tests") assert len(users) == 2
def test_fetch_chat_users_db_error(self, mock_mongo_processor): with pytest.raises(Exception): users = ChatHistory.fetch_chat_users(bot="tests") assert len(users) == 0
def test_flatten_conversation_range(self, mock_mongo_client): f_count, message = ChatHistory.flatten_conversations("tests") assert f_count["conversation_data"] == [] assert message is None
def test_flatten_conversation_error(self, mock_mongo_processor): with pytest.raises(Exception): f_count, message = ChatHistory.flatten_conversations("tests") assert not f_count assert message is None
def test_fallback_range(self, mock_mongo_client): f_count, message = ChatHistory.fallback_count_range("tests") assert f_count["fallback_counts"] == {} assert message is None
def test_fallback_range_error(self, mock_mongo_processor): with pytest.raises(Exception): f_count, message = ChatHistory.fallback_count_range("tests") assert not f_count assert message is None
def test_user_retention_range(self, mock_mongo_client): retention, message = ChatHistory.user_retention_range("tests") assert retention['retention_range'] == {} assert message is None
def test_user_retention_range_error(self, mock_mongo_processor): with pytest.raises(Exception): retention, message = ChatHistory.user_retention_range("tests") assert not retention assert message is None
def test_successful_conversation_range(self, mock_mongo_client): conversation_steps, message = ChatHistory.successful_conversation_range( "tests") assert conversation_steps["success_conversation_range"] == {} assert message is None