def successful_conversations(bot: Text, month: int = 1): """ Counts the number of successful conversations of the bot :param bot: bot id :param month: default is current month and max is last 6 months :return: number of successful conversations """ fallback_action, nlu_fallback_action = Utility.load_fallback_actions(bot) client, database, collection, message = ChatHistory.get_mongo_connection(bot) with client as client: db = client.get_database(database) conversations = db.get_collection(collection) total = [] fallback_count = [] try: total = list( conversations.aggregate([{"$match": {"latest_event_time": { "$gte": Utility.get_timestamp_previous_month(month)}}}, {"$group": {"_id": None, "count": {"$sum": 1}}}, {"$project": {"_id": 0, "count": 1}} ])) except Exception as e: message = str(e) try: fallback_count = list( conversations.aggregate([ {"$unwind": {"path": "$events", "includeArrayIndex": "arrayIndex"}}, {"$match": {"events.timestamp": {"$gte": Utility.get_timestamp_previous_month(month)}}}, {"$match": {'$or': [{"events.name": fallback_action}, {"events.name": nlu_fallback_action}]}}, {"$group": {"_id": "$sender_id"}}, {"$group": {"_id": None, "count": {"$sum": 1}}}, {"$project": {"_id": 0, "count": 1}} ])) except Exception as e: message = str(e) if not total: total_count = 0 else: total_count = total[0]['count'] if total[0]['count'] else 0 if not fallback_count: fallbacks_count = 0 else: fallbacks_count = fallback_count[0]['count'] if fallback_count[0]['count'] else 0 return ( {"successful_conversations": total_count-fallbacks_count}, message )
def fallback_count_range(bot: Text, month: int = 6): """ Computes the trend for fallback counts :param bot: bot id :param month: default is 6 months :return: dictionary of fallback counts for the previous months """ fallback_action, nlu_fallback_action = Utility.load_fallback_actions(bot) client, database, collection, message = ChatHistory.get_mongo_connection(bot) with client as client: db = client.get_database(database) conversations = db.get_collection(collection) fallback_counts = [] try: fallback_counts = list( conversations.aggregate([{"$unwind": {"path": "$events"}}, {"$match": {"events.event": "action", "events.timestamp": { "$gte": Utility.get_timestamp_previous_month( month)}}}, {"$match": {'$or': [{"events.name": fallback_action}, {"events.name": nlu_fallback_action}]}}, {"$addFields": {"month": { "$month": {"$toDate": {"$multiply": ["$events.timestamp", 1000]}}}}}, {"$group": {"_id": "$month", "count": {"$sum": 1}}}, {"$project": {"_id": 1, "count": 1}} ])) action_counts = list( conversations.aggregate([{"$unwind": {"path": "$events"}}, {"$match": {"$and": [{"events.event": "action"}, {"events.name": {"$nin": ['action_listen', 'action_session_start']}}]}}, {"$match": {"events.timestamp": { "$gte": Utility.get_timestamp_previous_month(month)}}}, {"$addFields": {"month": { "$month": {"$toDate": {"$multiply": ["$events.timestamp", 1000]}}}}}, {"$group": {"_id": "$month", "total_count": {"$sum": 1}}}, {"$project": {"_id": 1, "total_count": 1}} ])) except Exception as e: message = str(e) action_count = {d['_id']: d['total_count'] for d in action_counts} fallback_count = {d['_id']: d['count'] for d in fallback_counts} final_trend = {k: [fallback_count.get(k), action_count.get(k)] for k in list(fallback_count.keys())} return ( {"fallback_counts": final_trend}, message )
def successful_conversation_range(bot: Text, month: int = 6): """ Computes the trend for successful conversation count :param bot: bot id :param month: default is 6 months :return: dictionary of counts of successful bot conversations for the previous months """ fallback_action, nlu_fallback_action = Utility.load_fallback_actions(bot) client, database, collection, message = ChatHistory.get_mongo_connection(bot) with client as client: db = client.get_database(database) conversations = db.get_collection(collection) total = [] fallback_count = [] try: total = list( conversations.aggregate([{"$match": {"latest_event_time": { "$gte": Utility.get_timestamp_previous_month(month)}}}, {"$addFields": {"month": {"$month": {"$toDate": {"$multiply": ["$latest_event_time", 1000]}}}}}, {"$group": {"_id": "$month", "count": {"$sum": 1}}}, {"$project": {"_id": 1, "count": 1}} ])) fallback_count = list( conversations.aggregate([ {"$unwind": {"path": "$events", "includeArrayIndex": "arrayIndex"}}, {"$match": {"events.timestamp": {"$gte": Utility.get_timestamp_previous_month(month)}}}, {"$match": {'$or': [{"events.name": fallback_action}, {"events.name": nlu_fallback_action}]}}, {"$addFields": {"month": {"$month": {"$toDate": {"$multiply": ["$events.timestamp", 1000]}}}}}, {"$group": {"_id": {"month": "$month", "sender_id": "$sender_id"}}}, {"$group": {"_id": "$_id.month", "count": {"$sum": 1}}}, {"$project": {"_id": 1, "count": 1}} ])) except Exception as e: message = str(e) total_users = {d['_id']: d['count'] for d in total} final_fallback = {d['_id']: d['count'] for d in fallback_count} final_fallback = {k: final_fallback.get(k, 0) for k in total_users.keys()} success = {k: total_users[k] - final_fallback[k] for k in total_users.keys()} return ( {"success_conversation_range": success}, message )
def visitor_hit_fallback(bot: Text, month: int = 1): """ Counts the number of times, the agent was unable to provide a response to users :param bot: bot id :param month: default is current month and max is last 6 months :return: list of visitor fallback """ fallback_action, nlu_fallback_action = Utility.load_fallback_actions(bot) client, database, collection, message = ChatHistory.get_mongo_connection(bot) default_actions = Utility.load_default_actions() with client as client: db = client.get_database(database) conversations = db.get_collection(collection) values = [] try: values = list(conversations.aggregate([{"$unwind": "$events"}, {"$match": {"events.event": "action", "events.name": {"$nin": default_actions}, "events.timestamp": {"$gte": Utility.get_timestamp_previous_month(month)}}}, {"$group": {"_id": "$sender_id", "total_count": {"$sum": 1}, "events": {"$push": "$events"}}}, {"$unwind": "$events"}, {"$match": {'$or': [{"events.name": fallback_action}, {"events.name": nlu_fallback_action}]}}, {"$group": {"_id": None, "total_count": {"$first": "$total_count"}, "fallback_count": {"$sum": 1}}}, {"$project": {"total_count": 1, "fallback_count": 1, "_id": 0}} ], allowDiskUse=True)) except Exception as e: message = str(e) if not values: fallback_count = 0 total_count = 0 else: fallback_count = values[0]['fallback_count'] if values[0]['fallback_count'] else 0 total_count = values[0]['total_count'] if values[0]['total_count'] else 0 return ( {"fallback_count": fallback_count, "total_count": total_count}, message, )