Revert "Revert "store views on the shouts table, remove the viewed table""

This commit is contained in:
Igor Lobanov
2023-08-05 00:21:54 +02:00
committed by GitHub
parent 6e9699513a
commit 9d94464614
5 changed files with 29 additions and 99 deletions

View File

@@ -8,7 +8,7 @@ from auth.credentials import AuthCredentials
from base.exceptions import ObjectNotExist, OperationNotAllowed
from base.orm import local_session
from base.resolvers import query
from orm import ViewedEntry, TopicFollower
from orm import TopicFollower
from orm.reaction import Reaction, ReactionKind
from orm.shout import Shout, ShoutAuthor, ShoutTopic
from orm.user import AuthorFollower
@@ -101,24 +101,8 @@ async def load_shout(_, info, slug=None, shout_id=None):
try:
[shout, reacted_stat, commented_stat, rating_stat, last_comment] = session.execute(q).first()
viewed_stat_query = select().select_from(
Shout
).where(
Shout.id == shout.id
).join(
ViewedEntry
).group_by(
Shout.id
).add_columns(
func.sum(ViewedEntry.amount).label('viewed_stat')
)
# Debug tip:
# print(viewed_stat_query.compile(compile_kwargs={"literal_binds": True}))
viewed_stat = session.execute(viewed_stat_query).scalar()
shout.stat = {
"viewed": viewed_stat,
"viewed": shout.views,
"reacted": reacted_stat,
"commented": commented_stat,
"rating": rating_stat
@@ -133,23 +117,6 @@ async def load_shout(_, info, slug=None, shout_id=None):
raise ObjectNotExist("Slug was not found: %s" % slug)
def add_viewed_stat(session, shouts_map):
viewed_stat_query = select(
Shout.id
).where(
Shout.id.in_(shouts_map.keys())
).join(
ViewedEntry
).group_by(
Shout.id
).add_columns(
func.sum(ViewedEntry.amount).label('viewed_stat')
)
for [shout_id, viewed_stat] in session.execute(viewed_stat_query).unique():
shouts_map[shout_id].stat['viewed'] = viewed_stat
@query.field("loadShouts")
async def load_shouts_by(_, info, options):
"""
@@ -199,15 +166,13 @@ async def load_shouts_by(_, info, options):
for [shout, reacted_stat, commented_stat, rating_stat, last_comment] in session.execute(q).unique():
shouts.append(shout)
shout.stat = {
"viewed": 0,
"viewed": shout.views,
"reacted": reacted_stat,
"commented": commented_stat,
"rating": rating_stat
}
shouts_map[shout.id] = shout
add_viewed_stat(session, shouts_map)
return shouts
@@ -277,13 +242,11 @@ async def get_my_feed(_, info, options):
for [shout, reacted_stat, commented_stat, rating_stat, last_comment] in session.execute(q).unique():
shouts.append(shout)
shout.stat = {
"viewed": 0,
"viewed": shout.views,
"reacted": reacted_stat,
"commented": commented_stat,
"rating": rating_stat
}
shouts_map[shout.id] = shout
add_viewed_stat(session, shouts_map)
return shouts