original_docstring: 
          Retrieves the flattened conversation data of the bot
          :param collection: collection to connect to
          :param month: default is 3 months
          :param sort_by_date: This flag sorts the records by timestamp if set to True
          :return: dictionary of the bot users and their conversation data
          
docstring: Retrieves the flattened conversation data of the bot
docstring_tokens: Retrieves the flattened conversation data of the bot
code: def flatten_conversations(collection: Text, month: int = 3, sort_by_date: bool = True):
  
          """
          Retrieves the flattened conversation data of the bot
          :param collection: collection to connect to
          :param month: default is 3 months
          :param sort_by_date: This flag sorts the records by timestamp if set to True
          :return: dictionary of the bot users and their conversation data
          """
  
          client, message = HistoryProcessor.get_mongo_connection()
          message = ' '.join([message, f', collection: {collection}'])
          with client as client:
              db = client.get_database()
              conversations = db.get_collection(collection)
              user_data = []
              try:
  
                  user_data = list(
                      conversations.aggregate(
                          [{"$match": {"latest_event_time": {"$gte": Utility.get_timestamp_previous_month(month)}}},
                           {"$unwind": {"path": "$events", "includeArrayIndex": "arrayIndex"}},
                           {"$match": {"$or": [{"events.event": {"$in": ['bot', 'user']}},
                           {"$and": [{"events.event": "action"},
                           {"events.name": {"$nin": ['action_listen', 'action_session_start']}}]}]}},
                           {"$match": {"events.timestamp": {"$gte": Utility.get_timestamp_previous_month(month)}}},
                           {"$group": {"_id": "$sender_id", "events": {"$push": "$events"},
                              "allevents": {"$push": "$events"}}},
                           {"$unwind": "$events"},
                           {"$match": {"events.event": 'user'}},
                           {"$group": {"_id": "$_id", "events": {"$push": "$events"}, "user_array":
                           {"$push": "$events"}, "all_events": {"$first": "$allevents"}}},
                           {"$unwind": "$events"},
                           {"$project": {"user_input": "$events.text", "intent": "$events.parse_data.intent.name",
                              "message_id": "$events.message_id",
                              "timestamp": "$events.timestamp",
                              "confidence": "$events.parse_data.intent.confidence",
                              "action_bot_array": {
                              "$cond": [{"$gte": [{"$indexOfArray": ["$all_events", {"$arrayElemAt":
                              ["$user_array", {"$add": [{"$indexOfArray": ["$user_array","$events"]}, 1]}]}]},
                           {"$indexOfArray": ["$all_events", "$events"]}]},
                           {"$slice": ["$all_events", {"$add": [{"$indexOfArray":["$all_events", "$events"]}, 1]},
                           {"$subtract": [{"$subtract": [{"$indexOfArray": ["$all_events", {"$arrayElemAt":
                              ["$user_array", {"$add": [{"$indexOfArray": ["$user_array", "$events"]}, 1]}]}]},
                           {"$indexOfArray": ["$all_events", "$events"]}]}, 1]}]}, {"$slice": ["$all_events",
                           {"$add": [{"$indexOfArray": ["$all_events", "$events"]}, 1]}, 100]}]}}},
                           {"$addFields": {"t_stamp": {"$toDate": {"$multiply": ["$timestamp", 1000]}}}},
                           {"$project": {"user_input": 1, "intent": 1, "confidence": 1, "action": "$action_bot_array.name"
                            , "timestamp": "$t_stamp", "bot_response": "$action_bot_array.text", "sort": {
                               "$cond": {"if": sort_by_date, "then": "$t_stamp", "else": "_id"}}}},
                           {"$sort": {"sort": -1}},
                           {"$project": {"user_input": 1, "intent": 1, "confidence": 1, "action": 1,
                                         "timestamp": {'$dateToString': {'format': "%d-%m-%Y %H:%M:%S", 'date': '$timestamp'}}, "bot_response": 1}}
                           ], allowDiskUse=True))
              except Exception as e:
                  logger.error(e)
                  message = '\n'.join([message, str(e)])
  
              return (
                  {"conversation_data": user_data},
                  message
              )
