lint services

This commit is contained in:
tonyrewin 2022-09-03 14:10:28 +03:00
parent a89a44f660
commit f7b9a066b9
4 changed files with 44 additions and 64 deletions

View File

@ -1,16 +1,14 @@
import asyncio import asyncio
from datetime import datetime from datetime import datetime
from sqlalchemy.types import Enum
from sqlalchemy import Column, DateTime, ForeignKey, Boolean from sqlalchemy import Column, DateTime, ForeignKey, Boolean
from base.orm import Base
# from sqlalchemy.orm.attributes import flag_modified from orm.reaction import Reaction
from sqlalchemy import Enum
import enum
from base.orm import Base, local_session
from orm.topic import ShoutTopic from orm.topic import ShoutTopic
from enum import Enum as Enumeration
from sqlalchemy.types import Enum as ColumnEnum
class ReactionKind(enum.Enum): class ReactionKind(Enumeration):
AGREE = 1 # +1 AGREE = 1 # +1
DISAGREE = 2 # -1 DISAGREE = 2 # -1
PROOF = 3 # +1 PROOF = 3 # +1
@ -48,11 +46,11 @@ def kind_to_rate(kind) -> int:
class ReactedByDay(Base): class ReactedByDay(Base):
__tablename__ = "reacted_by_day" __tablename__ = "reacted_by_day"
id = None id = None # type: ignore
reaction = Column(ForeignKey("reaction.id"), primary_key=True) reaction = Column(ForeignKey("reaction.id"), primary_key=True)
shout = Column(ForeignKey("shout.slug"), primary_key=True) shout = Column(ForeignKey("shout.slug"), primary_key=True)
replyTo = Column(ForeignKey("reaction.id"), nullable=True) replyTo = Column(ForeignKey("reaction.id"), nullable=True)
kind: int = Column(Enum(ReactionKind), nullable=False, comment="Reaction kind") kind = Column(ColumnEnum(ReactionKind), nullable=False, comment="Reaction kind")
day = Column(DateTime, primary_key=True, default=datetime.now) day = Column(DateTime, primary_key=True, default=datetime.now)
comment = Column(Boolean, default=False) comment = Column(Boolean, default=False)
@ -82,7 +80,7 @@ class ReactedStorage:
self = ReactedStorage self = ReactedStorage
async with self.lock: async with self.lock:
return list( return list(
filter(lambda r: r.comment, self.reacted["shouts"].get(shout_slug, [])) filter(lambda r: r.comment, self.reacted["shouts"].get(shout_slug, {}))
) )
@staticmethod @staticmethod
@ -90,7 +88,7 @@ class ReactedStorage:
self = ReactedStorage self = ReactedStorage
async with self.lock: async with self.lock:
return list( return list(
filter(lambda r: r.comment, self.reacted["topics"].get(topic_slug, [])) filter(lambda r: r.comment, self.reacted["topics"].get(topic_slug, {}))
) )
@staticmethod @staticmethod
@ -98,7 +96,7 @@ class ReactedStorage:
self = ReactedStorage self = ReactedStorage
async with self.lock: async with self.lock:
return list( return list(
filter(lambda r: r.comment, self.reacted["reactions"].get(reaction_id)) filter(lambda r: r.comment, self.reacted["reactions"].get(reaction_id, {}))
) )
@staticmethod @staticmethod
@ -135,39 +133,29 @@ class ReactedStorage:
return rating return rating
@staticmethod @staticmethod
async def increment(reaction): async def increment(reaction: Reaction): # type: ignore
self = ReactedStorage self = ReactedStorage
async with self.lock: r = {
with local_session() as session: "day": datetime.now().replace(hour=0, minute=0, second=0, microsecond=0),
r = { "reaction": reaction.id,
"day": datetime.now().replace( "kind": reaction.kind,
hour=0, minute=0, second=0, microsecond=0 "shout": reaction.shout,
), }
"reaction": reaction.id, if reaction.replyTo:
"kind": reaction.kind, r["replyTo"] = reaction.replyTo
"shout": reaction.shout, if reaction.body:
} r["comment"] = True
if reaction.replyTo: reaction: ReactedByDay = ReactedByDay.create(**r) # type: ignore
r["replyTo"] = reaction.replyTo self.reacted["shouts"][reaction.shout] = self.reacted["shouts"].get(reaction.shout, [])
if reaction.body: self.reacted["shouts"][reaction.shout].append(reaction)
r["comment"] = True if reaction.replyTo:
reaction = ReactedByDay.create(**r) self.reacted["reaction"][reaction.replyTo] = self.reacted["reactions"].get(reaction.shout, [])
self.reacted["shouts"][reaction.shout] = self.reacted["shouts"].get( self.reacted["reaction"][reaction.replyTo].append(reaction)
reaction.shout, [] self.rating["reactions"][reaction.replyTo] = \
) self.rating["reactions"].get(reaction.replyTo, 0) + kind_to_rate(reaction.kind)
self.reacted["shouts"][reaction.shout].append(reaction) else:
if reaction.replyTo: self.rating["shouts"][reaction.replyTo] = \
self.reacted["reaction"][reaction.replyTo] = self.reacted[ self.rating["shouts"].get(reaction.shout, 0) + kind_to_rate(reaction.kind)
"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 @staticmethod
def init(session): def init(session):
@ -176,16 +164,11 @@ class ReactedStorage:
print("[stat.reacted] %d reactions total" % len(all_reactions)) print("[stat.reacted] %d reactions total" % len(all_reactions))
for reaction in all_reactions: for reaction in all_reactions:
shout = reaction.shout shout = reaction.shout
topics = ( topics = (session.query(ShoutTopic.topic).where(ShoutTopic.shout == shout).all())
session.query(ShoutTopic.topic).where(ShoutTopic.shout == shout).all()
)
kind = reaction.kind kind = reaction.kind
self.reacted["shouts"][shout] = self.reacted["shouts"].get(shout, []) self.reacted["shouts"][shout] = self.reacted["shouts"].get(shout, [])
self.reacted["shouts"][shout].append(reaction) self.reacted["shouts"][shout].append(reaction)
self.rating["shouts"][shout] = self.rating["shouts"].get( self.rating["shouts"][shout] = self.rating["shouts"].get(shout, 0) + kind_to_rate(kind)
shout, 0
) + kind_to_rate(kind)
for t in topics: for t in topics:
self.reacted["topics"][t] = self.reacted["topics"].get(t, []) self.reacted["topics"][t] = self.reacted["topics"].get(t, [])
@ -197,13 +180,11 @@ class ReactedStorage:
) # rating ) # rating
if reaction.replyTo: if reaction.replyTo:
self.reacted["reactions"][reaction.replyTo] = self.reacted[ self.reacted["reactions"][reaction.replyTo] = \
"reactions" self.reacted["reactions"].get(reaction.replyTo, [])
].get(reaction.replyTo, [])
self.reacted["reactions"][reaction.replyTo].append(reaction) self.reacted["reactions"][reaction.replyTo].append(reaction)
self.rating["reactions"][reaction.replyTo] = self.rating[ self.rating["reactions"][reaction.replyTo] = \
"reactions" self.rating["reactions"].get(reaction.replyTo, 0) + kind_to_rate(reaction.kind)
].get(reaction.replyTo, 0) + kind_to_rate(reaction.kind)
ttt = self.reacted["topics"].values() ttt = self.reacted["topics"].values()
print("[stat.reacted] %d topics reacted" % len(ttt)) print("[stat.reacted] %d topics reacted" % len(ttt))
print("[stat.reacted] %d shouts reacted" % len(self.reacted["shouts"])) print("[stat.reacted] %d shouts reacted" % len(self.reacted["shouts"]))

