store views on the shouts table, remove the viewed table

This commit is contained in:
Alexey Kulikov
2023-07-30 23:57:31 +01:00
parent 18e5cd4747
commit 41055d8501
6 changed files with 14 additions and 101 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