def main():
    try:
        # print to a file
        # f = file('log.txt', 'w')
        # sys.stdout = f

        # get MongoDB client and set it in DataService
        client = MongoClient('localhost', 27017) # or client = MongoClient('mongodb://localhost:27017/')
        DataService.init(client)

        # persist_download_history_and_app_info()
        user_download_history = load_user_download_history()
        app_info = load_app_info()
        persist_top_5_apps_for_app(user_download_history, app_info)

        # print for testing
        #print "app_info: \n  " + str(load_top_5_apps('C10107104'))

    except Exception as e:
        print "Exception! Go fix it!"
        print e

    finally:
        # clean up work
        if 'client' in locals():
            client.close()
Beispiel #2
0
def main():
    try:
        # print to a file
        # f = file('log.txt', 'w')
        # sys.stdout = f

        # get MongoDB client and set it in DataService
        client = MongoClient(
            'localhost',
            27017)  # or client = MongoClient('mongodb://localhost:27017/')
        DataService.init(client)

        # persist_download_history_and_app_info()
        user_download_history = load_user_download_history()
        app_info = load_app_info()
        persist_top_5_apps_for_app(user_download_history, app_info)

        # print for testing
        #print "app_info: \n  " + str(load_top_5_apps('C10107104'))

    except Exception as e:
        print "Exception! Go fix it!"
        print e

    finally:
        # clean up work
        if 'client' in locals():
            client.close()
Beispiel #3
0
def main():
    try:
        client = MongoClient('localhost', 27017)
        DataService.init(client)

        user_download_history = DataService.retrieve_user_download_history()
        app_info = DataService.retrieve_app_info()
        for app in app_info.keys():
            calculate_top_5(app, user_download_history.values())
    except Exception as e:
        print(e)
    finally:
        if 'client' in locals():
            client.close()
Beispiel #4
0
def main():
    try:
        client = MongoClient('localhost', 27017)
        DataService.init(client)

        user_download_history = DataService.retrieve_user_download_history()
        # calculate_top_5('C10063783', user_download_history.values())
        app_info = DataService.retrieve_app_info()
        for app in app_info.keys():
            calculate_top_5(app, user_download_history.values())

    except Exception as e:
        print(e)
    finally:
        if 'client' in locals():
            client.close()
def main():
    try:
        #get MongoDB client and set it in DataService
        client = MongoClient('localhost', 27017)
        DataService.init(client)
        #work flow
        user_download_history = DataService.retrieve_user_download_history()
        app_info = DataService.retrieve_app_info()
        for app in app_info.keys():
            calculate_top_5(app, user_download_history.values())
    except Exception as e:
        print(e)
    finally:
        #clean up work
        if 'client' in locals():
            client.close()
def main():
	try:
		client = MongoClient('localhost',27017)
		DataService.init(client)
		apps = DataService.retrieve_appinfo()
		#work flow

		for app in apps.keys():
			user_download_history = DataService.retrieve_user_download_history()
			top_5_app = calculate_Top_5(app, user_download_history.values())
			DataService.update_app_info({"app_id": app},{"$set": {"Top 5": top_5_app}})
	except Exception as e:
		print(e)
	finally:
		#clean up work
		if 'client' in locals():
			client.close()
Beispiel #7
0
def main():
	try:
		#get MongoDB client and set it in DataService
		client = MongoClient('localhost', 27017)
		DataService.init(client)

		#work flow
		user_download_history = DataService.retrieve_user_download_history()
		#calculate_top_5('C10107104', user_download_history.values())

		app_info = DataService.retrieve_app_info()
		for app in app_info.keys():
			calculate_top_5(app, user_download_history.values())

	except Exception, e:
		print("Exception detected:")
		print(e)
def main():
    try:
        start = time.clock()
        client = MongoClient('localhost', 27017)
        DataService.init(client)

        user_download_history = DataService.retrieve_user_download_history()
        apps = DataService.retrieve_app_info()
        for app in apps.keys():
            calculate_top_5(app, user_download_history.values())
        end = time.clock()
        print "time: " + str(end - start)
    except Exception as e:
        print e
    finally:
        if 'client' in locals():
            client.close()
Beispiel #9
0
def main():
	try:
		# get MongoDB client and set it in DataService
		client = MongoClient('localhost', 27017)
		DataService.init(client)

		# work flow
		user_download_history = DataService.retrieve_user_download_history()
		app_info = DataService.retrieve_app_info()
		for app in app_info.keys():
			calculate_top_5(app, user_download_history.values())
	except Exception as e:
		print e
	finally:
		# clean up
		if 'client' in locals():
			client.close()
def main():
    try:
        client = MongoClient('localhost', 27017)
        DataService.init(client)

        user_download_history = DataService.retrieve_user_download_history()
        all_app_id = DataService.get_all_app_id()

        for one_id in all_app_id:
            calculate_top_k(one_id, user_download_history.values(), 5)
        
        recommendations = DataService.retrieve_recommended_items()
        generate_recommendations(user_download_history, recommendations)

    except Exception as e:
        print(e)
    finally:
        if 'client' in locals():
            client.close()	
Beispiel #11
0
def main():
	start = time.clock()
	try:
		client = MongoClient('localhost', 27017)
		DataService.init(client)

		user_download_history = DataService.retrieve_user_download_history()
		app_info = DataService.retrieve_app_info()
		for app in app_info.keys():
			calculate_top_5(app, user_download_history.values())

	except Exception as e:
		print("Exception detected:")
		print(e)

	finally:
		if 'client' in locals():
			client.close()

	end = time.clock()

	print "time eplapsed = " + str(end - start)
