コード例 #1
0
ファイル: read.py プロジェクト: Gahon1995/mongo
def get_reads():
    """
        获取所有的read记录
    :return:
    """
    page_num = int(request.args.get('page', 1))
    page_size = int(request.args.get('size', 20))

    dbms = request.args.get('dbms')
    try:
        check_alias(db_alias=dbms)
    except DbmsAliasError:
        return Result.gen_failed('404', 'dbms error')

    uid = request.args.get('uid')
    aid = request.args.get('aid')
    cons = {'uid': uid, 'aid': aid}
    kwargs = {}
    for key, value in cons.items():
        if value is not None and value != '':
            kwargs[key] = value

    res = ReadService().get_reads(page_num=page_num,
                                  page_size=page_size,
                                  db_alias=dbms,
                                  **kwargs)
    _reads = list(read.to_dict() for read in res)
    total = ReadService().count(db_alias=dbms, **kwargs)
    data = {'total': total, 'list': _reads}

    return Result.gen_success(data)
コード例 #2
0
ファイル: popular_service.py プロジェクト: Gahon1995/mongo
    def update_popular(self,
                       _date=None,
                       db_alias=None,
                       daily_pop=None,
                       weekly_pop=None,
                       monthly_pop=None):
        if isinstance(_date, datetime.datetime):
            _date = _date.date()
        _date = _date or datetime.date.today()
        if daily_pop is None:
            daily_pop = ReadService().get_daily_popular(_date)
            weekly_pop = ReadService().get_weekly_popular(_date)
            monthly_pop = ReadService().get_monthly_popular(_date)
        if db_alias is None:
            for dbms in DBMS.all:
                self.update_popular(_date=_date,
                                    db_alias=dbms,
                                    daily_pop=daily_pop,
                                    weekly_pop=weekly_pop,
                                    monthly_pop=monthly_pop)
            pass
        else:

            check_alias(db_alias)

            self.__update_daily_rank(daily_pop, _date, db_alias)
            self.__update_weekly_rank(weekly_pop, _date, db_alias)
            self.__update_monthly_rank(monthly_pop, _date, db_alias)

            logger.info("update popular on {}, date:{}".format(
                db_alias, _date))
            pass
コード例 #3
0
ファイル: test_readService.py プロジェクト: Gahon1995/mongo
 def test_read_twice(self):
     aid = ArticleService().get_articles_by_title('title2')[0].aid
     uid = UserService().get_user_by_name('user2').uid
     read1 = self.readService.add_one(aid, uid, 1, 34, 2, 0, 'sdf', 0, 1)
     ReadService().pretty_reads([read1])
     read2 = self.readService.add_one(aid, uid, 1, 34, 2, 1, 'asdaff', 1, 1)
     ReadService().pretty_reads([read2])
     read1.reload()
     assert read1.readSequence == read2.readSequence
コード例 #4
0
def dashboard():
    from config import DBMS
    from service.user_service import UserService
    from service.article_service import ArticleService
    from service.read_service import ReadService
    from service.popular_service import PopularService
    dbms = request.args.get('dbms')
    nums = {
        'users':
        UserService().count(db_alias=dbms),
        'articles':
        ArticleService().count(db_alias=dbms),
        'reads':
        ReadService().count(db_alias=dbms),
        'populars':
        PopularService().count(temporalGranularity='daily', db_alias=dbms)
    }

    charts = {'users': [], 'articles': [], 'reads': []}

    for dbms in DBMS().get_all_dbms_by_region():
        charts['users'].append({
            'name': dbms,
            'value': UserService().count(db_alias=dbms)
        })
        charts['articles'].append({
            'name':
            dbms,
            'value':
            ArticleService().count(db_alias=dbms)
        })
        charts['reads'].append({
            'name': dbms,
            'value': ReadService().count(db_alias=dbms)
        })

    data = {'nums': nums, 'charts': charts}

    # if dbms == 'Beijing':
    #     data = {
    #         'users': 12354,
    #         'articles': 533366,
    #         'reads': 23424,
    #         'populars': 90372
    #     }
    # else:
    #     data = {
    #         'users': 63234,
    #         'articles': 1284,
    #         'reads': 724933,
    #         'populars': 8422
    #     }
    return Result.gen_success(data=data)
    pass
