def get_repeat_action_user_count(db=db, timedelta=timedelta(days=30)):
 start_date = (timezone.now() - timedelta)
 pipeline = [{'$match': {'date': {'$gt': start_date}}}, {'$group': {'_id': '$user', 'nodelog_id': {'$addToSet': '$_id'}}}]
 user_nodelog = db.nodelog.aggregate(pipeline)['result']
 repeat_action_count = 0
 repeat_action_user_age = []
 for i in user_nodelog:
  if i['_id']:
   user_id = i['_id']
   nodelog_id = i['nodelog_id']
   nodelogs = db.nodelog.find({'_id': {'$in': nodelog_id}}).sort([('date', 1)])
   repeat_action_date = {}
   for nodelog in nodelogs:
    action = nodelog['action']
    date = nodelog['date']
    if (action not in repeat_action_date):
     repeat_action_date[action] = date
    elif (abs((date - repeat_action_date[action]).total_seconds()) < 3):
     repeat_action_date[action] = date
    else:
     repeat_action_count += 1
     date_registered = db.user.find({'_id': user_id}).next()['date_registered']
     age = (date - date_registered).days
     repeat_action_user_age.append(age)
     break
 return {'repeat_action_count': repeat_action_count, 'repeat_action_age': repeat_action_user_age}

def get_repeat_action_user_count ( db = db , timedelta = timedelta ( days = 30 ) ) : start_date = ( timezone . now ( ) - timedelta ) pipeline = [ { '$match' : { 'date' : { '$gt' : start_date } } } , { '$group' : { '_id' : '$user' , 'nodelog_id' : { '$addToSet' : '$_id' } } } ] user_nodelog = db . nodelog . aggregate ( pipeline ) [ 'result' ] repeat_action_count = 0 repeat_action_user_age = [ ] for i in user_nodelog : if i [ '_id' ] : user_id = i [ '_id' ] nodelog_id = i [ 'nodelog_id' ] nodelogs = db . nodelog . find ( { '_id' : { '$in' : nodelog_id } } ) . sort ( [ ( 'date' , 1 ) ] ) repeat_action_date = { } for nodelog in nodelogs : action = nodelog [ 'action' ] date = nodelog [ 'date' ] if ( action not in repeat_action_date ) : repeat_action_date [ action ] = date elif ( abs ( ( date - repeat_action_date [ action ] ) . total_seconds ( ) ) < 3 ) : repeat_action_date [ action ] = date else : repeat_action_count += 1 date_registered = db . user . find ( { '_id' : user_id } ) . next ( ) [ 'date_registered' ] age = ( date - date_registered ) . days repeat_action_user_age . append ( age ) break return { 'repeat_action_count' : repeat_action_count , 'repeat_action_age' : repeat_action_user_age }
get the number of users that have repetitive actions during the last month .