Skip to content
Snippets Groups Projects

The calls 'list_votes'/'find_votes' work

Merged Mariusz Trela requested to merge mt-find-list-votes3 into develop
All threads resolved!
1 file
+ 30
11
Compare changes
  • Side-by-side
  • Inline
@@ -3,7 +3,8 @@ 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.utils.normalize import rep_to_raw, time_string_with_t
from hive.server.common.helpers import json_date
import datetime
@@ -167,11 +168,30 @@ class VotesPresentation(Enum):
CondenserApi = 3
BridgeApi = 4
def api_vote_info(rows, votes_presentation):
ret = []
for row in rows:
if votes_presentation == VotesPresentation.DatabaseApi:
ret.append(dict(voter = row.voter, author = row.author, permlink = row.permlink,
weight = row.weight, rshares = row.rshares, vote_percent = row.percent,
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 = row.rshares, voter = row.voter))
elif votes_presentation == VotesPresentation.BridgeApi:
ret.append(dict(rshares = row.rshares, voter = row.voter))
else:
ret.append(dict(percent = row.percent, reputation = rep_to_raw(row.reputation),
rshares = row.rshares, time = json_date(row.last_update),
voter = row.voter, weight = row.weight
))
return ret
@return_error_info
async def find_votes(context, params: dict, votes_presentation = VotesPresentation.DatabaseApi):
async def find_votes_impl(context, author: str, permlink: str, votes_presentation):
""" Returns all votes for the given post """
valid_account(params['author'])
valid_permlink(params['permlink'])
valid_account(author)
valid_permlink(permlink)
db = context['db']
sql = """
SELECT
@@ -181,39 +201,22 @@ async def find_votes(context, params: dict, votes_presentation = VotesPresentati
weight,
rshares,
percent,
time,
last_update,
num_changes,
reputation
FROM
hive_votes_accounts_permlinks_view
hive_votes_view
WHERE
author = :author AND permlink = :permlink
ORDER BY
voter_id
"""
ret = []
rows = await db.query_all(sql, author=params['author'], permlink=params['permlink'])
for row in rows:
if votes_presentation == VotesPresentation.DatabaseApi:
ret.append(dict(voter=row.voter, author=row.author, permlink=row.permlink,
weight=row.weight, rshares=row.rshares, vote_percent=row.percent,
last_update=str(row.time), num_changes=row.num_changes))
elif votes_presentation == VotesPresentation.CondenserApi:
ret.append(dict(percent=str(row.percent), reputation=rep_to_raw(row.reputation),
rshares=str(row.rshares), voter=row.voter))
elif votes_presentation == VotesPresentation.BridgeApi:
ret.append(dict(rshares=str(row.rshares), voter=row.voter))
else:
ret.append(dict(percent=row.percent, reputation=rep_to_raw(row.reputation),
rshares=number_to_json_value(row.rshares), time=time_string_with_t(row.time),
voter=row.voter, weight=number_to_json_value(row.weight)
))
return ret
rows = await db.query_all(sql, author=author, permlink=permlink)
return api_vote_info(rows, votes_presentation)
@return_error_info
async def list_votes(context, start: list, limit: int, order: str):
async def list_votes_impl(context, start: list, limit: int, order: str, votes_presentation):
""" Returns all votes, starting with the specified voter and/or author and permlink. """
supported_order_list = ["by_comment_voter", "by_voter_comment"]
assert order in supported_order_list, "Order {} is not supported".format(order)
@@ -221,46 +224,21 @@ async def list_votes(context, start: list, limit: int, order: str):
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_voter_comment":
sql = "select * from list_votes_by_voter_comment( '{}', '{}', '{}', {} )".format( start[0], start[1], start[2], limit )
else:
sql = "select * from list_votes_by_comment_voter( '{}', '{}', '{}', {} )".format( start[2], start[0], start[1], limit )
rows = await db.query_all(sql)
return api_vote_info(rows, votes_presentation)
@return_error_info
async def find_votes(context, author: str, permlink: str):
return await find_votes_impl( context, author, permlink, VotesPresentation.DatabaseApi)
if order == "by_comment_voter": # ABW: wrong! fat node sorted by ( comment_id, voter_id )
sql += """
WHERE
author >= :author AND
permlink >= :permlink AND
voter >= :voter
ORDER BY
author ASC,
permlink ASC,
id ASC
LIMIT
:limit
"""
return await db.query_all(sql, author=start[0], permlink=start[1], voter=start[2], limit=limit)
if order == "by_voter_comment": # ABW: wrong! fat node sorted by ( voter_id, comment_id )
sql += """
WHERE
voter >= :voter AND
author >= :author AND
permlink >= :permlink
ORDER BY
voter ASC,
id ASC
LIMIT
:limit
"""
return await db.query_all(sql, author=start[1], permlink=start[2], voter=start[0], limit=limit)
return []
@return_error_info
async def list_votes(context, start: list, limit: int, order: str):
return await list_votes_impl( context, start, limit, order, VotesPresentation.DatabaseApi)
Loading