def rank(self, offset=0, limit=10): pipeline = [{ "$project": { "_id": 1, "score": { "$cond": { "if": { "$not": { "$gt": ["$score", None] } }, "then": None, "else": "$score" } } } }, { "$sort": { "score": -1 } }, { "$skip": offset }, { "$limit": limit }] cursor = self.db.user.aggregate(pipeline) ranks = [] for document in cursor: id = document["_id"] score = document["score"] rank = qanda.Rank(id, score) ranks.append(rank) return ranks
def rank( self, offset = 0, limit = 10 ): pipeline = [ { "$unwind": { "path" : "$answer"}}, { "$unwind": { "path" : "$answer.vote", "preserveNullAndEmptyArrays" : True}}, { "$group": { "_id" : "$answer._id", "score" : { "$sum" : "$answer.vote.num" }}}, { "$project": {"_id": 1, "score": {"$cond": { "if": {"$and":[ {"$eq":["$score", 0]}, {"$not":{"$gt": ["$answer.vote", None]}} ]}, "then": None, "else": "$score" } } } }, { "$sort": { "score": -1 }}, { "$skip": offset }, { "$limit": limit } ] cursor = self.db.question.aggregate(pipeline) ranks = [] for document in cursor: id = document["_id"] score = document["score"] rank = qanda.Rank(id, score) ranks.append(rank) return ranks
def rank(self, offset=0, limit=10): """return entity ids in order of their ranking""" """offset limits the return to start at the offset-th rank""" """limit parameter limits the maximum number of user ids to be returned""" result = [] pipeline = [{ "$lookup": { "from": "answers", "foreignField": "qid", "localField": "_id", "as": "order" } }, { "$project": { "order": { "$size": "$order" } } }, { "$sort": { "order": -1 } }, { "$skip": offset }, { "$limit": limit }] r = self.db.question.aggregate(pipeline) for question in r: result.append(qanda.Rank(question["_id"], question["order"])) return result
def rank(self, offset=0, limit=10): #Selects questions that have answers and without answers stmt = """SELECT qid, count(*) AS c FROM answer GROUP BY qid UNION SELECT id, 0 AS c FROM question where id not in (SELECT qid FROM answer group by qid) ORDER BY c DESC LIMIT ? OFFSET ?""" rows = self.cursor.execute(stmt, (limit, offset)).fetchall() ranks = [] for row in rows: id = row[0] score = row[1] rank = qanda.Rank(id, score) ranks.append(rank) return ranks
def rank(self, offset=0, limit=10): #Select answers with vote or without votes from left joinning #the answer table and vote table stmt = """SELECT a.id, SUM(v.vote) AS rank FROM answer a LEFT JOIN vote v on a.id=v.aid GROUP BY a.id ORDER BY rank DESC LIMIT? OFFSET?""" rows = self.cursor.execute(stmt, (limit, offset)).fetchall() ranks = [] for row in rows: id = row[0] score = row[1] rank = qanda.Rank(id, score) ranks.append(rank) return ranks
def rank( self, offset = 0, limit = 10 ): """return entity ids in order of their ranking""" """offset limits the return to start at the offset-th rank""" """limit parameter limits the maximum number of user ids to be returned""" all_aid_count = [] pipeline = [ {"$lookup": {"from": "vote", "foreignField": "v_aid", "localField": "_id", "as": "vote1"}}, {"$unwind" : {"path" :"$vote1", "preserveNullAndEmptyArrays":True}}, {"$group": {"_id": "$_id", "total": {"$sum": "$vote1.v_vote"}}}, {"$sort": {"total": -1}}, {"$skip": offset}, {"$limit": limit} ] r = self.db.answers.aggregate(pipeline) for aids in r: aid_obj = qanda.Rank(aids["_id"], aids["total"]) all_aid_count.append(aid_obj) return all_aid_count
def rank(self, offset=0, limit=10): #Creates a temp view of users who did not answer their own questions #Then joins it with users that did not answer any questions to select #all users that answer other's questions or no question stmt1 = """ SELECT u.id, s.score FROM user u LEFT JOIN (SELECT uid, COUNT(*) AS score FROM ans_own_quest_view GROUP BY uid) s on u.id = s.uid ORDER BY s.score DESC LIMIT %s OFFSET %s; """ self.cursor.execute(stmt1, (limit, offset)) rows = self.cursor.fetchall() ranks = [] for row in rows: id = row[0] score = row[1] rank = qanda.Rank(id, score) ranks.append(rank) return ranks