def getFullInfluenceScore():
    partialScoreCollection = getClusterInfluencerScoreCollection()
    userClusterContributionCollection = getUserClusterContributionCollection()

    fbfriends = getFriendsCollection()
    fullScoreCollection = getFullInfluenceScoreCollection()

    for friend in fbfriends.find():
        fid = friend['id']
        partial = partialScoreCollection.find_one({'_id': fid})
        contribution = userClusterContributionCollection.find_one({'_id': fid})

        score = []

        for i in range(len(contribution['contribution'])):
            avg = 1.0
            try:
                avg *= sum(partial['cluster'][str(i)]) / len(
                    partial['cluster'][str(i)])
            except ZeroDivisionError:
                avg = 0.0
            score.append(avg * contribution['contribution'][i])

        if len(score) > 0:
            document = {}
            document['_id'] = fid
            document['name'] = friend['name']
            document['score'] = score
            fullScoreCollection.insert(document)
            print friend['name'], score
def getFullInfluenceScore():
	partialScoreCollection = getClusterInfluencerScoreCollection()
	userClusterContributionCollection = getUserClusterContributionCollection()

	fbfriends = getFriendsCollection()
	fullScoreCollection = getFullInfluenceScoreCollection()

	for friend in fbfriends.find():
		fid = friend['id']
		partial = partialScoreCollection.find_one({'_id' : fid})
		contribution = userClusterContributionCollection.find_one({'_id' : fid})

		score = []

		for i in range(len(contribution['contribution'])):
			avg = 1.0
			try:
				avg *= sum(partial['cluster'][str(i)])/len(partial['cluster'][str(i)])
			except ZeroDivisionError:
				avg = 0.0
			score.append(avg*contribution['contribution'][i])

		if len(score) > 0:
			document = {}
			document['_id'] = fid
			document['name'] = friend['name']
			document['score'] = score
			fullScoreCollection.insert(document)
			print friend['name'], score
def clusterLevelResults():
    scoreCollection = getFullInfluenceScoreCollection()
    clusterResCollection = getclusterLevelResultCollection()

    numCluster = len(scoreCollection.find_one()['score'])
    clusterResCollection.drop()
    for i in range(numCluster):
        doc = {}
        i = str(i)
        doc['_id'] = i
        doc['users'] = []
        clusterResCollection.insert(doc)

    for scores in scoreCollection.find():
        fid = scores['_id']
        name = scores['name']
        userScore = scores['score']
        for i in range(len(userScore)):
            doc = {}
            doc['id'] = fid
            doc['name'] = name
            doc['score'] = userScore[i]
            cluster = str(i)
            print i, doc
            clusterResCollection.update({'_id': cluster}, {
                '$push': {
                    'users': {
                        '$each': [doc],
                        '$sort': {
                            'score': -1
                        },
                        '$slice': -1000
                    }
                }
            },
                                        upsert=False)
def clusterLevelResults():
    scoreCollection = getFullInfluenceScoreCollection()
    clusterResCollection = getclusterLevelResultCollection()

    numCluster = len(scoreCollection.find_one()['score'])
    clusterResCollection.drop()
    for i in range(numCluster):
        doc = {}
        i = str(i)
        doc['_id'] = i
        doc['users'] = []
        clusterResCollection.insert(doc)

    for scores in scoreCollection.find():
        fid = scores['_id']
        name = scores['name']
        userScore = scores['score']
        for i in range(len(userScore)):
            doc = {}
            doc['id'] = fid
            doc['name'] = name
            doc['score'] = userScore[i]
            cluster = str(i)
            print i, doc
            clusterResCollection.update({'_id': cluster},
                                        {
                                            '$push': {
                                                'users': {
                                                    '$each': [doc],
                                                    '$sort': {'score': -1},
                                                    '$slice': -1000
                                                }
                                            }

                                        }
                                        , upsert=False)