View File

@ -4,7 +4,6 @@ from services.stat.reacted import ReactedStorage
from services.stat.viewed import ViewedStorage from services.stat.viewed import ViewedStorage
from services.zine.shoutauthor import ShoutAuthorStorage from services.zine.shoutauthor import ShoutAuthorStorage
from orm.topic import ShoutTopic, TopicFollower from orm.topic import ShoutTopic, TopicFollower
from typing import Dict
class TopicStat: class TopicStat:

View File

@ -39,7 +39,7 @@ class ViewedStorage:
for t in topics: for t in topics:
old_topic_value = self.viewed["topics"].get(t, 0) old_topic_value = self.viewed["topics"].get(t, 0)
self.viewed["topics"][t] = old_topic_value + value self.viewed["topics"][t] = old_topic_value + value
if not shout in self.this_day_views: if shout not in self.this_day_views:
self.this_day_views[shout] = view self.this_day_views[shout] = view
this_day_view = self.this_day_views[shout] this_day_view = self.this_day_views[shout]
if this_day_view.day < view.day: if this_day_view.day < view.day:

View File

@ -20,7 +20,7 @@ class ShoutsCache:
stmt = ( stmt = (
select(Shout) select(Shout)
.options(selectinload(Shout.authors), selectinload(Shout.topics)) .options(selectinload(Shout.authors), selectinload(Shout.topics))
.where(Shout.publishedAt != None) .where(bool(Shout.publishedAt))
.order_by(desc("publishedAt")) .order_by(desc("publishedAt"))
.limit(ShoutsCache.limit) .limit(ShoutsCache.limit)
) )
@ -62,7 +62,7 @@ class ShoutsCache:
selectinload(Shout.topics), selectinload(Shout.topics),
) )
.join(Reaction, Reaction.shout == Shout.slug) .join(Reaction, Reaction.shout == Shout.slug)
.where(and_(Shout.publishedAt != None, Reaction.deletedAt == None)) .where(and_(bool(Shout.publishedAt), bool(Reaction.deletedAt)))
.group_by(Shout.slug) .group_by(Shout.slug)
.order_by(desc("reactionCreatedAt")) .order_by(desc("reactionCreatedAt"))
.limit(ShoutsCache.limit) .limit(ShoutsCache.limit)
@ -89,7 +89,7 @@ class ShoutsCache:
selectinload(Shout.reactions), selectinload(Shout.reactions),
) )
.join(Reaction) .join(Reaction)
.where(and_(Shout.publishedAt != None, Reaction.deletedAt == None)) .where(and_(bool(Shout.publishedAt), bool(Reaction.deletedAt)))
.group_by(Shout.slug) .group_by(Shout.slug)
.order_by(desc("reacted")) .order_by(desc("reacted"))
.limit(ShoutsCache.limit) .limit(ShoutsCache.limit)
@ -113,7 +113,7 @@ class ShoutsCache:
select(Shout, func.count(Reaction.id).label("reacted")) select(Shout, func.count(Reaction.id).label("reacted"))
.options(selectinload(Shout.authors), selectinload(Shout.topics)) .options(selectinload(Shout.authors), selectinload(Shout.topics))
.join(Reaction) .join(Reaction)
.where(and_(Shout.createdAt > month_ago, Shout.publishedAt != None)) .where(and_(Shout.createdAt > month_ago, bool(Reaction.deletedAt)))
.group_by(Shout.slug) .group_by(Shout.slug)
.order_by(desc("reacted")) .order_by(desc("reacted"))
.limit(ShoutsCache.limit) .limit(ShoutsCache.limit)
@ -136,7 +136,7 @@ class ShoutsCache:
select(Shout, func.sum(ViewedByDay.value).label("viewed")) select(Shout, func.sum(ViewedByDay.value).label("viewed"))
.options(selectinload(Shout.authors), selectinload(Shout.topics)) .options(selectinload(Shout.authors), selectinload(Shout.topics))
.join(ViewedByDay) .join(ViewedByDay)
.where(and_(ViewedByDay.day > month_ago, Shout.publishedAt != None)) .where(and_(Shout.createdAt > month_ago, bool(Reaction.deletedAt)))
.group_by(Shout.slug) .group_by(Shout.slug)
.order_by(desc("viewed")) .order_by(desc("viewed"))
.limit(ShoutsCache.limit) .limit(ShoutsCache.limit)