types fixes
This commit is contained in:
parent
0ca83cc91e
commit
003fa1bbac
|
@ -14,6 +14,7 @@ class TopicFollower(Base):
|
||||||
created_at = Column(Integer, nullable=False, default=int(time.time()))
|
created_at = Column(Integer, nullable=False, default=int(time.time()))
|
||||||
auto = Column(Boolean, nullable=False, default=False)
|
auto = Column(Boolean, nullable=False, default=False)
|
||||||
|
|
||||||
|
|
||||||
class Topic(Base):
|
class Topic(Base):
|
||||||
__tablename__ = 'topic'
|
__tablename__ = 'topic'
|
||||||
|
|
||||||
|
|
|
@ -32,10 +32,9 @@ def update_author(_, info, profile):
|
||||||
# TODO: caching query
|
# TODO: caching query
|
||||||
@query.field('get_authors_all')
|
@query.field('get_authors_all')
|
||||||
def get_authors_all(_, _info):
|
def get_authors_all(_, _info):
|
||||||
authors = []
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
authors = session.query(Author).all()
|
authors = session.query(Author).all()
|
||||||
return authors
|
return authors
|
||||||
|
|
||||||
|
|
||||||
def count_author_comments_rating(session, author_id) -> int:
|
def count_author_comments_rating(session, author_id) -> int:
|
||||||
|
@ -96,77 +95,84 @@ def count_author_shouts_rating(session, author_id) -> int:
|
||||||
return shouts_likes - shouts_dislikes
|
return shouts_likes - shouts_dislikes
|
||||||
|
|
||||||
|
|
||||||
def load_author_with_stats(q):
|
def load_author_ratings(author):
|
||||||
result = get_authors_with_stat(q)
|
with local_session() as session:
|
||||||
|
comments_count = (
|
||||||
if result:
|
session.query(Reaction)
|
||||||
[author] = result
|
.filter(
|
||||||
with local_session() as session:
|
and_(
|
||||||
comments_count = (
|
Reaction.created_by == author.id,
|
||||||
session.query(Reaction)
|
Reaction.kind == ReactionKind.COMMENT.value,
|
||||||
.filter(
|
Reaction.deleted_at.is_(None),
|
||||||
and_(
|
|
||||||
Reaction.created_by == author.id,
|
|
||||||
Reaction.kind == ReactionKind.COMMENT.value,
|
|
||||||
Reaction.deleted_at.is_(None),
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
.count()
|
|
||||||
)
|
)
|
||||||
likes_count = (
|
.count()
|
||||||
session.query(AuthorRating)
|
)
|
||||||
.filter(
|
likes_count = (
|
||||||
and_(AuthorRating.author == author.id, AuthorRating.plus.is_(True))
|
session.query(AuthorRating)
|
||||||
|
.filter(
|
||||||
|
and_(AuthorRating.author == author.id, AuthorRating.plus.is_(True))
|
||||||
|
)
|
||||||
|
.count()
|
||||||
|
)
|
||||||
|
dislikes_count = (
|
||||||
|
session.query(AuthorRating)
|
||||||
|
.filter(
|
||||||
|
and_(
|
||||||
|
AuthorRating.author == author.id, AuthorRating.plus.is_not(True)
|
||||||
)
|
)
|
||||||
.count()
|
|
||||||
)
|
)
|
||||||
dislikes_count = (
|
.count()
|
||||||
session.query(AuthorRating)
|
)
|
||||||
.filter(
|
author.stat['rating'] = likes_count - dislikes_count
|
||||||
and_(
|
author.stat['rating_shouts'] = count_author_shouts_rating(
|
||||||
AuthorRating.author == author.id, AuthorRating.plus.is_not(True)
|
session, author.id
|
||||||
)
|
)
|
||||||
)
|
author.stat['rating_comments'] = count_author_comments_rating(
|
||||||
.count()
|
session, author.id
|
||||||
)
|
)
|
||||||
author.stat['rating'] = likes_count - dislikes_count
|
author.stat['commented'] = comments_count
|
||||||
author.stat['rating_shouts'] = count_author_shouts_rating(
|
return author
|
||||||
session, author.id
|
|
||||||
)
|
|
||||||
author.stat['rating_comments'] = count_author_comments_rating(
|
|
||||||
session, author.id
|
|
||||||
)
|
|
||||||
author.stat['commented'] = comments_count
|
|
||||||
return author
|
|
||||||
|
|
||||||
|
|
||||||
@query.field('get_author')
|
@query.field('get_author')
|
||||||
def get_author(_, _info, slug='', author_id=None):
|
def get_author(_, _info, slug='', author_id=None):
|
||||||
q = None
|
q = None
|
||||||
if slug or author_id:
|
author = None
|
||||||
if bool(slug):
|
try:
|
||||||
q = select(Author).where(Author.slug == slug)
|
if slug or author_id:
|
||||||
if author_id:
|
if bool(slug):
|
||||||
q = select(Author).where(Author.id == author_id)
|
q = select(Author).where(Author.slug == slug)
|
||||||
|
if author_id:
|
||||||
|
q = select(Author).where(Author.id == int(author_id))
|
||||||
|
|
||||||
return load_author_with_stats(q)
|
[author, ] = get_authors_with_stat(q)
|
||||||
|
author = load_author_ratings(author)
|
||||||
|
except Exception as exc:
|
||||||
|
logger.error(exc)
|
||||||
|
return author
|
||||||
|
|
||||||
|
|
||||||
async def get_author_by_user_id(user_id: str):
|
async def get_author_by_user_id(user_id: str):
|
||||||
redis_key = f'user:{user_id}:author'
|
redis_key = f'user:{user_id}:author'
|
||||||
res = await redis.execute('GET', redis_key)
|
author = None
|
||||||
if isinstance(res, str):
|
try:
|
||||||
author = json.loads(res)
|
res = await redis.execute('GET', redis_key)
|
||||||
if author.get('id'):
|
if isinstance(res, str):
|
||||||
logger.debug(f'got cached author: {author}')
|
author = json.loads(res)
|
||||||
return author
|
if author.get('id'):
|
||||||
|
logger.debug(f'got cached author: {author}')
|
||||||
|
return author
|
||||||
|
|
||||||
logger.info(f'getting author id for {user_id}')
|
logger.info(f'getting author id for {user_id}')
|
||||||
q = select(Author).filter(Author.user == user_id)
|
q = select(Author).filter(Author.user == user_id)
|
||||||
author = load_author_with_stats(q)
|
|
||||||
if author:
|
[author, ] = get_authors_with_stat(q)
|
||||||
|
author = load_author_ratings(author)
|
||||||
update_author(author)
|
update_author(author)
|
||||||
return author
|
except Exception as exc:
|
||||||
|
logger.error(exc)
|
||||||
|
return author
|
||||||
|
|
||||||
|
|
||||||
@query.field('get_author_id')
|
@query.field('get_author_id')
|
||||||
|
@ -186,7 +192,7 @@ def load_authors_by(_, _info, by, limit, offset):
|
||||||
q.join(ShoutAuthor)
|
q.join(ShoutAuthor)
|
||||||
.join(ShoutTopic)
|
.join(ShoutTopic)
|
||||||
.join(Topic)
|
.join(Topic)
|
||||||
.where(Topic.slug == by['topic'])
|
.where(Topic.slug == str(by['topic']))
|
||||||
)
|
)
|
||||||
|
|
||||||
if by.get('last_seen'): # in unix time
|
if by.get('last_seen'): # in unix time
|
||||||
|
@ -219,6 +225,7 @@ def get_author_follows(_, _info, slug='', user=None, author_id=None):
|
||||||
else:
|
else:
|
||||||
raise ValueError('Author not found')
|
raise ValueError('Author not found')
|
||||||
|
|
||||||
|
|
||||||
@query.field('get_author_follows_topics')
|
@query.field('get_author_follows_topics')
|
||||||
def get_author_follows_topics(_, _info, slug='', user=None, author_id=None):
|
def get_author_follows_topics(_, _info, slug='', user=None, author_id=None):
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
|
@ -290,7 +297,7 @@ def create_author(user_id: str, slug: str, name: str = ''):
|
||||||
|
|
||||||
|
|
||||||
@query.field('get_author_followers')
|
@query.field('get_author_followers')
|
||||||
def get_author_followers(_, _info, slug):
|
def get_author_followers(_, _info, slug: str):
|
||||||
author_alias = aliased(Author)
|
author_alias = aliased(Author)
|
||||||
alias_author_followers = aliased(AuthorFollower)
|
alias_author_followers = aliased(AuthorFollower)
|
||||||
alias_author_authors = aliased(AuthorFollower)
|
alias_author_authors = aliased(AuthorFollower)
|
||||||
|
@ -299,9 +306,9 @@ def get_author_followers(_, _info, slug):
|
||||||
|
|
||||||
q = (
|
q = (
|
||||||
select(author_alias)
|
select(author_alias)
|
||||||
.join(alias_author_authors, alias_author_authors.follower == author_alias.id)
|
.join(alias_author_authors, alias_author_authors.follower == int(author_alias.id))
|
||||||
.join(
|
.join(
|
||||||
alias_author_followers, alias_author_followers.author == author_alias.id
|
alias_author_followers, alias_author_followers.author == int(author_alias.id)
|
||||||
)
|
)
|
||||||
.filter(author_alias.slug == slug)
|
.filter(author_alias.slug == slug)
|
||||||
.add_columns(
|
.add_columns(
|
||||||
|
@ -311,7 +318,7 @@ def get_author_followers(_, _info, slug):
|
||||||
'followers_stat'
|
'followers_stat'
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.outerjoin(alias_shout_author, author_alias.id == alias_shout_author.author)
|
.outerjoin(alias_shout_author, author_alias.id == int(alias_shout_author.author))
|
||||||
.group_by(author_alias.id)
|
.group_by(author_alias.id)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -81,7 +81,7 @@ async def get_communities_all(_, _info):
|
||||||
|
|
||||||
|
|
||||||
@query.field('get_community')
|
@query.field('get_community')
|
||||||
async def get_community(_, _info, slug):
|
async def get_community(_, _info, slug: str):
|
||||||
q = select(Community).where(Community.slug == slug)
|
q = select(Community).where(Community.slug == slug)
|
||||||
q = add_community_stat_columns(q)
|
q = add_community_stat_columns(q)
|
||||||
|
|
||||||
|
|
|
@ -186,8 +186,8 @@ def author_unfollow(follower_id, slug):
|
||||||
def get_topic_followers(_, _info, slug: str, topic_id: int) -> List[Author]:
|
def get_topic_followers(_, _info, slug: str, topic_id: int) -> List[Author]:
|
||||||
q = select(Author)
|
q = select(Author)
|
||||||
q = (
|
q = (
|
||||||
q.join(TopicFollower, TopicFollower.follower == Author.id)
|
q.join(TopicFollower, TopicFollower.follower == int(Author.id))
|
||||||
.join(Topic, Topic.id == TopicFollower.topic)
|
.join(Topic, Topic.id == int(TopicFollower.topic))
|
||||||
.filter(or_(Topic.slug == slug, Topic.id == topic_id))
|
.filter(or_(Topic.slug == slug, Topic.id == topic_id))
|
||||||
)
|
)
|
||||||
return get_authors_with_stat(q)
|
return get_authors_with_stat(q)
|
||||||
|
|
|
@ -23,17 +23,17 @@ def add_reaction_stat_columns(q, aliased_reaction):
|
||||||
q = q.outerjoin(aliased_reaction).add_columns(
|
q = q.outerjoin(aliased_reaction).add_columns(
|
||||||
func.sum(aliased_reaction.id).label('reacted_stat'),
|
func.sum(aliased_reaction.id).label('reacted_stat'),
|
||||||
func.sum(
|
func.sum(
|
||||||
case((aliased_reaction.kind == ReactionKind.COMMENT.value, 1), else_=0)
|
case((aliased_reaction.kind == str(ReactionKind.COMMENT.value), 1), else_=0)
|
||||||
).label('comments_stat'),
|
).label('comments_stat'),
|
||||||
func.sum(
|
func.sum(
|
||||||
case((aliased_reaction.kind == ReactionKind.LIKE.value, 1), else_=0)
|
case((aliased_reaction.kind == str(ReactionKind.LIKE.value), 1), else_=0)
|
||||||
).label('likes_stat'),
|
).label('likes_stat'),
|
||||||
func.sum(
|
func.sum(
|
||||||
case((aliased_reaction.kind == ReactionKind.DISLIKE.value, 1), else_=0)
|
case((aliased_reaction.kind == str(ReactionKind.DISLIKE.value), 1), else_=0)
|
||||||
).label('dislikes_stat'),
|
).label('dislikes_stat'),
|
||||||
func.max(
|
func.max(
|
||||||
case(
|
case(
|
||||||
(aliased_reaction.kind != ReactionKind.COMMENT.value, None),
|
(aliased_reaction.kind != str(ReactionKind.COMMENT.value), None),
|
||||||
else_=aliased_reaction.created_at,
|
else_=aliased_reaction.created_at,
|
||||||
)
|
)
|
||||||
).label('last_comment'),
|
).label('last_comment'),
|
||||||
|
@ -57,8 +57,7 @@ def check_to_feature(session, approver_id, reaction):
|
||||||
"""set shout to public if publicated approvers amount > 4"""
|
"""set shout to public if publicated approvers amount > 4"""
|
||||||
if not reaction.reply_to and is_positive(reaction.kind):
|
if not reaction.reply_to and is_positive(reaction.kind):
|
||||||
if is_featured_author(session, approver_id):
|
if is_featured_author(session, approver_id):
|
||||||
approvers = []
|
approvers = [approver_id, ]
|
||||||
approvers.append(approver_id)
|
|
||||||
# now count how many approvers are voted already
|
# now count how many approvers are voted already
|
||||||
reacted_readers = (
|
reacted_readers = (
|
||||||
session.query(Reaction).where(Reaction.shout == reaction.shout).all()
|
session.query(Reaction).where(Reaction.shout == reaction.shout).all()
|
||||||
|
@ -189,7 +188,7 @@ async def create_reaction(_, info, reaction):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
reply_to = reaction.get('reply_to')
|
reply_to = reaction.get('reply_to')
|
||||||
if reply_to:
|
if reply_to and isinstance(reply_to, int):
|
||||||
q = q.filter(Reaction.reply_to == reply_to)
|
q = q.filter(Reaction.reply_to == reply_to)
|
||||||
rating_reactions = session.execute(q).all()
|
rating_reactions = session.execute(q).all()
|
||||||
same_rating = filter(
|
same_rating = filter(
|
||||||
|
@ -352,8 +351,8 @@ async def load_reactions_by(_, info, by, limit=50, offset=0):
|
||||||
|
|
||||||
q = (
|
q = (
|
||||||
select(Reaction, Author, Shout)
|
select(Reaction, Author, Shout)
|
||||||
.join(Author, Reaction.created_by == Author.id)
|
.join(Author, Reaction.created_by == int(Author.id))
|
||||||
.join(Shout, Reaction.shout == Shout.id)
|
.join(Shout, Reaction.shout == int(Shout.id))
|
||||||
)
|
)
|
||||||
|
|
||||||
# calculate counters
|
# calculate counters
|
||||||
|
@ -431,7 +430,7 @@ async def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[S
|
||||||
# Shouts where follower reacted
|
# Shouts where follower reacted
|
||||||
q2 = (
|
q2 = (
|
||||||
select(Shout)
|
select(Shout)
|
||||||
.join(Reaction, Reaction.shout_id == Shout.id)
|
.join(Reaction, Reaction.shout_id == int(Shout.id))
|
||||||
.options(joinedload(Shout.reactions), joinedload(Shout.authors))
|
.options(joinedload(Shout.reactions), joinedload(Shout.authors))
|
||||||
.filter(Reaction.created_by == follower_id)
|
.filter(Reaction.created_by == follower_id)
|
||||||
.group_by(Shout.id)
|
.group_by(Shout.id)
|
||||||
|
@ -442,24 +441,24 @@ async def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[S
|
||||||
combined_query = (
|
combined_query = (
|
||||||
union(q1, q2).order_by(desc('last_comment')).limit(limit).offset(offset)
|
union(q1, q2).order_by(desc('last_comment')).limit(limit).offset(offset)
|
||||||
)
|
)
|
||||||
|
|
||||||
results = session.execute(combined_query).scalars()
|
results = session.execute(combined_query).scalars()
|
||||||
with local_session() as session:
|
for [
|
||||||
for [
|
shout,
|
||||||
shout,
|
reacted_stat,
|
||||||
reacted_stat,
|
commented_stat,
|
||||||
commented_stat,
|
likes_stat,
|
||||||
likes_stat,
|
dislikes_stat,
|
||||||
dislikes_stat,
|
last_comment,
|
||||||
last_comment,
|
] in results:
|
||||||
] in results:
|
shout.stat = {
|
||||||
shout.stat = {
|
'viewed': await ViewedStorage.get_shout(shout.slug),
|
||||||
'viewed': await ViewedStorage.get_shout(shout.slug),
|
'rating': int(likes_stat or 0) - int(dislikes_stat or 0),
|
||||||
'rating': int(likes_stat or 0) - int(dislikes_stat or 0),
|
'reacted': reacted_stat,
|
||||||
'reacted': reacted_stat,
|
'commented': commented_stat,
|
||||||
'commented': commented_stat,
|
'last_comment': last_comment,
|
||||||
'last_comment': last_comment,
|
}
|
||||||
}
|
shouts.append(shout)
|
||||||
shouts.append(shout)
|
|
||||||
|
|
||||||
return shouts
|
return shouts
|
||||||
|
|
||||||
|
|
|
@ -207,7 +207,7 @@ async def load_shouts_drafts(_, info):
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
reader = session.query(Author).filter(Author.user == user_id).first()
|
reader = session.query(Author).filter(Author.user == user_id).first()
|
||||||
if isinstance(reader, Author):
|
if isinstance(reader, Author):
|
||||||
q = q.filter(Shout.created_by == reader.id)
|
q = q.filter(Shout.created_by == int(reader.id))
|
||||||
q = q.group_by(Shout.id)
|
q = q.group_by(Shout.id)
|
||||||
for [shout] in session.execute(q).unique():
|
for [shout] in session.execute(q).unique():
|
||||||
main_topic = (
|
main_topic = (
|
||||||
|
@ -240,16 +240,16 @@ async def load_shouts_feed(_, info, options):
|
||||||
reader = session.query(Author).filter(Author.user == user_id).first()
|
reader = session.query(Author).filter(Author.user == user_id).first()
|
||||||
if reader:
|
if reader:
|
||||||
reader_followed_authors = select(AuthorFollower.author).where(
|
reader_followed_authors = select(AuthorFollower.author).where(
|
||||||
AuthorFollower.follower == reader.id
|
AuthorFollower.follower == int(reader.id)
|
||||||
)
|
)
|
||||||
reader_followed_topics = select(TopicFollower.topic).where(
|
reader_followed_topics = select(TopicFollower.topic).where(
|
||||||
TopicFollower.follower == reader.id
|
TopicFollower.follower == int(reader.id)
|
||||||
)
|
)
|
||||||
|
|
||||||
subquery = (
|
subquery = (
|
||||||
select(Shout.id)
|
select(Shout.id)
|
||||||
.where(Shout.id == ShoutAuthor.shout)
|
.where(Shout.id == int(ShoutAuthor.shout))
|
||||||
.where(Shout.id == ShoutTopic.shout)
|
.where(Shout.id == int(ShoutTopic.shout))
|
||||||
.where(
|
.where(
|
||||||
(ShoutAuthor.author.in_(reader_followed_authors))
|
(ShoutAuthor.author.in_(reader_followed_authors))
|
||||||
| (ShoutTopic.topic.in_(reader_followed_topics))
|
| (ShoutTopic.topic.in_(reader_followed_topics))
|
||||||
|
@ -430,10 +430,10 @@ async def load_shouts_random_top(_, _info, options):
|
||||||
desc(
|
desc(
|
||||||
func.sum(
|
func.sum(
|
||||||
case(
|
case(
|
||||||
(Reaction.kind == ReactionKind.LIKE.value, 1),
|
(Reaction.kind == str(ReactionKind.LIKE.value), 1),
|
||||||
(Reaction.kind == ReactionKind.AGREE.value, 1),
|
(Reaction.kind == str(ReactionKind.AGREE.value), 1),
|
||||||
(Reaction.kind == ReactionKind.DISLIKE.value, -1),
|
(Reaction.kind == str(ReactionKind.DISLIKE.value), -1),
|
||||||
(Reaction.kind == ReactionKind.DISAGREE.value, -1),
|
(Reaction.kind == str(ReactionKind.DISAGREE.value), -1),
|
||||||
else_=0,
|
else_=0,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -467,8 +467,8 @@ async def load_shouts_random_topic(_, info, limit: int = 10):
|
||||||
return {'topic': topic, 'shouts': shouts}
|
return {'topic': topic, 'shouts': shouts}
|
||||||
return {
|
return {
|
||||||
'error': 'failed to get random topic after few retries',
|
'error': 'failed to get random topic after few retries',
|
||||||
shouts: [],
|
'shouts': [],
|
||||||
topic: {},
|
'topic': {},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -74,7 +74,7 @@ def author_follows_authors(author_id: int):
|
||||||
af = aliased(AuthorFollower, name="af")
|
af = aliased(AuthorFollower, name="af")
|
||||||
q = (
|
q = (
|
||||||
select(Author).select_from(
|
select(Author).select_from(
|
||||||
join(Author, af, Author.id == af.author)
|
join(Author, af, Author.id == int(af.author))
|
||||||
).where(af.follower == author_id)
|
).where(af.follower == author_id)
|
||||||
)
|
)
|
||||||
q = add_author_stat_columns(q)
|
q = add_author_stat_columns(q)
|
||||||
|
@ -84,7 +84,7 @@ def author_follows_authors(author_id: int):
|
||||||
def author_follows_topics(author_id: int):
|
def author_follows_topics(author_id: int):
|
||||||
q = (
|
q = (
|
||||||
select(Topic).select_from(
|
select(Topic).select_from(
|
||||||
join(Topic, TopicFollower, Topic.id == TopicFollower.topic)
|
join(Topic, TopicFollower, Topic.id == int(TopicFollower.topic))
|
||||||
).where(TopicFollower.follower == author_id)
|
).where(TopicFollower.follower == author_id)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ def get_topics_by_community(_, _info, community_id: int):
|
||||||
|
|
||||||
|
|
||||||
@query.field('get_topics_by_author')
|
@query.field('get_topics_by_author')
|
||||||
def get_topics_by_author(_, _info, author_id=None, slug='', user=''):
|
def get_topics_by_author(_, _info, author_id=0, slug='', user=''):
|
||||||
q = select(Topic)
|
q = select(Topic)
|
||||||
if author_id:
|
if author_id:
|
||||||
q = q.join(Author).where(Author.id == author_id)
|
q = q.join(Author).where(Author.id == author_id)
|
||||||
|
@ -35,7 +35,7 @@ def get_topics_by_author(_, _info, author_id=None, slug='', user=''):
|
||||||
|
|
||||||
|
|
||||||
@query.field('get_topic')
|
@query.field('get_topic')
|
||||||
def get_topic(_, _info, slug):
|
def get_topic(_, _info, slug: str):
|
||||||
q = select(Topic).filter(Topic.slug == slug)
|
q = select(Topic).filter(Topic.slug == slug)
|
||||||
topics = get_topics_with_stat(q)
|
topics = get_topics_with_stat(q)
|
||||||
if topics:
|
if topics:
|
||||||
|
|
|
@ -22,11 +22,13 @@ REGISTRY: Dict[str, type] = {}
|
||||||
|
|
||||||
|
|
||||||
# Перехватчики для журнала запросов SQLAlchemy
|
# Перехватчики для журнала запросов SQLAlchemy
|
||||||
|
# noinspection PyUnusedLocal
|
||||||
@event.listens_for(Engine, 'before_cursor_execute')
|
@event.listens_for(Engine, 'before_cursor_execute')
|
||||||
def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
|
def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
|
||||||
conn.query_start_time = time.time()
|
conn.query_start_time = time.time()
|
||||||
|
|
||||||
|
|
||||||
|
# noinspection PyUnusedLocal
|
||||||
@event.listens_for(Engine, 'after_cursor_execute')
|
@event.listens_for(Engine, 'after_cursor_execute')
|
||||||
def after_cursor_execute(conn, cursor, statement, parameters, context, executemany):
|
def after_cursor_execute(conn, cursor, statement, parameters, context, executemany):
|
||||||
if hasattr(conn, '_query_start_time'):
|
if hasattr(conn, '_query_start_time'):
|
||||||
|
@ -36,6 +38,7 @@ def after_cursor_execute(conn, cursor, statement, parameters, context, executema
|
||||||
logger.debug(f"\n{statement}\n{'*' * math.floor(elapsed)} {elapsed:.3f} s")
|
logger.debug(f"\n{statement}\n{'*' * math.floor(elapsed)} {elapsed:.3f} s")
|
||||||
|
|
||||||
|
|
||||||
|
# noinspection PyUnusedLocal
|
||||||
def local_session(src=''):
|
def local_session(src=''):
|
||||||
return Session(bind=engine, expire_on_commit=False)
|
return Session(bind=engine, expire_on_commit=False)
|
||||||
|
|
||||||
|
|
|
@ -77,7 +77,7 @@ async def update_follows_for_user(
|
||||||
await redis.execute('SET', redis_key, json.dumps(follows))
|
await redis.execute('SET', redis_key, json.dumps(follows))
|
||||||
|
|
||||||
|
|
||||||
async def handle_author_follower_change(connection, author_id, follower_id, is_insert):
|
async def handle_author_follower_change(connection, author_id: int, follower_id: int, is_insert: bool):
|
||||||
q = select(Author).filter(Author.id == author_id)
|
q = select(Author).filter(Author.id == author_id)
|
||||||
authors = get_authors_with_stat(q)
|
authors = get_authors_with_stat(q)
|
||||||
author = authors[0]
|
author = authors[0]
|
||||||
|
@ -102,7 +102,7 @@ async def handle_author_follower_change(connection, author_id, follower_id, is_i
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def handle_topic_follower_change(connection, topic_id, follower_id, is_insert):
|
async def handle_topic_follower_change(connection, topic_id: int, follower_id: int, is_insert: bool):
|
||||||
q = select(Topic).filter(Topic.id == topic_id)
|
q = select(Topic).filter(Topic.id == topic_id)
|
||||||
topics = get_topics_with_stat(q)
|
topics = get_topics_with_stat(q)
|
||||||
topic = topics[0]
|
topic = topics[0]
|
||||||
|
|
|
@ -65,7 +65,7 @@ class ViewedStorage:
|
||||||
logger.info(f' * Миграция проводилась: {self.start_date}')
|
logger.info(f' * Миграция проводилась: {self.start_date}')
|
||||||
|
|
||||||
# Запуск фоновой задачи
|
# Запуск фоновой задачи
|
||||||
asyncio.create_task(self.worker())
|
_task = asyncio.create_task(self.worker())
|
||||||
else:
|
else:
|
||||||
logger.info(' * Пожалуйста, добавьте ключевой файл Google Analytics')
|
logger.info(' * Пожалуйста, добавьте ключевой файл Google Analytics')
|
||||||
self.disabled = True
|
self.disabled = True
|
||||||
|
@ -84,6 +84,7 @@ class ViewedStorage:
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f'Ошибка загрузки предварительно подсчитанных просмотров: {e}')
|
logger.error(f'Ошибка загрузки предварительно подсчитанных просмотров: {e}')
|
||||||
|
|
||||||
|
# noinspection PyTypeChecker
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def update_pages():
|
async def update_pages():
|
||||||
"""Запрос всех страниц от Google Analytics, отсортированных по количеству просмотров"""
|
"""Запрос всех страниц от Google Analytics, отсортированных по количеству просмотров"""
|
||||||
|
|
|
@ -1,78 +0,0 @@
|
||||||
from unittest.mock import Mock
|
|
||||||
from resolvers.stat import query_follows
|
|
||||||
|
|
||||||
|
|
||||||
def test_query_follows():
|
|
||||||
user_id = 'user123'
|
|
||||||
|
|
||||||
# Mocking database session and ORM models
|
|
||||||
mock_session = Mock()
|
|
||||||
mock_Author = Mock()
|
|
||||||
mock_ShoutAuthor = Mock()
|
|
||||||
mock_AuthorFollower = Mock()
|
|
||||||
mock_Topic = Mock()
|
|
||||||
mock_ShoutTopic = Mock()
|
|
||||||
mock_TopicFollower = Mock()
|
|
||||||
|
|
||||||
# Mocking expected query results
|
|
||||||
expected_result = {
|
|
||||||
'topics': [(1, 5, 10, 15), (2, 8, 12, 20)], # Example topics query result
|
|
||||||
'authors': [(101, 3, 6, 9), (102, 4, 7, 11)], # Example authors query result
|
|
||||||
'communities': [{'id': 1, 'name': 'Дискурс', 'slug': 'discours'}],
|
|
||||||
}
|
|
||||||
|
|
||||||
# Set up mocks to return expected results when queried
|
|
||||||
mock_session.query().select_from().outerjoin().all.side_effect = [
|
|
||||||
expected_result['authors'], # Authors query result
|
|
||||||
expected_result['topics'], # Topics query result
|
|
||||||
]
|
|
||||||
|
|
||||||
# Call the function to test
|
|
||||||
result = query_follows(
|
|
||||||
user_id,
|
|
||||||
session=mock_session,
|
|
||||||
Author=mock_Author,
|
|
||||||
ShoutAuthor=mock_ShoutAuthor,
|
|
||||||
AuthorFollower=mock_AuthorFollower,
|
|
||||||
Topic=mock_Topic,
|
|
||||||
ShoutTopic=mock_ShoutTopic,
|
|
||||||
TopicFollower=mock_TopicFollower,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Assertions
|
|
||||||
assert result['topics'] == expected_result['topics']
|
|
||||||
assert result['authors'] == expected_result['authors']
|
|
||||||
assert result['communities'] == expected_result['communities']
|
|
||||||
|
|
||||||
# Assert that mock session was called with expected queries
|
|
||||||
expected_queries = [
|
|
||||||
mock_session.query(
|
|
||||||
mock_Author.id,
|
|
||||||
mock_ShoutAuthor.author,
|
|
||||||
mock_AuthorFollower.author,
|
|
||||||
mock_AuthorFollower.follower,
|
|
||||||
)
|
|
||||||
.select_from(mock_Author)
|
|
||||||
.outerjoin(mock_ShoutAuthor, mock_Author.id == mock_ShoutAuthor.author)
|
|
||||||
.outerjoin(mock_AuthorFollower, mock_Author.id == mock_AuthorFollower.author)
|
|
||||||
.outerjoin(
|
|
||||||
mock_AuthorFollower, mock_Author.id == mock_AuthorFollower.follower
|
|
||||||
)
|
|
||||||
.all,
|
|
||||||
mock_session.query(
|
|
||||||
mock_Topic.id,
|
|
||||||
mock_ShoutTopic.topic,
|
|
||||||
mock_ShoutTopic.topic,
|
|
||||||
mock_TopicFollower.topic,
|
|
||||||
)
|
|
||||||
.select_from(mock_Topic)
|
|
||||||
.outerjoin(mock_ShoutTopic, mock_Topic.id == mock_ShoutTopic.topic)
|
|
||||||
.outerjoin(mock_ShoutTopic, mock_Topic.id == mock_ShoutTopic.topic)
|
|
||||||
.outerjoin(mock_TopicFollower, mock_Topic.id == mock_TopicFollower.topic)
|
|
||||||
.all,
|
|
||||||
]
|
|
||||||
mock_session.query.assert_has_calls(expected_queries)
|
|
||||||
|
|
||||||
|
|
||||||
# Run the test
|
|
||||||
test_query_follows()
|
|
Loading…
Reference in New Issue
Block a user