wip queries

This commit is contained in:
tonyrewin 2022-08-04 20:06:22 +03:00
parent 7fd2bfaf96
commit bcd912ed2e
4 changed files with 26 additions and 9 deletions

View File

@ -22,7 +22,7 @@ async def confirm(*_, confirm_token):
user.save() user.save()
return { "token": auth_token, "user" : user} return { "token": auth_token, "user" : user}
else: else:
return { "error": "Email not confirmed"} return { "error": "email not confirmed"}
@mutation.field("registerUser") @mutation.field("registerUser")
@ -84,8 +84,8 @@ async def login(_, info: GraphQLResolveInfo, email: str, password: str = ""):
with local_session() as session: with local_session() as session:
orm_user = session.query(User).filter(User.email == email).first() orm_user = session.query(User).filter(User.email == email).first()
if orm_user is None: if orm_user is None:
print(f"signIn {email}: invalid email") print(f"signIn {email}: email not found")
return {"error" : "invalid email"} return {"error" : "email not found"}
if not password: if not password:
print(f"signIn {email}: send auth email") print(f"signIn {email}: send auth email")

View File

@ -1,4 +1,4 @@
from sqlalchemy import and_ from sqlalchemy import and_, desc, func, select
from sqlalchemy.orm import selectinload, joinedload from sqlalchemy.orm import selectinload, joinedload
from orm.reaction import Reaction from orm.reaction import Reaction
from orm.base import local_session from orm.base import local_session
@ -8,6 +8,7 @@ from resolvers.base import mutation, query
from auth.authenticate import login_required from auth.authenticate import login_required
from datetime import datetime from datetime import datetime
from storages.reactions import ReactionsStorage from storages.reactions import ReactionsStorage
from storages.shoutscache import ShoutsCache
from storages.viewed import ViewedStorage from storages.viewed import ViewedStorage
from typing import List from typing import List
@ -120,7 +121,17 @@ def get_shout_reactions(_, info, slug) -> List[Shout]:
def get_all_reactions(_, info, page=1, size=10) -> List[Reaction]: def get_all_reactions(_, info, page=1, size=10) -> List[Reaction]:
reactions = [] reactions = []
with local_session() as session: with local_session() as session:
q = session.query(Reaction).\
# reactions = session.execute(select(Reaction, func.max(Reaction.createdAt).label("reactionCreatedAt")).\
# join(Shout, Reaction.shout == Shout.slug).\
# group_by(Reaction.id).\
# order_by(desc("reactionCreatedAt")).\
# join( User, Reaction.createdBy == User.slug ).\
# join( Shout, Reaction.shout == Shout.slug ).\
# filter( Reaction.deletedAt == None ).\
# limit(size).offset(page * size).all())
reactions = session.query(Reaction).\
options( options(
joinedload(User), joinedload(User),
joinedload(Shout) joinedload(Shout)

View File

@ -81,7 +81,7 @@ class ReactionsStorage:
rating_by_shout[shout] = 0 rating_by_shout[shout] = 0
shout_reactions_by_kinds = session.query(Reaction).\ shout_reactions_by_kinds = session.query(Reaction).\
where(and_(Reaction.deletedAt == None, Reaction.shout == shout)).\ where(and_(Reaction.deletedAt == None, Reaction.shout == shout)).\
group_by(Reaction.kind) group_by(Reaction.kind, Reaction.id).all()
for kind, reactions in shout_reactions_by_kinds: for kind, reactions in shout_reactions_by_kinds:
rating_by_shout[shout] += len(reactions) * kind_to_rate(kind) rating_by_shout[shout] += len(reactions) * kind_to_rate(kind)
async with ReactionsStorage.lock: async with ReactionsStorage.lock:
@ -144,10 +144,13 @@ class ReactionsStorage:
try: try:
with local_session() as session: with local_session() as session:
await ReactionsStorage.prepare_all(session) await ReactionsStorage.prepare_all(session)
print("[storage.reactions] all reactions prepared")
await ReactionsStorage.prepare_by_shout(session) await ReactionsStorage.prepare_by_shout(session)
print("[storage.reactions] reactions by shouts prepared")
await ReactionsStorage.calc_ratings(session) await ReactionsStorage.calc_ratings(session)
print("[storage.reactions] reactions ratings prepared")
await ReactionsStorage.prepare_by_topic(session) await ReactionsStorage.prepare_by_topic(session)
print("[storage.reactions] updated") print("[storage.reactions] reactions topics prepared")
except Exception as err: except Exception as err:
print("[storage.reactions] errror: %s" % (err)) print("[storage.reactions] errror: %s" % (err))
await asyncio.sleep(ReactionsStorage.period) await asyncio.sleep(ReactionsStorage.period)

View File

@ -52,8 +52,11 @@ class ShoutsCache:
async def prepare_recent_reacted(): async def prepare_recent_reacted():
with local_session() as session: with local_session() as session:
stmt = select(Shout, func.max(Reaction.createdAt).label("reactionCreatedAt")).\ stmt = select(Shout, func.max(Reaction.createdAt).label("reactionCreatedAt")).\
options(selectinload(Shout.authors), selectinload(Shout.topics)).\ options(
join(Reaction).\ selectinload(Shout.authors),
selectinload(Shout.topics),
).\
join(Reaction, Reaction.shout == Shout.slug).\
where(and_(Shout.publishedAt != None, Reaction.deletedAt == None)).\ where(and_(Shout.publishedAt != None, Reaction.deletedAt == None)).\
group_by(Shout.slug).\ group_by(Shout.slug).\
order_by(desc("reactionCreatedAt")).\ order_by(desc("reactionCreatedAt")).\