From a426cc603d2afaee0880ae66b63df9b6f03bf44e Mon Sep 17 00:00:00 2001
From: Marcin Ickiewicz <mickiewicz@syncad.com>
Date: Mon, 24 Aug 2020 12:33:05 +0200
Subject: [PATCH] unify live sync of hot/trends with posts active

---
 hive/db/schema.py      |  2 +-
 hive/indexer/blocks.py | 12 ++++++------
 hive/indexer/votes.py  | 11 -----------
 hive/utils/trends.py   |  2 ++
 tests/tests_api        |  2 +-
 5 files changed, 10 insertions(+), 19 deletions(-)

diff --git a/hive/db/schema.py b/hive/db/schema.py
index 0eacd1674..648f5d058 100644
--- a/hive/db/schema.py
+++ b/hive/db/schema.py
@@ -773,7 +773,7 @@ def setup(db):
         promoted DECIMAL(10,3),
         payout DECIMAL(10,3),
         last_payout_at TIMESTAMP,
-        cashout_time TIMESTAMP, 
+        cashout_time TIMESTAMP,
         is_paidout BOOLEAN,
         children INT,
         votes INT,
diff --git a/hive/indexer/blocks.py b/hive/indexer/blocks.py
index 88b6ffa7e..4803c9b07 100644
--- a/hive/indexer/blocks.py
+++ b/hive/indexer/blocks.py
@@ -17,6 +17,7 @@ from time import perf_counter
 
 from hive.utils.stats import OPStatusManager as OPSM
 from hive.utils.stats import FlushStatusManager as FSM
+from hive.utils.trends import update_hot_and_tranding_for_block_range
 from hive.utils.post_active import update_active_starting_from_posts_on_block
 
 log = logging.getLogger(__name__)
@@ -62,7 +63,7 @@ class Blocks:
         Votes.flush()
         Posts.flush()
         block_num = int(block['block_id'][:8], base=16)
-        cls.on_live_blocks_processed( block_num, block_num, is_initial_sync )
+        cls.on_live_blocks_processed( block_num, block_num )
         time_end = perf_counter()
         log.info("[PROCESS BLOCK] %fs", time_end - time_start)
         return ret
@@ -103,8 +104,8 @@ class Blocks:
         flush_time = register_time(flush_time, "Follow", folllow_items)
         flush_time = register_time(flush_time, "Posts", Posts.flush())
 
-        if first_block > -1:
-            cls.on_live_blocks_processed( first_block, last_num, is_initial_sync )
+        if is_initial_sync and first_block > -1:
+            cls.on_live_blocks_processed( first_block, last_num )
 
         DB.query("COMMIT")
 
@@ -393,11 +394,10 @@ class Blocks:
         # TODO: manually re-process here the blocks which were just popped.
 
     @classmethod
-    def on_live_blocks_processed( cls, first_block, last_block, is_initial_sync ):
+    def on_live_blocks_processed( cls, first_block, last_block ):
         """Is invoked when processing of block range is done and received
            informations from hived are already stored in db
         """
-        if is_initial_sync:
-            return;
 
+        update_hot_and_tranding_for_block_range( first_block, last_block )
         update_active_starting_from_posts_on_block( first_block, last_block )
diff --git a/hive/indexer/votes.py b/hive/indexer/votes.py
index e01006c72..a509047c3 100644
--- a/hive/indexer/votes.py
+++ b/hive/indexer/votes.py
@@ -4,7 +4,6 @@ import logging
 
 from hive.db.db_state import DbState
 from hive.db.adapter import Db
-from hive.utils.trends import update_hot_and_tranding_for_block_range
 
 log = logging.getLogger(__name__)
 DB = Db.instance()
@@ -97,19 +96,12 @@ class Votes:
 
             values = []
             values_limit = 1000
-            first_block = 0
-            last_block = 0
 
             for _, vd in cls._votes_data.items():
-
-                first_block = min( first_block, vd['block_num'] )
-                last_block = max( last_block, vd['block_num'] )
-
                 values.append("('{}', '{}', '{}', {}, {}, {}, '{}'::timestamp, {}, {})".format(
                     vd['voter'], vd['author'], vd['permlink'], vd['weight'], vd['rshares'],
                     vd['vote_percent'], vd['last_update'], vd['block_num'], vd['is_effective']))
 
-
                 if len(values) >= values_limit:
                     values_str = ','.join(values)
                     actual_query = sql.format(values_str)
@@ -123,9 +115,6 @@ class Votes:
                 values.clear()
 
             n = len(cls._votes_data)
-            if not DbState.is_initial_sync():
-                update_hot_and_tranding_for_block_range( first_block, last_block )
             cls._votes_data.clear()
         cls.inside_flush = False
         return n
-
diff --git a/hive/utils/trends.py b/hive/utils/trends.py
index b33bfc3a4..acbe9f3f9 100644
--- a/hive/utils/trends.py
+++ b/hive/utils/trends.py
@@ -35,6 +35,8 @@ def update_hot_and_tranding_for_block_range( first_block = NO_CONSTRAINT, last_b
         sql = hot_and_trend_sql.format( "WHERE block_num >= {}".format( first_block ) )
     elif first_block == NO_CONSTRAINT:
         sql = hot_and_trend_sql.format( "WHERE block_num <= {}".format( last_block ) )
+    elif first_block == last_block:
+        sql = hot_and_trend_sql.format( "WHERE block_num = {}".format( last_block ) )
     else:
         sql = hot_and_trend_sql.format( "WHERE block_num >= {} AND block_num <= {}".format( first_block, last_block ) )
     DB.query_no_return(sql)
diff --git a/tests/tests_api b/tests/tests_api
index 385a6826f..cf58faa12 160000
--- a/tests/tests_api
+++ b/tests/tests_api
@@ -1 +1 @@
-Subproject commit 385a6826f33d7818ba0e04bb2144bf5f275d1acf
+Subproject commit cf58faa12d82363b055a8d9313a0cfe5b7e0a024
-- 
GitLab