add authors and subscriptions in Topic stat
This commit is contained in:
parent
01a974e974
commit
a98d5f6ee6
2
main.py
2
main.py
|
@ -32,8 +32,8 @@ async def start_up():
|
||||||
git_task = asyncio.create_task(GitTask.git_task_worker())
|
git_task = asyncio.create_task(GitTask.git_task_worker())
|
||||||
shouts_cache_task = asyncio.create_task(ShoutsCache.worker())
|
shouts_cache_task = asyncio.create_task(ShoutsCache.worker())
|
||||||
view_storage_task = asyncio.create_task(ShoutViewStorage.worker())
|
view_storage_task = asyncio.create_task(ShoutViewStorage.worker())
|
||||||
topic_stat_task = asyncio.create_task(TopicStat.worker())
|
|
||||||
shout_author_task = asyncio.create_task(ShoutAuthorStorage.worker())
|
shout_author_task = asyncio.create_task(ShoutAuthorStorage.worker())
|
||||||
|
topic_stat_task = asyncio.create_task(TopicStat.worker())
|
||||||
|
|
||||||
async def shutdown():
|
async def shutdown():
|
||||||
await redis.disconnect()
|
await redis.disconnect()
|
||||||
|
|
27
orm/shout.py
27
orm/shout.py
|
@ -3,7 +3,7 @@ from datetime import datetime, timedelta
|
||||||
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 sqlalchemy.orm.attributes import flag_modified
|
||||||
from orm import Permission, User, Topic
|
from orm import Permission, User, Topic, TopicSubscription
|
||||||
from orm.comment import Comment
|
from orm.comment import Comment
|
||||||
from orm.base import Base, local_session
|
from orm.base import Base, local_session
|
||||||
|
|
||||||
|
@ -148,6 +148,8 @@ class ShoutViewStorage:
|
||||||
|
|
||||||
class TopicStat:
|
class TopicStat:
|
||||||
shouts_by_topic = {}
|
shouts_by_topic = {}
|
||||||
|
authors_by_topic = {}
|
||||||
|
subs_by_topic = {}
|
||||||
lock = asyncio.Lock()
|
lock = asyncio.Lock()
|
||||||
|
|
||||||
period = 30*60 #sec
|
period = 30*60 #sec
|
||||||
|
@ -164,6 +166,21 @@ class TopicStat:
|
||||||
else:
|
else:
|
||||||
self.shouts_by_topic[topic] = [shout]
|
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)
|
||||||
|
|
||||||
|
subs = session.query(TopicSubscription)
|
||||||
|
for sub in subs:
|
||||||
|
topic = sub.topic
|
||||||
|
user = sub.user
|
||||||
|
if topic in self.subs_by_topic:
|
||||||
|
self.subs_by_topic[topic].append(user)
|
||||||
|
else:
|
||||||
|
self.subs_by_topic[topic] = [user]
|
||||||
|
|
||||||
async def get_shouts(topic):
|
async def get_shouts(topic):
|
||||||
self = TopicStat
|
self = TopicStat
|
||||||
async with self.lock:
|
async with self.lock:
|
||||||
|
@ -174,7 +191,13 @@ class TopicStat:
|
||||||
self = TopicStat
|
self = TopicStat
|
||||||
async with self.lock:
|
async with self.lock:
|
||||||
shouts = self.shouts_by_topic.get(topic, [])
|
shouts = self.shouts_by_topic.get(topic, [])
|
||||||
stat = { "shouts" : len(shouts) }
|
subs = self.subs_by_topic.get(topic, [])
|
||||||
|
authors = self.authors_by_topic.get(topic, set())
|
||||||
|
stat = {
|
||||||
|
"shouts" : len(shouts),
|
||||||
|
"authors" : len(authors),
|
||||||
|
"subscriptions" : len(subs)
|
||||||
|
}
|
||||||
|
|
||||||
views = 0
|
views = 0
|
||||||
for shout in shouts:
|
for shout in shouts:
|
||||||
|
|
|
@ -32,17 +32,6 @@ async def topics_by_author(_, info, author):
|
||||||
slugs.update([topic.slug for topic in shout.topics])
|
slugs.update([topic.slug for topic in shout.topics])
|
||||||
return await TopicStorage.get_topics(slugs)
|
return await TopicStorage.get_topics(slugs)
|
||||||
|
|
||||||
@query.field("getTopicAuthors")
|
|
||||||
async def topics_by_author(_, info, slug, count, page):
|
|
||||||
shouts = await TopicStat.get_shouts(slug)
|
|
||||||
authors = set()
|
|
||||||
for shout in shouts:
|
|
||||||
authors.update(await ShoutAuthorStorage.get_authors(shout))
|
|
||||||
authors = list(authors)
|
|
||||||
authors.sort() #TODO sort by username
|
|
||||||
authors = authors[count * page : count * (page + 1) ]
|
|
||||||
return [await UserStorage.get_user(author) for author in authors]
|
|
||||||
|
|
||||||
@mutation.field("createTopic")
|
@mutation.field("createTopic")
|
||||||
@login_required
|
@login_required
|
||||||
async def create_topic(_, info, input):
|
async def create_topic(_, info, input):
|
||||||
|
|
|
@ -170,7 +170,6 @@ type Query {
|
||||||
topicsBySlugs(slugs: [String]): [Topic]!
|
topicsBySlugs(slugs: [String]): [Topic]!
|
||||||
topicsByCommunity(community: String!): [Topic]!
|
topicsByCommunity(community: String!): [Topic]!
|
||||||
topicsByAuthor(author: String!): [Topic]!
|
topicsByAuthor(author: String!): [Topic]!
|
||||||
getTopicAuthors(slug: String!, count: Int!, page: Int!): [User]!
|
|
||||||
|
|
||||||
# getOnlineUsers: [User!]!
|
# getOnlineUsers: [User!]!
|
||||||
|
|
||||||
|
@ -335,8 +334,8 @@ type Community {
|
||||||
type TopicStat {
|
type TopicStat {
|
||||||
shouts: Int!
|
shouts: Int!
|
||||||
views: Int!
|
views: Int!
|
||||||
subscriptions: Int
|
subscriptions: Int!
|
||||||
authors: Int
|
authors: Int!
|
||||||
}
|
}
|
||||||
|
|
||||||
type Topic {
|
type Topic {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user