Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • hive/hivemind
1 result
Select Git revision
Show changes
Commits on Source (11)
Showing with 296 additions and 28 deletions
......@@ -112,7 +112,7 @@ class DbState:
'hive_posts_block_num_idx',
'hive_posts_cashout_time_id_idx',
'hive_posts_updated_at_idx',
'hive_posts_payout_plus_pending_payout_id',
'hive_posts_payout_plus_pending_payout_id_is_paidout_idx',
'hive_votes_block_num_idx',
......
......@@ -150,7 +150,7 @@ def build_metadata():
sa.Index('hive_posts_block_num_idx', 'block_num'),
sa.Index('hive_posts_cashout_time_id_idx', 'cashout_time', 'id'),
sa.Index('hive_posts_updated_at_idx', sa.text('updated_at DESC')),
sa.Index('hive_posts_payout_plus_pending_payout_id', sa.text('(payout+pending_payout), id'))
sa.Index('hive_posts_payout_plus_pending_payout_id_is_paidout_idx', sa.text('(payout+pending_payout), id, is_paidout'))
)
sa.Table(
......
......@@ -62,3 +62,65 @@ BEGIN
END
$function$
language plpgsql STABLE;
DROP FUNCTION IF EXISTS condenser_get_discussions_by_created_with_empty_tag;
CREATE FUNCTION condenser_get_discussions_by_created_with_empty_tag( in _tag VARCHAR, in _author VARCHAR, in _permlink VARCHAR, in _limit SMALLINT )
RETURNS SETOF bridge_api_post
AS
$function$
DECLARE
__post_id INT;
__community VARCHAR;
__category_id INT;
BEGIN
__post_id = find_comment_id( _author, _permlink, True );
__community = ( SELECT substring(_tag from '^hive-') );
__category_id = ( SELECT id FROM hive_category_data WHERE category = _tag );
RETURN QUERY SELECT
hp.id,
hp.author,
hp.parent_author,
hp.author_rep,
hp.root_title,
hp.beneficiaries,
hp.max_accepted_payout,
hp.percent_hbd,
hp.url,
hp.permlink,
hp.parent_permlink_or_category,
hp.title,
hp.body,
hp.category,
hp.depth,
hp.promoted,
hp.payout,
hp.pending_payout,
hp.payout_at,
hp.is_paidout,
hp.children,
hp.votes,
hp.created_at,
hp.updated_at,
hp.rshares,
hp.abs_rshares,
hp.json,
hp.is_hidden,
hp.is_grayed,
hp.total_votes,
hp.sc_trend,
hp.role_title,
hp.community_title,
hp.role_id,
hp.is_pinned,
hp.curator_payout_value
FROM hive_posts_view hp
WHERE ( __post_id = 0 OR hp.id < __post_id )
AND ( ( __community IS NULL ) OR ( ( __community IS NOT NULL ) AND ( __category_id = 0 OR hp.category_id = __category_id ) ) )
ORDER BY hp.id DESC LIMIT _limit;
END
$function$
language plpgsql STABLE;
......@@ -62,3 +62,65 @@ BEGIN
END
$function$
language plpgsql STABLE;
DROP FUNCTION IF EXISTS condenser_get_discussions_by_hot_with_empty_tag;
CREATE FUNCTION condenser_get_discussions_by_hot_with_empty_tag( in _tag VARCHAR, in _author VARCHAR, in _permlink VARCHAR, in _limit SMALLINT )
RETURNS SETOF bridge_api_post
AS
$function$
DECLARE
__post_id INT;
__community VARCHAR;
__category_id INT;
BEGIN
__post_id = find_comment_id( _author, _permlink, True );
__community = ( SELECT substring(_tag from '^hive-') );
__category_id = ( SELECT id FROM hive_category_data WHERE category = _tag );
RETURN QUERY SELECT
hp.id,
hp.author,
hp.parent_author,
hp.author_rep,
hp.root_title,
hp.beneficiaries,
hp.max_accepted_payout,
hp.percent_hbd,
hp.url,
hp.permlink,
hp.parent_permlink_or_category,
hp.title,
hp.body,
hp.category,
hp.depth,
hp.promoted,
hp.payout,
hp.pending_payout,
hp.payout_at,
hp.is_paidout,
hp.children,
hp.votes,
hp.created_at,
hp.updated_at,
hp.rshares,
hp.abs_rshares,
hp.json,
hp.is_hidden,
hp.is_grayed,
hp.total_votes,
hp.sc_trend,
hp.role_title,
hp.community_title,
hp.role_id,
hp.is_pinned,
hp.curator_payout_value
FROM hive_posts_view hp
WHERE hp.is_paidout = '0' AND ( __post_id = 0 OR hp.id < __post_id )
AND ( ( __community IS NULL ) OR ( ( __community IS NOT NULL ) AND ( ( __category_id = 0 OR hp.category_id = __category_id ) AND hp.depth = 0 ) ) )
ORDER BY hp.sc_hot DESC, hp.id DESC LIMIT _limit;
END
$function$
language plpgsql STABLE;
......@@ -62,3 +62,66 @@ BEGIN
END
$function$
language plpgsql STABLE;
DROP FUNCTION IF EXISTS condenser_get_discussions_by_promoted_with_empty_tag;
CREATE FUNCTION condenser_get_discussions_by_promoted_with_empty_tag( in _tag VARCHAR, in _author VARCHAR, in _permlink VARCHAR, in _limit SMALLINT )
RETURNS SETOF bridge_api_post
AS
$function$
DECLARE
__post_id INT;
__community VARCHAR;
__category_id INT;
BEGIN
__post_id = find_comment_id( _author, _permlink, True );
__community = ( SELECT substring(_tag from '^hive-') );
__category_id = ( SELECT id FROM hive_category_data WHERE category = _tag );
RETURN QUERY SELECT
hp.id,
hp.author,
hp.parent_author,
hp.author_rep,
hp.root_title,
hp.beneficiaries,
hp.max_accepted_payout,
hp.percent_hbd,
hp.url,
hp.permlink,
hp.parent_permlink_or_category,
hp.title,
hp.body,
hp.category,
hp.depth,
hp.promoted,
hp.payout,
hp.pending_payout,
hp.payout_at,
hp.is_paidout,
hp.children,
hp.votes,
hp.created_at,
hp.updated_at,
hp.rshares,
hp.abs_rshares,
hp.json,
hp.is_hidden,
hp.is_grayed,
hp.total_votes,
hp.sc_trend,
hp.role_title,
hp.community_title,
hp.role_id,
hp.is_pinned,
hp.curator_payout_value
FROM hive_posts_view hp
INNER JOIN hive_post_tags hpt ON hpt.post_id = hp.id
WHERE hp.promoted > 0 AND ( __post_id = 0 OR hp.id < __post_id )
AND ( ( __community IS NULL ) OR ( ( __community IS NOT NULL ) AND ( __category_id = 0 OR hp.category_id = __category_id ) ) )
ORDER BY hp.promoted DESC, hp.id DESC LIMIT _limit;
END
$function$
language plpgsql STABLE;
......@@ -62,3 +62,65 @@ BEGIN
END
$function$
language plpgsql STABLE;
DROP FUNCTION IF EXISTS condenser_get_discussions_by_trending_with_empty_tag;
CREATE FUNCTION condenser_get_discussions_by_trending_with_empty_tag( in _tag VARCHAR, in _author VARCHAR, in _permlink VARCHAR, in _limit SMALLINT )
RETURNS SETOF bridge_api_post
AS
$function$
DECLARE
__post_id INT;
__community VARCHAR;
__category_id INT;
BEGIN
__post_id = find_comment_id( _author, _permlink, True );
__community = ( SELECT substring(_tag from '^hive-') );
__category_id = ( SELECT id FROM hive_category_data WHERE category = _tag );
RETURN QUERY SELECT
hp.id,
hp.author,
hp.parent_author,
hp.author_rep,
hp.root_title,
hp.beneficiaries,
hp.max_accepted_payout,
hp.percent_hbd,
hp.url,
hp.permlink,
hp.parent_permlink_or_category,
hp.title,
hp.body,
hp.category,
hp.depth,
hp.promoted,
hp.payout,
hp.pending_payout,
hp.payout_at,
hp.is_paidout,
hp.children,
hp.votes,
hp.created_at,
hp.updated_at,
hp.rshares,
hp.abs_rshares,
hp.json,
hp.is_hidden,
hp.is_grayed,
hp.total_votes,
hp.sc_trend,
hp.role_title,
hp.community_title,
hp.role_id,
hp.is_pinned,
hp.curator_payout_value
FROM hive_posts_view hp
WHERE hp.is_paidout = '0' AND ( __post_id = 0 OR hp.id < __post_id )
AND ( ( __community IS NULL ) OR ( ( __community IS NOT NULL ) AND ( ( __category_id = 0 OR hp.category_id = __category_id ) AND hp.depth = 0 ) ) )
ORDER BY hp.sc_trend DESC, hp.id DESC LIMIT _limit;
END
$function$
language plpgsql STABLE;
......@@ -6,15 +6,11 @@ LANGUAGE 'plpgsql'
AS
$function$
DECLARE
FIRST_BLOCK_TIME TIMESTAMP;
LAST_BLOCK_TIME TIMESTAMP;
__block_limit INT := 1200*24*90; --- 1200 blocks is equal to 1hr, so 90 days
BEGIN
FIRST_BLOCK_TIME = ( SELECT created_at FROM hive_blocks WHERE num = _first_block );
LAST_BLOCK_TIME = ( SELECT created_at FROM hive_blocks WHERE num = _last_block );
IF (LAST_BLOCK_TIME - '90 days'::interval) > FIRST_BLOCK_TIME THEN
FIRST_BLOCK_TIME = LAST_BLOCK_TIME - '90 days'::interval;
IF (_last_block - __block_limit) > _first_block THEN
_first_block = _last_block - __block_limit;
END IF;
INSERT INTO hive_mentions( post_id, account_id, block_num )
......@@ -23,20 +19,18 @@ BEGIN
hive_accounts ha
INNER JOIN
(
SELECT T.id_post, LOWER( ( SELECT trim( T.mention::text, '{""}') ) ) mention, T.author_id, T.block_num
SELECT T.id_post, LOWER( ( SELECT trim( T.mention::text, '{""}') ) ) AS mention, T.author_id, T.block_num
FROM
(
SELECT
hp.id, REGEXP_MATCHES( hpd.body, '(?:^|[^a-zA-Z0-9_!#$%&*@\\/])(?:@)([a-zA-Z0-9\\.-]{1,16}[a-zA-Z0-9])(?![a-z])', 'g') mention, hp.author_id, hp.block_num
hp.id, REGEXP_MATCHES( hpd.body, '(?:^|[^a-zA-Z0-9_!#$%&*@\\/])(?:@)([a-zA-Z0-9\\.-]{1,16}[a-zA-Z0-9])(?![a-z])', 'g') AS mention, hp.author_id, hp.block_num
FROM hive_posts hp
INNER JOIN hive_post_data hpd ON hp.id = hpd.id
WHERE
(
hp.created_at >= FIRST_BLOCK_TIME
)
INNER JOIN hive_post_data hpd ON hp.id = hpd.id
WHERE hp.block_num >= _first_block
)T( id_post, mention, author_id, block_num )
)T( id_post, mention, author_id, block_num ) ON ha.name = T.mention
WHERE ha.id != T.author_id
ORDER BY T.id_post
ON CONFLICT DO NOTHING;
END
......
......@@ -4,7 +4,7 @@ import logging
import ujson as json
from hive.server.common.mutes import Mutes
from hive.server.common.helpers import json_date
from hive.server.common.helpers import json_date, get_hive_accounts_info_view_query_string
from hive.server.database_api.methods import find_votes_impl, VotesPresentation
from hive.utils.normalize import sbd_amount, rep_log10
from hive.indexer.votes import Votes
......@@ -54,8 +54,7 @@ def append_statistics_to_post(post, row, is_pinned, blacklists_for_user=None, ov
async def load_profiles(db, names):
"""`get_accounts`-style lookup for `get_state` compat layer."""
sql = """SELECT * FROM hive_accounts_info_view
WHERE name IN :names"""
sql = get_hive_accounts_info_view_query_string( names )
rows = await db.query_all(sql, names=tuple(names))
return [_bridge_profile_object(row) for row in rows]
......
......@@ -66,6 +66,21 @@ def json_date(date=None):
if not date or date == datetime.datetime.max: return '1969-12-31T23:59:59'
return 'T'.join(str(date).split(' '))
def get_hive_accounts_info_view_query_string(names):
values = []
for name in names:
values.append("('{}')".format( name ))
values_str = ','.join(values)
sql = """
SELECT *
FROM hive_accounts_info_view v
JOIN
(
VALUES {}
)T( _name ) ON v.name = T._name
""".format( values_str )
return sql
def last_month():
"""Get the date 1 month ago."""
return datetime.datetime.now() + relativedelta(months=-1)
......
......@@ -138,7 +138,7 @@ async def get_state(context, path: str):
sort = valid_sort(part[0])
tag = valid_tag(part[1].lower(), allow_empty=True)
pids = await get_posts_by_given_sort(context, sort, '', '', 20, tag)
state['content'] = _keyed_posts(await load_posts(db, pids))
state['content'] = _keyed_posts(pids)
state['discussion_idx'] = {tag: {sort: list(state['content'].keys())}}
state['tag_idx'] = {'trending': await get_top_trending_tags_summary(context)}
......
......@@ -247,13 +247,25 @@ async def get_posts_by_given_sort(context, sort: str, start_author: str = '', st
posts = []
if sort == 'created':
sql = "SELECT * FROM condenser_get_discussions_by_created( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
if tag == '':
sql = "SELECT * FROM condenser_get_discussions_by_created_with_empty_tag( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
else:
sql = "SELECT * FROM condenser_get_discussions_by_created( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
elif sort == 'trending':
sql = "SELECT * FROM condenser_get_discussions_by_trending( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
if tag == '':
sql = "SELECT * FROM condenser_get_discussions_by_trending_with_empty_tag( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
else:
sql = "SELECT * FROM condenser_get_discussions_by_trending( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
elif sort == 'hot':
sql = "SELECT * FROM condenser_get_discussions_by_hot( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
if tag == '':
sql = "SELECT * FROM condenser_get_discussions_by_hot_with_empty_tag( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
else:
sql = "SELECT * FROM condenser_get_discussions_by_hot( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
elif sort == 'promoted':
sql = "SELECT * FROM condenser_get_discussions_by_promoted( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
if tag == '':
sql = "SELECT * FROM condenser_get_discussions_by_promoted_with_empty_tag( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
else:
sql = "SELECT * FROM condenser_get_discussions_by_promoted( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
elif sort == 'post_by_payout':
sql = "SELECT * FROM condenser_get_post_discussions_by_payout( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
elif sort == 'comment_by_payout':
......
......@@ -5,7 +5,7 @@ import ujson as json
from hive.utils.normalize import sbd_amount
from hive.server.common.mutes import Mutes
from hive.server.common.helpers import json_date
from hive.server.common.helpers import json_date, get_hive_accounts_info_view_query_string
from hive.server.database_api.methods import find_votes_impl, VotesPresentation
from hive.utils.account import safe_db_profile_metadata
......@@ -15,8 +15,7 @@ log = logging.getLogger(__name__)
async def load_accounts(db, names):
"""`get_accounts`-style lookup for `get_state` compat layer."""
sql = """SELECT * FROM hive_accounts_info_view
WHERE name IN :names"""
sql = get_hive_accounts_info_view_query_string( names )
rows = await db.query_all(sql, names=tuple(names))
return [_condenser_account_object(row) for row in rows]
......
Subproject commit ae53a82f46e6338a04e40b4966f8e7b0e64e6c86
Subproject commit ee534480e5739b75968089859ee3663dfdabb126