Skip to content
Snippets Groups Projects
Commit 5f7432f9 authored by Mariusz Trela's avatar Mariusz Trela Committed by Mariusz Trela
Browse files

The call `list_votes` sorted by `voter_comment` works

parent 685a9512
No related branches found
No related tags found
4 merge requests!456Release candidate v1 24,!230Setup monitoring with pghero,!138Small typos fixed,!129The calls 'list_votes'/'find_votes' work
This commit is part of merge request !129. Comments created here will be created in the context of that merge request.
......@@ -112,7 +112,10 @@ class DbState:
'hive_accounts_ix5', # (cached_at, name)
'hive_post_tags_tag_id_idx'
'hive_post_tags_tag_id_idx',
'hive_votes_voter_id_permlink_id_idx',
'hive_votes_permlink_id_voter_id_idx'
]
to_return = []
......
......@@ -211,6 +211,8 @@ def build_metadata():
sa.Index('hive_votes_post_id_idx', 'post_id'),
sa.Index('hive_votes_voter_id_idx', 'voter_id'),
sa.Index('hive_votes_voter_id_permlink_id_idx', 'voter_id', 'permlink_id'),
sa.Index('hive_votes_permlink_id_voter_id_idx', 'permlink_id', 'voter_id'),
sa.Index('hive_votes_block_num_idx', 'block_num')
)
......@@ -844,10 +846,10 @@ def setup(db):
sql = """
DROP VIEW IF EXISTS hive_votes_accounts_permlinks_view
;
CREATE VIEW hive_votes_accounts_permlinks_view
CREATE OR REPLACE VIEW hive_votes_accounts_permlinks_view
AS
SELECT
ha_v.id as voter_id,
hv.voter_id as voter_id,
ha_a.name as author,
hpd.permlink as permlink,
vote_percent as percent,
......@@ -857,7 +859,7 @@ def setup(db):
ha_v.name as voter,
weight,
num_changes,
hpd.id as permlink_id,
hv.permlink_id as permlink_id,
post_id,
is_effective
FROM
......@@ -869,6 +871,76 @@ def setup(db):
"""
db.query_no_return(sql)
sql = """
DROP VIEW IF EXISTS list_votes_by_voter_comment;
CREATE OR REPLACE FUNCTION public.list_votes_by_voter_comment
(
in _VOTER hive_accounts.name%TYPE,
in _AUTHOR hive_accounts.name%TYPE,
in _PERMLINK hive_permlink_data.permlink%TYPE,
in _LIMIT INT
)
RETURNS TABLE
(
voter hive_accounts.name%TYPE,
author hive_accounts.name%TYPE,
permlink hive_permlink_data.permlink%TYPE,
weight hive_votes.weight%TYPE,
rshares hive_votes.rshares%TYPE,
percent hive_votes.vote_percent%TYPE,
last_update hive_votes.last_update%TYPE,
num_changes hive_votes.num_changes%TYPE,
reputation hive_accounts.reputation%TYPE
)
LANGUAGE 'plpgsql'
VOLATILE
AS $BODY$
DECLARE _VOTER_ID INT;
DECLARE _PERMLINK_ID INT;
BEGIN
IF _VOTER = '' THEN
_VOTER_ID = 0;
ELSE
_VOTER_ID =
(
SELECT id FROM hive_accounts
WHERE name=_VOTER
);
END IF;
_PERMLINK_ID = find_comment_id( _AUTHOR, _PERMLINK, True);
RETURN QUERY
(
SELECT
v.voter,
v.author,
v.permlink,
v.weight,
v.rshares,
v.percent,
v.time,
v.num_changes,
v.reputation
FROM
hive_votes_accounts_permlinks_view v
WHERE
( v.voter_id = _VOTER_ID and v.permlink_id >= _PERMLINK_ID )
OR
( v.voter_id > _VOTER_ID )
ORDER BY
voter_id,
permlink_id
LIMIT _LIMIT
);
END
$BODY$;
"""
db.query_no_return(sql)
sql = """
DROP FUNCTION IF EXISTS find_comment_id(character varying, character varying, boolean)
;
......
......@@ -4,6 +4,7 @@ from enum import Enum
from hive.server.common.helpers import return_error_info, valid_limit, valid_account, valid_permlink, valid_date
from hive.server.database_api.objects import database_post_object
from hive.utils.normalize import rep_to_raw, number_to_json_value, time_string_with_t
from hive.server.common.helpers import json_date
import datetime
......@@ -173,7 +174,7 @@ def result_presentation(rows, votes_presentation):
if votes_presentation == VotesPresentation.DatabaseApi:
ret.append(dict(voter=row.voter, author=row.author, permlink=row.permlink,
weight=number_to_json_value(row.weight), rshares=number_to_json_value(row.rshares), vote_percent=row.percent,
last_update=str(row.time), num_changes=row.num_changes))
last_update=json_date(row.last_update), num_changes=row.num_changes))
elif votes_presentation == VotesPresentation.CondenserApi:
ret.append(dict(percent=str(row.percent), reputation=rep_to_raw(row.reputation),
rshares=number_to_json_value(row.rshares), voter=row.voter))
......@@ -223,20 +224,7 @@ async def list_votes(context, start: list, limit: int, order: str, votes_present
assert len(start) == 3, "Expecting 3 elements in start array"
db = context['db']
sql = """
SELECT
voter,
author,
permlink,
weight,
rshares,
percent,
time,
num_changes,
reputation
FROM
hive_votes_accounts_permlinks_view
"""
sql = ""
if order == "by_comment_voter":
sql += """
......@@ -253,17 +241,8 @@ async def list_votes(context, start: list, limit: int, order: str, votes_present
rows = await db.query_all(sql, author=start[0], permlink=start[1], voter=start[2], limit=limit)
if order == "by_voter_comment":
sql += """
WHERE
voter >= :voter AND
author >= :author AND
permlink >= :permlink
ORDER BY
voter_id ASC,
post_id ASC
LIMIT
:limit
"""
rows = await db.query_all(sql, author=start[1], permlink=start[2], voter=start[0], limit=limit)
sql = "select * from list_votes_by_voter_comment( '{}', '{}', '{}', {} )".format( start[0], start[1], start[2], limit )
rows = await db.query_all(sql)
return result_presentation(rows, votes_presentation)
Subproject commit 43f46f320af704f4ac173005696ab8526e8d08f2
Subproject commit 4216ec21d977cb6ee08f4f655b3cf40165f7292d
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