コード例 #5
0
ファイル: read.py プロジェクト: Gahon1995/mongo
def get_user_read_history(uid):
    """
        获取指定用户的所有read记录
    :return:
    """

    region = request.args.get('region')

    if region not in DBMS().region['values']:
        return Result.gen_failed('404', 'region error')
    else:
        dbms = DBMS().get_best_dbms_by_region(region)

    kwargs = {'uid': uid}
    # kwargs = {}

    data = None
    # _REDIS_KEY_ = f"READ_LIST:{dbms}:{page_num}:{page_size}:{kwargs}"
    # data = RedisService().get_redis(dbms).get_dict(_REDIS_KEY_)
    if data is None or data == {}:
        res = ReadService().get_reads(sort_by='-timestamp',
                                      db_alias=dbms,
                                      **kwargs)
        records = {}
        for read in res:
            if read.aid not in records.keys():
                article = ArticleService().get_one_by_aid(aid=read.aid,
                                                          only=['title'])
                if article is not None:
                    # record = read.to_dict()
                    read.title = article.title
                    records[read.aid] = read
            else:
                records[read.aid].readOrNot = records[
                    read.aid].readOrNot or read.readOrNot
                records[read.aid].agreeOrNot = records[
                    read.aid].agreeOrNot or read.agreeOrNot
                records[read.aid].shareOrNot = records[
                    read.aid].shareOrNot or read.shareOrNot
                records[read.aid].commentOrNot = records[
                    read.aid].commentOrNot or read.commentOrNot
                records[read.aid].readTimeLength += read.readTimeLength
                records[read.aid].readSequence += read.readSequence
                records[read.aid].timestamp = max(records[read.aid].timestamp,
                                                  read.timestamp)

        records = list(
            read.to_dict(other=['title']) for (key, read) in records.items())

        total = ReadService().count(db_alias=dbms, **kwargs)
        data = {'total': total, 'list': records}

    return Result.gen_success(data)
コード例 #6
0
ファイル: ArticleUI.py プロジェクト: Gahon1995/mongo
def read_an_article(user, article):
    # from service.read_service import Read, ReadService
    import time

    time_read_start = time.time()

    # input("\n按回车返回")
    # TODO 阅读完后的操作,点赞、评论、分享等

    aid = article.aid
    uid = user.uid
    commentOrNot, commentDetail, agreeOrNot, shareOrNot = 0, '', 0, 0
    while True:
        print("1. 点赞 \t2. 评论\t3. 分享\t4. 返回")
        mode = input("请选择操作: ")
        if mode == '1':
            agreeOrNot = 1
            print("点赞成功")
        elif mode == '2':
            commentOrNot = 1
            detail = input("请输入评论信息: ")
            commentDetail = detail
            print("评论成功")
        elif mode == '3':
            shareOrNot = 1
            print("分享成功")
        elif mode == '4':
            time_read_end = time.time()
            readTimeLength = time_read_end - time_read_start
            ReadService().add_one(aid, uid, 1, readTimeLength, 1, commentOrNot, commentDetail, agreeOrNot, shareOrNot)
            return
        else:
            print("输入错入,请重新输入")

    pass
コード例 #7
0
ファイル: genDatabase.py プロジェクト: Gahon1995/mongo
def gen_reads():
    print()
    for i in range(READS_NUM):
        print_bar(i, READS_NUM)
        data = gen_an_read(i)

        data['rid'] = i
        region = uid_region[data["uid"]]
        category = aid_category[data["aid"]]

        ReadService().import_from_dict(data, region=region, category=category)
        if (i + 1) % 100000 == 0:
            logger.info("saving reads")
            ReadService().update_many()
            # BeReadService().update_many()
    ReadService().update_many()
    BeReadService().update_many()
コード例 #8
0
ファイル: read.py プロジェクト: Gahon1995/mongo
def delete_read(rid):
    if rid is None:
        return Result.gen_failed('404', 'uid not found')
    _REDIS_KEY_ = f"{READS_ITEM}:{rid}"

    RedisService().delete_key_to_all(f"{READS_ITEM}:{rid}")

    ReadService().del_read_by_rid(rid)

    return Result.gen_success(msg='删除成功')
