<commit_before>from icecream.settings import SQLALCHEMY

import sys
sys.path.append('../devrank-sqlclient')

from sqlalchemy import or_
from models import *
from client import *


class DevRankModel(object):
    def __init__(self):
        self.db = DevRankDB(SQLALCHEMY)
        self.db.connect()

    def _get_gravatar_url(self, session, user):
        u = session.query(User).filter(User.login==user).first()
        return u and u.avatar_url or None

    def _get_users_by_login(self, session, users):
        '''[ (login, id, avatar_url), (login, id, avatar_url) ... ]'''
        return session.query(User.login, User.id, User.avatar_url) \
                      .filter(User.login.in_(users)).all()

    def _get_followings(self, session, users):
        return session.query(Follower) \
                      .join(User, or_(User.id == Follower.src_id, \
                                      User.id == Follower.dest_id)) \
                      .filter(User.login.in_(users)).all()

    def get_user_id(self, login):
        s = self.db.makesession()
        user = s.query(User).filter_by(login=login).first()
        if user == None:
            return None
        return user.id

    def search(self, query, me_id=None, page=1, page_per_row=20):
        # TODO search using query and me.
        if me_id == None:
            return self.__search_global(query)
        search_sql = u'''
select distinct * from
(
    select
        distinct u.*
    from friend_relation fr
    inner join users u on fr.dest_id = u.id
    inner join repos r on u.id = r.owner_id
    where src_id = :id
    :where_clause_qry
    # 3-depth
    union all
    select
        distinct u.*
    from friend_relation fr1
    inner join friend_relation fr2 on fr1.dest_id = fr2.src_id
    inner join users u on fr2.dest_id = u.id
    inner join repos r on u.id = r.owner_id
    where fr1.src_id = :id
    :where_clause_qry
    # 4-depth
    union all
    select
        distinct u.*
    from friend_relation fr1
    inner join friend_relation fr2 on fr1.dest_id = fr2.src_id
    inner join friend_relation fr3 on fr2.dest_id = fr3.src_id
    inner join users u on fr3.dest_id = u.id
    inner join repos r on u.id = r.owner_id
    where fr1.src_id = :id
    :where_clause_qry
    order by devrank_score desc
) friends
limit :limit_clause
        '''
        limit_clause = '%d, %d' % ((page - 1) * page_per_row, page_per_row)
        where_clause_qry = '''
            and (r.description like ':like_qry'
            or r.language like ':like_qry'
            or u.location like ':like_qry'
            or u.login like ':like_qry'
            or u.name like ':like_qry')
        '''.replace(':like_qry', '%%%s%%' % query)
        sql = search_sql.replace(':where_clause_qry', where_clause_qry) \
                        .replace(':limit_clause', limit_clause)
        s = self.db.makesession()
        users = s.query(User).from_statement(sql) \
                       .params(id=me_id).all()
        for u in users:
            if u.devrank_score:
                u.devrank_score *= 1000000
            else:
                u.devrank_score = 0
            u.public_repos = self._get_repos_count(s, u.id)
            u.followers = self._get_followers_count(s, u.id)
            u.following = self._get_following_count(s, u.id)
        return users

    def __search_global(self, query, page=1, page_per_row=20):
        s = self.db.makesession()
        likequery = '%%%s%%' % query
        users = s.query(User).join(Repo, User.id == Repo.owner_id) \
                 .filter(or_(Repo.description.like(likequery), \
                             Repo.language.like(likequery), \
                             User.location.like(likequery), \
                             User.login.like(likequery), \
                             User.name.like(likequery))) \
                 .order_by(User.devrank_score.desc()) \
                 .distinct() \
                 .limit(page_per_row) \
                 .offset((page - 1) * page_per_row) \
                 .all()<commit_msg>www: Remove multiply to score<commit_after>from icecream.settings import SQLALCHEMY

import sys
sys.path.append('../devrank-sqlclient')

from sqlalchemy import or_
from models import *
from client import *


class DevRankModel(object):
    def __init__(self):
        self.db = DevRankDB(SQLALCHEMY)
        self.db.connect()

    def _get_gravatar_url(self, session, user):
        u = session.query(User).filter(User.login==user).first()
        return u and u.avatar_url or None

    def _get_users_by_login(self, session, users):
        '''[ (login, id, avatar_url), (login, id, avatar_url) ... ]'''
        return session.query(User.login, User.id, User.avatar_url) \
                      .filter(User.login.in_(users)).all()

    def _get_followings(self, session, users):
        return session.query(Follower) \
                      .join(User, or_(User.id == Follower.src_id, \
                                      User.id == Follower.dest_id)) \
                      .filter(User.login.in_(users)).all()

    def get_user_id(self, login):
        s = self.db.makesession()
        user = s.query(User).filter_by(login=login).first()
        if user == None:
            return None
        return user.id

    def search(self, query, me_id=None, page=1, page_per_row=20):
        # TODO search using query and me.
        if me_id == None:
            return self.__search_global(query)
        search_sql = u'''
select distinct * from
(
    select
        distinct u.*
    from friend_relation fr
    inner join users u on fr.dest_id = u.id
    inner join repos r on u.id = r.owner_id
    where src_id = :id
    :where_clause_qry
    # 3-depth
    union all
    select
        distinct u.*
    from friend_relation fr1
    inner join friend_relation fr2 on fr1.dest_id = fr2.src_id
    inner join users u on fr2.dest_id = u.id
    inner join repos r on u.id = r.owner_id
    where fr1.src_id = :id
    :where_clause_qry
    # 4-depth
    union all
    select
        distinct u.*
    from friend_relation fr1
    inner join friend_relation fr2 on fr1.dest_id = fr2.src_id
    inner join friend_relation fr3 on fr2.dest_id = fr3.src_id
    inner join users u on fr3.dest_id = u.id
    inner join repos r on u.id = r.owner_id
    where fr1.src_id = :id
    :where_clause_qry
    order by devrank_score desc
) friends
limit :limit_clause
        '''
        limit_clause = '%d, %d' % ((page - 1) * page_per_row, page_per_row)
        where_clause_qry = '''
            and (r.description like ':like_qry'
            or r.language like ':like_qry'
            or u.location like ':like_qry'
            or u.login like ':like_qry'
            or u.name like ':like_qry')
        '''.replace(':like_qry', '%%%s%%' % query)
        sql = search_sql.replace(':where_clause_qry', where_clause_qry) \
                        .replace(':limit_clause', limit_clause)
        s = self.db.makesession()
        users = s.query(User).from_statement(sql) \
                       .params(id=me_id).all()
        for u in users:
            u.public_repos = self._get_repos_count(s, u.id)
            u.followers = self._get_followers_count(s, u.id)
            u.following = self._get_following_count(s, u.id)
        return users

    def __search_global(self, query, page=1, page_per_row=20):
        s = self.db.makesession()
        likequery = '%%%s%%' % query
        users = s.query(User).join(Repo, User.id == Repo.owner_id) \
                 .filter(or_(Repo.description.like(likequery), \
                             Repo.language.like(likequery), \
                             User.location.like(likequery), \
                             User.login.like(likequery), \
                             User.name.like(likequery))) \
                 .order_by(User.devrank_score.desc()) \
                 .distinct() \
                 .limit(page_per_row) \
                 .offset((page - 1) * page_per_row) \
                 .all()
{'fraction_non_alphanumeric': 0.11149956299163441, 'fraction_numerical': 0.0072418529154700964, 'mean_word_length': 1.8844076341375584, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2541318', 'n_tokens_mistral': 2631, 'n_tokens_neox': 2545, 'n_words': 584}