Beispiel #12
0
def main():
    try:
        client = MongoClient('localhost', 27017)
        DataService.init(client)

        
        # work flow
        user_download_history = DataService.retrieve_user_download_history()
        app_info = DataService.retrieve_app_info()

        # print("Calculating Top 5 related apps for apps...")
        # start = time.clock()
        # for app in app_info.keys():
        #     # print(app)
        #     calculate_app_top_5(app, user_download_history.values())
        # # calculate_top_5('C10107104', user_download_history.values())
        # end = time.clock()

        # print "time elapsed = " + str(end - start)

        print("Calculating Top 5 recommended apps for users...")

        start = time.clock()
        for user_id, download_history in user_download_history.iteritems():
            # print(app)
            calculate_user_top_5(user_id, download_history, user_download_history.values())
        # calculate_top_5('C10107104', user_download_history.values())
        end = time.clock()

        print "time elapsed = " + str(end - start)

    except Exception as e:
        print(e)
    finally:
        # clean up work
        if 'client' in locals():
            client.close()
Beispiel #13
0
def main():
	try:
		start = time.clock()
		client = MongoClient('localhost', 27017)
		DataService.init(client)

		user_download_history = DataService.retrieve_user_download_history()
		app_ids = DataService.retrieve_all_app_id()

		top_5_apps = {}
		
		for appp in app_ids:
			calculate_top_5(appp, user_download_history.values(),top_5_apps)

		for users in user_download_history.keys():
			users_app = user_download_history[users]
			all_possible_app = {}
			#sorted_tups = calculate_top_5_user(users, user_download_history,top_5_apps,all_possible_app)
			for used_app_id in users_app:
				top_5_possible_app = top_5_apps[used_app_id]
				for i in top_5_possible_app:
					if i in all_possible_app:
						all_possible_app[i] = all_possible_app[i] + 1
					else:
						all_possible_app[i] = 1
			sorted_tups = sorted(all_possible_app.items(), key = operator.itemgetter(1), reverse = True)
			top_5_user_app = [sorted_tups[0][0], sorted_tups[1][0], sorted_tups[2][0], sorted_tups[3][0],sorted_tups[4][0]]
			print("top_5_app " + str(users) + ":\t" + str(top_5_user_app))

		end = time.clock()
		print "time elapsed = " + str(end - start)
	except Exception as e:
		print(e)
	finally:
		if 'client' in locals():
			client.close()
Beispiel #14
0
def main():
    noInput = True # weather user input a right command
    processes = []
    try:
        while noInput:
            flag = input("If you want to calculate each app's recommend apps" 
                         + " please press 1, If you want to calculate"
                         + " recommend apps for each user, please press"
                         + " 2: ")
            if flag == 1:
                noInput = False

                # start time
                start = time.clock()

                # set the shared dictionary
                rslt = {}
                result = multiprocessing.Manager().dict(rslt)

                # control the number of apps each process deals with
                count = 0

                # a list of appID
                keys = []

                # get client and init it with DataService class
                client = MongoClient("localhost", 27017)
                DataService.init(client)

                # start analyzing
                userDownloadHistory = DataService.retrieveUserDownloadHistory()
                allApp = DataService.retrieveAppInfo()

                length = len(allApp)
                for i in range(0, length, 500):
                    # each process will deal with 500 apps
                    p = multiprocessing.Process(target = appWrapper,
                        args = (allApp.keys()[i: min(i + 500, length)],
                            userDownloadHistory.values(), result))
                    processes.append(p)

                # set process to daemon and start
                for p in processes:
                    p.daemon = True
                    p.start()

                # main process will wait other processes
                for p in processes:
                    p.join()
                    
                # update MongoDB
                for key in result.keys():
                    DataService.updateAppInfo({"app_id": key},
                              {"$set": {"top 5 apps": result[key]}})
            elif flag == 2:
                noInput = False

                # start time
                start = time.clock()

                # set the shared dictionary
                rslt = {}
                result = multiprocessing.Manager().dict(rslt)

                # control the number of users each process deals with
                count = 0

                # a list of userID
                keys = []

                # get client and init it with DataService class
                client = MongoClient("localhost", 27017)
                DataService.init(client)

                # start analyzing
                userDownloadHistory = DataService.retrieveUserDownloadHistory()

                for key in userDownloadHistory.keys():
                    keys.append(key)
                    count += 1
                    # each process will deal with 500 users
                    if count == 500:
                        p = multiprocessing.Process(target = userWrapper,
                                args = (keys, userDownloadHistory, result))
                        processes.append(p)
                        keys = []
                        count = 0
                # the rest users
                p = multiprocessing.Process(target = userWrapper,
                        args = (keys, userDownloadHistory, result))
                processes.append(p)
                
                # set process to daemon and start
                for p in processes:
                    p.daemon = True
                    p.start()

                # main process will wait other processes
                for p in processes:
                    p.join()

                # update MongoDB
                for key in result.keys():
                    DataService.updateUserInfo({"user_id": key},
                          {"$set": {"top 5 recommended apps": result[key]}})
            else:
                print "Sorry, you press the wrong number, please try again."
    except Exception as e:
        print e
    finally:
        # close client
        if "client" in locals():
            client.close()
    end = time.clock()
    print "Finished! The elapsed time is " + str(end - start)