add viewShout
This commit is contained in:
parent
f23d375b4d
commit
6e451c8095
3
main.py
3
main.py
|
@ -13,7 +13,7 @@ from auth.oauth import oauth_login, oauth_authorize
|
||||||
from auth.email import email_authorize
|
from auth.email import email_authorize
|
||||||
from redis import redis
|
from redis import redis
|
||||||
from resolvers.base import resolvers
|
from resolvers.base import resolvers
|
||||||
from resolvers.zine import GitTask, TopShouts
|
from resolvers.zine import GitTask, TopShouts, db_flush_worker
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
@ -29,6 +29,7 @@ async def start_up():
|
||||||
await redis.connect()
|
await redis.connect()
|
||||||
git_task = asyncio.create_task(GitTask.git_task_worker())
|
git_task = asyncio.create_task(GitTask.git_task_worker())
|
||||||
top_shouts_task = asyncio.create_task(TopShouts.worker())
|
top_shouts_task = asyncio.create_task(TopShouts.worker())
|
||||||
|
db_flush_task = asyncio.create_task(db_flush_worker())
|
||||||
|
|
||||||
async def shutdown():
|
async def shutdown():
|
||||||
await redis.disconnect()
|
await redis.disconnect()
|
||||||
|
|
31
orm/shout.py
31
orm/shout.py
|
@ -2,6 +2,7 @@ from typing import List
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from sqlalchemy import Table, Column, Integer, String, ForeignKey, DateTime, Boolean
|
from sqlalchemy import Table, Column, Integer, String, ForeignKey, DateTime, Boolean
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
|
from sqlalchemy.orm.attributes import flag_modified
|
||||||
from orm import Permission, User, Topic
|
from orm import Permission, User, Topic
|
||||||
from orm.comment import Comment
|
from orm.comment import Comment
|
||||||
from orm.base import Base
|
from orm.base import Base
|
||||||
|
@ -62,20 +63,44 @@ class ShoutViewByDay(Base):
|
||||||
|
|
||||||
id = None
|
id = None
|
||||||
shout_id = Column(ForeignKey('shout.id'), primary_key = True)
|
shout_id = Column(ForeignKey('shout.id'), primary_key = True)
|
||||||
day: str = Column(DateTime, primary_key = True, default = datetime.now)
|
day = Column(DateTime, primary_key = True, default = datetime.now)
|
||||||
value = Column(Integer)
|
value = Column(Integer)
|
||||||
|
|
||||||
class ShoutViewStorage:
|
class ShoutViewStorage:
|
||||||
|
|
||||||
def __init__(self, session):
|
def __init__(self, session):
|
||||||
self.views = session.query(ShoutViewByDay).all()
|
self.views = session.query(ShoutViewByDay).all()
|
||||||
|
self.this_day_views = {}
|
||||||
|
for view in self.views:
|
||||||
|
shout_id = view.shout_id
|
||||||
|
if not shout_id in self.this_day_views:
|
||||||
|
self.this_day_views[shout_id] = view
|
||||||
|
this_day_view = self.this_day_views[shout_id]
|
||||||
|
if this_day_view.day < view.day:
|
||||||
|
self.this_day_views[shout_id] = view
|
||||||
|
|
||||||
def get_view(self, shout_id):
|
def get_view(self, shout_id):
|
||||||
shout_views = list(filter(lambda x: x.shout_id == shout_id, self.views))
|
shout_views = list(filter(lambda x: x.shout_id == shout_id, self.views))
|
||||||
return reduce((lambda x, y: x + y.value), shout_views, 0)
|
return reduce((lambda x, y: x + y.value), shout_views, 0)
|
||||||
|
|
||||||
def add_view(self, view):
|
def inc_view(self, shout_id):
|
||||||
self.views.append(view)
|
this_day_view = self.this_day_views.get(shout_id)
|
||||||
|
if not this_day_view:
|
||||||
|
this_day_view = ShoutViewByDay.create(shout_id = shout_id, value = 1)
|
||||||
|
self.this_day_views[shout_id] = this_day_view
|
||||||
|
self.views.append(this_day_view)
|
||||||
|
else:
|
||||||
|
this_day_view.value = this_day_view.value + 1
|
||||||
|
this_day_view.modified = True
|
||||||
|
|
||||||
|
def flush_changes(self, session):
|
||||||
|
for view in self.this_day_views.values():
|
||||||
|
if getattr(view, "modified", False):
|
||||||
|
session.add(view)
|
||||||
|
flag_modified(view, "value")
|
||||||
|
view.modified = False
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
|
||||||
class Shout(Base):
|
class Shout(Base):
|
||||||
__tablename__ = 'shout'
|
__tablename__ = 'shout'
|
||||||
|
|
|
@ -165,6 +165,17 @@ class TopShouts:
|
||||||
print("top shouts worker error = %s" % (err))
|
print("top shouts worker error = %s" % (err))
|
||||||
await asyncio.sleep(TopShouts.period)
|
await asyncio.sleep(TopShouts.period)
|
||||||
|
|
||||||
|
async def db_flush_worker():
|
||||||
|
print("db flush worker start")
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
print("flush changes")
|
||||||
|
with local_session() as session:
|
||||||
|
view_storage.flush_changes(session)
|
||||||
|
except Exception as err:
|
||||||
|
print("db flush worker error = %s" % (err))
|
||||||
|
await asyncio.sleep(30*60)
|
||||||
|
|
||||||
|
|
||||||
@query.field("topShoutsByView")
|
@query.field("topShoutsByView")
|
||||||
async def top_shouts_by_view(_, info, limit):
|
async def top_shouts_by_view(_, info, limit):
|
||||||
|
@ -284,6 +295,11 @@ async def rate_shout(_, info, shout_id, value):
|
||||||
|
|
||||||
return {"error" : ""}
|
return {"error" : ""}
|
||||||
|
|
||||||
|
@mutation.field("viewShout")
|
||||||
|
async def view_shout(_, info, shout_id):
|
||||||
|
view_storage.inc_view(shout_id)
|
||||||
|
return {"error" : ""}
|
||||||
|
|
||||||
@query.field("getShoutBySlug")
|
@query.field("getShoutBySlug")
|
||||||
async def get_shout_by_slug(_, info, slug):
|
async def get_shout_by_slug(_, info, slug):
|
||||||
slug_fields = [node.name.value for node in info.field_nodes[0].selection_set.selections]
|
slug_fields = [node.name.value for node in info.field_nodes[0].selection_set.selections]
|
||||||
|
|
|
@ -69,6 +69,7 @@ type Mutation {
|
||||||
updateShout(id: Int!, input: ShoutInput!): ShoutResult!
|
updateShout(id: Int!, input: ShoutInput!): ShoutResult!
|
||||||
deleteShout(id: Int!): Result!
|
deleteShout(id: Int!): Result!
|
||||||
rateShout(shout_id: Int!, value: Int!): Result!
|
rateShout(shout_id: Int!, value: Int!): Result!
|
||||||
|
viewShout(shout_id: Int!): Result!
|
||||||
|
|
||||||
# user profile
|
# user profile
|
||||||
# rateUser(value: Int!): Result!
|
# rateUser(value: Int!): Result!
|
||||||
|
|
Loading…
Reference in New Issue
Block a user