format and lint orm
This commit is contained in:
@@ -1,35 +1,33 @@
|
||||
|
||||
import asyncio
|
||||
from sqlalchemy.orm import selectinload
|
||||
from orm.rbac import Role
|
||||
|
||||
|
||||
class RoleStorage:
|
||||
roles = {}
|
||||
lock = asyncio.Lock()
|
||||
roles = {}
|
||||
lock = asyncio.Lock()
|
||||
|
||||
@staticmethod
|
||||
def init(session):
|
||||
self = RoleStorage
|
||||
roles = session.query(Role).\
|
||||
options(selectinload(Role.permissions)).all()
|
||||
self.roles = dict([(role.id, role) for role in roles])
|
||||
print('[auth.roles] %d precached' % len(roles))
|
||||
|
||||
@staticmethod
|
||||
def init(session):
|
||||
self = RoleStorage
|
||||
roles = session.query(Role).options(selectinload(Role.permissions)).all()
|
||||
self.roles = dict([(role.id, role) for role in roles])
|
||||
print("[auth.roles] %d precached" % len(roles))
|
||||
|
||||
@staticmethod
|
||||
async def get_role(id):
|
||||
self = RoleStorage
|
||||
async with self.lock:
|
||||
return self.roles.get(id)
|
||||
@staticmethod
|
||||
async def get_role(id):
|
||||
self = RoleStorage
|
||||
async with self.lock:
|
||||
return self.roles.get(id)
|
||||
|
||||
@staticmethod
|
||||
async def add_role(role):
|
||||
self = RoleStorage
|
||||
async with self.lock:
|
||||
self.roles[id] = role
|
||||
@staticmethod
|
||||
async def add_role(role):
|
||||
self = RoleStorage
|
||||
async with self.lock:
|
||||
self.roles[id] = role
|
||||
|
||||
@staticmethod
|
||||
async def del_role(id):
|
||||
self = RoleStorage
|
||||
async with self.lock:
|
||||
del self.roles[id]
|
||||
@staticmethod
|
||||
async def del_role(id):
|
||||
self = RoleStorage
|
||||
async with self.lock:
|
||||
del self.roles[id]
|
||||
|
@@ -4,47 +4,50 @@ from orm.user import User
|
||||
|
||||
|
||||
class UserStorage:
|
||||
users = {}
|
||||
lock = asyncio.Lock()
|
||||
users = {}
|
||||
lock = asyncio.Lock()
|
||||
|
||||
@staticmethod
|
||||
def init(session):
|
||||
self = UserStorage
|
||||
users = session.query(User).\
|
||||
options(selectinload(User.roles), selectinload(User.ratings)).all()
|
||||
self.users = dict([(user.id, user) for user in users])
|
||||
print('[auth.users] %d precached' % len(self.users))
|
||||
@staticmethod
|
||||
def init(session):
|
||||
self = UserStorage
|
||||
users = (
|
||||
session.query(User)
|
||||
.options(selectinload(User.roles), selectinload(User.ratings))
|
||||
.all()
|
||||
)
|
||||
self.users = dict([(user.id, user) for user in users])
|
||||
print("[auth.users] %d precached" % len(self.users))
|
||||
|
||||
@staticmethod
|
||||
async def get_user(id):
|
||||
self = UserStorage
|
||||
async with self.lock:
|
||||
return self.users.get(id)
|
||||
@staticmethod
|
||||
async def get_user(id):
|
||||
self = UserStorage
|
||||
async with self.lock:
|
||||
return self.users.get(id)
|
||||
|
||||
@staticmethod
|
||||
async def get_all_users():
|
||||
self = UserStorage
|
||||
async with self.lock:
|
||||
aaa = list(self.users.values())
|
||||
aaa.sort(key=lambda user: user.createdAt)
|
||||
return aaa
|
||||
@staticmethod
|
||||
async def get_all_users():
|
||||
self = UserStorage
|
||||
async with self.lock:
|
||||
aaa = list(self.users.values())
|
||||
aaa.sort(key=lambda user: user.createdAt)
|
||||
return aaa
|
||||
|
||||
@staticmethod
|
||||
async def get_user_by_slug(slug):
|
||||
self = UserStorage
|
||||
async with self.lock:
|
||||
for user in self.users.values():
|
||||
if user.slug == slug:
|
||||
return user
|
||||
@staticmethod
|
||||
async def get_user_by_slug(slug):
|
||||
self = UserStorage
|
||||
async with self.lock:
|
||||
for user in self.users.values():
|
||||
if user.slug == slug:
|
||||
return user
|
||||
|
||||
@staticmethod
|
||||
async def add_user(user):
|
||||
self = UserStorage
|
||||
async with self.lock:
|
||||
self.users[user.id] = user
|
||||
@staticmethod
|
||||
async def add_user(user):
|
||||
self = UserStorage
|
||||
async with self.lock:
|
||||
self.users[user.id] = user
|
||||
|
||||
@staticmethod
|
||||
async def del_user(id):
|
||||
self = UserStorage
|
||||
async with self.lock:
|
||||
del self.users[id]
|
||||
@staticmethod
|
||||
async def del_user(id):
|
||||
self = UserStorage
|
||||
async with self.lock:
|
||||
del self.users[id]
|
||||
|
@@ -2,180 +2,209 @@ import asyncio
|
||||
from datetime import datetime
|
||||
from sqlalchemy.types import Enum
|
||||
from sqlalchemy import Column, DateTime, ForeignKey, Boolean
|
||||
|
||||
# from sqlalchemy.orm.attributes import flag_modified
|
||||
from sqlalchemy import Enum
|
||||
import enum
|
||||
from base.orm import Base, local_session
|
||||
from orm.topic import ShoutTopic
|
||||
|
||||
|
||||
class ReactionKind(enum.Enum):
|
||||
AGREE = 1 # +1
|
||||
DISAGREE = 2 # -1
|
||||
PROOF = 3 # +1
|
||||
DISPROOF = 4 # -1
|
||||
ASK = 5 # +0 bookmark
|
||||
PROPOSE = 6 # +0
|
||||
QUOTE = 7 # +0 bookmark
|
||||
COMMENT = 8 # +0
|
||||
ACCEPT = 9 # +1
|
||||
REJECT = 0 # -1
|
||||
LIKE = 11 # +1
|
||||
DISLIKE = 12 # -1
|
||||
# TYPE = <reaction index> # rating diff
|
||||
AGREE = 1 # +1
|
||||
DISAGREE = 2 # -1
|
||||
PROOF = 3 # +1
|
||||
DISPROOF = 4 # -1
|
||||
ASK = 5 # +0 bookmark
|
||||
PROPOSE = 6 # +0
|
||||
QUOTE = 7 # +0 bookmark
|
||||
COMMENT = 8 # +0
|
||||
ACCEPT = 9 # +1
|
||||
REJECT = 0 # -1
|
||||
LIKE = 11 # +1
|
||||
DISLIKE = 12 # -1
|
||||
# TYPE = <reaction index> # rating diff
|
||||
|
||||
|
||||
def kind_to_rate(kind) -> int:
|
||||
if kind in [
|
||||
ReactionKind.AGREE,
|
||||
ReactionKind.LIKE,
|
||||
ReactionKind.PROOF,
|
||||
ReactionKind.ACCEPT
|
||||
]: return 1
|
||||
elif kind in [
|
||||
ReactionKind.DISAGREE,
|
||||
ReactionKind.DISLIKE,
|
||||
ReactionKind.DISPROOF,
|
||||
ReactionKind.REJECT
|
||||
]: return -1
|
||||
else: return 0
|
||||
|
||||
class ReactedByDay(Base):
|
||||
__tablename__ = "reacted_by_day"
|
||||
if kind in [
|
||||
ReactionKind.AGREE,
|
||||
ReactionKind.LIKE,
|
||||
ReactionKind.PROOF,
|
||||
ReactionKind.ACCEPT,
|
||||
]:
|
||||
return 1
|
||||
elif kind in [
|
||||
ReactionKind.DISAGREE,
|
||||
ReactionKind.DISLIKE,
|
||||
ReactionKind.DISPROOF,
|
||||
ReactionKind.REJECT,
|
||||
]:
|
||||
return -1
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
class ReactedByDay(Base):
|
||||
__tablename__ = "reacted_by_day"
|
||||
|
||||
id = None
|
||||
reaction = Column(ForeignKey("reaction.id"), primary_key=True)
|
||||
shout = Column(ForeignKey("shout.slug"), primary_key=True)
|
||||
replyTo = Column(ForeignKey("reaction.id"), nullable=True)
|
||||
kind: int = Column(Enum(ReactionKind), nullable=False, comment="Reaction kind")
|
||||
day = Column(DateTime, primary_key=True, default=datetime.now)
|
||||
comment = Column(Boolean, default=False)
|
||||
|
||||
id = None
|
||||
reaction = Column(ForeignKey("reaction.id"), primary_key = True)
|
||||
shout = Column(ForeignKey('shout.slug'), primary_key=True)
|
||||
replyTo = Column(ForeignKey('reaction.id'), nullable=True)
|
||||
kind: int = Column(Enum(ReactionKind), nullable=False, comment="Reaction kind")
|
||||
day = Column(DateTime, primary_key=True, default=datetime.now)
|
||||
comment = Column(Boolean, default=False)
|
||||
|
||||
|
||||
class ReactedStorage:
|
||||
reacted = {
|
||||
'shouts': {},
|
||||
'topics': {},
|
||||
'reactions': {}
|
||||
}
|
||||
rating = {
|
||||
'shouts': {},
|
||||
'topics': {},
|
||||
'reactions': {}
|
||||
}
|
||||
reactions = []
|
||||
to_flush = []
|
||||
period = 30*60 # sec
|
||||
lock = asyncio.Lock()
|
||||
reacted = {"shouts": {}, "topics": {}, "reactions": {}}
|
||||
rating = {"shouts": {}, "topics": {}, "reactions": {}}
|
||||
reactions = []
|
||||
to_flush = []
|
||||
period = 30 * 60 # sec
|
||||
lock = asyncio.Lock()
|
||||
|
||||
@staticmethod
|
||||
async def get_shout(shout_slug):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
return self.reacted['shouts'].get(shout_slug, [])
|
||||
|
||||
@staticmethod
|
||||
async def get_topic(topic_slug):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
return self.reacted['topics'].get(topic_slug, [])
|
||||
@staticmethod
|
||||
async def get_shout(shout_slug):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
return self.reacted["shouts"].get(shout_slug, [])
|
||||
|
||||
@staticmethod
|
||||
async def get_comments(shout_slug):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
return list(filter(lambda r: r.comment, self.reacted['shouts'].get(shout_slug, [])))
|
||||
@staticmethod
|
||||
async def get_topic(topic_slug):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
return self.reacted["topics"].get(topic_slug, [])
|
||||
|
||||
@staticmethod
|
||||
async def get_topic_comments(topic_slug):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
return list(filter(lambda r: r.comment, self.reacted['topics'].get(topic_slug, [])))
|
||||
@staticmethod
|
||||
async def get_comments(shout_slug):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
return list(
|
||||
filter(lambda r: r.comment, self.reacted["shouts"].get(shout_slug, []))
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
async def get_reaction_comments(reaction_id):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
return list(filter(lambda r: r.comment, self.reacted['reactions'].get(reaction_id)))
|
||||
@staticmethod
|
||||
async def get_topic_comments(topic_slug):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
return list(
|
||||
filter(lambda r: r.comment, self.reacted["topics"].get(topic_slug, []))
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
async def get_reaction(reaction_id):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
return self.reacted['reactions'].get(reaction_id, [])
|
||||
@staticmethod
|
||||
async def get_reaction_comments(reaction_id):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
return list(
|
||||
filter(lambda r: r.comment, self.reacted["reactions"].get(reaction_id))
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
async def get_rating(shout_slug):
|
||||
self = ReactedStorage
|
||||
rating = 0
|
||||
async with self.lock:
|
||||
for r in self.reacted['shouts'].get(shout_slug, []):
|
||||
rating = rating + kind_to_rate(r.kind)
|
||||
return rating
|
||||
@staticmethod
|
||||
async def get_reaction(reaction_id):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
return self.reacted["reactions"].get(reaction_id, [])
|
||||
|
||||
@staticmethod
|
||||
async def get_topic_rating(topic_slug):
|
||||
self = ReactedStorage
|
||||
rating = 0
|
||||
async with self.lock:
|
||||
for r in self.reacted['topics'].get(topic_slug, []):
|
||||
rating = rating + kind_to_rate(r.kind)
|
||||
return rating
|
||||
@staticmethod
|
||||
async def get_rating(shout_slug):
|
||||
self = ReactedStorage
|
||||
rating = 0
|
||||
async with self.lock:
|
||||
for r in self.reacted["shouts"].get(shout_slug, []):
|
||||
rating = rating + kind_to_rate(r.kind)
|
||||
return rating
|
||||
|
||||
@staticmethod
|
||||
async def get_reaction_rating(reaction_id):
|
||||
self = ReactedStorage
|
||||
rating = 0
|
||||
async with self.lock:
|
||||
for r in self.reacted['reactions'].get(reaction_id, []):
|
||||
rating = rating + kind_to_rate(r.kind)
|
||||
return rating
|
||||
@staticmethod
|
||||
async def get_topic_rating(topic_slug):
|
||||
self = ReactedStorage
|
||||
rating = 0
|
||||
async with self.lock:
|
||||
for r in self.reacted["topics"].get(topic_slug, []):
|
||||
rating = rating + kind_to_rate(r.kind)
|
||||
return rating
|
||||
|
||||
@staticmethod
|
||||
async def increment(reaction):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
with local_session() as session:
|
||||
r = {
|
||||
"day": datetime.now().replace(hour=0, minute=0, second=0, microsecond=0),
|
||||
"reaction": reaction.id,
|
||||
"kind": reaction.kind,
|
||||
"shout": reaction.shout
|
||||
}
|
||||
if reaction.replyTo: r['replyTo'] = reaction.replyTo
|
||||
if reaction.body: r['comment'] = True
|
||||
reaction = ReactedByDay.create(**r)
|
||||
self.reacted['shouts'][reaction.shout] = self.reacted['shouts'].get(reaction.shout, [])
|
||||
self.reacted['shouts'][reaction.shout].append(reaction)
|
||||
if reaction.replyTo:
|
||||
self.reacted['reaction'][reaction.replyTo] = self.reacted['reactions'].get(reaction.shout, [])
|
||||
self.reacted['reaction'][reaction.replyTo].append(reaction)
|
||||
self.rating['reactions'][reaction.replyTo] = self.rating['reactions'].get(reaction.replyTo, 0) + kind_to_rate(reaction.kind)
|
||||
else:
|
||||
self.rating['shouts'][reaction.replyTo] = self.rating['shouts'].get(reaction.shout, 0) + kind_to_rate(reaction.kind)
|
||||
@staticmethod
|
||||
async def get_reaction_rating(reaction_id):
|
||||
self = ReactedStorage
|
||||
rating = 0
|
||||
async with self.lock:
|
||||
for r in self.reacted["reactions"].get(reaction_id, []):
|
||||
rating = rating + kind_to_rate(r.kind)
|
||||
return rating
|
||||
|
||||
@staticmethod
|
||||
def init(session):
|
||||
self = ReactedStorage
|
||||
all_reactions = session.query(ReactedByDay).all()
|
||||
print('[stat.reacted] %d reactions total' % len(all_reactions))
|
||||
for reaction in all_reactions:
|
||||
shout = reaction.shout
|
||||
topics = session.query(ShoutTopic.topic).where(ShoutTopic.shout == shout).all()
|
||||
kind = reaction.kind
|
||||
|
||||
self.reacted['shouts'][shout] = self.reacted['shouts'].get(shout, [])
|
||||
self.reacted['shouts'][shout].append(reaction)
|
||||
self.rating['shouts'][shout] = self.rating['shouts'].get(shout, 0) + kind_to_rate(kind)
|
||||
|
||||
for t in topics:
|
||||
self.reacted['topics'][t] = self.reacted['topics'].get(t, [])
|
||||
self.reacted['topics'][t].append(reaction)
|
||||
self.rating['topics'][t] = self.rating['topics'].get(t, 0) + kind_to_rate(kind) # rating
|
||||
|
||||
if reaction.replyTo:
|
||||
self.reacted['reactions'][reaction.replyTo] = self.reacted['reactions'].get(reaction.replyTo, [])
|
||||
self.reacted['reactions'][reaction.replyTo].append(reaction)
|
||||
self.rating['reactions'][reaction.replyTo] = self.rating['reactions'].get(reaction.replyTo, 0) + kind_to_rate(reaction.kind)
|
||||
ttt = self.reacted['topics'].values()
|
||||
print('[stat.reacted] %d topics reacted' % len(ttt))
|
||||
print('[stat.reacted] %d shouts reacted' % len(self.reacted['shouts']))
|
||||
print('[stat.reacted] %d reactions reacted' % len(self.reacted['reactions']))
|
||||
@staticmethod
|
||||
async def increment(reaction):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
with local_session() as session:
|
||||
r = {
|
||||
"day": datetime.now().replace(
|
||||
hour=0, minute=0, second=0, microsecond=0
|
||||
),
|
||||
"reaction": reaction.id,
|
||||
"kind": reaction.kind,
|
||||
"shout": reaction.shout,
|
||||
}
|
||||
if reaction.replyTo:
|
||||
r["replyTo"] = reaction.replyTo
|
||||
if reaction.body:
|
||||
r["comment"] = True
|
||||
reaction = ReactedByDay.create(**r)
|
||||
self.reacted["shouts"][reaction.shout] = self.reacted["shouts"].get(
|
||||
reaction.shout, []
|
||||
)
|
||||
self.reacted["shouts"][reaction.shout].append(reaction)
|
||||
if reaction.replyTo:
|
||||
self.reacted["reaction"][reaction.replyTo] = self.reacted[
|
||||
"reactions"
|
||||
].get(reaction.shout, [])
|
||||
self.reacted["reaction"][reaction.replyTo].append(reaction)
|
||||
self.rating["reactions"][reaction.replyTo] = self.rating[
|
||||
"reactions"
|
||||
].get(reaction.replyTo, 0) + kind_to_rate(reaction.kind)
|
||||
else:
|
||||
self.rating["shouts"][reaction.replyTo] = self.rating["shouts"].get(
|
||||
reaction.shout, 0
|
||||
) + kind_to_rate(reaction.kind)
|
||||
|
||||
@staticmethod
|
||||
def init(session):
|
||||
self = ReactedStorage
|
||||
all_reactions = session.query(ReactedByDay).all()
|
||||
print("[stat.reacted] %d reactions total" % len(all_reactions))
|
||||
for reaction in all_reactions:
|
||||
shout = reaction.shout
|
||||
topics = (
|
||||
session.query(ShoutTopic.topic).where(ShoutTopic.shout == shout).all()
|
||||
)
|
||||
kind = reaction.kind
|
||||
|
||||
self.reacted["shouts"][shout] = self.reacted["shouts"].get(shout, [])
|
||||
self.reacted["shouts"][shout].append(reaction)
|
||||
self.rating["shouts"][shout] = self.rating["shouts"].get(
|
||||
shout, 0
|
||||
) + kind_to_rate(kind)
|
||||
|
||||
for t in topics:
|
||||
self.reacted["topics"][t] = self.reacted["topics"].get(t, [])
|
||||
self.reacted["topics"][t].append(reaction)
|
||||
self.rating["topics"][t] = self.rating["topics"].get(
|
||||
t, 0
|
||||
) + kind_to_rate(
|
||||
kind
|
||||
) # rating
|
||||
|
||||
if reaction.replyTo:
|
||||
self.reacted["reactions"][reaction.replyTo] = self.reacted[
|
||||
"reactions"
|
||||
].get(reaction.replyTo, [])
|
||||
self.reacted["reactions"][reaction.replyTo].append(reaction)
|
||||
self.rating["reactions"][reaction.replyTo] = self.rating[
|
||||
"reactions"
|
||||
].get(reaction.replyTo, 0) + kind_to_rate(reaction.kind)
|
||||
ttt = self.reacted["topics"].values()
|
||||
print("[stat.reacted] %d topics reacted" % len(ttt))
|
||||
print("[stat.reacted] %d shouts reacted" % len(self.reacted["shouts"]))
|
||||
print("[stat.reacted] %d reactions reacted" % len(self.reacted["reactions"]))
|
||||
|
@@ -5,81 +5,84 @@ from services.stat.viewed import ViewedStorage
|
||||
from services.zine.shoutauthor import ShoutAuthorStorage
|
||||
from orm.topic import ShoutTopic, TopicFollower
|
||||
from typing import Dict
|
||||
|
||||
|
||||
class TopicStat:
|
||||
shouts_by_topic = {}
|
||||
authors_by_topic = {}
|
||||
followers_by_topic = {}
|
||||
lock = asyncio.Lock()
|
||||
period = 30*60 #sec
|
||||
shouts_by_topic = {}
|
||||
authors_by_topic = {}
|
||||
followers_by_topic = {}
|
||||
lock = asyncio.Lock()
|
||||
period = 30 * 60 # sec
|
||||
|
||||
@staticmethod
|
||||
async def load_stat(session):
|
||||
self = TopicStat
|
||||
self.shouts_by_topic = {}
|
||||
self.authors_by_topic = {}
|
||||
shout_topics = session.query(ShoutTopic).all()
|
||||
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, ]
|
||||
@staticmethod
|
||||
async def load_stat(session):
|
||||
self = TopicStat
|
||||
self.shouts_by_topic = {}
|
||||
self.authors_by_topic = {}
|
||||
shout_topics = session.query(ShoutTopic).all()
|
||||
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,
|
||||
]
|
||||
|
||||
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)
|
||||
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)
|
||||
|
||||
print('[stat.topics] authors sorted')
|
||||
print('[stat.topics] shouts sorted')
|
||||
|
||||
self.followers_by_topic = {}
|
||||
followings = session.query(TopicFollower)
|
||||
for flw in followings:
|
||||
topic = flw.topic
|
||||
user = flw.follower
|
||||
if topic in self.followers_by_topic:
|
||||
self.followers_by_topic[topic].append(user)
|
||||
else:
|
||||
self.followers_by_topic[topic] = [user]
|
||||
print('[stat.topics] followers sorted')
|
||||
print("[stat.topics] authors sorted")
|
||||
print("[stat.topics] shouts sorted")
|
||||
|
||||
@staticmethod
|
||||
async def get_shouts(topic):
|
||||
self = TopicStat
|
||||
async with self.lock:
|
||||
return self.shouts_by_topic.get(topic, [])
|
||||
self.followers_by_topic = {}
|
||||
followings = session.query(TopicFollower)
|
||||
for flw in followings:
|
||||
topic = flw.topic
|
||||
user = flw.follower
|
||||
if topic in self.followers_by_topic:
|
||||
self.followers_by_topic[topic].append(user)
|
||||
else:
|
||||
self.followers_by_topic[topic] = [user]
|
||||
print("[stat.topics] followers sorted")
|
||||
|
||||
@staticmethod
|
||||
async def get_stat(topic):
|
||||
self = TopicStat
|
||||
async with self.lock:
|
||||
shouts = self.shouts_by_topic.get(topic, [])
|
||||
followers = self.followers_by_topic.get(topic, [])
|
||||
authors = self.authors_by_topic.get(topic, [])
|
||||
@staticmethod
|
||||
async def get_shouts(topic):
|
||||
self = TopicStat
|
||||
async with self.lock:
|
||||
return self.shouts_by_topic.get(topic, [])
|
||||
|
||||
return {
|
||||
"shouts" : len(shouts),
|
||||
"authors" : len(authors),
|
||||
"followers" : len(followers),
|
||||
"viewed": await ViewedStorage.get_topic(topic),
|
||||
"reacted" : len(await ReactedStorage.get_topic(topic)),
|
||||
"commented": len(await ReactedStorage.get_topic_comments(topic)),
|
||||
"rating" : await ReactedStorage.get_topic_rating(topic),
|
||||
}
|
||||
@staticmethod
|
||||
async def get_stat(topic):
|
||||
self = TopicStat
|
||||
async with self.lock:
|
||||
shouts = self.shouts_by_topic.get(topic, [])
|
||||
followers = self.followers_by_topic.get(topic, [])
|
||||
authors = self.authors_by_topic.get(topic, [])
|
||||
|
||||
@staticmethod
|
||||
async def worker():
|
||||
self = TopicStat
|
||||
while True:
|
||||
try:
|
||||
with local_session() as session:
|
||||
async with self.lock:
|
||||
await self.load_stat(session)
|
||||
print("[stat.topics] periodical update")
|
||||
except Exception as err:
|
||||
print("[stat.topics] errror: %s" % (err))
|
||||
await asyncio.sleep(self.period)
|
||||
return {
|
||||
"shouts": len(shouts),
|
||||
"authors": len(authors),
|
||||
"followers": len(followers),
|
||||
"viewed": await ViewedStorage.get_topic(topic),
|
||||
"reacted": len(await ReactedStorage.get_topic(topic)),
|
||||
"commented": len(await ReactedStorage.get_topic_comments(topic)),
|
||||
"rating": await ReactedStorage.get_topic_rating(topic),
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
async def worker():
|
||||
self = TopicStat
|
||||
while True:
|
||||
try:
|
||||
with local_session() as session:
|
||||
async with self.lock:
|
||||
await self.load_stat(session)
|
||||
print("[stat.topics] periodical update")
|
||||
except Exception as err:
|
||||
print("[stat.topics] errror: %s" % (err))
|
||||
await asyncio.sleep(self.period)
|
||||
|
@@ -7,109 +7,112 @@ from orm.topic import ShoutTopic
|
||||
|
||||
|
||||
class ViewedByDay(Base):
|
||||
__tablename__ = "viewed_by_day"
|
||||
__tablename__ = "viewed_by_day"
|
||||
|
||||
id = None
|
||||
shout = Column(ForeignKey('shout.slug'), primary_key=True)
|
||||
day = Column(DateTime, primary_key=True, default=datetime.now)
|
||||
value = Column(Integer)
|
||||
id = None
|
||||
shout = Column(ForeignKey("shout.slug"), primary_key=True)
|
||||
day = Column(DateTime, primary_key=True, default=datetime.now)
|
||||
value = Column(Integer)
|
||||
|
||||
|
||||
class ViewedStorage:
|
||||
viewed = {
|
||||
'shouts': {},
|
||||
'topics': {},
|
||||
'reactions': {}
|
||||
}
|
||||
this_day_views = {}
|
||||
to_flush = []
|
||||
period = 30*60 # sec
|
||||
lock = asyncio.Lock()
|
||||
viewed = {"shouts": {}, "topics": {}, "reactions": {}}
|
||||
this_day_views = {}
|
||||
to_flush = []
|
||||
period = 30 * 60 # sec
|
||||
lock = asyncio.Lock()
|
||||
|
||||
@staticmethod
|
||||
def init(session):
|
||||
self = ViewedStorage
|
||||
views = session.query(ViewedByDay).all()
|
||||
@staticmethod
|
||||
def init(session):
|
||||
self = ViewedStorage
|
||||
views = session.query(ViewedByDay).all()
|
||||
|
||||
for view in views:
|
||||
shout = view.shout
|
||||
topics = session.query(ShoutTopic.topic).filter(ShoutTopic.shout == shout).all()
|
||||
value = view.value
|
||||
if shout:
|
||||
old_value = self.viewed['shouts'].get(shout, 0)
|
||||
self.viewed['shouts'][shout] = old_value + value
|
||||
for t in topics:
|
||||
old_topic_value = self.viewed['topics'].get(t, 0)
|
||||
self.viewed['topics'][t] = old_topic_value + value
|
||||
if not shout in self.this_day_views:
|
||||
self.this_day_views[shout] = view
|
||||
this_day_view = self.this_day_views[shout]
|
||||
if this_day_view.day < view.day:
|
||||
self.this_day_views[shout] = view
|
||||
|
||||
print('[stat.viewed] %d shouts viewed' % len(views))
|
||||
for view in views:
|
||||
shout = view.shout
|
||||
topics = (
|
||||
session.query(ShoutTopic.topic).filter(ShoutTopic.shout == shout).all()
|
||||
)
|
||||
value = view.value
|
||||
if shout:
|
||||
old_value = self.viewed["shouts"].get(shout, 0)
|
||||
self.viewed["shouts"][shout] = old_value + value
|
||||
for t in topics:
|
||||
old_topic_value = self.viewed["topics"].get(t, 0)
|
||||
self.viewed["topics"][t] = old_topic_value + value
|
||||
if not shout in self.this_day_views:
|
||||
self.this_day_views[shout] = view
|
||||
this_day_view = self.this_day_views[shout]
|
||||
if this_day_view.day < view.day:
|
||||
self.this_day_views[shout] = view
|
||||
|
||||
@staticmethod
|
||||
async def get_shout(shout_slug):
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
return self.viewed['shouts'].get(shout_slug, 0)
|
||||
print("[stat.viewed] %d shouts viewed" % len(views))
|
||||
|
||||
@staticmethod
|
||||
async def get_topic(topic_slug):
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
return self.viewed['topics'].get(topic_slug, 0)
|
||||
@staticmethod
|
||||
async def get_shout(shout_slug):
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
return self.viewed["shouts"].get(shout_slug, 0)
|
||||
|
||||
@staticmethod
|
||||
async def get_reaction(reaction_id):
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
return self.viewed['reactions'].get(reaction_id, 0)
|
||||
@staticmethod
|
||||
async def get_topic(topic_slug):
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
return self.viewed["topics"].get(topic_slug, 0)
|
||||
|
||||
@staticmethod
|
||||
async def increment(shout_slug):
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
this_day_view = self.this_day_views.get(shout_slug)
|
||||
day_start = datetime.now().replace(hour=0, minute=0, second=0)
|
||||
if not this_day_view or this_day_view.day < day_start:
|
||||
if this_day_view and getattr(this_day_view, "modified", False):
|
||||
self.to_flush.append(this_day_view)
|
||||
this_day_view = ViewedByDay.create(shout=shout_slug, value=1)
|
||||
self.this_day_views[shout_slug] = this_day_view
|
||||
else:
|
||||
this_day_view.value = this_day_view.value + 1
|
||||
this_day_view.modified = True
|
||||
self.viewed['shouts'][shout_slug] = self.viewed['shouts'].get(shout_slug, 0) + 1
|
||||
with local_session() as session:
|
||||
topics = session.query(ShoutTopic.topic).where(ShoutTopic.shout == shout_slug).all()
|
||||
for t in topics:
|
||||
self.viewed['topics'][t] = self.viewed['topics'].get(t, 0) + 1
|
||||
flag_modified(this_day_view, "value")
|
||||
|
||||
@staticmethod
|
||||
async def get_reaction(reaction_id):
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
return self.viewed["reactions"].get(reaction_id, 0)
|
||||
|
||||
@staticmethod
|
||||
async def flush_changes(session):
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
for view in self.this_day_views.values():
|
||||
if getattr(view, "modified", False):
|
||||
session.add(view)
|
||||
flag_modified(view, "value")
|
||||
view.modified = False
|
||||
for view in self.to_flush:
|
||||
session.add(view)
|
||||
self.to_flush.clear()
|
||||
session.commit()
|
||||
@staticmethod
|
||||
async def increment(shout_slug):
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
this_day_view = self.this_day_views.get(shout_slug)
|
||||
day_start = datetime.now().replace(hour=0, minute=0, second=0)
|
||||
if not this_day_view or this_day_view.day < day_start:
|
||||
if this_day_view and getattr(this_day_view, "modified", False):
|
||||
self.to_flush.append(this_day_view)
|
||||
this_day_view = ViewedByDay.create(shout=shout_slug, value=1)
|
||||
self.this_day_views[shout_slug] = this_day_view
|
||||
else:
|
||||
this_day_view.value = this_day_view.value + 1
|
||||
this_day_view.modified = True
|
||||
self.viewed["shouts"][shout_slug] = (
|
||||
self.viewed["shouts"].get(shout_slug, 0) + 1
|
||||
)
|
||||
with local_session() as session:
|
||||
topics = (
|
||||
session.query(ShoutTopic.topic)
|
||||
.where(ShoutTopic.shout == shout_slug)
|
||||
.all()
|
||||
)
|
||||
for t in topics:
|
||||
self.viewed["topics"][t] = self.viewed["topics"].get(t, 0) + 1
|
||||
flag_modified(this_day_view, "value")
|
||||
|
||||
@staticmethod
|
||||
async def worker():
|
||||
while True:
|
||||
try:
|
||||
with local_session() as session:
|
||||
await ViewedStorage.flush_changes(session)
|
||||
print("[stat.viewed] periodical flush")
|
||||
except Exception as err:
|
||||
print("[stat.viewed] errror: %s" % (err))
|
||||
await asyncio.sleep(ViewedStorage.period)
|
||||
@staticmethod
|
||||
async def flush_changes(session):
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
for view in self.this_day_views.values():
|
||||
if getattr(view, "modified", False):
|
||||
session.add(view)
|
||||
flag_modified(view, "value")
|
||||
view.modified = False
|
||||
for view in self.to_flush:
|
||||
session.add(view)
|
||||
self.to_flush.clear()
|
||||
session.commit()
|
||||
|
||||
@staticmethod
|
||||
async def worker():
|
||||
while True:
|
||||
try:
|
||||
with local_session() as session:
|
||||
await ViewedStorage.flush_changes(session)
|
||||
print("[stat.viewed] periodical flush")
|
||||
except Exception as err:
|
||||
print("[stat.viewed] errror: %s" % (err))
|
||||
await asyncio.sleep(ViewedStorage.period)
|
||||
|
@@ -3,60 +3,67 @@ from pathlib import Path
|
||||
import asyncio
|
||||
from settings import SHOUTS_REPO
|
||||
|
||||
|
||||
class GitTask:
|
||||
''' every shout update use a new task '''
|
||||
queue = asyncio.Queue()
|
||||
"""every shout update use a new task"""
|
||||
|
||||
def __init__(self, input, username, user_email, comment):
|
||||
self.slug = input["slug"]
|
||||
self.shout_body = input["body"]
|
||||
self.username = username
|
||||
self.user_email = user_email
|
||||
self.comment = comment
|
||||
queue = asyncio.Queue()
|
||||
|
||||
GitTask.queue.put_nowait(self)
|
||||
|
||||
def init_repo(self):
|
||||
repo_path = "%s" % (SHOUTS_REPO)
|
||||
|
||||
Path(repo_path).mkdir()
|
||||
|
||||
cmd = "cd %s && git init && " \
|
||||
"git config user.name 'discours' && " \
|
||||
"git config user.email 'discours@discours.io' && " \
|
||||
"touch initial && git add initial && " \
|
||||
"git commit -m 'init repo'" \
|
||||
% (repo_path)
|
||||
output = subprocess.check_output(cmd, shell=True)
|
||||
print(output)
|
||||
def __init__(self, input, username, user_email, comment):
|
||||
self.slug = input["slug"]
|
||||
self.shout_body = input["body"]
|
||||
self.username = username
|
||||
self.user_email = user_email
|
||||
self.comment = comment
|
||||
|
||||
def execute(self):
|
||||
repo_path = "%s" % (SHOUTS_REPO)
|
||||
|
||||
if not Path(repo_path).exists():
|
||||
self.init_repo()
|
||||
GitTask.queue.put_nowait(self)
|
||||
|
||||
#cmd = "cd %s && git checkout master" % (repo_path)
|
||||
#output = subprocess.check_output(cmd, shell=True)
|
||||
#print(output)
|
||||
def init_repo(self):
|
||||
repo_path = "%s" % (SHOUTS_REPO)
|
||||
|
||||
shout_filename = "%s.mdx" % (self.slug)
|
||||
shout_full_filename = "%s/%s" % (repo_path, shout_filename)
|
||||
with open(shout_full_filename, mode='w', encoding='utf-8') as shout_file:
|
||||
shout_file.write(bytes(self.shout_body,'utf-8').decode('utf-8','ignore'))
|
||||
Path(repo_path).mkdir()
|
||||
|
||||
author = "%s <%s>" % (self.username, self.user_email)
|
||||
cmd = "cd %s && git add %s && git commit -m '%s' --author='%s'" % \
|
||||
(repo_path, shout_filename, self.comment, author)
|
||||
output = subprocess.check_output(cmd, shell=True)
|
||||
print(output)
|
||||
|
||||
@staticmethod
|
||||
async def git_task_worker():
|
||||
print("[service.git] starting task worker")
|
||||
while True:
|
||||
task = await GitTask.queue.get()
|
||||
try:
|
||||
task.execute()
|
||||
except Exception as err:
|
||||
print("[service.git] worker error: %s" % (err))
|
||||
cmd = (
|
||||
"cd %s && git init && "
|
||||
"git config user.name 'discours' && "
|
||||
"git config user.email 'discours@discours.io' && "
|
||||
"touch initial && git add initial && "
|
||||
"git commit -m 'init repo'" % (repo_path)
|
||||
)
|
||||
output = subprocess.check_output(cmd, shell=True)
|
||||
print(output)
|
||||
|
||||
def execute(self):
|
||||
repo_path = "%s" % (SHOUTS_REPO)
|
||||
|
||||
if not Path(repo_path).exists():
|
||||
self.init_repo()
|
||||
|
||||
# cmd = "cd %s && git checkout master" % (repo_path)
|
||||
# output = subprocess.check_output(cmd, shell=True)
|
||||
# print(output)
|
||||
|
||||
shout_filename = "%s.mdx" % (self.slug)
|
||||
shout_full_filename = "%s/%s" % (repo_path, shout_filename)
|
||||
with open(shout_full_filename, mode="w", encoding="utf-8") as shout_file:
|
||||
shout_file.write(bytes(self.shout_body, "utf-8").decode("utf-8", "ignore"))
|
||||
|
||||
author = "%s <%s>" % (self.username, self.user_email)
|
||||
cmd = "cd %s && git add %s && git commit -m '%s' --author='%s'" % (
|
||||
repo_path,
|
||||
shout_filename,
|
||||
self.comment,
|
||||
author,
|
||||
)
|
||||
output = subprocess.check_output(cmd, shell=True)
|
||||
print(output)
|
||||
|
||||
@staticmethod
|
||||
async def git_task_worker():
|
||||
print("[service.git] starting task worker")
|
||||
while True:
|
||||
task = await GitTask.queue.get()
|
||||
try:
|
||||
task.execute()
|
||||
except Exception as err:
|
||||
print("[service.git] worker error: %s" % (err))
|
||||
|
@@ -1,47 +1,46 @@
|
||||
|
||||
import asyncio
|
||||
from base.orm import local_session
|
||||
from orm.shout import ShoutAuthor
|
||||
|
||||
|
||||
class ShoutAuthorStorage:
|
||||
authors_by_shout = {}
|
||||
lock = asyncio.Lock()
|
||||
period = 30*60 #sec
|
||||
authors_by_shout = {}
|
||||
lock = asyncio.Lock()
|
||||
period = 30 * 60 # sec
|
||||
|
||||
@staticmethod
|
||||
async def load(session):
|
||||
self = ShoutAuthorStorage
|
||||
sas = session.query(ShoutAuthor).all()
|
||||
for sa in sas:
|
||||
self.authors_by_shout[sa.shout] = self.authors_by_shout.get(sa.shout, [])
|
||||
self.authors_by_shout[sa.shout].append([sa.user, sa.caption])
|
||||
print('[zine.authors] %d shouts preprocessed' % len(self.authors_by_shout))
|
||||
@staticmethod
|
||||
async def load(session):
|
||||
self = ShoutAuthorStorage
|
||||
sas = session.query(ShoutAuthor).all()
|
||||
for sa in sas:
|
||||
self.authors_by_shout[sa.shout] = self.authors_by_shout.get(sa.shout, [])
|
||||
self.authors_by_shout[sa.shout].append([sa.user, sa.caption])
|
||||
print("[zine.authors] %d shouts preprocessed" % len(self.authors_by_shout))
|
||||
|
||||
@staticmethod
|
||||
async def get_authors(shout):
|
||||
self = ShoutAuthorStorage
|
||||
async with self.lock:
|
||||
return self.authors_by_shout.get(shout, [])
|
||||
@staticmethod
|
||||
async def get_authors(shout):
|
||||
self = ShoutAuthorStorage
|
||||
async with self.lock:
|
||||
return self.authors_by_shout.get(shout, [])
|
||||
|
||||
@staticmethod
|
||||
async def get_author_caption(shout, author):
|
||||
self = ShoutAuthorStorage
|
||||
async with self.lock:
|
||||
for a in self.authors_by_shout.get(shout, []):
|
||||
if author in a:
|
||||
return a[1]
|
||||
return { "error": "author caption not found" }
|
||||
@staticmethod
|
||||
async def get_author_caption(shout, author):
|
||||
self = ShoutAuthorStorage
|
||||
async with self.lock:
|
||||
for a in self.authors_by_shout.get(shout, []):
|
||||
if author in a:
|
||||
return a[1]
|
||||
return {"error": "author caption not found"}
|
||||
|
||||
@staticmethod
|
||||
async def worker():
|
||||
self = ShoutAuthorStorage
|
||||
while True:
|
||||
try:
|
||||
with local_session() as session:
|
||||
async with self.lock:
|
||||
await self.load(session)
|
||||
print("[zine.authors] state updated")
|
||||
except Exception as err:
|
||||
print("[zine.authors] errror: %s" % (err))
|
||||
await asyncio.sleep(self.period)
|
||||
@staticmethod
|
||||
async def worker():
|
||||
self = ShoutAuthorStorage
|
||||
while True:
|
||||
try:
|
||||
with local_session() as session:
|
||||
async with self.lock:
|
||||
await self.load(session)
|
||||
print("[zine.authors] state updated")
|
||||
except Exception as err:
|
||||
print("[zine.authors] errror: %s" % (err))
|
||||
await asyncio.sleep(self.period)
|
||||
|
@@ -1,4 +1,3 @@
|
||||
|
||||
import asyncio
|
||||
from datetime import datetime, timedelta
|
||||
from sqlalchemy import and_, desc, func, select
|
||||
@@ -11,148 +10,159 @@ from services.stat.viewed import ViewedByDay
|
||||
|
||||
|
||||
class ShoutsCache:
|
||||
limit = 200
|
||||
period = 60*60 #1 hour
|
||||
lock = asyncio.Lock()
|
||||
limit = 200
|
||||
period = 60 * 60 # 1 hour
|
||||
lock = asyncio.Lock()
|
||||
|
||||
@staticmethod
|
||||
async def prepare_recent_published():
|
||||
with local_session() as session:
|
||||
stmt = select(Shout).\
|
||||
options(selectinload(Shout.authors), selectinload(Shout.topics)).\
|
||||
where(Shout.publishedAt != None).\
|
||||
order_by(desc("publishedAt")).\
|
||||
limit(ShoutsCache.limit)
|
||||
shouts = []
|
||||
for row in session.execute(stmt):
|
||||
shout = row.Shout
|
||||
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
||||
shouts.append(shout)
|
||||
async with ShoutsCache.lock:
|
||||
ShoutsCache.recent_published = shouts
|
||||
print("[zine.cache] %d recently published shouts " % len(shouts))
|
||||
@staticmethod
|
||||
async def prepare_recent_published():
|
||||
with local_session() as session:
|
||||
stmt = (
|
||||
select(Shout)
|
||||
.options(selectinload(Shout.authors), selectinload(Shout.topics))
|
||||
.where(Shout.publishedAt != None)
|
||||
.order_by(desc("publishedAt"))
|
||||
.limit(ShoutsCache.limit)
|
||||
)
|
||||
shouts = []
|
||||
for row in session.execute(stmt):
|
||||
shout = row.Shout
|
||||
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
||||
shouts.append(shout)
|
||||
async with ShoutsCache.lock:
|
||||
ShoutsCache.recent_published = shouts
|
||||
print("[zine.cache] %d recently published shouts " % len(shouts))
|
||||
|
||||
@staticmethod
|
||||
async def prepare_recent_all():
|
||||
with local_session() as session:
|
||||
stmt = select(Shout).\
|
||||
options(
|
||||
selectinload(Shout.authors),
|
||||
selectinload(Shout.topics)
|
||||
).\
|
||||
order_by(desc("createdAt")).\
|
||||
limit(ShoutsCache.limit)
|
||||
shouts = []
|
||||
for row in session.execute(stmt):
|
||||
shout = row.Shout
|
||||
# shout.topics = [t.slug for t in shout.topics]
|
||||
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
||||
shouts.append(shout)
|
||||
async with ShoutsCache.lock:
|
||||
ShoutsCache.recent_all = shouts
|
||||
print("[zine.cache] %d recently created shouts " % len(shouts))
|
||||
@staticmethod
|
||||
async def prepare_recent_all():
|
||||
with local_session() as session:
|
||||
stmt = (
|
||||
select(Shout)
|
||||
.options(selectinload(Shout.authors), selectinload(Shout.topics))
|
||||
.order_by(desc("createdAt"))
|
||||
.limit(ShoutsCache.limit)
|
||||
)
|
||||
shouts = []
|
||||
for row in session.execute(stmt):
|
||||
shout = row.Shout
|
||||
# shout.topics = [t.slug for t in shout.topics]
|
||||
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
||||
shouts.append(shout)
|
||||
async with ShoutsCache.lock:
|
||||
ShoutsCache.recent_all = shouts
|
||||
print("[zine.cache] %d recently created shouts " % len(shouts))
|
||||
|
||||
@staticmethod
|
||||
async def prepare_recent_reacted():
|
||||
with local_session() as session:
|
||||
stmt = select(Shout, func.max(Reaction.createdAt).label("reactionCreatedAt")).\
|
||||
options(
|
||||
selectinload(Shout.authors),
|
||||
selectinload(Shout.topics),
|
||||
).\
|
||||
join(Reaction, Reaction.shout == Shout.slug).\
|
||||
where(and_(Shout.publishedAt != None, Reaction.deletedAt == None)).\
|
||||
group_by(Shout.slug).\
|
||||
order_by(desc("reactionCreatedAt")).\
|
||||
limit(ShoutsCache.limit)
|
||||
shouts = []
|
||||
for row in session.execute(stmt):
|
||||
shout = row.Shout
|
||||
# shout.topics = [t.slug for t in shout.topics]
|
||||
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
||||
shouts.append(shout)
|
||||
async with ShoutsCache.lock:
|
||||
ShoutsCache.recent_reacted = shouts
|
||||
print("[zine.cache] %d recently reacted shouts " % len(shouts))
|
||||
@staticmethod
|
||||
async def prepare_recent_reacted():
|
||||
with local_session() as session:
|
||||
stmt = (
|
||||
select(Shout, func.max(Reaction.createdAt).label("reactionCreatedAt"))
|
||||
.options(
|
||||
selectinload(Shout.authors),
|
||||
selectinload(Shout.topics),
|
||||
)
|
||||
.join(Reaction, Reaction.shout == Shout.slug)
|
||||
.where(and_(Shout.publishedAt != None, Reaction.deletedAt == None))
|
||||
.group_by(Shout.slug)
|
||||
.order_by(desc("reactionCreatedAt"))
|
||||
.limit(ShoutsCache.limit)
|
||||
)
|
||||
shouts = []
|
||||
for row in session.execute(stmt):
|
||||
shout = row.Shout
|
||||
# shout.topics = [t.slug for t in shout.topics]
|
||||
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
||||
shouts.append(shout)
|
||||
async with ShoutsCache.lock:
|
||||
ShoutsCache.recent_reacted = shouts
|
||||
print("[zine.cache] %d recently reacted shouts " % len(shouts))
|
||||
|
||||
@staticmethod
|
||||
async def prepare_top_overall():
|
||||
with local_session() as session:
|
||||
# with reacted times counter
|
||||
stmt = (
|
||||
select(Shout, func.count(Reaction.id).label("reacted"))
|
||||
.options(
|
||||
selectinload(Shout.authors),
|
||||
selectinload(Shout.topics),
|
||||
selectinload(Shout.reactions),
|
||||
)
|
||||
.join(Reaction)
|
||||
.where(and_(Shout.publishedAt != None, Reaction.deletedAt == None))
|
||||
.group_by(Shout.slug)
|
||||
.order_by(desc("reacted"))
|
||||
.limit(ShoutsCache.limit)
|
||||
)
|
||||
shouts = []
|
||||
# with rating synthetic counter
|
||||
for row in session.execute(stmt):
|
||||
shout = row.Shout
|
||||
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
||||
shouts.append(shout)
|
||||
shouts.sort(key=lambda shout: shout.rating, reverse=True)
|
||||
async with ShoutsCache.lock:
|
||||
print("[zine.cache] %d top shouts " % len(shouts))
|
||||
ShoutsCache.top_overall = shouts
|
||||
|
||||
@staticmethod
|
||||
async def prepare_top_overall():
|
||||
with local_session() as session:
|
||||
# with reacted times counter
|
||||
stmt = select(Shout,
|
||||
func.count(Reaction.id).label("reacted")).\
|
||||
options(selectinload(Shout.authors), selectinload(Shout.topics), selectinload(Shout.reactions)).\
|
||||
join(Reaction).\
|
||||
where(and_(Shout.publishedAt != None, Reaction.deletedAt == None)).\
|
||||
group_by(Shout.slug).\
|
||||
order_by(desc("reacted")).\
|
||||
limit(ShoutsCache.limit)
|
||||
shouts = []
|
||||
# with rating synthetic counter
|
||||
for row in session.execute(stmt):
|
||||
shout = row.Shout
|
||||
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
||||
shouts.append(shout)
|
||||
shouts.sort(key = lambda shout: shout.rating, reverse = True)
|
||||
async with ShoutsCache.lock:
|
||||
print("[zine.cache] %d top shouts " % len(shouts))
|
||||
ShoutsCache.top_overall = shouts
|
||||
@staticmethod
|
||||
async def prepare_top_month():
|
||||
month_ago = datetime.now() - timedelta(days=30)
|
||||
with local_session() as session:
|
||||
stmt = (
|
||||
select(Shout, func.count(Reaction.id).label("reacted"))
|
||||
.options(selectinload(Shout.authors), selectinload(Shout.topics))
|
||||
.join(Reaction)
|
||||
.where(and_(Shout.createdAt > month_ago, Shout.publishedAt != None))
|
||||
.group_by(Shout.slug)
|
||||
.order_by(desc("reacted"))
|
||||
.limit(ShoutsCache.limit)
|
||||
)
|
||||
shouts = []
|
||||
for row in session.execute(stmt):
|
||||
shout = row.Shout
|
||||
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
||||
shouts.append(shout)
|
||||
shouts.sort(key=lambda shout: shout.rating, reverse=True)
|
||||
async with ShoutsCache.lock:
|
||||
print("[zine.cache] %d top month shouts " % len(shouts))
|
||||
ShoutsCache.top_month = shouts
|
||||
|
||||
@staticmethod
|
||||
async def prepare_top_month():
|
||||
month_ago = datetime.now() - timedelta(days = 30)
|
||||
with local_session() as session:
|
||||
stmt = select(Shout, func.count(Reaction.id).label("reacted")).\
|
||||
options(selectinload(Shout.authors), selectinload(Shout.topics)).\
|
||||
join(Reaction).\
|
||||
where(and_(Shout.createdAt > month_ago, Shout.publishedAt != None)).\
|
||||
group_by(Shout.slug).\
|
||||
order_by(desc("reacted")).\
|
||||
limit(ShoutsCache.limit)
|
||||
shouts = []
|
||||
for row in session.execute(stmt):
|
||||
shout = row.Shout
|
||||
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
||||
shouts.append(shout)
|
||||
shouts.sort(key = lambda shout: shout.rating, reverse = True)
|
||||
async with ShoutsCache.lock:
|
||||
print("[zine.cache] %d top month shouts " % len(shouts))
|
||||
ShoutsCache.top_month = shouts
|
||||
@staticmethod
|
||||
async def prepare_top_viewed():
|
||||
month_ago = datetime.now() - timedelta(days=30)
|
||||
with local_session() as session:
|
||||
stmt = (
|
||||
select(Shout, func.sum(ViewedByDay.value).label("viewed"))
|
||||
.options(selectinload(Shout.authors), selectinload(Shout.topics))
|
||||
.join(ViewedByDay)
|
||||
.where(and_(ViewedByDay.day > month_ago, Shout.publishedAt != None))
|
||||
.group_by(Shout.slug)
|
||||
.order_by(desc("viewed"))
|
||||
.limit(ShoutsCache.limit)
|
||||
)
|
||||
shouts = []
|
||||
for row in session.execute(stmt):
|
||||
shout = row.Shout
|
||||
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
||||
shouts.append(shout)
|
||||
# shouts.sort(key = lambda shout: shout.viewed, reverse = True)
|
||||
async with ShoutsCache.lock:
|
||||
print("[zine.cache] %d top viewed shouts " % len(shouts))
|
||||
ShoutsCache.top_viewed = shouts
|
||||
|
||||
@staticmethod
|
||||
async def prepare_top_viewed():
|
||||
month_ago = datetime.now() - timedelta(days = 30)
|
||||
with local_session() as session:
|
||||
stmt = select(Shout, func.sum(ViewedByDay.value).label("viewed")).\
|
||||
options(selectinload(Shout.authors), selectinload(Shout.topics)).\
|
||||
join(ViewedByDay).\
|
||||
where(and_(ViewedByDay.day > month_ago, Shout.publishedAt != None)).\
|
||||
group_by(Shout.slug).\
|
||||
order_by(desc("viewed")).\
|
||||
limit(ShoutsCache.limit)
|
||||
shouts = []
|
||||
for row in session.execute(stmt):
|
||||
shout = row.Shout
|
||||
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
||||
shouts.append(shout)
|
||||
# shouts.sort(key = lambda shout: shout.viewed, reverse = True)
|
||||
async with ShoutsCache.lock:
|
||||
print("[zine.cache] %d top viewed shouts " % len(shouts))
|
||||
ShoutsCache.top_viewed = shouts
|
||||
|
||||
@staticmethod
|
||||
async def worker():
|
||||
while True:
|
||||
try:
|
||||
await ShoutsCache.prepare_top_month()
|
||||
await ShoutsCache.prepare_top_overall()
|
||||
await ShoutsCache.prepare_top_viewed()
|
||||
await ShoutsCache.prepare_recent_published()
|
||||
await ShoutsCache.prepare_recent_all()
|
||||
await ShoutsCache.prepare_recent_reacted()
|
||||
print("[zine.cache] periodical update")
|
||||
except Exception as err:
|
||||
print("[zine.cache] error: %s" % (err))
|
||||
raise err
|
||||
await asyncio.sleep(ShoutsCache.period)
|
||||
@staticmethod
|
||||
async def worker():
|
||||
while True:
|
||||
try:
|
||||
await ShoutsCache.prepare_top_month()
|
||||
await ShoutsCache.prepare_top_overall()
|
||||
await ShoutsCache.prepare_top_viewed()
|
||||
await ShoutsCache.prepare_recent_published()
|
||||
await ShoutsCache.prepare_recent_all()
|
||||
await ShoutsCache.prepare_recent_reacted()
|
||||
print("[zine.cache] periodical update")
|
||||
except Exception as err:
|
||||
print("[zine.cache] error: %s" % (err))
|
||||
raise err
|
||||
await asyncio.sleep(ShoutsCache.period)
|
||||
|
@@ -3,56 +3,58 @@ from orm.topic import Topic
|
||||
|
||||
|
||||
class TopicStorage:
|
||||
topics = {}
|
||||
lock = asyncio.Lock()
|
||||
topics = {}
|
||||
lock = asyncio.Lock()
|
||||
|
||||
@staticmethod
|
||||
def init(session):
|
||||
self = TopicStorage
|
||||
topics = session.query(Topic)
|
||||
self.topics = dict([(topic.slug, topic) for topic in topics])
|
||||
for topic in self.topics.values():
|
||||
self.load_parents(topic)
|
||||
|
||||
print('[zine.topics] %d precached' % len(self.topics.keys()))
|
||||
@staticmethod
|
||||
def init(session):
|
||||
self = TopicStorage
|
||||
topics = session.query(Topic)
|
||||
self.topics = dict([(topic.slug, topic) for topic in topics])
|
||||
for topic in self.topics.values():
|
||||
self.load_parents(topic)
|
||||
|
||||
@staticmethod
|
||||
def load_parents(topic):
|
||||
self = TopicStorage
|
||||
parents = []
|
||||
for parent in self.topics.values():
|
||||
if topic.slug in parent.children:
|
||||
parents.append(parent.slug)
|
||||
topic.parents = parents
|
||||
return topic
|
||||
print("[zine.topics] %d precached" % len(self.topics.keys()))
|
||||
|
||||
@staticmethod
|
||||
async def get_topics_all(page, size):
|
||||
end = page * size
|
||||
start = end - size
|
||||
self = TopicStorage
|
||||
async with self.lock:
|
||||
return list(self.topics.values())[start:end]
|
||||
@staticmethod
|
||||
def load_parents(topic):
|
||||
self = TopicStorage
|
||||
parents = []
|
||||
for parent in self.topics.values():
|
||||
if topic.slug in parent.children:
|
||||
parents.append(parent.slug)
|
||||
topic.parents = parents
|
||||
return topic
|
||||
|
||||
@staticmethod
|
||||
async def get_topics_by_slugs(slugs):
|
||||
self = TopicStorage
|
||||
async with self.lock:
|
||||
if not slugs:
|
||||
return self.topics.values()
|
||||
topics = filter(lambda topic: topic.slug in slugs, self.topics.values())
|
||||
return list(topics)
|
||||
@staticmethod
|
||||
async def get_topics_all(page, size):
|
||||
end = page * size
|
||||
start = end - size
|
||||
self = TopicStorage
|
||||
async with self.lock:
|
||||
return list(self.topics.values())[start:end]
|
||||
|
||||
@staticmethod
|
||||
async def get_topics_by_community(community):
|
||||
self = TopicStorage
|
||||
async with self.lock:
|
||||
topics = filter(lambda topic: topic.community == community, self.topics.values())
|
||||
return list(topics)
|
||||
@staticmethod
|
||||
async def get_topics_by_slugs(slugs):
|
||||
self = TopicStorage
|
||||
async with self.lock:
|
||||
if not slugs:
|
||||
return self.topics.values()
|
||||
topics = filter(lambda topic: topic.slug in slugs, self.topics.values())
|
||||
return list(topics)
|
||||
|
||||
@staticmethod
|
||||
async def add_topic(topic):
|
||||
self = TopicStorage
|
||||
async with self.lock:
|
||||
self.topics[topic.slug] = topic
|
||||
self.load_parents(topic)
|
||||
@staticmethod
|
||||
async def get_topics_by_community(community):
|
||||
self = TopicStorage
|
||||
async with self.lock:
|
||||
topics = filter(
|
||||
lambda topic: topic.community == community, self.topics.values()
|
||||
)
|
||||
return list(topics)
|
||||
|
||||
@staticmethod
|
||||
async def add_topic(topic):
|
||||
self = TopicStorage
|
||||
async with self.lock:
|
||||
self.topics[topic.slug] = topic
|
||||
self.load_parents(topic)
|
||||
|
Reference in New Issue
Block a user