shout rating and view with timestamp

This commit is contained in:
knst-kotov 2021-08-31 18:15:27 +03:00
parent def6a59163
commit eba8e6d6af
4 changed files with 36 additions and 11 deletions

View File

@ -4,7 +4,7 @@ from orm.user import User
from orm.message import Message from orm.message import Message
from orm.topic import Topic from orm.topic import Topic
from orm.notification import Notification from orm.notification import Notification
from orm.shout import Shout, ShoutAuthor, ShoutTopic, ShoutRating from orm.shout import Shout, ShoutAuthor, ShoutTopic, ShoutRating, ShoutViewByDay
from orm.base import Base, engine from orm.base import Base, engine
__all__ = ["User", "Role", "Operation", "Permission", "Message", "Shout", "Topic", "Notification"] __all__ = ["User", "Role", "Operation", "Permission", "Message", "Shout", "Topic", "Notification"]

View File

@ -20,11 +20,20 @@ class ShoutTopic(Base):
topic = Column(ForeignKey('topic.id'), primary_key = True) topic = Column(ForeignKey('topic.id'), primary_key = True)
class ShoutRating(Base): class ShoutRating(Base):
__tablename__ = "shout_ratings" __tablename__ = "shout_rating"
id = None id = None
rater_id = Column(ForeignKey('user.id'), primary_key = True) rater_id = Column(ForeignKey('user.id'), primary_key = True)
shout_id = Column(ForeignKey('shout.id'), primary_key = True) shout_id = Column(ForeignKey('shout.id'), primary_key = True)
ts: str = Column(DateTime, nullable=False, default = datetime.now, comment="Timestamp")
value = Column(Integer)
class ShoutViewByDay(Base):
__tablename__ = "shout_view_by_day"
id = None
shout_id = Column(ForeignKey('shout.id'), primary_key = True)
day: str = Column(DateTime, primary_key = True, default = datetime.now)
value = Column(Integer) value = Column(Integer)
class Shout(Base): class Shout(Base):
@ -40,7 +49,6 @@ class Shout(Base):
replyTo: int = Column(ForeignKey("shout.id"), nullable=True) replyTo: int = Column(ForeignKey("shout.id"), nullable=True)
versionOf: int = Column(ForeignKey("shout.id"), nullable=True) versionOf: int = Column(ForeignKey("shout.id"), nullable=True)
tags: str = Column(String, nullable=True) tags: str = Column(String, nullable=True)
views: int = Column(Integer, default=0)
published: bool = Column(Boolean, default=False) published: bool = Column(Boolean, default=False)
publishedAt: str = Column(DateTime, nullable=True) publishedAt: str = Column(DateTime, nullable=True)
cover: str = Column(String, nullable = True) cover: str = Column(String, nullable = True)
@ -49,6 +57,6 @@ class Shout(Base):
layout: str = Column(String, nullable = True) layout: str = Column(String, nullable = True)
authors = relationship(lambda: User, secondary=ShoutAuthor.__tablename__) # NOTE: multiple authors authors = relationship(lambda: User, secondary=ShoutAuthor.__tablename__) # NOTE: multiple authors
topics = relationship(lambda: Topic, secondary=ShoutTopic.__tablename__) topics = relationship(lambda: Topic, secondary=ShoutTopic.__tablename__)
rating: int = Column(Integer, nullable=True, comment="Rating")
ratings = relationship(ShoutRating, foreign_keys=ShoutRating.shout_id) ratings = relationship(ShoutRating, foreign_keys=ShoutRating.shout_id)
views = relationship(ShoutViewByDay)
old_id: str = Column(String, nullable = True) old_id: str = Column(String, nullable = True)

View File

@ -1,4 +1,4 @@
from orm import Shout, ShoutAuthor, ShoutTopic, ShoutRating, User, Community, Resource from orm import Shout, ShoutAuthor, ShoutTopic, ShoutRating, ShoutViewByDay, User, Community, Resource
from orm.base import local_session from orm.base import local_session
from resolvers.base import mutation, query from resolvers.base import mutation, query
@ -8,7 +8,7 @@ from settings import SHOUTS_REPO
import subprocess import subprocess
import asyncio import asyncio
from datetime import datetime from datetime import datetime, timedelta
from pathlib import Path from pathlib import Path
from sqlalchemy import select, func, desc from sqlalchemy import select, func, desc
@ -69,20 +69,37 @@ class GitTask:
@query.field("topShoutsByView") @query.field("topShoutsByView")
async def top_shouts_by_view(_, info, limit): async def top_shouts_by_view(_, info, limit):
month_ago = datetime.now() - timedelta(days = 30)
with local_session() as session: with local_session() as session:
shouts = session.query(Shout).order_by(Shout.views.desc()).limit(limit).all() stmt = select(Shout, func.sum(ShoutViewByDay.value).label("view")).\
join(ShoutViewByDay).\
where(ShoutViewByDay.day > month_ago).\
group_by(Shout.id).\
order_by(desc("view")).\
limit(limit)
shouts = []
for row in session.execute(stmt):
shout = row.Shout
shout.view = row.view
shouts.append(shout)
return shouts return shouts
@query.field("topShoutsByRating") @query.field("topShoutsByRating")
async def top_shouts(_, info, limit): async def top_shouts(_, info, limit):
month_ago = datetime.now() - timedelta(days = 30)
with local_session() as session: with local_session() as session:
stmt = select(Shout, func.sum(ShoutRating.value).label("shout_rating")).\ stmt = select(Shout, func.sum(ShoutRating.value).label("rating")).\
join(ShoutRating).\ join(ShoutRating).\
where(ShoutRating.ts > month_ago).\
group_by(Shout.id).\ group_by(Shout.id).\
order_by(desc("shout_rating")).\ order_by(desc("rating")).\
limit(limit) limit(limit)
shouts = [row.Shout for row in session.execute(stmt)] shouts = []
for row in session.execute(stmt):
shout = row.Shout
shout.rating = row.rating
shouts.append(shout)
return shouts return shouts

View File

@ -203,7 +203,7 @@ type Shout {
versionOf: String versionOf: String
visibleForRoles: [String] # role ids are strings visibleForRoles: [String] # role ids are strings
visibleForUsers: [Int] visibleForUsers: [Int]
views: Int view: Int
old_id: String old_id: String
} }