def get_multi_product_metrics(db=db, timedelta=timedelta(days=365)):
 start_date = (timezone.now() - timedelta)
 pipeline = [{'$match': {'date': {'$gt': start_date}}}, {'$group': {'_id': '$user', 'node_id': {'$addToSet': '$params.node'}, 'action': {'$addToSet': '$action'}}}]
 user_nodes = db.nodelog.aggregate(pipeline)['result']
 multi_product_count = 0
 cross_product_count = 0
 multi_action_count = 0
 for user_node in user_nodes:
  if user_node['_id']:
   user_id = user_node['_id']
   node_id = user_node['node_id']
   products = []
   nodes = db.node.find({'_id': {'$in': node_id}})
   for node in nodes:
    products.append(get_entry_point(node['system_tags']))
   if (len(set(products)) > 1):
    multi_product_count += 1
   user = db.user.find_one({'_id': user_id})
   user_entry_point = get_entry_point(user['system_tags'])
   for product in products:
    if (user_entry_point != product):
     cross_product_count += 1
     break
   if (len(set(user_node['action'])) > 1):
    multi_action_count += 1
 return {'multi_product_count': multi_product_count, 'cross_product_count': cross_product_count, 'multi_action_count': multi_action_count}

def get_multi_product_metrics ( db = db , timedelta = timedelta ( days = 365 ) ) : start_date = ( timezone . now ( ) - timedelta ) pipeline = [ { '$match' : { 'date' : { '$gt' : start_date } } } , { '$group' : { '_id' : '$user' , 'node_id' : { '$addToSet' : '$params.node' } , 'action' : { '$addToSet' : '$action' } } } ] user_nodes = db . nodelog . aggregate ( pipeline ) [ 'result' ] multi_product_count = 0 cross_product_count = 0 multi_action_count = 0 for user_node in user_nodes : if user_node [ '_id' ] : user_id = user_node [ '_id' ] node_id = user_node [ 'node_id' ] products = [ ] nodes = db . node . find ( { '_id' : { '$in' : node_id } } ) for node in nodes : products . append ( get_entry_point ( node [ 'system_tags' ] ) ) if ( len ( set ( products ) ) > 1 ) : multi_product_count += 1 user = db . user . find_one ( { '_id' : user_id } ) user_entry_point = get_entry_point ( user [ 'system_tags' ] ) for product in products : if ( user_entry_point != product ) : cross_product_count += 1 break if ( len ( set ( user_node [ 'action' ] ) ) > 1 ) : multi_action_count += 1 return { 'multi_product_count' : multi_product_count , 'cross_product_count' : cross_product_count , 'multi_action_count' : multi_action_count }
get the number of users using 2+ products within a period of time .