コード例 #9
0
ファイル: ArticleUI.py プロジェクト: Gahon1995/mongo
def history_admin():
    for index, dbms in enumerate(DBMS.all):
        print("{}. {}".format(index + 1, dbms))
    db = input("请选择查看的数据库: ")
    if db == '':
        return

    while not db.isdigit() or int(db) < 1 or int(db) > len(DBMS.all):
        db = input("输入错误,请重新选择: ")

    db_alias = DBMS.all[int(db) - 1]
    total = ReadService().count(db_alias=db_alias)
    show_history_admin(total, db_alias=db_alias)
コード例 #10
0
ファイル: import_to_mongo.py プロジェクト: Gahon1995/mongo
def read():
    i = 0
    with open(data_path + read_file_name, 'r') as f:
        for line in f:
            data = json.loads(line)
            article = ArticleService.get_an_article(title='title' + data['aid'])
            name = 'user' + data['uid'] if data['uid'] != 0 else 'admin'
            user = UserService.get_user_by_name(name=name)
            new_read = Read()
            new_read.aid = article
            new_read.uid = user
            new_read.readOrNot = int(data['readOrNot'])
            new_read.readTimeLength = int(data['readTimeLength'])
            new_read.readSequence = int(data['readSequence'])
            new_read.commentOrNot = int(data['commentOrNot'])
            new_read.commentDetail = data['commentDetail']
            new_read.agreeOrNot = int(data['agreeOrNot'])
            new_read.shareOrNot = int(data['shareOrNot'])
            ReadService.add_one(new_read)

            i += 1
            print_bar(i, READS_NUM)
コード例 #11
0
ファイル: read.py プロジェクト: Gahon1995/mongo
def update_read_record():
    """
        对用户的阅读行为添加记录
    :return:
    """
    # TODO uid 和region 通过登录的用户进行获取
    # print(request.json)
    uid = int(request.json.get('uid'))
    aid = int(request.json.get('aid'))
    region = request.json.get('region')
    category = request.json.get('category')

    readOrNot = int(request.json.get('readOrNot', 0))
    readTimeLength = int(request.json.get('readTimeLength', 0))
    readSequence = int(request.json.get('readSequence', 0))
    agreeOrNot = int(request.json.get('agreeOrNot', 0))
    commentOrNot = int(request.json.get('commentOrNot', 0))
    shareOrNot = int(request.json.get('shareOrNot', 0))
    commentDetail = request.json.get('commentDetail', '')

    if region not in DBMS().region['values'] or category not in DBMS(
    ).category['values']:
        return Result.gen_failed(500, msg='region or category error')

    if uid is None or aid is None:
        return Result.gen_failed(500, "uid or aid 不能为空")

    rid = -343
    for dbms in get_dbms_by_region(region):
        rid = ReadService().new_record(aid=aid,
                                       uid=uid,
                                       readOrNot=readOrNot,
                                       readTimeLength=readTimeLength,
                                       readSequence=readSequence,
                                       agreeOrNot=agreeOrNot,
                                       commentOrNot=commentOrNot,
                                       shareOrNot=shareOrNot,
                                       commentDetail=commentDetail,
                                       db_alias=dbms).rid

    for dbms in get_dbms_by_category(category):
        BeReadService().new_record(aid=aid,
                                   uid=uid,
                                   readOrNot=readOrNot,
                                   commentOrNot=commentOrNot,
                                   agreeOrNot=agreeOrNot,
                                   shareOrNot=shareOrNot,
                                   db_alias=dbms)

    return Result.gen_success(msg='success', data={'rid': rid})
    pass
コード例 #12
0
ファイル: ArticleUI.py プロジェクト: Gahon1995/mongo
def show_history_admin(total, page_num=1, page_size=20, db_alias=None, **kwargs):
    # TODO 数据量大时分页显示
    # x.clear()
    x.clear_rows()
    x.field_names = ('index', 'aid', 'uid', 'readTimeLength', 'readSequence',
                     'agreeOrNot', 'commentOrNot', 'shareOrNot', 'commentDetail', 'readTime')
    # TODO 展示历史阅读数据
    #       根据Read 表的数据展示当前用户的阅读历史
    #       目前只展示DBMS1里边的阅读数据
    reads = ReadService().get_reads(page_num, page_size, db_alias=db_alias)
    for index, read in enumerate(reads):
        x.add_row((index, read.aid, read.uid, read.readTimeLength, read.readSequence,
                   read.agreeOrNot, read.commentOrNot, read.shareOrNot, read.commentDetail, read.timestamp))

    print(x)
    show_next(page_num, page_size, total, show_history_admin, **kwargs)
    # input("按回车键返回")
    pass
