Skip to content

Commit

Permalink
Postgrest - improve methods for listing votes
Browse files Browse the repository at this point in the history
  • Loading branch information
asuch committed Oct 21, 2024
1 parent 52303e6 commit ce9d534
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ BEGIN
_permlink = hivemind_postgrest_utilities.valid_permlink(_start->>2, True);
END IF;

_post_id = hivemind_postgrest_utilities.find_comment_id( _author, _permlink, True);
_voter_id = hivemind_postgrest_utilities.find_account_id(_voter, True);

IF _order_by_comment_voter THEN
RETURN jsonb_build_object('votes', (hivemind_postgrest_utilities.list_votes(_author, _permlink, _limit, 'database_list_by_comment_voter'::hivemind_postgrest_utilities.list_votes_case, 'database_api'::hivemind_postgrest_utilities.vote_presentation, _voter)));
RETURN jsonb_build_object('votes', (hivemind_postgrest_utilities.list_votes(_post_id, _limit, 'database_list_by_comment_voter'::hivemind_postgrest_utilities.list_votes_case, 'database_api'::hivemind_postgrest_utilities.vote_presentation, _voter_id)));
ELSE
RETURN jsonb_build_object('votes', (hivemind_postgrest_utilities.list_votes(_author, _permlink, _limit, 'database_list_by_voter_comment'::hivemind_postgrest_utilities.list_votes_case, 'database_api'::hivemind_postgrest_utilities.vote_presentation, _voter)));
RETURN jsonb_build_object('votes', (hivemind_postgrest_utilities.list_votes(_post_id, _limit, 'database_list_by_voter_comment'::hivemind_postgrest_utilities.list_votes_case, 'database_api'::hivemind_postgrest_utilities.vote_presentation, _voter_id)));
END IF;
END;
$$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ BEGIN
WHEN _row.author_rep = 0 THEN _tmp_amount
ELSE ROUND(_tmp_amount, 2)
END),
'active_votes', hivemind_postgrest_utilities.list_votes(_row.author, _row.permlink, /* in python code it was hardcoded */ 1000,
'active_votes', hivemind_postgrest_utilities.list_votes(_row.id, /* in python code it was hardcoded */ 1000,
'create_post'::hivemind_postgrest_utilities.list_votes_case, 'bridge_api'::hivemind_postgrest_utilities.vote_presentation),
'blacklists', (CASE
WHEN _row.blacklists IS NOT NULL AND _row.blacklists <> '' THEN to_jsonb(string_to_array(_row.blacklists, ','))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ BEGIN
'beneficiaries', _row.beneficiaries,
'max_accepted_payout', _row.max_accepted_payout,
'percent_hbd', _row.percent_hbd,
'active_votes', hivemind_postgrest_utilities.list_votes(_row.author, _row.permlink, /* in python code it was hardcoded */ 1000,
'active_votes', hivemind_postgrest_utilities.list_votes(_row.id, /* in python code it was hardcoded */ 1000,
'create_post'::hivemind_postgrest_utilities.list_votes_case, (SELECT CASE
WHEN _content_additions THEN 'active_votes'::hivemind_postgrest_utilities.vote_presentation
ELSE 'condenser_api'::hivemind_postgrest_utilities.vote_presentation END))
Expand Down
136 changes: 59 additions & 77 deletions hive/db/sql_scripts/postgrest/utilities/list_votes.sql
Original file line number Diff line number Diff line change
@@ -1,56 +1,50 @@
DROP TYPE IF EXISTS hivemind_postgrest_utilities.vote_presentation CASCADE;
CREATE TYPE hivemind_postgrest_utilities.vote_presentation AS ENUM( 'database_api', 'condenser_api', 'bridge_api', 'active_votes');

DROP FUNCTION IF EXISTS hivemind_postgrest_utilities.create_votes_json_array;
CREATE FUNCTION hivemind_postgrest_utilities.create_votes_json_array(IN _votes JSONB, IN _presentation_mode hivemind_postgrest_utilities.vote_presentation)
DROP FUNCTION IF EXISTS hivemind_postgrest_utilities.apply_vote_presentation;
CREATE FUNCTION hivemind_postgrest_utilities.apply_vote_presentation(IN _vote RECORD, IN _presentation_mode hivemind_postgrest_utilities.vote_presentation)
RETURNS JSONB
LANGUAGE plpgsql
IMMUTABLE
AS
$function$
DECLARE
_vote JSONB;
_result JSONB;
BEGIN
FOR _vote IN SELECT * FROM jsonb_array_elements(_votes) LOOP
IF _presentation_mode = 'condenser_api' THEN
_result = COALESCE(_result, '[]'::jsonb) || jsonb_build_object(
'percent', _vote->>'percent',
'reputation', _vote->'reputation',
'rshares', _vote->'rshares',
'voter', _vote->>'voter'
)::jsonb;
RETURN jsonb_build_object(
'percent', _vote.percent::TEXT,
'reputation', _vote.reputation,
'rshares', _vote.rshares,
'voter', _vote.voter
);
ELSIF _presentation_mode = 'database_api' THEN
_result = COALESCE(_result, '[]'::jsonb) || jsonb_build_object(
'id', _vote->'id',
'voter', _vote->>'voter',
'author', _vote->>'author',
'permlink', _vote->>'permlink',
'weight', _vote->'weight',
'rshares', _vote->'rshares',
'vote_percent', _vote->'percent',
'last_update', hivemind_postgrest_utilities.json_date(to_timestamp(_vote->>'last_update', 'YYYY-MM-DD"T"HH24:MI:SS')),
'num_changes', _vote->'num_changes'
)::jsonb;
RETURN jsonb_build_object(
'id', _vote.id,
'voter', _vote.voter,
'author', _vote.author,
'permlink', _vote.permlink,
'weight', _vote.weight,
'rshares', _vote.rshares,
'vote_percent', _vote.percent,
'last_update', hivemind_postgrest_utilities.json_date(to_timestamp(_vote.last_update::TEXT, 'YYYY-MM-DD"T"HH24:MI:SS')),
'num_changes', _vote.num_changes
);
ELSIF _presentation_mode = 'bridge_api' THEN
_result = COALESCE(_result, '[]'::jsonb) || jsonb_build_object(
'rshares', _vote->'rshares',
'voter', _vote->>'voter'
)::jsonb;
RETURN jsonb_build_object(
'rshares', _vote.rshares,
'voter', _vote.voter
);
ELSIF _presentation_mode = 'active_votes' THEN
_result = COALESCE(_result, '[]'::jsonb) || jsonb_build_object(
'percent', _vote->'percent',
'reputation', _vote->'reputation',
'rshares', _vote->'rshares',
'voter', _vote->>'voter',
'time', hivemind_postgrest_utilities.json_date(to_timestamp(_vote->>'last_update', 'YYYY-MM-DD"T"HH24:MI:SS')),
'weight', _vote->'weight'
RETURN jsonb_build_object(
'percent', _vote.percent,
'reputation', _vote.reputation,
'rshares', _vote.rshares,
'voter', _vote.voter,
'time', hivemind_postgrest_utilities.json_date(to_timestamp(_vote.last_update::TEXT, 'YYYY-MM-DD"T"HH24:MI:SS')),
'weight', _vote.weight
)::jsonb;
ELSE
RAISE EXCEPTION '%', hivemind_postgrest_utilities.raise_parameter_validation_exception('create_votes_json_array - unspecified vote presentation mode');
END IF;
END LOOP;
RETURN _result;
END;
$function$
;
Expand All @@ -59,65 +53,53 @@ DROP TYPE IF EXISTS hivemind_postgrest_utilities.list_votes_case CASCADE;
CREATE TYPE hivemind_postgrest_utilities.list_votes_case AS ENUM( 'create_post', 'database_list_by_comment_voter', 'database_list_by_voter_comment');

DROP FUNCTION IF EXISTS hivemind_postgrest_utilities.list_votes;
CREATE FUNCTION hivemind_postgrest_utilities.list_votes(IN _author TEXT, IN _permlink TEXT, IN _limit INT, IN _case hivemind_postgrest_utilities.list_votes_case, IN _presentation_mode hivemind_postgrest_utilities.vote_presentation, IN _voter TEXT DEFAULT NULL)
CREATE FUNCTION hivemind_postgrest_utilities.list_votes(IN _post_id INT, IN _limit INT, IN _case hivemind_postgrest_utilities.list_votes_case, IN _presentation_mode hivemind_postgrest_utilities.vote_presentation, IN _voter_id INT DEFAULT NULL)
RETURNS JSONB
LANGUAGE plpgsql
STABLE
AS
$function$
DECLARE
_post_id INT;
_voter_id INT;
_result JSONB;
BEGIN
IF _case = ANY(ARRAY['database_list_by_comment_voter'::hivemind_postgrest_utilities.list_votes_case,'database_list_by_voter_comment'::hivemind_postgrest_utilities.list_votes_case]) THEN
assert _voter IS NOT NULL;
_voter_id = hivemind_postgrest_utilities.find_account_id(_voter, True);
assert _voter_id IS NOT NULL;
END IF;

_post_id = hivemind_postgrest_utilities.find_comment_id( _author, _permlink, True);
_result = hivemind_postgrest_utilities.create_votes_json_array(
(
SELECT jsonb_agg(to_jsonb( r )) FROM (
SELECT
v.id,
v.voter,
v.author,
v.permlink,
v.weight,
v.rshares,
v.percent,
v.last_update,
v.num_changes,
v.reputation
FROM
hivemind_app.hive_votes_view v
WHERE
(
CASE
RETURN (
SELECT to_jsonb(result.array) FROM (
SELECT ARRAY (
SELECT hivemind_postgrest_utilities.apply_vote_presentation(r, _presentation_mode) FROM (
SELECT
v.id,
v.voter,
v.author,
v.permlink,
v.weight,
v.rshares,
v.percent,
v.last_update,
v.num_changes,
v.reputation
FROM
hivemind_app.hive_votes_view v
WHERE
( CASE
WHEN _case = 'create_post' THEN v.post_id = _post_id
WHEN _case = 'database_list_by_comment_voter' THEN (v.post_id = _post_id AND v.voter_id >= _voter_id)
WHEN _case = 'database_list_by_voter_comment' THEN (v.voter_id = _voter_id AND v.post_id >= _post_id)
END
END
)
ORDER BY
(
CASE
ORDER BY
( CASE
WHEN _case = 'create_post' THEN v.voter_id
WHEN _case = 'database_list_by_comment_voter' THEN v.voter_id
WHEN _case = 'database_list_by_voter_comment' THEN v.post_id
END
END
)
LIMIT _limit
) r ),
_presentation_mode
LIMIT _limit
) r
)
) result
);

IF _result IS NULL THEN
RETURN '[]'::jsonb;
ELSE
RETURN _result;
END IF;
END;
$function$
;

0 comments on commit ce9d534

Please sign in to comment.