Skip to content
Snippets Groups Projects
Commit 6d244fe6 authored by Marcin's avatar Marcin
Browse files

issue #87: net_votes and total_votes is calculated during sync

parent 1a9d8154
No related branches found
No related tags found
No related merge requests found
...@@ -115,6 +115,8 @@ def build_metadata(): ...@@ -115,6 +115,8 @@ def build_metadata():
sa.Column('abs_rshares', sa.Numeric, nullable=False, server_default='0'), sa.Column('abs_rshares', sa.Numeric, nullable=False, server_default='0'),
sa.Column('vote_rshares', sa.Numeric, nullable=False, server_default='0'), sa.Column('vote_rshares', sa.Numeric, nullable=False, server_default='0'),
sa.Column('total_vote_weight', sa.Numeric, nullable=False, server_default='0'), sa.Column('total_vote_weight', sa.Numeric, nullable=False, server_default='0'),
sa.Column('total_votes', sa.BigInteger, nullable=False, server_default='0'),
sa.Column('net_votes', sa.BigInteger, nullable=False, server_default='0'),
sa.Column('active', sa.DateTime, nullable=False, server_default='1970-01-01 00:00:00'), sa.Column('active', sa.DateTime, nullable=False, server_default='1970-01-01 00:00:00'),
sa.Column('cashout_time', sa.DateTime, nullable=False, server_default='1970-01-01 00:00:00'), sa.Column('cashout_time', sa.DateTime, nullable=False, server_default='1970-01-01 00:00:00'),
sa.Column('percent_hbd', sa.Integer, nullable=False, server_default='10000'), sa.Column('percent_hbd', sa.Integer, nullable=False, server_default='10000'),
...@@ -203,7 +205,7 @@ def build_metadata(): ...@@ -203,7 +205,7 @@ def build_metadata():
sa.Index('hive_votes_voter_id_post_id_idx', 'voter_id', 'post_id'), # probably this index is redundant to hive_votes_voter_id_last_update_idx because of starting voter_id. sa.Index('hive_votes_voter_id_post_id_idx', 'voter_id', 'post_id'), # probably this index is redundant to hive_votes_voter_id_last_update_idx because of starting voter_id.
sa.Index('hive_votes_voter_id_last_update_idx', 'voter_id', 'last_update'), # this index is critical for hive_accounts_info_view performance sa.Index('hive_votes_voter_id_last_update_idx', 'voter_id', 'last_update'), # this index is critical for hive_accounts_info_view performance
sa.Index('hive_votes_post_id_voter_id_idx', 'post_id', 'voter_id'), sa.Index('hive_votes_post_id_voter_id_idx', 'post_id', 'voter_id'),
sa.Index('hive_votes_block_num_idx', 'block_num') # this is also important for hive_accounts_info_view sa.Index('hive_votes_block_num_idx', 'block_num') # this is also important for hive_accounts_info_view
) )
sa.Table( sa.Table(
......
...@@ -32,22 +32,8 @@ SELECT hp.id, ...@@ -32,22 +32,8 @@ SELECT hp.id,
hp.updated_at, hp.updated_at,
hp.vote_rshares AS rshares, hp.vote_rshares AS rshares,
hp.abs_rshares AS abs_rshares, hp.abs_rshares AS abs_rshares,
COALESCE( hp.total_votes AS total_votes,
( hp.net_votes as net_votes,
SELECT COUNT( 1 )
FROM hive_votes v
WHERE v.post_id = hp.id AND v.is_effective
GROUP BY v.post_id
), 0
) AS total_votes,
COALESCE(
(
SELECT SUM( CASE v.rshares > 0 WHEN True THEN 1 ELSE -1 END )
FROM hive_votes v
WHERE v.post_id = hp.id AND NOT v.rshares = 0
GROUP BY v.post_id
), 0
) AS net_votes,
hpd.json, hpd.json,
ha_a.reputation AS author_rep, ha_a.reputation AS author_rep,
hp.is_hidden, hp.is_hidden,
......
...@@ -17,12 +17,20 @@ SET ...@@ -17,12 +17,20 @@ SET
, vote_rshares = votes_rshares.rshares , vote_rshares = votes_rshares.rshares
, sc_hot = CASE hp.is_paidout WHEN True Then 0 ELSE calculate_hot( votes_rshares.rshares, hp.created_at) END , sc_hot = CASE hp.is_paidout WHEN True Then 0 ELSE calculate_hot( votes_rshares.rshares, hp.created_at) END
, sc_trend = CASE hp.is_paidout WHEN True Then 0 ELSE calculate_tranding( votes_rshares.rshares, hp.created_at) END , sc_trend = CASE hp.is_paidout WHEN True Then 0 ELSE calculate_tranding( votes_rshares.rshares, hp.created_at) END
, total_votes = votes_rshares.total_votes
, net_votes = votes_rshares.net_votes
FROM FROM
( (
SELECT SELECT
hv.post_id hv.post_id
, SUM( hv.rshares ) as rshares , SUM( hv.rshares ) as rshares
, SUM( ABS( hv.rshares ) ) as abs_rshares , SUM( ABS( hv.rshares ) ) as abs_rshares
, SUM( CASE hv.is_effective WHEN True THEN 1 ELSE 0 END ) as total_votes
, SUM( CASE
WHEN hv.rshares > 0 THEN 1
WHEN hv.rshares = 0 THEN 0
ELSE -1
END ) as net_votes
FROM hive_votes hv FROM hive_votes hv
WHERE EXISTS WHERE EXISTS
( (
...@@ -33,7 +41,12 @@ FROM ...@@ -33,7 +41,12 @@ FROM
GROUP BY hv.post_id GROUP BY hv.post_id
) as votes_rshares ) as votes_rshares
WHERE hp.id = votes_rshares.post_id WHERE hp.id = votes_rshares.post_id
AND (hp.abs_rshares != votes_rshares.abs_rshares OR hp.vote_rshares != votes_rshares.rshares); AND (
hp.abs_rshares != votes_rshares.abs_rshares
OR hp.vote_rshares != votes_rshares.rshares
OR hp.total_votes != votes_rshares.total_votes
OR hp.net_votes != votes_rshares.net_votes
);
RESET work_mem; RESET work_mem;
RESET enable_seqscan; RESET enable_seqscan;
END; END;
......
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