From c717f387eabfcf212adc83d62894562646be17e0 Mon Sep 17 00:00:00 2001 From: Alexey Kulikov Date: Fri, 4 Aug 2023 00:31:55 +0100 Subject: [PATCH] split old and ackee --- migration/tables/content_items.py | 2 +- orm/shout.py | 8 ++++++-- services/stat/viewed.py | 16 ++++++++++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/migration/tables/content_items.py b/migration/tables/content_items.py index 6c8e069b..09ef4cb0 100644 --- a/migration/tables/content_items.py +++ b/migration/tables/content_items.py @@ -193,7 +193,7 @@ async def migrate(entry, storage): await content_ratings_to_reactions(entry, shout_dict["slug"]) # shout views - await ViewedStorage.increment(shout_dict["slug"], amount=entry.get("views", 1)) + await ViewedStorage.increment(shout_dict["slug"], amount=entry.get("views", 1), viewer='old-discours') # del shout_dict['ratings'] storage["shouts"]["by_oid"][entry["_id"]] = shout_dict diff --git a/orm/shout.py b/orm/shout.py index 3c89181d..dfe9d749 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -1,7 +1,7 @@ from datetime import datetime from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String, JSON -from sqlalchemy.orm import relationship +from sqlalchemy.orm import column_property, relationship from base.orm import Base, local_session from orm.reaction import Reaction @@ -61,7 +61,11 @@ class Shout(Base): authors = relationship(lambda: User, secondary=ShoutAuthor.__tablename__) topics = relationship(lambda: Topic, secondary=ShoutTopic.__tablename__) - views = Column(Integer, default=0) + # views from the old Discours website + viewsOld = Column(Integer, default=0) + # views from Ackee tracker on the new Discours website + viewsAckee = Column(Integer, default=0) + views = column_property(viewsOld + viewsAckee) reactions = relationship(lambda: Reaction) # TODO: these field should be used or modified diff --git a/services/stat/viewed.py b/services/stat/viewed.py index 9d4e3148..a3b243a5 100644 --- a/services/stat/viewed.py +++ b/services/stat/viewed.py @@ -156,14 +156,26 @@ class ViewedStorage: self.by_topics[topic.slug][shout_slug] = self.by_shouts[shout_slug] @staticmethod - async def increment(shout_slug, amount=1): + async def increment(shout_slug, amount=1, viewer='ackee'): """ the only way to change views counter """ self = ViewedStorage async with self.lock: # TODO optimize, currenty we execute 1 DB transaction per shout with local_session() as session: shout = session.query(Shout).where(Shout.slug == shout_slug).one() - shout.views += amount + if viewer == 'old-discours': + if shout.viewsOld == amount: + print(f"amount: {amount}") + else: + print(f"amount changed: {shout.viewsOld} --> {amount}") + shout.viewsOld = amount + else: + if shout.viewsAckee == amount: + print(f"amount: {amount}") + else: + print(f"amount changed: {shout.viewsAckee} --> {amount}") + shout.viewsAckee = amount + session.commit() # this part is currently unused