コード例 #13
0
ファイル: ArticleUI.py プロジェクト: Gahon1995/mongo
def show_history(user, total, page_num=1, page_size=20, **kwargs):
    # TODO 数据量大时分页显示
    x.clear()
    x.field_names = ('index', 'aid', 'readTimeLength', 'readSequence',
                     'agreeOrNot', 'commentOrNot', 'shareOrNot', 'commentDetail', 'timestamp')
    # TODO 展示历史阅读数据
    #       根据Read 表的数据展示当前用户的阅读历史
    reads = ReadService().get_history(user.uid, page_num, page_size)
    for index, read in enumerate(reads):
        # title = ReadService().get_by_rid(rid=read.aid)
        x.add_row((index, read.aid, read.readTimeLength, read.readSequence,
                   read.agreeOrNot, read.commentOrNot, read.shareOrNot, read.commentDetail,
                   timestamp_to_str(read.timestamp)))

    print(x)
    show_next(page_num, page_size, total, show_history, **kwargs)
    # input("按回车键返回")
    pass
コード例 #14
0
ファイル: read.py プロジェクト: Gahon1995/mongo
def get_read(rid):
    read = {}

    _REDIS_KEY_ = f"{READS_ITEM}:{rid}"
    for dbms in DBMS().get_all_dbms_by_region():
        read = RedisService().get_dict(dbms, _REDIS_KEY_)

    if read == {}:
        read = ReadService().get_by_rid(rid=rid)
        if read is None:
            return Result.gen_failed(404, 'user not found')

        read = read.to_dict()

        for dbms in DBMS().get_all_dbms_by_region():
            RedisService().set_dict(dbms, _REDIS_KEY_, read)

    return Result.gen_success(read)
    pass
コード例 #15
0
ファイル: dashboard.py プロジェクト: Gahon1995/mongo
def get_dashboard_info():
    from config import DBMS
    from service.user_service import UserService
    from service.article_service import ArticleService
    from service.read_service import ReadService
    from service.popular_service import PopularService
    from service.redis_service import RedisService
    dbms = request.args.get('dbms')
    print('get info start time: {}'.format(datetime.datetime.now()))

    if dbms not in DBMS().all:
        return Result.gen_failed(code=500, msg='dbms error')

    _KEY_ = "DASHBOARD"
    data = RedisService().get_dict(dbms, _KEY_)
    if data is not None and data != {}:
        return Result.gen_success(data=data)

    nums = {
        'users':
        UserService().count(db_alias=dbms),
        'articles':
        ArticleService().count(db_alias=dbms),
        'reads':
        ReadService().count(db_alias=dbms),
        'populars':
        PopularService().count(temporalGranularity='daily', db_alias=dbms)
    }
    nodes = get_nodes(dbms)

    charts = {'users': [], 'articles': [], 'reads': []}

    for dbms1 in DBMS().get_all_dbms_by_region():
        if dbms1 == dbms:
            charts['users'].append({'name': dbms1, 'value': nums['users']})
            charts['articles'].append({
                'name': dbms1,
                'value': nums['articles']
            })
            charts['reads'].append({'name': dbms1, 'value': nums['reads']})
        else:
            charts['users'].append({
                'name': dbms1,
                'value': UserService().count(db_alias=dbms1)
            })
            charts['articles'].append({
                'name':
                dbms1,
                'value':
                ArticleService().count(db_alias=dbms1)
            })
            charts['reads'].append({
                'name': dbms1,
                'value': ReadService().count(db_alias=dbms1)
            })

    data = {'nums': nums, 'charts': charts, 'nodes': nodes}
    print('end info start time: {}'.format(datetime.datetime.now()))

    RedisService().set_dict(dbms, _KEY_, data)
    return Result.gen_success(data=data)
    pass
コード例 #16
0
ファイル: ArticleUI.py プロジェクト: Gahon1995/mongo
def history(user):
    total = ReadService().count(uid=user.uid, db_alias=get_best_dbms_by_region(user.region))
    if total == 0:
        print("当前没有任何阅读记录哟")
        return
    show_history(user, total)
コード例 #17
0
ファイル: test_readService.py プロジェクト: Gahon1995/mongo
 def setup_class(cls) -> None:
     super().setup_class()
     cls.readService = ReadService()