def updateWeightsByJson(userId, categoryWeights): print("invoked") catWeights = json.loads(categoryWeights) #print(catWeights['nature']) incStr = { "$inc": { "categories.landmark": catWeights['landmark'], "categories.nature": catWeights['nature'], "categories.shopping": catWeights['shopping'], "categories.theatre": catWeights['theatre'], "categories.restaurant": catWeights['restaurant'] } } #incStr = '''{"$inc":{"''' #for key,value in catWeights.items(): #print (key,value) #incStr += "categories."+key+":"+value #incStr +='''}''' #print(incStr) conn = MongoConnection() user_coll = conn.getUsersCollection() row = user_coll.update_one({"userId": userId}, incStr) if (row.acknowledged): print("Weights updated successfully") return row.acknowledged
def fetchPlacesbyCategoriesDuration(categories, duration): catArr = categories.lower().split(",") inQry = {"category": {"$in": catArr}} dbConn = MongoConnection() places_coll = dbConn.getPlacesCollection() places = places_coll.find(inQry) locations = [] validLocations = [] currDuration = 0 #print(places.count()) if (places): for aPlace in places: locations.append(aPlace) shuffleList(locations) for aLoc in locations: print(aLoc['name'], aLoc['duration']) if ((currDuration + aLoc['duration']) < duration): currDuration += aLoc['duration'] validLocations.append(aLoc) else: break print(currDuration) print([aPlace['name'] for aPlace in validLocations])
def fetchUser(userId): query = {"userId": userId} conn = MongoConnection() user_coll = conn.getUsersCollection() row = user_coll.find_one(query) #print (row) return row
def insertUser(name, age, gender, budget=0, duration=0, visited={}, prefCategories=""): landmark = nature = shopping = theatre = restaurant = 0 for aCat in prefCategories.split(","): #print(aCat) if (aCat == "landmark"): landmark = 1 elif (aCat == "nature"): nature = 1 elif (aCat == "shopping"): shopping = 1 elif (aCat == "theatre"): theatre = 1 else: restaurant = 1 '''print("landmark",landmark) print("nature",nature) print("shopping",shopping) print("theatre",theatre) print("restaurant",restaurant)''' userId = randint(1000, 9999) categories = { "landmark": landmark, "nature": nature, "shopping": shopping, "theatre": theatre, "restaurant": restaurant } userDetails = { "userId": userId, "name": name, "age": age, "gender": gender, "avgBudget": budget, "avgDuration": duration, "visited": visited, "categories": categories } conn = MongoConnection() print(conn.dummy()) user_coll = conn.getUsersCollection() row = user_coll.insert_one(userDetails) print(row.inserted_id) if (row.acknowledged): return userId
def fetchPlacesByCategories(categories): inQry = {"category": {"$in": categories}} dbConn = MongoConnection() places_coll = dbConn.getPlacesCollection() places = places_coll.find(inQry) locations = [] if (places): for aPlace in places: locations.append(aPlace) return shuffleList(locations)
def updateWeights(userId, landmarkWeight, natureWeight, shopWeight, theatreWeight, restoWeight): print("categories.landmark", landmarkWeight, "categories.natureWeight", natureWeight, "categories.shopWeight", shopWeight, "categories.theatreWeight", theatreWeight, "categories.restoWeight", restoWeight) incStr = { "$inc": { "categories.landmark": landmarkWeight, "categories.nature": natureWeight, "categories.shopping": shopWeight, "categories.theatre": theatreWeight, "categories.restaurant": restoWeight } } conn = MongoConnection() user_coll = conn.getUsersCollection() user_coll.update_one({"userId": userId}, incStr)
def fetchPlacesByCategoriesBudget(categories, budget): current_app.logger.info(categories) current_app.logger.info(budget) query = dict() query["category"] = {"$in": categories} query["Price"] = {"$lte": budget} #print(query) dbConn = MongoConnection() places_coll = dbConn.getPlacesCollection() current_app.logger.info(query) places = places_coll.find(query, { "_id": 0, "suitableAge": 0, "location": 0, "wayToGetThere": 0 }) locations = [] if (places): for aPlace in places: locations.append(aPlace) return shuffleList(locations)
def fetchCategoryWeightsForSimilarUsers(age, gender, avgBudget, avgDuration): #age = 60 #avgBudget = '10' #avgDuration = '6' #gender = 'M' match_query = { "$match": { "age": { "$gte": age - 5, "$lte": age + 5 }, "gender": gender, "avgBudget": str(avgBudget + 1), "avgDuration": str(avgDuration) } } group_query = { "$group": { "_id": 0, "landmark": { "$avg": "$categories.landmark" }, "nature": { "$avg": "$categories.nature" }, "restaurant": { "$avg": "$categories.restaurant" }, "shopping": { "$avg": "$categories.shopping" }, "theatre": { "$avg": "$categories.theatre" } } } project_query = { "$project": { "_id": 0, "landmark": { "$ceil": "$landmark" }, "nature": { "$ceil": "$nature" }, "restaurant": { "$ceil": "$restaurant" }, "shopping": { "$ceil": "$shopping" }, "theatre": { "$ceil": "$theatre" } } } pipeline = [match_query, group_query] current_app.logger.info(pipeline) #print(pipeline) conn = MongoConnection() #sprint(aColl for aColl in conn.db.list_collections()) user_coll = conn.db[DUMMY_USERS_DB] row = user_coll.aggregate(pipeline) val = list(row) #print(val) return val