Skip to content
Snippets Groups Projects
Commit 6f29716c authored by Jason Salyers's avatar Jason Salyers
Browse files

[JES] Redo the get_discussion method. Still needs some work but should be a...

[JES] Redo the get_discussion method. Still needs some work but should be a good improvement over the old one for now. Also clean up some debug logs that got left behind
parent ca8c58ea
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
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
import logging import logging
from hive.server.bridge_api.objects import load_posts_keyed from hive.server.bridge_api.objects import load_posts_keyed, _condenser_post_object
from hive.server.bridge_api.methods import append_statistics_to_post
from hive.server.common.helpers import ( from hive.server.common.helpers import (
return_error_info, return_error_info,
valid_account, valid_account,
...@@ -17,11 +18,77 @@ async def get_discussion(context, author, permlink): ...@@ -17,11 +18,77 @@ async def get_discussion(context, author, permlink):
author = valid_account(author) author = valid_account(author)
permlink = valid_permlink(permlink) permlink = valid_permlink(permlink)
root_id = await _get_post_id(db, author, permlink)
if not root_id:
return {}
return await _load_discussion(db, root_id) sql = """
WITH RECURSIVE child_posts AS (
SELECT id, parent_id FROM hive_posts WHERE author = :author AND permlink = :permlink
UNION
SELECT children.id, children.parent_id FROM hive_posts children JOIN child_posts ON (children.parent_id = child_posts.id)
)
SELECT child_posts.id, child_posts.parent_id, hive_posts_cache.post_id, hive_posts_cache.author, hive_posts_cache.permlink,
hive_posts_cache.title, hive_posts_cache.body, hive_posts_cache.category, hive_posts_cache.depth,
hive_posts_cache.promoted, hive_posts_cache.payout, hive_posts_cache.payout_at,
hive_posts_cache.is_paidout, hive_posts_cache.children, hive_posts_cache.votes,
hive_posts_cache.created_at, hive_posts_cache.updated_at, hive_posts_cache.rshares,
hive_posts_cache.raw_json, hive_posts_cache.json, hive_accounts.reputation AS author_rep,
hive_posts_cache.is_hidden AS is_hidden, hive_posts_cache.is_grayed AS is_grayed,
hive_posts_cache.total_votes AS total_votes, hive_posts_cache.flag_weight AS flag_weight,
hive_posts_cache.sc_trend AS sc_trend, hive_accounts.id AS acct_author_id
FROM child_posts JOIN hive_posts_cache ON (child_posts.id = hive_posts_cache.post_id)
JOIN hive_posts ON (hive_posts_cache.post_id = hive_posts.id)
JOIN hive_accounts ON (hive_posts_cache.author = hive_accounts.name)
WHERE NOT hive_posts.is_deleted AND NOT hive_posts.is_muted
"""
rows = await db.query_all(sql, author=author, permlink=permlink)
root_id = rows[0]['id']
all_posts = {}
root_post = _condenser_post_object(rows[0])
root_post = append_statistics_to_post(root_post, rows[0], False)
root_post['replies'] = []
all_posts[root_id] = root_post
id_to_parent_id_map = {}
id_to_parent_id_map[root_id] = None
for index in range(1, len(rows)):
id_to_parent_id_map[rows[index]['id']] = rows[index]['parent_id']
post = _condenser_post_object(rows[index])
post = append_statistics_to_post(post, rows[index], False)
post['replies'] = []
all_posts[post['post_id']] = post
discussion_map = {}
build_discussion_map(root_id, id_to_parent_id_map, discussion_map)
for key in discussion_map:
children = discussion_map[key]
if children and len(children) > 0:
post = all_posts[key]
for child_id in children:
post['replies'].append(_ref(all_posts[child_id]))
#result has to be in form of dictionary of dictionaries {post_ref: post}
results = {}
for key in all_posts:
post_ref = _ref(all_posts[key])
results[post_ref] = all_posts[key]
return results
def build_discussion_map(parent_id, posts, results):
results[parent_id] = get_children(parent_id, posts)
if (results[parent_id] == []):
return
else:
for post_id in results[parent_id]:
build_discussion_map(post_id, posts, results)
def get_children(parent_id, posts):
results = []
for key in posts:
if posts[key] == parent_id:
results.append(key)
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."""
......
...@@ -10,7 +10,6 @@ from aiopg.sa import create_engine ...@@ -10,7 +10,6 @@ from aiopg.sa import create_engine
from hive.utils.stats import Stats from hive.utils.stats import Stats
logging.getLogger('sqlalchemy.engine').setLevel(logging.WARNING) logging.getLogger('sqlalchemy.engine').setLevel(logging.WARNING)
logging.FileHandler('hive_database_timer.log')
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def sqltimer(function): def sqltimer(function):
......
...@@ -6,10 +6,7 @@ import logging ...@@ -6,10 +6,7 @@ import logging
from time import perf_counter as perf from time import perf_counter as perf
from hive.utils.system import colorize, peak_usage_mb from hive.utils.system import colorize, peak_usage_mb
file_handler = logging.FileHandler('database_timer.log')
file_handler.setLevel(logging.INFO)
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
log.addHandler(file_handler)
def _normalize_sql(sql, maxlen=180): def _normalize_sql(sql, maxlen=180):
"""Collapse whitespace and middle-truncate if needed.""" """Collapse whitespace and middle-truncate if needed."""
......
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