Ejemplo n.º 1
0
def update_populars():
    from utils.func import timestamp_to_datetime
    timestamp = request.json.get('timestamp')
    if timestamp is not None:
        _date = timestamp_to_datetime(timestamp).date()
        print("更新热门文章: 日期 -> {}".format(_date))
        PopularService().update_popular(_date=_date)
    else:
        PopularService().update_popular()
    return Result.gen_success()
    # return Result.gen_failed(234, 'shibai')
    pass
Ejemplo n.º 2
0
def update_popular():
    print("开始更新")
    PopularService().update_popular()
    # print("更新一周热门")
    # PopularService().update_monthly_rank()
    # print("更新一月热门")
    # PopularService().update_weekly_rank()
    print("更新完成")
    pass
Ejemplo n.º 3
0
def show_daily_popular(_date=None):
    _date = _date or datetime.date.today()
    articles = PopularService().get_daily_articles(_date)
    if len(articles) == 0:
        print("当前并无相关记录")
    pretty_models(articles, ['aid', 'title', 'count'])
    # show_rank(rank)
    input("\n按回车键返回")
    pass
Ejemplo n.º 4
0
def gen_populars():
    print()
    timeBegin = 1506332297000
    timeEnd = timeBegin + READS_NUM * 10000
    for timestamp in range(timeBegin, timeEnd, 86400000):
        logger.info('gen popular on date: {}'.format(
            timestamp_to_datetime(timestamp).date()))
        PopularService().update_popular(
            _date=timestamp_to_datetime(timestamp).date())
Ejemplo n.º 5
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
Ejemplo n.º 6
0
def get_today_populars():
    level = request.args.get('level', 'daily')
    timestamp = request.args.get("t")
    dbms = request.args.get('dbms')

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

    articles = PopularService().get_articles(timestamp, level, db_alias=dbms)
    articles = list(article.to_dict(only=['aid', 'title', 'count', 'category']) for article in articles)
    return Result.gen_success(data=articles)
Ejemplo n.º 7
0
def delete_popular(pid):
    if pid is None:
        return Result.gen_failed('404', 'pid not found')

    dbms = request.args.get('dbms')
    if dbms is None:
        return Result.gen_failed('5000', '请带上dbms参数')
    # 删除缓存
    RedisService().get_redis(dbms).delete(f"POPULAR:{pid}")
    RedisService().get_redis(dbms).delete_by_pattern(f"POPULAR_LIST:{dbms}:*")

    PopularService().del_by_id(pid)

    return Result.gen_success('删除成功')
Ejemplo n.º 8
0
def get_populars():
    page_num = int(request.args.get('page', 1))
    page_size = int(request.args.get('size', 20))
    dbms = request.args.get('dbms')
    temporalGranularity = request.args.get('level')
    sort_by = request.args.get('sort_by') or 'timestamp'

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

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

    _REDIS_KEY_ = f"POPULAR_LIST:{dbms}:{page_num}:{page_size}:{temporalGranularity}:{sort_by}:{kwargs}"
    data = RedisService().get_redis(dbms).get_dict(_REDIS_KEY_)
    if data is None or data == {}:
        ranks = PopularService().get_ranks(temporalGranularity=temporalGranularity, db_alias=dbms,
                                           page_num=page_num,
                                           page_size=page_size, sort_by=sort_by, **kwargs)

        ranks = list(rank.to_dict() for rank in ranks)
        total = PopularService().count(temporalGranularity=temporalGranularity, db_alias=dbms, **kwargs)
        data = {
            'total': total,
            'list': ranks
        }
        RedisService().get_redis(dbms).set_dict(_REDIS_KEY_, data)
    return Result.gen_success(data)
    pass
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
    def test_get_monthly_articles(self):
        _date = str_to_datetime(self._date)
        populars = PopularService().get_monthly_articles(_date, DBMS.DBMS2)

        pretty_models(populars, ['aid', 'title', 'count'])
Ejemplo n.º 11
0
 def test_get_monthly_popular(self):
     # _date = '2017-09-25'
     # _date = '2017-10-06'
     popular = PopularService()._get_monthly_rank(str_to_datetime(self._date), db_alias=DBMS.DBMS2)
     pretty_models([popular], PopularService.field_names)
Ejemplo n.º 12
0
 def test_update_popular(self):
     _date = timestamp_to_datetime(1506342197000).date()
     PopularService().update_popular(_date)