2021-06-28 09:08:09 +00:00
|
|
|
from typing import List
|
2021-09-29 12:59:48 +00:00
|
|
|
from datetime import datetime, timedelta
|
2021-12-17 10:22:31 +00:00
|
|
|
from sqlalchemy import Table, Column, Integer, String, ForeignKey, DateTime, Boolean, func
|
2021-08-20 08:08:32 +00:00
|
|
|
from sqlalchemy.orm import relationship
|
2021-09-27 14:59:44 +00:00
|
|
|
from sqlalchemy.orm.attributes import flag_modified
|
2021-12-15 09:17:16 +00:00
|
|
|
from orm import Permission, User, Topic, TopicSubscription
|
2021-09-05 08:56:15 +00:00
|
|
|
from orm.comment import Comment
|
2021-09-29 12:59:48 +00:00
|
|
|
from orm.base import Base, local_session
|
2021-06-28 09:08:09 +00:00
|
|
|
|
2021-09-24 14:39:37 +00:00
|
|
|
from functools import reduce
|
|
|
|
|
2021-09-29 12:59:48 +00:00
|
|
|
import asyncio
|
|
|
|
|
2022-06-21 12:21:02 +00:00
|
|
|
class ShoutCommentsSubscription(Base):
|
|
|
|
__tablename__ = "shout_comments_subscription"
|
|
|
|
|
|
|
|
id = None
|
|
|
|
subscriber = Column(ForeignKey('user.slug'), primary_key = True)
|
|
|
|
shout = Column(ForeignKey('shout.slug'), primary_key = True)
|
|
|
|
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
|
2022-06-29 10:47:53 +00:00
|
|
|
auto = Column(Boolean, nullable=False, default = False)
|
|
|
|
deletedAt: str = Column(DateTime, nullable=True)
|
2022-06-21 12:21:02 +00:00
|
|
|
|
2021-08-28 10:13:50 +00:00
|
|
|
class ShoutAuthor(Base):
|
|
|
|
__tablename__ = "shout_author"
|
|
|
|
|
|
|
|
id = None
|
2021-12-13 07:50:33 +00:00
|
|
|
shout = Column(ForeignKey('shout.slug'), primary_key = True)
|
2022-01-31 11:34:43 +00:00
|
|
|
user = Column(ForeignKey('user.slug'), primary_key = True)
|
2021-09-05 08:56:15 +00:00
|
|
|
|
2021-09-05 07:16:28 +00:00
|
|
|
class ShoutViewer(Base):
|
2021-09-05 08:56:15 +00:00
|
|
|
__tablename__ = "shout_viewer"
|
|
|
|
|
|
|
|
id = None
|
2021-12-13 07:50:33 +00:00
|
|
|
shout = Column(ForeignKey('shout.slug'), primary_key = True)
|
2021-09-05 08:56:15 +00:00
|
|
|
user = Column(ForeignKey('user.id'), primary_key = True)
|
2021-08-20 08:08:32 +00:00
|
|
|
|
2021-08-28 15:12:13 +00:00
|
|
|
class ShoutTopic(Base):
|
|
|
|
__tablename__ = 'shout_topic'
|
|
|
|
|
|
|
|
id = None
|
2021-12-13 07:50:33 +00:00
|
|
|
shout = Column(ForeignKey('shout.slug'), primary_key = True)
|
2021-12-12 09:44:54 +00:00
|
|
|
topic = Column(ForeignKey('topic.slug'), primary_key = True)
|
2021-06-28 09:08:09 +00:00
|
|
|
|
2021-08-30 07:41:59 +00:00
|
|
|
class ShoutRating(Base):
|
2021-08-31 15:15:27 +00:00
|
|
|
__tablename__ = "shout_rating"
|
2021-08-25 21:20:53 +00:00
|
|
|
|
|
|
|
id = None
|
2022-01-14 12:19:57 +00:00
|
|
|
rater = Column(ForeignKey('user.slug'), primary_key = True)
|
2021-12-13 07:50:33 +00:00
|
|
|
shout = Column(ForeignKey('shout.slug'), primary_key = True)
|
2021-09-25 11:40:37 +00:00
|
|
|
ts = Column(DateTime, nullable=False, default = datetime.now, comment="Timestamp")
|
2021-08-31 15:15:27 +00:00
|
|
|
value = Column(Integer)
|
|
|
|
|
2021-09-24 14:39:37 +00:00
|
|
|
class ShoutRatingStorage:
|
|
|
|
|
2021-09-29 13:37:08 +00:00
|
|
|
ratings = []
|
2021-09-24 14:39:37 +00:00
|
|
|
|
2021-09-29 13:37:08 +00:00
|
|
|
lock = asyncio.Lock()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def init(session):
|
2022-01-14 12:19:57 +00:00
|
|
|
ShoutRatingStorage.ratings = session.query(ShoutRating).all()
|
2021-09-29 13:37:08 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2021-12-14 16:37:49 +00:00
|
|
|
async def get_total_rating(shout_slug):
|
2021-09-29 13:37:08 +00:00
|
|
|
async with ShoutRatingStorage.lock:
|
2021-12-13 07:50:33 +00:00
|
|
|
shout_ratings = list(filter(lambda x: x.shout == shout_slug, ShoutRatingStorage.ratings))
|
2021-09-24 14:39:37 +00:00
|
|
|
return reduce((lambda x, y: x + y.value), shout_ratings, 0)
|
|
|
|
|
2021-12-14 16:37:49 +00:00
|
|
|
@staticmethod
|
|
|
|
async def get_ratings(shout_slug):
|
|
|
|
async with ShoutRatingStorage.lock:
|
|
|
|
shout_ratings = list(filter(lambda x: x.shout == shout_slug, ShoutRatingStorage.ratings))
|
|
|
|
return shout_ratings
|
|
|
|
|
2021-09-29 13:37:08 +00:00
|
|
|
@staticmethod
|
|
|
|
async def update_rating(new_rating):
|
|
|
|
async with ShoutRatingStorage.lock:
|
|
|
|
rating = next((x for x in ShoutRatingStorage.ratings \
|
2021-12-13 07:50:33 +00:00
|
|
|
if x.rater == new_rating.rater and x.shout == new_rating.shout), None)
|
2021-09-29 13:37:08 +00:00
|
|
|
if rating:
|
|
|
|
rating.value = new_rating.value
|
|
|
|
rating.ts = new_rating.ts
|
|
|
|
else:
|
|
|
|
ShoutRatingStorage.ratings.append(new_rating)
|
2021-09-24 14:39:37 +00:00
|
|
|
|
|
|
|
|
2021-08-31 15:15:27 +00:00
|
|
|
class ShoutViewByDay(Base):
|
|
|
|
__tablename__ = "shout_view_by_day"
|
|
|
|
|
|
|
|
id = None
|
2021-12-13 07:50:33 +00:00
|
|
|
shout = Column(ForeignKey('shout.slug'), primary_key = True)
|
2021-09-27 14:59:44 +00:00
|
|
|
day = Column(DateTime, primary_key = True, default = datetime.now)
|
2021-08-25 21:20:53 +00:00
|
|
|
value = Column(Integer)
|
|
|
|
|
2021-09-24 14:39:37 +00:00
|
|
|
class ShoutViewStorage:
|
|
|
|
|
2021-12-15 12:01:22 +00:00
|
|
|
view_by_shout = {}
|
2021-09-29 12:59:48 +00:00
|
|
|
this_day_views = {}
|
2021-12-15 12:01:22 +00:00
|
|
|
to_flush = []
|
2021-09-29 12:59:48 +00:00
|
|
|
|
|
|
|
period = 30*60 #sec
|
|
|
|
|
|
|
|
lock = asyncio.Lock()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def init(session):
|
|
|
|
self = ShoutViewStorage
|
2021-12-15 12:01:22 +00:00
|
|
|
views = session.query(ShoutViewByDay).all()
|
|
|
|
for view in views:
|
|
|
|
shout = view.shout
|
|
|
|
value = view.value
|
|
|
|
old_value = self.view_by_shout.get(shout, 0)
|
|
|
|
self.view_by_shout[shout] = old_value + value;
|
|
|
|
if not shout in self.this_day_views:
|
|
|
|
self.this_day_views[shout] = view
|
|
|
|
this_day_view = self.this_day_views[shout]
|
2021-09-27 14:59:44 +00:00
|
|
|
if this_day_view.day < view.day:
|
2021-12-15 12:01:22 +00:00
|
|
|
self.this_day_views[shout] = view
|
2021-09-24 14:39:37 +00:00
|
|
|
|
2021-09-29 12:59:48 +00:00
|
|
|
@staticmethod
|
2021-12-13 07:50:33 +00:00
|
|
|
async def get_view(shout_slug):
|
2021-12-15 12:01:22 +00:00
|
|
|
self = ShoutViewStorage
|
|
|
|
async with self.lock:
|
|
|
|
return self.view_by_shout.get(shout_slug, 0)
|
2021-09-24 14:39:37 +00:00
|
|
|
|
2021-09-29 12:59:48 +00:00
|
|
|
@staticmethod
|
2021-12-13 07:50:33 +00:00
|
|
|
async def inc_view(shout_slug):
|
2021-09-29 12:59:48 +00:00
|
|
|
self = ShoutViewStorage
|
2021-12-15 12:01:22 +00:00
|
|
|
async with self.lock:
|
2021-12-13 07:50:33 +00:00
|
|
|
this_day_view = self.this_day_views.get(shout_slug)
|
2021-09-29 12:59:48 +00:00
|
|
|
day_start = datetime.now().replace(hour = 0, minute = 0, second = 0)
|
|
|
|
if not this_day_view or this_day_view.day < day_start:
|
2021-12-15 12:01:22 +00:00
|
|
|
if this_day_view and getattr(this_day_view, "modified", False):
|
|
|
|
self.to_flush.append(this_day_view)
|
2021-12-13 07:50:33 +00:00
|
|
|
this_day_view = ShoutViewByDay.create(shout = shout_slug, value = 1)
|
|
|
|
self.this_day_views[shout_slug] = this_day_view
|
2021-09-29 12:59:48 +00:00
|
|
|
else:
|
|
|
|
this_day_view.value = this_day_view.value + 1
|
2021-12-15 12:01:22 +00:00
|
|
|
|
|
|
|
this_day_view.modified = True
|
|
|
|
|
|
|
|
old_value = self.view_by_shout.get(shout_slug, 0)
|
|
|
|
self.view_by_shout[shout_slug] = old_value + 1;
|
2021-09-29 12:59:48 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def flush_changes(session):
|
2021-12-15 12:01:22 +00:00
|
|
|
self = ShoutViewStorage
|
|
|
|
async with self.lock:
|
|
|
|
for view in self.this_day_views.values():
|
2021-09-29 12:59:48 +00:00
|
|
|
if getattr(view, "modified", False):
|
|
|
|
session.add(view)
|
|
|
|
flag_modified(view, "value")
|
|
|
|
view.modified = False
|
2021-12-15 12:01:22 +00:00
|
|
|
for view in self.to_flush:
|
|
|
|
session.add(view)
|
|
|
|
self.to_flush.clear()
|
2021-09-27 14:59:44 +00:00
|
|
|
session.commit()
|
|
|
|
|
2021-09-29 12:59:48 +00:00
|
|
|
@staticmethod
|
|
|
|
async def worker():
|
2022-07-01 06:39:19 +00:00
|
|
|
print("[shout.views] worker start")
|
2021-09-29 12:59:48 +00:00
|
|
|
while True:
|
|
|
|
try:
|
2022-07-01 06:39:19 +00:00
|
|
|
print("[shout.views] worker flush changes")
|
2021-09-29 12:59:48 +00:00
|
|
|
with local_session() as session:
|
|
|
|
await ShoutViewStorage.flush_changes(session)
|
|
|
|
except Exception as err:
|
2022-07-01 06:39:19 +00:00
|
|
|
print("[shout.views] worker error: %s" % (err))
|
2021-09-29 12:59:48 +00:00
|
|
|
await asyncio.sleep(ShoutViewStorage.period)
|
|
|
|
|
2021-12-13 16:51:01 +00:00
|
|
|
class TopicStat:
|
|
|
|
shouts_by_topic = {}
|
2021-12-15 09:17:16 +00:00
|
|
|
authors_by_topic = {}
|
|
|
|
subs_by_topic = {}
|
2021-12-15 12:01:22 +00:00
|
|
|
views_by_topic = {}
|
2021-12-13 16:51:01 +00:00
|
|
|
lock = asyncio.Lock()
|
|
|
|
|
|
|
|
period = 30*60 #sec
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def load_stat(session):
|
|
|
|
self = TopicStat
|
2021-12-17 08:37:55 +00:00
|
|
|
|
|
|
|
self.shouts_by_topic = {}
|
|
|
|
self.authors_by_topic = {}
|
|
|
|
self.subs_by_topic = {}
|
|
|
|
self.views_by_topic = {}
|
|
|
|
|
2021-12-13 16:51:01 +00:00
|
|
|
shout_topics = session.query(ShoutTopic)
|
|
|
|
for shout_topic in shout_topics:
|
|
|
|
topic = shout_topic.topic
|
|
|
|
shout = shout_topic.shout
|
|
|
|
if topic in self.shouts_by_topic:
|
|
|
|
self.shouts_by_topic[topic].append(shout)
|
|
|
|
else:
|
|
|
|
self.shouts_by_topic[topic] = [shout]
|
|
|
|
|
2021-12-15 09:17:16 +00:00
|
|
|
authors = await ShoutAuthorStorage.get_authors(shout)
|
|
|
|
if topic in self.authors_by_topic:
|
|
|
|
self.authors_by_topic[topic].update(authors)
|
|
|
|
else:
|
|
|
|
self.authors_by_topic[topic] = set(authors)
|
|
|
|
|
2021-12-15 12:01:22 +00:00
|
|
|
old_views = self.views_by_topic.get(topic, 0)
|
|
|
|
self.views_by_topic[topic] = old_views + await ShoutViewStorage.get_view(shout)
|
|
|
|
|
2021-12-15 09:17:16 +00:00
|
|
|
subs = session.query(TopicSubscription)
|
|
|
|
for sub in subs:
|
|
|
|
topic = sub.topic
|
2022-01-31 11:34:43 +00:00
|
|
|
user = sub.subscriber
|
2021-12-15 09:17:16 +00:00
|
|
|
if topic in self.subs_by_topic:
|
|
|
|
self.subs_by_topic[topic].append(user)
|
|
|
|
else:
|
|
|
|
self.subs_by_topic[topic] = [user]
|
|
|
|
|
2021-12-17 10:22:31 +00:00
|
|
|
@staticmethod
|
2021-12-13 18:05:00 +00:00
|
|
|
async def get_shouts(topic):
|
|
|
|
self = TopicStat
|
|
|
|
async with self.lock:
|
|
|
|
return self.shouts_by_topic.get(topic, [])
|
|
|
|
|
2021-12-13 16:51:01 +00:00
|
|
|
@staticmethod
|
|
|
|
async def get_stat(topic):
|
|
|
|
self = TopicStat
|
|
|
|
async with self.lock:
|
|
|
|
shouts = self.shouts_by_topic.get(topic, [])
|
2021-12-15 09:17:16 +00:00
|
|
|
subs = self.subs_by_topic.get(topic, [])
|
2021-12-15 12:01:22 +00:00
|
|
|
authors = self.authors_by_topic.get(topic, [])
|
|
|
|
views = self.views_by_topic.get(topic, 0)
|
|
|
|
|
|
|
|
return {
|
2021-12-15 09:17:16 +00:00
|
|
|
"shouts" : len(shouts),
|
|
|
|
"authors" : len(authors),
|
2021-12-15 12:01:22 +00:00
|
|
|
"subscriptions" : len(subs),
|
|
|
|
"views" : views
|
2021-12-15 09:17:16 +00:00
|
|
|
}
|
2021-12-13 16:51:01 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def worker():
|
|
|
|
self = TopicStat
|
2022-07-01 06:39:19 +00:00
|
|
|
print("[topic.stats] worker start")
|
2021-12-13 16:51:01 +00:00
|
|
|
while True:
|
|
|
|
try:
|
2022-07-01 06:39:19 +00:00
|
|
|
print("[topic.stats] worker load stat")
|
2021-12-13 16:51:01 +00:00
|
|
|
with local_session() as session:
|
|
|
|
async with self.lock:
|
|
|
|
await self.load_stat(session)
|
|
|
|
except Exception as err:
|
2022-07-01 06:39:19 +00:00
|
|
|
print("[topic.stats] worker error: %s" % (err))
|
2021-12-13 16:51:01 +00:00
|
|
|
await asyncio.sleep(self.period)
|
2021-09-24 14:39:37 +00:00
|
|
|
|
2021-12-13 18:05:00 +00:00
|
|
|
class ShoutAuthorStorage:
|
|
|
|
authors_by_shout = {}
|
|
|
|
lock = asyncio.Lock()
|
|
|
|
|
|
|
|
period = 30*60 #sec
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def load(session):
|
|
|
|
self = ShoutAuthorStorage
|
|
|
|
authors = session.query(ShoutAuthor)
|
|
|
|
for author in authors:
|
|
|
|
user = author.user
|
|
|
|
shout = author.shout
|
|
|
|
if shout in self.authors_by_shout:
|
|
|
|
self.authors_by_shout[shout].append(user)
|
|
|
|
else:
|
|
|
|
self.authors_by_shout[shout] = [user]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def get_authors(shout):
|
|
|
|
self = ShoutAuthorStorage
|
|
|
|
async with self.lock:
|
|
|
|
return self.authors_by_shout.get(shout, [])
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def worker():
|
|
|
|
self = ShoutAuthorStorage
|
2022-07-01 06:39:19 +00:00
|
|
|
print("[shout.authors] worker start")
|
2021-12-13 18:05:00 +00:00
|
|
|
while True:
|
|
|
|
try:
|
2022-07-01 06:39:19 +00:00
|
|
|
print("[shout.authors] worker load stat")
|
2021-12-13 18:05:00 +00:00
|
|
|
with local_session() as session:
|
|
|
|
async with self.lock:
|
|
|
|
await self.load(session)
|
|
|
|
except Exception as err:
|
2022-07-01 06:39:19 +00:00
|
|
|
print("[shout.authors] worker error: %s" % (err))
|
2021-12-13 18:05:00 +00:00
|
|
|
await asyncio.sleep(self.period)
|
|
|
|
|
2021-12-17 10:22:31 +00:00
|
|
|
class CommentStat:
|
|
|
|
stat_by_topic = {}
|
|
|
|
lock = asyncio.Lock()
|
|
|
|
|
|
|
|
period = 30*60 #sec
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def load(session):
|
|
|
|
self = CommentStat
|
|
|
|
|
|
|
|
stats = session.query(Comment.shout, func.count(Comment.id).label("count")).\
|
|
|
|
group_by(Comment.shout)
|
|
|
|
self.stat_by_topic = dict([(stat.shout, stat.count) for stat in stats])
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def get_stat(shout):
|
|
|
|
self = CommentStat
|
|
|
|
async with self.lock:
|
|
|
|
return self.stat_by_topic.get(shout, 0)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def worker():
|
|
|
|
self = CommentStat
|
2022-07-01 06:39:19 +00:00
|
|
|
print("[comment.stats] worker start")
|
2021-12-17 10:22:31 +00:00
|
|
|
while True:
|
|
|
|
try:
|
2022-07-01 06:39:19 +00:00
|
|
|
print("[comment.stats] worker load stat")
|
2021-12-17 10:22:31 +00:00
|
|
|
with local_session() as session:
|
|
|
|
async with self.lock:
|
|
|
|
await self.load(session)
|
|
|
|
except Exception as err:
|
2022-07-01 06:39:19 +00:00
|
|
|
print("[comment.stats] worker error: %s" % (err))
|
2021-12-17 10:22:31 +00:00
|
|
|
await asyncio.sleep(self.period)
|
|
|
|
|
2021-06-28 09:08:09 +00:00
|
|
|
class Shout(Base):
|
2021-08-07 16:14:20 +00:00
|
|
|
__tablename__ = 'shout'
|
2021-06-28 09:08:09 +00:00
|
|
|
|
2022-06-14 19:35:28 +00:00
|
|
|
id = None
|
2021-08-25 21:20:53 +00:00
|
|
|
|
2022-06-14 19:35:28 +00:00
|
|
|
slug: str = Column(String, primary_key=True)
|
2022-07-07 13:55:13 +00:00
|
|
|
community: str = Column(Integer, ForeignKey("community.id"), nullable=False, comment="Community")
|
2021-08-07 16:14:20 +00:00
|
|
|
body: str = Column(String, nullable=False, comment="Body")
|
|
|
|
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
|
|
|
|
updatedAt: str = Column(DateTime, nullable=True, comment="Updated at")
|
2021-12-13 07:50:33 +00:00
|
|
|
replyTo: int = Column(ForeignKey("shout.slug"), nullable=True)
|
|
|
|
versionOf: int = Column(ForeignKey("shout.slug"), nullable=True)
|
2021-08-08 12:23:12 +00:00
|
|
|
tags: str = Column(String, nullable=True)
|
2022-07-07 13:55:13 +00:00
|
|
|
publishedBy: int = Column(ForeignKey("user.id"), nullable=True)
|
2021-08-20 08:08:32 +00:00
|
|
|
publishedAt: str = Column(DateTime, nullable=True)
|
|
|
|
cover: str = Column(String, nullable = True)
|
2021-08-25 21:20:53 +00:00
|
|
|
title: str = Column(String, nullable = True)
|
|
|
|
subtitle: str = Column(String, nullable = True)
|
2021-09-05 07:16:28 +00:00
|
|
|
comments = relationship(Comment)
|
2021-08-20 08:08:32 +00:00
|
|
|
layout: str = Column(String, nullable = True)
|
2021-08-28 10:13:50 +00:00
|
|
|
authors = relationship(lambda: User, secondary=ShoutAuthor.__tablename__) # NOTE: multiple authors
|
2021-08-28 15:12:13 +00:00
|
|
|
topics = relationship(lambda: Topic, secondary=ShoutTopic.__tablename__)
|
2021-12-15 18:13:54 +00:00
|
|
|
mainTopic = Column(ForeignKey("topic.slug"), nullable=True)
|
2021-09-05 07:16:28 +00:00
|
|
|
visibleFor = relationship(lambda: User, secondary=ShoutViewer.__tablename__)
|
2022-07-07 13:55:13 +00:00
|
|
|
draft: bool = Column(Boolean, default=True)
|
|
|
|
oid: str = Column(String, nullable=True)
|
2021-12-17 10:22:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
async def stat(self):
|
|
|
|
return {
|
|
|
|
"views": await ShoutViewStorage.get_view(self.slug),
|
|
|
|
"comments": await CommentStat.get_stat(self.slug),
|
|
|
|
"ratings": await ShoutRatingStorage.get_total_rating(self.slug)
|
|
|
|
}
|