page views

This commit is contained in:
bniwredyc 2023-01-18 13:43:56 +01:00
parent e38ab0a066
commit 560bb18bfc
4 changed files with 76 additions and 42 deletions

View File

@ -10,7 +10,7 @@ from migration.tables.comments import migrate_2stage as migrateComment_2stage
from migration.tables.content_items import get_shout_slug from migration.tables.content_items import get_shout_slug
from migration.tables.content_items import migrate as migrateShout from migration.tables.content_items import migrate as migrateShout
from migration.tables.topics import migrate as migrateTopic from migration.tables.topics import migrate as migrateTopic
from migration.tables.users import migrate as migrateUser from migration.tables.users import migrate as migrateUser, post_migrate as users_post_migrate
from migration.tables.remarks import migrate as migrateRemark from migration.tables.remarks import migrate as migrateRemark
from migration.tables.users import migrate_2stage as migrateUser_2stage from migration.tables.users import migrate_2stage as migrateUser_2stage
from orm.reaction import Reaction from orm.reaction import Reaction
@ -42,6 +42,7 @@ async def users_handle(storage):
ce = 0 ce = 0
for entry in storage["users"]["data"]: for entry in storage["users"]["data"]:
ce += migrateUser_2stage(entry, id_map) ce += migrateUser_2stage(entry, id_map)
users_post_migrate()
async def topics_handle(storage): async def topics_handle(storage):
@ -180,8 +181,8 @@ async def all_handle(storage, args):
await topics_handle(storage) await topics_handle(storage)
print("[migration] users and topics are migrated") print("[migration] users and topics are migrated")
await shouts_handle(storage, args) await shouts_handle(storage, args)
print("[migration] remarks...") # print("[migration] remarks...")
await remarks_handle(storage) # await remarks_handle(storage)
print("[migration] migrating comments") print("[migration] migrating comments")
await comments_handle(storage) await comments_handle(storage)
# export_email_subscriptions() # export_email_subscriptions()

View File

@ -176,7 +176,7 @@ async def migrate(entry, storage):
await content_ratings_to_reactions(entry, shout_dict["slug"]) await content_ratings_to_reactions(entry, shout_dict["slug"])
# shout views # 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'] # del shout_dict['ratings']
storage["shouts"]["by_oid"][entry["_id"]] = shout_dict storage["shouts"]["by_oid"][entry["_id"]] = shout_dict

View File

@ -1,7 +1,9 @@
import re
from bs4 import BeautifulSoup
from dateutil.parser import parse from dateutil.parser import parse
from sqlalchemy.exc import IntegrityError from sqlalchemy.exc import IntegrityError
from bs4 import BeautifulSoup
import re
from base.orm import local_session from base.orm import local_session
from orm.user import AuthorFollower, User, UserRating from orm.user import AuthorFollower, User, UserRating
@ -108,6 +110,20 @@ def migrate(entry):
return user_dict return user_dict
def post_migrate():
old_discours_dict = {
"slug": "old-discours",
"username": "old-discours",
"email": "old@discours.io",
"name": "Просмотры на старой версии сайта"
}
with local_session() as session:
old_discours_user = User.create(**old_discours_dict)
session.add(old_discours_user)
session.commit()
def migrate_2stage(entry, id_map): def migrate_2stage(entry, id_map):
ce = 0 ce = 0
for rating_entry in entry.get("ratings", []): for rating_entry in entry.get("ratings", []):

View File

@ -1,16 +1,17 @@
import asyncio import asyncio
import time import time
from datetime import timedelta, timezone, datetime from datetime import timedelta, timezone, datetime
from os import environ, path
from ssl import create_default_context
from gql import Client, gql from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport from gql.transport.aiohttp import AIOHTTPTransport
from base.orm import local_session
from sqlalchemy import func from sqlalchemy import func
from base.orm import local_session
from orm import User, Topic from orm import User, Topic
from orm.shout import ShoutTopic, Shout from orm.shout import ShoutTopic, Shout
from orm.viewed import ViewedEntry from orm.viewed import ViewedEntry
from ssl import create_default_context
from os import environ, path
load_facts = gql(""" load_facts = gql("""
query getDomains { query getDomains {
@ -64,7 +65,7 @@ class ViewedStorage:
views = None views = None
pages = None pages = None
domains = None domains = None
period = 24 * 60 * 60 # one time a day period = 60 * 60 # every hour
client = None client = None
auth_result = None auth_result = None
disabled = False disabled = False
@ -98,8 +99,8 @@ class ViewedStorage:
p = page["value"].split("?")[0] p = page["value"].split("?")[0]
slug = p.split('discours.io/')[-1] slug = p.split('discours.io/')[-1]
shouts[slug] = page["count"] shouts[slug] = page["count"]
for slug, v in shouts: for slug in shouts.keys():
await ViewedStorage.increment(slug, v) await ViewedStorage.increment(slug, shouts[slug])
except Exception: except Exception:
pass pass
print("[stat.viewed] ⎪ %d pages collected " % len(shouts.keys())) print("[stat.viewed] ⎪ %d pages collected " % len(shouts.keys()))
@ -164,15 +165,31 @@ class ViewedStorage:
self = ViewedStorage self = ViewedStorage
async with self.lock: async with self.lock:
with local_session() as session: with local_session() as session:
# TODO: user slug -> id
viewed = session.query(
ViewedEntry
).join(
Shout
).join(
User
).filter(
User.slug == viewer,
Shout.slug == shout_slug
).first()
if viewed:
viewed.amount = amount
print("amount: %d" % amount)
else:
shout = session.query(Shout).where(Shout.slug == shout_slug).one() shout = session.query(Shout).where(Shout.slug == shout_slug).one()
viewer = session.query(User).where(User.slug == viewer).one() viewer = session.query(User).where(User.slug == viewer).one()
new_viewed = ViewedEntry.create(**{
viewed = ViewedEntry.create(**{
"viewer": viewer.id, "viewer": viewer.id,
"shout": shout.id, "shout": shout.id,
"amount": amount "amount": amount
}) })
session.add(viewed) session.add(new_viewed)
session.commit() session.commit()
self.by_shouts[shout_slug] = self.by_shouts.get(shout_slug, 0) + amount self.by_shouts[shout_slug] = self.by_shouts.get(shout_slug, 0) + amount
self.update_topics(session, shout_slug) self.update_topics(session, shout_slug)
@ -184,7 +201,7 @@ class ViewedStorage:
self = ViewedStorage self = ViewedStorage
if self.disabled: if self.disabled:
return return
async with self.lock:
while True: while True:
try: try:
print("[stat.viewed] - updating views...") print("[stat.viewed] - updating views...")