From cc9c0c7eeb2778fbc2ebf455a79054359364809b Mon Sep 17 00:00:00 2001
From: Marcin Ickiewicz <mickiewicz@syncad.com>
Date: Thu, 29 Oct 2020 10:56:23 +0100
Subject: [PATCH] issue #87: net_votes and total_votes are calculated during
 sync

---
 hive/db/schema.py                            |  4 +++-
 hive/db/sql_scripts/hive_posts_view.sql      | 18 ++----------------
 hive/db/sql_scripts/update_posts_rshares.sql | 15 ++++++++++++++-
 3 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/hive/db/schema.py b/hive/db/schema.py
index a1fc3ec4c..7d7b2187b 100644
--- a/hive/db/schema.py
+++ b/hive/db/schema.py
@@ -115,6 +115,8 @@ def build_metadata():
         sa.Column('abs_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_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('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'),
@@ -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_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_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(
diff --git a/hive/db/sql_scripts/hive_posts_view.sql b/hive/db/sql_scripts/hive_posts_view.sql
index 537b1bdb9..008a04230 100644
--- a/hive/db/sql_scripts/hive_posts_view.sql
+++ b/hive/db/sql_scripts/hive_posts_view.sql
@@ -32,22 +32,8 @@ SELECT hp.id,
   hp.updated_at,
   hp.vote_rshares AS rshares,
   hp.abs_rshares AS abs_rshares,
-    COALESCE(
-      (
-        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,
+  hp.total_votes AS total_votes,
+  hp.net_votes as net_votes,
   hpd.json,
   ha_a.reputation AS author_rep,
   hp.is_hidden,
diff --git a/hive/db/sql_scripts/update_posts_rshares.sql b/hive/db/sql_scripts/update_posts_rshares.sql
index d93373ddd..98210253c 100644
--- a/hive/db/sql_scripts/update_posts_rshares.sql
+++ b/hive/db/sql_scripts/update_posts_rshares.sql
@@ -17,12 +17,20 @@ SET
   , 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_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
   (
     SELECT
         hv.post_id
       , SUM( hv.rshares ) as 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
     WHERE EXISTS
       (
@@ -33,7 +41,12 @@ FROM
     GROUP BY hv.post_id
   ) as votes_rshares
 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 enable_seqscan;
 END;
-- 
GitLab