Skip to content
Snippets Groups Projects
Commit 382f8bbc authored by Gandalf's avatar Gandalf
Browse files

[JES] Update schema to add a new index. Also changes an index that we decided...

[JES] Update schema to add a new index. Also changes an index that we decided we'd rather have the full verson of. Re-write get_disccuion_by in condenser_api to look nicer
parent 195eb42c
No related branches found
No related tags found
5 merge requests!456Release candidate v1 24,!230Setup monitoring with pghero,!135Enable postgres monitoring on CI server,!16Dk issue 3 concurrent block query rebase,!15Dk issue 3 concurrent block query
......@@ -88,7 +88,7 @@ def build_metadata():
sa.ForeignKeyConstraint(['parent_id'], ['hive_posts.id'], name='hive_posts_fk3'),
sa.UniqueConstraint('author', 'permlink', name='hive_posts_ux1'),
sa.Index('hive_posts_ix3', 'author', 'depth', 'id', postgresql_where=sql_text("is_deleted = '0'")), # API: author blog/comments
sa.Index('hive_posts_ix4', 'parent_id', 'id'), #postgresql_where=sql_text("is_deleted = '0'")), # API: fetching children #[JES] We decided we want the full index since posts can be deleted/undeleted
sa.Index('hive_posts_ix4', 'parent_id DESC NULLS LAST', 'id'), #postgresql_where=sql_text("is_deleted = '0'")), # API: fetching children #[JES] We decided we want the full index since posts can be deleted/undeleted
sa.Index('hive_posts_ix5', 'id', postgresql_where=sql_text("is_pinned = '1' AND is_deleted = '0'")), # API: pinned post status
sa.Index('hive_posts_ix6', 'community_id', 'id', postgresql_where=sql_text("community_id IS NOT NULL AND is_pinned = '1' AND is_deleted = '0'")), # API: community pinned
)
......@@ -225,6 +225,7 @@ def build_metadata():
sa.Index('hive_posts_cache_ix32', 'community_id', 'created_at', 'post_id', postgresql_where=sql_text("community_id IS NOT NULL AND is_grayed = '0' AND depth = 0")), # API: community created
sa.Index('hive_posts_cache_ix33', 'community_id', 'payout', 'post_id', postgresql_where=sql_text("community_id IS NOT NULL AND is_grayed = '0' AND is_paidout = '0'")), # API: community payout
sa.Index('hive_posts_cache_ix34', 'community_id', 'payout', 'post_id', postgresql_where=sql_text("community_id IS NOT NULL AND is_grayed = '1' AND is_paidout = '0'")), # API: community muted
sa.Index('hive_posts_cache_ix35', 'author', 'depth'),
)
sa.Table(
......
......@@ -188,25 +188,66 @@ async def get_discussions_by(discussion_type, context, start_author: str = '',
assert not filter_tags, 'filter tags not supported'
assert discussion_type in ['trending', 'hot', 'created', 'promoted',
'payout', 'payout_comments'], 'invalid discussion type'
valid_account(start_author, allow_empty=True)
valid_permlink(start_permlink, allow_empty=True)
valid_limit(limit, 100)
valid_tag(tag, allow_empty=True)
db = context['db']
query_information = await cursor.pids_by_query(
db,
discussion_type,
valid_account(start_author, allow_empty=True),
valid_permlink(start_permlink, allow_empty=True),
valid_limit(limit, 100),
valid_tag(tag, allow_empty=True))
sql = "---get_discussions_by_" + discussion_type + "\r\n" + SELECT_FRAGMENT
assert len(query_information) == 4, 'generated query is malformed, aborting'
sql = query_information[0]
sql = "---get_discussions_by_" + discussion_type + "\r\n" + sql
sql_tag = query_information[1]
sql_start_id = query_information[2]
sql_limit = query_information[3]
result = await db.query_all(sql, tag=sql_tag, start_id=sql_start_id, limit=sql_limit, start_author=start_author, start_permlink=start_permlink)
posts = await resultset_to_posts(db=db, resultset=result, truncate_body=0)
if tag and tag != 'all':
sql = sql + """ JOIN hive_post_tags on (hive_posts_cache.post_id = hive_post_tags.tag) """
sql = sql + """ WHERE NOT hive_posts.is_deleted """
if discussion_type == 'trending':
sql = sql + """ AND NOT hive_posts_cache.is_paidout %s ORDER BY sc_trend DESC LIMIT :limit """
elif discussion_type == 'hot':
sql = sql + """ AND NOT hive_posts_cache.is_paidout %s ORDER BY sc_hot DESC LIMIT :limit """
elif discussion_type == 'created':
sql = sql + """ AND hive_posts.depth = 0 %s ORDER BY hive_posts_cache.created_at DESC LIMIT :limit """
elif discussion_type == 'promoted':
sql = sql + """ AND NOT hive_posts_cache.is_paidout AND hive_posts.promoted > 0
%s ORDER BY hive_posts_cache.promoted DESC LIMIT :limit """
elif discussion_type == 'payout':
sql = sql + """ AND NOT hive_posts_cache.is_paidout AND hive_posts_cache.depth = 0
%s ORDER BY hive_posts_cache.payout DESC LIMIT :limit """
elif discussion_type == 'payout_comments':
sql = sql + """ AND NOT hive_posts_cache.is_paidout AND hive_posts_cache.depth > 0
%s ORDER BY hive_posts_cache.payout DESC LIMIT :limit """
if tag and tag != 'all':
if tag[:5] == 'hive-':
sql = sql % """ AND hive_posts_category = :tag %s """
else:
sql = sql % """ AND hive_post_tags.tag = :tag %s """
if start_author and start_permlink:
if discussion_type == 'trending':
sql = sql % """ AND hive_posts_cache.sc_trend <= (SELECT sc_trend FROM hive_posts_cache WHERE permlink = :permlink AND author = :author)
AND hive_posts_cache.post_id != (SELECT post_id FROM hive_posts_cache WHERE permlink = :permlink AND author = :author) """
elif discussion_type == 'hot':
sql = sql % """ AND hive_posts_cache.sc_hot <= (SELECT sc_hot FROM hive_posts_cache WHERE permlink = :permlink AND author = :author)
AND hive_posts_cache.post_id != (SELECT post_id FROM hive_posts_cache WHERE permlink = :permlink AND author = :author) """
elif discussion_type == 'created':
sql = sql % """ AND hive_posts_cache.post_id < (SELECT post_id FROM hive_posts_cache WHERE permlink = :permlink AND author = :author) """
elif discussion_type == 'promoted':
sql = sql % """ AND hive_posts_cache.promoted <= (SELECT promoted FROM hive_posts_cache WHERE permlink = :permlink AND author = :author)
AND hive_posts_cache.post_id != (SELECT post_id FROM hive_posts_cache WHERE permlink = :permlink AND author = :author) """
else:
sql = sql % """ AND hive_posts_cache.payout <= (SELECT payout FROM hive_posts_cache where permlink = :permlink AND author = :author)
AND hive_posts_cache.post_id != (SELECT post_id FROM hive_posts_cache WHERE permlink = :permlink AND author = :author) """
else:
sql = sql % """ """
result = await db.query_all(sql, tag=tag, limit=limit, author=start_author, permlink=start_permlink)
posts = []
for row in result:
post = _condenser_post_object(row, truncate_body)
post['active_votes'] = _mute_votes(post['active_votes'], Mutes.all())
posts.append(post)
#posts = await resultset_to_posts(db=db, resultset=result, truncate_body=truncate_body)
return posts
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment