def get_articles(): # 获取请求参数 page_num = int(request.args.get('page', 1)) # 页码 page_size = int(request.args.get('size', 20)) # 每页数量 dbms = request.args.get('dbms') # 请求数据库 sort_by = request.args.get('sort_by') if sort_by is not None and sort_by not in ArticleService.field_names: if sort_by[1:] not in ArticleService.field_names: sort_by = None try: # 检查数据库地址正确性 check_alias(db_alias=dbms) except DbmsAliasError: return Result.gen_failed('404', 'dbms error') # 获取查询参数 title = request.args.get('title') authors = request.args.get('authors') category = request.args.get('category') articleTags = request.args.get('articleTags') language = request.args.get('language') detail = request.args.get('detail', '1') # print(request.data) # print(exclude) cons = { 'title__contains': title, 'authors': authors, 'category': category, 'articleTags': articleTags, 'language': language } # 去除为空的查询参数 kwargs = {} for key, value in cons.items(): if value is not None and value != '': kwargs[key] = value if detail != '1': kwargs['exclude'] = ['text', 'image', 'video'] # 尝试从dbms对应的Redis中获取该数据 _REDIS_KEY_ = f"{ARTICLES_LIST}:{dbms}:{page_num}:{page_size}:{sort_by}:{kwargs}" data = RedisService().get_dict(dbms, _REDIS_KEY_) # 判断数据是否存在 if data is None or data == {}: # 不存在 arts = ArticleService().get_articles(page_num=page_num, page_size=page_size, db_alias=dbms, sort_by=sort_by, **kwargs) arts = list(art.to_dict() for art in arts) total = ArticleService().count(db_alias=dbms, **kwargs) data = { 'total': total, 'list': arts } # 将该数据存入Redis中 RedisService().set_dict(dbms, _REDIS_KEY_, data) return Result.gen_success(data) pass
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)
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
def get_users_list(): 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') name = request.args.get('name') gender = request.args.get('gender') cons = {'name': name, 'gender': gender} kwargs = {} for key, value in cons.items(): if value is not None and value != '': kwargs[key] = value _REDIS_KEY_ = f"{USERS_LIST}:{page_num}:{page_size}:{kwargs}" data = RedisService().get_dict(dbms, _REDIS_KEY_) if data is None or data == {}: res = UserService().get_users(page_num=page_num, page_size=page_size, db_alias=dbms, **kwargs) _users = list() total = UserService().count(db_alias=dbms, **kwargs) for user in res: _users.append(user.to_dict()) data = {'total': total, 'list': list(_users)} RedisService().set_dict(dbms, _REDIS_KEY_, data) return Result.gen_success(data)
def count(self, db_alias=None, **kwargs): """ 计算db_alias所对应的的数据库下满足条件的用户数量 :param db_alias: :param kwargs: 查询参数字典, 为空则统计所有用户数量 :return: """ check_alias(db_alias) return self.get_model(db_alias).count(**kwargs)
def next_id(self, name): if Config.is_import: return import_next_id(name) else: db_alias = DBMS.DBMS1 model = self.get_model(db_alias) check_alias(db_alias) kwargs = {'inc__' + name: 1} ids = model.objects(ids=0).only(name).modify(upsert=True, **kwargs) # logger.debug('set {}: {}'.format(name, getattr(ids, name))) return getattr(ids, name)
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)
def get_ranks(self, temporalGranularity, db_alias, page_num=1, page_size=20, only: list = None, exclude: list = None, sort_by=None, **kwargs): check_alias(db_alias) return self.get_model(db_alias).get_all( page_num, page_size, only=only, exclude=exclude, sort_by=sort_by, temporalGranularity=temporalGranularity, **kwargs) pass
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
def get_redis(self, dbms) -> Redis: check_alias(dbms) return self.redis[dbms]
def __sync_id(self, name, value, db_alias=None): check_alias(db_alias=db_alias) kwargs = {name: value} self.get_model(db_alias).get_one(ids=0).update(**kwargs)
def __del_by_filed(self, field, value, db_alias=None, **kwargs): check_alias(db_alias) kwargs[field] = value re = self.get_model(db_alias).delete_one(**kwargs) return re