Skip to content
Snippets Groups Projects
Commit 41f62f61 authored by Bartek Wrona's avatar Bartek Wrona
Browse files

Merge branch 'dk-get-discussion-sql-fix' into 'develop'

get_discussion SQL query fixes

See merge request !53
parents cfb463b1 2cb703f1
No related branches found
No related tags found
4 merge requests!456Release candidate v1 24,!230Setup monitoring with pghero,!135Enable postgres monitoring on CI server,!53get_discussion SQL query fixes
...@@ -593,7 +593,9 @@ def setup(db): ...@@ -593,7 +593,9 @@ def setup(db):
AS AS
SELECT hp.id, SELECT hp.id,
hp.community_id, hp.community_id,
hp.parent_id,
ha_a.name AS author, ha_a.name AS author,
hp.author_id,
hpd_p.permlink, hpd_p.permlink,
hpd.title, hpd.title,
hpd.body, hpd.body,
...@@ -637,6 +639,7 @@ def setup(db): ...@@ -637,6 +639,7 @@ def setup(db):
hp.sc_hot, hp.sc_hot,
hp.is_deleted, hp.is_deleted,
hp.is_pinned, hp.is_pinned,
hp.is_muted,
hr.title AS role_title, hr.title AS role_title,
hr.role_id AS role_id, hr.role_id AS role_id,
hc.title AS community_title, hc.title AS community_title,
......
...@@ -24,27 +24,29 @@ async def get_discussion(context, author, permlink, observer=None): ...@@ -24,27 +24,29 @@ async def get_discussion(context, author, permlink, observer=None):
sql = """ sql = """
WITH RECURSIVE child_posts (id, parent_id) AS ( WITH RECURSIVE child_posts (id, parent_id) AS (
SELECT id, parent_id FROM hive_posts WHERE author_id = (SELECT id FROM hive_accounts WHERE name = :author) SELECT id, parent_id FROM hive_posts_view hpv WHERE hpv.author = :author
AND permlink_id = (SELECT id FROM hive_permlink_data WHERE permlink = :permlink) AND hpv.permlink = :permlink AND NOT hpv.is_deleted AND NOT hpv.is_muted
AND NOT hp.is_deleted AND NOT hp.is_muted
UNION ALL UNION ALL
SELECT children.id, children.parent_id FROM hive_posts children INNER JOIN child_posts ON (children.parent_id = child_posts.id) SELECT children.id, children.parent_id FROM hive_posts children INNER JOIN child_posts ON (children.parent_id = child_posts.id)
WHERE NOT children.is_deleted AND NOT children.is_muted WHERE NOT children.is_deleted AND NOT children.is_muted
) )
SELECT child_posts.id, child_posts.parent_id, hive_posts.id, hive_accounts.name as author, hpd_p.permlink as permlink, SELECT child_posts.id, child_posts.parent_id, hpv.id as post_id, hpv.author, hpv.permlink,
hpd.title as title, hpd.body as body, hcd.category as category, hive_posts.depth, hpv.title, hpv.body, hpv.category, hpv.depth,
hive_posts.promoted, hive_posts.payout, hive_posts.payout_at, hpv.promoted, hpv.payout, hpv.payout_at,
hive_posts.is_paidout, hive_posts.children, hive_posts.votes, hpv.is_paidout, hpv.children, hpv.votes,
hive_posts.created_at, hive_posts.updated_at, hive_posts.rshares, hpv.created_at, hpv.updated_at, hpv.rshares,
hive_posts.raw_json, hive_posts.json, hive_accounts.reputation AS author_rep, hpv.json, hpv.author_rep,
hive_posts.is_hidden AS is_hidden, hive_posts.is_grayed AS is_grayed, hpv.is_hidden, hpv.is_grayed,
hive_posts.total_votes AS total_votes, hive_posts.flag_weight AS flag_weight, hpv.total_votes, hpv.flag_weight,
hive_posts.sc_trend AS sc_trend, hive_accounts.id AS acct_author_id hpv.sc_trend, hpv.author_id AS acct_author_id,
FROM child_posts JOIN hive_accounts ON (hive_posts.author_id = hive_accounts.id) hpv.root_author, hpv.root_permlink, hpv.parent_author, hpv.parent_permlink,
INNER JOIN hive_permlink_data hpd_p ON hpd_p.id = hive_posts.permlink_id hpv.allow_replies, hpv.allow_votes,
INNER JOIN hive_post_data hpd ON hpd.id = hive_posts.id hpv.allow_curation_rewards, hpv.url, hpv.root_title, hpv.beneficiaries,
INNER JOIN hive_category_data hcd ON hcd.id = hp.category_id hpv.max_accepted_payout, hpv.percent_hbd, hpv.curator_payout_value
WHERE NOT hive_posts.is_deleted AND NOT hive_posts.is_muted FROM
child_posts
INNER JOIN hive_posts_view hpv ON (hpv.id = child_posts.id)
WHERE NOT hpv.is_deleted AND NOT hpv.is_muted
LIMIT 2000 LIMIT 2000
""" """
...@@ -91,18 +93,18 @@ async def get_discussion(context, author, permlink, observer=None): ...@@ -91,18 +93,18 @@ async def get_discussion(context, author, permlink, observer=None):
def build_discussion_map(parent_id, posts, results): def build_discussion_map(parent_id, posts, results):
results[parent_id] = get_children(parent_id, posts) results[parent_id] = get_children(parent_id, posts)
if (results[parent_id] == []): if results[parent_id] == []:
return return
else:
for post_id in results[parent_id]: for post_id in results[parent_id]:
build_discussion_map(post_id, posts, results) build_discussion_map(post_id, posts, results)
def get_children(parent_id, posts): def get_children(parent_id, posts):
results = [] results = []
for key in posts: for key in posts:
if posts[key] == parent_id: if posts[key] == parent_id:
results.append(key) results.append(key)
return results; return results
async def _get_post_id(db, author, permlink): async def _get_post_id(db, author, permlink):
"""Given an author/permlink, retrieve the id from db.""" """Given an author/permlink, retrieve the id from db."""
......
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