original_docstring: 
          Computes the trend for user retention percentages
  
          :param collection: collection to connect to
          :param month: default is 6 months
          :return: dictionary of user retention percentages for the previous months
          
docstring: Computes the trend for user retention percentages
docstring_tokens: Computes the trend for user retention percentages
code: def user_retention_range(collection: Text, month: int = 6):
  
          """
          Computes the trend for user retention percentages
  
          :param collection: collection to connect to
          :param month: default is 6 months
          :return: dictionary of user retention percentages for the previous months
          """
  
          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)
              total = []
              repeating_users = []
              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}}
                      ]))
  
                  repeating_users = list(
                      conversations.aggregate([{"$unwind": {"path": "$events", "includeArrayIndex": "arrayIndex"}},
                                               {"$match": {
                                                   "events.name": {"$regex": ".*session_start*.", "$options": "$i"}}},
                                               {"$group": {"_id": '$sender_id', "count": {"$sum": 1},
                                                           "latest_event_time": {"$first": "$latest_event_time"}}},
                                               {"$match": {"count": {"$gte": 2}}},
                                               {"$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}}
                                               ]))
              except Exception as e:
                  logger.error(e)
                  message = '\n'.join([message, str(e)])
              total_users = {d['_id']: d['count'] for d in total}
              repeat_users = {d['_id']: d['count'] for d in repeating_users}
              retention = {k: 100 * (repeat_users[k] / total_users[k]) for k in repeat_users.keys()}
              return (
                  {"retention_range": retention},
                  message
              )
code_tokens: def user_retention_range ( collection : Text , month : int = 6 ) : 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 ) total = [ ] repeating_users = [ ] 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 } } ] ) ) repeating_users = list ( conversations . aggregate ( [ { "$unwind" : { "path" : "$events" , "includeArrayIndex" : "arrayIndex" } } , { "$match" : { "events.name" : { "$regex" : ".*session_start*." , "$options" : "$i" } } } , { "$group" : { "_id" : '$sender_id' , "count" : { "$sum" : 1 } , "latest_event_time" : { "$first" : "$latest_event_time" } } } , { "$match" : { "count" : { "$gte" : 2 } } } , { "$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 } } ] ) ) except Exception as e : logger . error ( e ) message = '\n' . join ( [ message , str ( e ) ] ) total_users = { d [ '_id' ] : d [ 'count' ] for d in total } repeat_users = { d [ '_id' ] : d [ 'count' ] for d in repeating_users } retention = { k : 100 * ( repeat_users [ k ] / total_users [ k ] ) for k in repeat_users . keys ( ) } return ( { "retention_range" : retention } , message )
short_docstring: Computes the trend for user retention percentages
short_docstring_tokens: Computes the trend for user retention percentages
comment: """
          Computes the trend for user retention percentages
  
          :param collection: collection to connect to
          :param month: default is 6 months
          :return: dictionary of user retention percentages for the previous months
          """