code_tokens: def flatten_conversations ( collection : Text , month : int = 3 , sort_by_date : bool = True ) : client , message = HistoryProcessor . get_mongo_connection ( ) message = ' ' . join ( [ message , f', collection: {collection}' ] ) with client as client : db = client . get_database ( ) conversations = db . get_collection ( collection ) user_data = [ ] try : user_data = list ( conversations . aggregate ( [ { "$match" : { "latest_event_time" : { "$gte" : Utility . get_timestamp_previous_month ( month ) } } } , { "$unwind" : { "path" : "$events" , "includeArrayIndex" : "arrayIndex" } } , { "$match" : { "$or" : [ { "events.event" : { "$in" : [ 'bot' , 'user' ] } } , { "$and" : [ { "events.event" : "action" } , { "events.name" : { "$nin" : [ 'action_listen' , 'action_session_start' ] } } ] } ] } } , { "$match" : { "events.timestamp" : { "$gte" : Utility . get_timestamp_previous_month ( month ) } } } , { "$group" : { "_id" : "$sender_id" , "events" : { "$push" : "$events" } , "allevents" : { "$push" : "$events" } } } , { "$unwind" : "$events" } , { "$match" : { "events.event" : 'user' } } , { "$group" : { "_id" : "$_id" , "events" : { "$push" : "$events" } , "user_array" : { "$push" : "$events" } , "all_events" : { "$first" : "$allevents" } } } , { "$unwind" : "$events" } , { "$project" : { "user_input" : "$events.text" , "intent" : "$events.parse_data.intent.name" , "message_id" : "$events.message_id" , "timestamp" : "$events.timestamp" , "confidence" : "$events.parse_data.intent.confidence" , "action_bot_array" : { "$cond" : [ { "$gte" : [ { "$indexOfArray" : [ "$all_events" , { "$arrayElemAt" : [ "$user_array" , { "$add" : [ { "$indexOfArray" : [ "$user_array" , "$events" ] } , 1 ] } ] } ] } , { "$indexOfArray" : [ "$all_events" , "$events" ] } ] } , { "$slice" : [ "$all_events" , { "$add" : [ { "$indexOfArray" : [ "$all_events" , "$events" ] } , 1 ] } , { "$subtract" : [ { "$subtract" : [ { "$indexOfArray" : [ "$all_events" , { "$arrayElemAt" : [ "$user_array" , { "$add" : [ { "$indexOfArray" : [ "$user_array" , "$events" ] } , 1 ] } ] } ] } , { "$indexOfArray" : [ "$all_events" , "$events" ] } ] } , 1 ] } ] } , { "$slice" : [ "$all_events" , { "$add" : [ { "$indexOfArray" : [ "$all_events" , "$events" ] } , 1 ] } , 100 ] } ] } } } , { "$addFields" : { "t_stamp" : { "$toDate" : { "$multiply" : [ "$timestamp" , 1000 ] } } } } , { "$project" : { "user_input" : 1 , "intent" : 1 , "confidence" : 1 , "action" : "$action_bot_array.name" , "timestamp" : "$t_stamp" , "bot_response" : "$action_bot_array.text" , "sort" : { "$cond" : { "if" : sort_by_date , "then" : "$t_stamp" , "else" : "_id" } } } } , { "$sort" : { "sort" : - 1 } } , { "$project" : { "user_input" : 1 , "intent" : 1 , "confidence" : 1 , "action" : 1 , "timestamp" : { '$dateToString' : { 'format' : "%d-%m-%Y %H:%M:%S" , 'date' : '$timestamp' } } , "bot_response" : 1 } } ] , allowDiskUse = True ) ) except Exception as e : logger . error ( e ) message = '\n' . join ( [ message , str ( e ) ] ) return ( { "conversation_data" : user_data } , message )
short_docstring: Retrieves the flattened conversation data of the bot
short_docstring_tokens: Retrieves the flattened conversation data of the bot
comment: """
          Retrieves the flattened conversation data of the bot
          :param collection: collection to connect to
          :param month: default is 3 months
          :param sort_by_date: This flag sorts the records by timestamp if set to True
          :return: dictionary of the bot users and their conversation data
          """