limit-offset-patch
This commit is contained in:
parent
90439d931d
commit
66cd20514e
|
@ -6,11 +6,12 @@ from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
|||
from orm.topic import TopicFollower
|
||||
from orm.user import AuthorFollower
|
||||
from typing import List
|
||||
from services.zine.shoutscache import prepare_shouts
|
||||
|
||||
|
||||
@query.field("shoutsForFeed")
|
||||
@login_required
|
||||
def get_user_feed(_, info, page, size) -> List[Shout]:
|
||||
def get_user_feed(_, info, offset, limit) -> List[Shout]:
|
||||
user = info.context["request"].user
|
||||
shouts = []
|
||||
with local_session() as session:
|
||||
|
@ -28,23 +29,23 @@ def get_user_feed(_, info, page, size) -> List[Shout]:
|
|||
.where(TopicFollower.follower == user.slug)
|
||||
.order_by(desc(Shout.createdAt))
|
||||
)
|
||||
shouts = shouts.union(topicrows).limit(size).offset(page * size).all()
|
||||
shouts = shouts.union(topicrows).limit(limit).offset(offset).all()
|
||||
return shouts
|
||||
|
||||
|
||||
@query.field("myCandidates")
|
||||
@login_required
|
||||
async def user_unpublished_shouts(_, info, page=1, size=10) -> List[Shout]:
|
||||
async def user_unpublished_shouts(_, info, offset, limit) -> List[Shout]:
|
||||
user = info.context["request"].user
|
||||
shouts = []
|
||||
with local_session() as session:
|
||||
shouts = (
|
||||
shouts = prepare_shouts(
|
||||
session.query(Shout)
|
||||
.join(ShoutAuthor)
|
||||
.where(and_(not bool(Shout.publishedAt), ShoutAuthor.user == user.slug))
|
||||
.order_by(desc(Shout.createdAt))
|
||||
.limit(size)
|
||||
.offset(page * size)
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
.all()
|
||||
)
|
||||
return shouts
|
||||
|
|
|
@ -16,7 +16,7 @@ from typing import List
|
|||
|
||||
|
||||
@query.field("userReactedShouts")
|
||||
async def get_user_reacted_shouts(_, info, slug, page, size) -> List[Shout]:
|
||||
async def get_user_reacted_shouts(_, _info, slug, offset, limit) -> List[Shout]:
|
||||
user = await UserStorage.get_user_by_slug(slug)
|
||||
if not user:
|
||||
return []
|
||||
|
@ -26,8 +26,8 @@ async def get_user_reacted_shouts(_, info, slug, page, size) -> List[Shout]:
|
|||
.join(Reaction)
|
||||
.where(Reaction.createdBy == user.slug)
|
||||
.order_by(desc(Reaction.createdAt))
|
||||
.limit(size)
|
||||
.offset(page * size)
|
||||
.limit(limit)
|
||||
.offset()
|
||||
.all()
|
||||
)
|
||||
return shouts
|
||||
|
@ -99,7 +99,7 @@ async def get_current_user(_, info):
|
|||
|
||||
|
||||
@query.field("getUsersBySlugs")
|
||||
async def get_users_by_slugs(_, info, slugs):
|
||||
async def get_users_by_slugs(_, _info, slugs):
|
||||
with local_session() as session:
|
||||
users = (
|
||||
session.query(User)
|
||||
|
@ -111,7 +111,7 @@ async def get_users_by_slugs(_, info, slugs):
|
|||
|
||||
|
||||
@query.field("getUserRoles")
|
||||
async def get_user_roles(_, info, slug):
|
||||
async def get_user_roles(_, _info, slug):
|
||||
with local_session() as session:
|
||||
user = session.query(User).where(User.slug == slug).first()
|
||||
roles = (
|
||||
|
@ -183,14 +183,10 @@ def author_unfollow(user, slug):
|
|||
|
||||
|
||||
@query.field("authorsAll")
|
||||
def get_authors_all(_, info, page, size):
|
||||
end = page * size
|
||||
start = end - size
|
||||
return list(UserStorage.get_all_users())[start:end] # type: ignore
|
||||
def get_authors_all(_, _info, offset, limit):
|
||||
return list(UserStorage.get_all_users())[offset : offset + limit] # type: ignore
|
||||
|
||||
|
||||
@query.field("topAuthors")
|
||||
def get_top_authors(_, info, page, size):
|
||||
end = page * size
|
||||
start = end - size
|
||||
return list(UserStorage.get_top_users())[start:end] # type: ignore
|
||||
def get_top_authors(_, _info, offset, limit):
|
||||
return list(UserStorage.get_top_users())[offset : offset + limit] # type: ignore
|
||||
|
|
|
@ -113,7 +113,7 @@ async def delete_reaction(_, info, id):
|
|||
|
||||
|
||||
@query.field("reactionsByShout")
|
||||
async def get_shout_reactions(_, info, slug, page, size):
|
||||
async def get_shout_reactions(_, info, slug, offset, limit):
|
||||
offset = page * size
|
||||
reactions = []
|
||||
with local_session() as session:
|
||||
|
@ -130,7 +130,7 @@ async def get_shout_reactions(_, info, slug, page, size):
|
|||
|
||||
|
||||
@query.field("reactionsForShouts")
|
||||
async def get_reactions_for_shouts(_, info, shouts, page, size):
|
||||
async def get_reactions_for_shouts(_, info, shouts, offset, limit):
|
||||
offset = page * size
|
||||
reactions = []
|
||||
with local_session() as session:
|
||||
|
|
|
@ -4,7 +4,7 @@ from orm.topic import Topic
|
|||
from base.orm import local_session
|
||||
from base.resolvers import mutation, query
|
||||
from services.zine.shoutauthor import ShoutAuthorStorage
|
||||
from services.zine.shoutscache import ShoutsCache
|
||||
from services.zine.shoutscache import ShoutsCache, prepare_shouts
|
||||
from services.stat.viewed import ViewedStorage
|
||||
from resolvers.profile import author_follow, author_unfollow
|
||||
from resolvers.topics import topic_follow, topic_unfollow
|
||||
|
@ -22,39 +22,39 @@ async def increment_view(_, _info, shout):
|
|||
|
||||
|
||||
@query.field("topViewed")
|
||||
async def top_viewed(_, _info, page, size):
|
||||
async def top_viewed(_, _info, offset, limit):
|
||||
async with ShoutsCache.lock:
|
||||
return ShoutsCache.top_viewed[((page - 1) * size) : (page * size)]
|
||||
return ShoutsCache.top_viewed[offset : offset + limit]
|
||||
|
||||
|
||||
@query.field("topMonth")
|
||||
async def top_month(_, _info, page, size):
|
||||
async def top_month(_, _info, offset, limit):
|
||||
async with ShoutsCache.lock:
|
||||
return ShoutsCache.top_month[((page - 1) * size) : (page * size)]
|
||||
return ShoutsCache.top_month[offset : offset + limit]
|
||||
|
||||
|
||||
@query.field("topOverall")
|
||||
async def top_overall(_, _info, page, size):
|
||||
async def top_overall(_, _info, offset, limit):
|
||||
async with ShoutsCache.lock:
|
||||
return ShoutsCache.top_overall[((page - 1) * size) : (page * size)]
|
||||
return ShoutsCache.top_overall[offset : offset + limit]
|
||||
|
||||
|
||||
@query.field("recentPublished")
|
||||
async def recent_published(_, _info, page, size):
|
||||
async def recent_published(_, _info, offset, limit):
|
||||
async with ShoutsCache.lock:
|
||||
return ShoutsCache.recent_published[((page - 1) * size) : (page * size)]
|
||||
return ShoutsCache.top_overall[offset : offset + limit]
|
||||
|
||||
|
||||
@query.field("recentAll")
|
||||
async def recent_all(_, _info, page, size):
|
||||
async def recent_all(_, _info, offset, limit):
|
||||
async with ShoutsCache.lock:
|
||||
return ShoutsCache.recent_all[((page - 1) * size) : (page * size)]
|
||||
return ShoutsCache.recent_all[offset : offset + limit]
|
||||
|
||||
|
||||
@query.field("recentReacted")
|
||||
async def recent_reacted(_, _info, page, size):
|
||||
async def recent_reacted(_, _info, offset, limit):
|
||||
async with ShoutsCache.lock:
|
||||
return ShoutsCache.recent_reacted[((page - 1) * size) : (page * size)]
|
||||
return ShoutsCache.recent_all[offset : offset + limit]
|
||||
|
||||
|
||||
@mutation.field("viewShout")
|
||||
|
@ -70,7 +70,6 @@ async def get_shout_by_slug(_, info, slug):
|
|||
]
|
||||
selected_fields = set(["authors", "topics"]).intersection(all_fields)
|
||||
select_options = [selectinload(getattr(Shout, field)) for field in selected_fields]
|
||||
shout = {}
|
||||
with local_session() as session:
|
||||
# s = text(open("src/queries/shout-by-slug.sql", "r").read() % slug)
|
||||
shout = (
|
||||
|
@ -90,18 +89,17 @@ async def get_shout_by_slug(_, info, slug):
|
|||
|
||||
|
||||
@query.field("searchQuery")
|
||||
async def get_search_results(_, _info, query, page, size):
|
||||
async def get_search_results(_, _info, query, offset, limit):
|
||||
# TODO: remove the copy of searchByTopics
|
||||
# with search ranking query
|
||||
page = page - 1
|
||||
with local_session() as session:
|
||||
shouts = (
|
||||
session.query(Shout)
|
||||
.join(ShoutTopic)
|
||||
.where(and_(ShoutTopic.topic.in_(query), bool(Shout.publishedAt)))
|
||||
.order_by(desc(Shout.publishedAt))
|
||||
.limit(size)
|
||||
.offset(page * size)
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
)
|
||||
|
||||
for s in shouts:
|
||||
|
@ -112,16 +110,15 @@ async def get_search_results(_, _info, query, page, size):
|
|||
|
||||
|
||||
@query.field("shoutsByTopics")
|
||||
async def shouts_by_topics(_, _info, slugs, page, size):
|
||||
page = page - 1
|
||||
async def shouts_by_topics(_, _info, slugs, offset, limit):
|
||||
with local_session() as session:
|
||||
shouts = (
|
||||
shouts = prepare_shouts(
|
||||
session.query(Shout)
|
||||
.join(ShoutTopic)
|
||||
.where(and_(ShoutTopic.topic.in_(slugs), bool(Shout.publishedAt)))
|
||||
.order_by(desc(Shout.publishedAt))
|
||||
.limit(size)
|
||||
.offset(page * size)
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
)
|
||||
|
||||
for s in shouts:
|
||||
|
@ -131,17 +128,15 @@ async def shouts_by_topics(_, _info, slugs, page, size):
|
|||
|
||||
|
||||
@query.field("shoutsByCollection")
|
||||
async def shouts_by_collection(_, _info, collection, page, size):
|
||||
page = page - 1
|
||||
shouts = []
|
||||
async def shouts_by_collection(_, _info, collection, offset, limit):
|
||||
with local_session() as session:
|
||||
shouts = (
|
||||
shouts = prepare_shouts(
|
||||
session.query(Shout)
|
||||
.join(ShoutCollection, ShoutCollection.collection == collection)
|
||||
.where(and_(ShoutCollection.shout == Shout.slug, bool(Shout.publishedAt)))
|
||||
.order_by(desc(Shout.publishedAt))
|
||||
.limit(size)
|
||||
.offset(page * size)
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
)
|
||||
for s in shouts:
|
||||
for a in s.authors:
|
||||
|
@ -150,17 +145,15 @@ async def shouts_by_collection(_, _info, collection, page, size):
|
|||
|
||||
|
||||
@query.field("shoutsByAuthors")
|
||||
async def shouts_by_authors(_, _info, slugs, page, size):
|
||||
page = page - 1
|
||||
async def shouts_by_authors(_, _info, slugs, offset, limit):
|
||||
with local_session() as session:
|
||||
|
||||
shouts = (
|
||||
session.query(Shout)
|
||||
.join(ShoutAuthor)
|
||||
.where(and_(ShoutAuthor.user.in_(slugs), bool(Shout.publishedAt)))
|
||||
.order_by(desc(Shout.publishedAt))
|
||||
.limit(size)
|
||||
.offset(page * size)
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
)
|
||||
|
||||
for s in shouts:
|
||||
|
@ -173,11 +166,10 @@ SINGLE_COMMUNITY = True
|
|||
|
||||
|
||||
@query.field("shoutsByCommunities")
|
||||
async def shouts_by_communities(_, info, slugs, page, size):
|
||||
async def shouts_by_communities(_, info, slugs, offset, limit):
|
||||
if SINGLE_COMMUNITY:
|
||||
return recent_published(_, info, page, size)
|
||||
return recent_published(_, info, offset, limit)
|
||||
else:
|
||||
page = page - 1
|
||||
with local_session() as session:
|
||||
# TODO fix postgres high load
|
||||
shouts = (
|
||||
|
@ -193,8 +185,8 @@ async def shouts_by_communities(_, info, slugs, page, size):
|
|||
)
|
||||
)
|
||||
.order_by(desc(Shout.publishedAt))
|
||||
.limit(size)
|
||||
.offset(page * size)
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
)
|
||||
|
||||
for s in shouts:
|
||||
|
|
|
@ -223,28 +223,28 @@ type Query {
|
|||
userFollowedCommunities(slug: String!): [Community]!
|
||||
userReactedShouts(slug: String!): [Shout]! # test
|
||||
getUserRoles(slug: String!): [Role]!
|
||||
authorsAll(page: Int!, size: Int!): [User]!
|
||||
authorsAll(offset: Int!, limit: Int!): [User]!
|
||||
|
||||
# shouts
|
||||
getShoutBySlug(slug: String!): Shout!
|
||||
shoutsForFeed(page: Int!, size: Int!): [Shout]! # test
|
||||
shoutsByTopics(slugs: [String]!, page: Int!, size: Int!): [Shout]!
|
||||
shoutsByAuthors(slugs: [String]!, page: Int!, size: Int!): [Shout]!
|
||||
shoutsByCommunities(slugs: [String]!, page: Int!, size: Int!): [Shout]!
|
||||
myCandidates(page: Int!, size: Int!): [Shout]! # test
|
||||
topViewed(page: Int!, size: Int!): [Shout]!
|
||||
# topReacted(page: Int!, size: Int!): [Shout]!
|
||||
topAuthors(page: Int!, size: Int!): [Author]!
|
||||
topMonth(page: Int!, size: Int!): [Shout]!
|
||||
topOverall(page: Int!, size: Int!): [Shout]!
|
||||
recentPublished(page: Int!, size: Int!): [Shout]! # homepage
|
||||
recentReacted(page: Int!, size: Int!): [Shout]! # test
|
||||
recentAll(page: Int!, size: Int!): [Shout]!
|
||||
shoutsForFeed(offset: Int!, limit: Int!): [Shout]! # test
|
||||
shoutsByTopics(slugs: [String]!, offset: Int!, limit: Int!): [Shout]!
|
||||
shoutsByAuthors(slugs: [String]!, offset: Int!, limit: Int!): [Shout]!
|
||||
shoutsByCommunities(slugs: [String]!, offset: Int!, limit: Int!): [Shout]!
|
||||
myCandidates(offset: Int!, limit: Int!): [Shout]! # test
|
||||
topViewed(offset: Int!, limit: Int!): [Shout]!
|
||||
# topReacted(offset: Int!, limit: Int!): [Shout]!
|
||||
topAuthors(offset: Int!, limit: Int!): [Author]!
|
||||
topMonth(offset: Int!, limit: Int!): [Shout]!
|
||||
topOverall(offset: Int!, limit: Int!): [Shout]!
|
||||
recentPublished(offset: Int!, limit: Int!): [Shout]! # homepage
|
||||
recentReacted(offset: Int!, limit: Int!): [Shout]! # test
|
||||
recentAll(offset: Int!, limit: Int!): [Shout]!
|
||||
|
||||
# reactons
|
||||
reactionsByAuthor(slug: String!, page: Int!, size: Int!): [Reaction]!
|
||||
reactionsByShout(slug: String!, page: Int!, size: Int!): [Reaction]!
|
||||
reactionsForShouts(shouts: [String]!, page: Int!, size: Int!): [Reaction]!
|
||||
reactionsByAuthor(slug: String!, offset: Int!, limit: Int!): [Reaction]!
|
||||
reactionsByShout(slug: String!, offset: Int!, limit: Int!): [Reaction]!
|
||||
reactionsForShouts(shouts: [String]!, offset: Int!, limit: Int!): [Reaction]!
|
||||
|
||||
# collab
|
||||
getCollabs: [Collab]!
|
||||
|
@ -258,14 +258,14 @@ type Query {
|
|||
# collections
|
||||
collectionsAll: [Collection]!
|
||||
getUserCollections(author: String!): [Collection]!
|
||||
shoutsByCollection(collection: String!, page: Int, size: Int): [Shout]!
|
||||
shoutsByCollection(collection: String!, offset: Int!, limit: Int!): [Shout]!
|
||||
|
||||
# communities
|
||||
getCommunity(slug: String): Community!
|
||||
getCommunities: [Community]! # all
|
||||
|
||||
# search
|
||||
searchQuery(q: String, page: Int, size: Int): [Shout]
|
||||
searchQuery(q: String, offset: Int!, limit: Int!): [Shout]
|
||||
}
|
||||
|
||||
############################################ Subscription
|
||||
|
|
|
@ -8,11 +8,10 @@ from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
|||
from services.stat.viewed import ViewedByDay
|
||||
|
||||
|
||||
def shoutify(session, stmt):
|
||||
async def prepare_shouts(session, stmt):
|
||||
shouts = []
|
||||
for s in list(map(lambda r: r.Shout, session.execute(stmt))):
|
||||
if not bool(s.stat):
|
||||
raise Exception
|
||||
s.stats = await s.stat
|
||||
shouts.append(s)
|
||||
return shouts
|
||||
|
||||
|
@ -35,7 +34,7 @@ class ShoutsCache:
|
|||
@staticmethod
|
||||
async def prepare_recent_published():
|
||||
with local_session() as session:
|
||||
shouts = shoutify(session, (
|
||||
shouts = await prepare_shouts(session, (
|
||||
select(Shout)
|
||||
.options(selectinload(Shout.authors), selectinload(Shout.topics))
|
||||
.where(bool(Shout.publishedAt))
|
||||
|
@ -49,7 +48,7 @@ class ShoutsCache:
|
|||
@staticmethod
|
||||
async def prepare_recent_all():
|
||||
with local_session() as session:
|
||||
shouts = shoutify(session, (
|
||||
shouts = await prepare_shouts(session, (
|
||||
select(Shout)
|
||||
.options(selectinload(Shout.authors), selectinload(Shout.topics))
|
||||
.order_by(desc("createdAt"))
|
||||
|
@ -62,7 +61,7 @@ class ShoutsCache:
|
|||
@staticmethod
|
||||
async def prepare_recent_reacted():
|
||||
with local_session() as session:
|
||||
shouts = shoutify(session, (
|
||||
shouts = await prepare_shouts(session, (
|
||||
select(Shout, func.max(Reaction.createdAt).label("reactionCreatedAt"))
|
||||
.options(
|
||||
selectinload(Shout.authors),
|
||||
|
@ -82,7 +81,7 @@ class ShoutsCache:
|
|||
async def prepare_top_overall():
|
||||
with local_session() as session:
|
||||
# with reacted times counter
|
||||
shouts = shoutify(session, (
|
||||
shouts = await prepare_shouts(session, (
|
||||
select(Shout, func.count(Reaction.id).label("reacted"))
|
||||
.options(
|
||||
selectinload(Shout.authors),
|
||||
|
@ -95,7 +94,7 @@ class ShoutsCache:
|
|||
.order_by(desc("reacted"))
|
||||
.limit(ShoutsCache.limit)
|
||||
))
|
||||
shouts.sort(key=lambda s: s.stat['rating'], reverse=True)
|
||||
shouts.sort(key=lambda s: s.stats['rating'], reverse=True)
|
||||
async with ShoutsCache.lock:
|
||||
print("[zine.cache] %d top shouts " % len(shouts))
|
||||
ShoutsCache.top_overall = shouts
|
||||
|
@ -104,7 +103,7 @@ class ShoutsCache:
|
|||
async def prepare_top_month():
|
||||
month_ago = datetime.now() - timedelta(days=30)
|
||||
with local_session() as session:
|
||||
shouts = shoutify(session, (
|
||||
shouts = await prepare_shouts(session, (
|
||||
select(Shout, func.count(Reaction.id).label("reacted"))
|
||||
.options(selectinload(Shout.authors), selectinload(Shout.topics))
|
||||
.join(Reaction)
|
||||
|
@ -113,7 +112,7 @@ class ShoutsCache:
|
|||
.order_by(desc("reacted"))
|
||||
.limit(ShoutsCache.limit)
|
||||
))
|
||||
shouts.sort(key=lambda s: s.stat['rating'], reverse=True)
|
||||
shouts.sort(key=lambda s: s.stats['rating'], reverse=True)
|
||||
async with ShoutsCache.lock:
|
||||
print("[zine.cache] %d top month shouts " % len(shouts))
|
||||
ShoutsCache.top_month = shouts
|
||||
|
@ -122,7 +121,7 @@ class ShoutsCache:
|
|||
async def prepare_top_viewed():
|
||||
month_ago = datetime.now() - timedelta(days=30)
|
||||
with local_session() as session:
|
||||
shouts = shoutify(session, (
|
||||
shouts = await prepare_shouts(session, (
|
||||
select(Shout, func.sum(ViewedByDay.value).label("viewed"))
|
||||
.options(selectinload(Shout.authors), selectinload(Shout.topics))
|
||||
.join(ViewedByDay)
|
||||
|
@ -131,7 +130,7 @@ class ShoutsCache:
|
|||
.order_by(desc("viewed"))
|
||||
.limit(ShoutsCache.limit)
|
||||
))
|
||||
shouts.sort(key=lambda s: s.stat['viewed'], reverse=True)
|
||||
shouts.sort(key=lambda s: s.stats['viewed'], reverse=True)
|
||||
async with ShoutsCache.lock:
|
||||
print("[zine.cache] %d top viewed shouts " % len(shouts))
|
||||
ShoutsCache.top_viewed = shouts
|
||||
|
|
Loading…
Reference in New Issue
Block a user