diff --git a/hive/indexer/votes.py b/hive/indexer/votes.py index b11ae622670de3a24a70a0a430b7c65bcb958395..db2a8db5cd2a316c6c14ae0b31b43223350e73cd 100644 --- a/hive/indexer/votes.py +++ b/hive/indexer/votes.py @@ -34,6 +34,7 @@ class Votes(DbAdapterHolder): if key in cls._votes_data: cls._votes_data[key]["vote_percent"] = weight cls._votes_data[key]["last_update"] = date + # only effective vote edits increase num_changes counter else: cls._votes_data[key] = dict(voter=voter, author=author, @@ -43,6 +44,7 @@ class Votes(DbAdapterHolder): rshares=0, last_update=date, is_effective=False, + num_changes=0, block_num=block_num) @classmethod @@ -55,17 +57,19 @@ class Votes(DbAdapterHolder): cls._votes_data[key]["weight"] = vop["weight"] cls._votes_data[key]["rshares"] = vop["rshares"] cls._votes_data[key]["is_effective"] = True - cls._votes_data[key]["block_num"] = vop['block_num'] + cls._votes_data[key]["num_changes"] += 1 + cls._votes_data[key]["block_num"] = vop["block_num"] else: - cls._votes_data[key] = dict(voter=vop['voter'], - author=vop['author'], - permlink=escape_characters(vop['permlink']), + cls._votes_data[key] = dict(voter=vop["voter"], + author=vop["author"], + permlink=escape_characters(vop["permlink"]), vote_percent=0, weight=vop["weight"], rshares=vop["rshares"], - last_update='1970-01-01 00:00:00', + last_update="1970-01-01 00:00:00", is_effective=True, - block_num=vop['block_num']) + num_changes=0, + block_num=vop["block_num"]) @classmethod def flush(cls): """ Flush vote data from cache to database """ @@ -77,16 +81,16 @@ class Votes(DbAdapterHolder): sql = """ INSERT INTO hive_votes - (post_id, voter_id, author_id, permlink_id, weight, rshares, vote_percent, last_update, block_num, is_effective) + (post_id, voter_id, author_id, permlink_id, weight, rshares, vote_percent, last_update, num_changes, block_num, is_effective) SELECT hp.id as post_id, ha_v.id as voter_id, ha_a.id as author_id, hpd_p.id as permlink_id, - t.weight, t.rshares, t.vote_percent, t.last_update, t.block_num, t.is_effective + t.weight, t.rshares, t.vote_percent, t.last_update, t.num_changes, t.block_num, t.is_effective FROM ( VALUES - -- order_id, voter, author, permlink, weight, rshares, vote_percent, last_update, block_num, is_effective + -- order_id, voter, author, permlink, weight, rshares, vote_percent, last_update, num_changes, block_num, is_effective {} - ) AS T(order_id, voter, author, permlink, weight, rshares, vote_percent, last_update, block_num, is_effective) + ) AS T(order_id, voter, author, permlink, weight, rshares, vote_percent, last_update, num_changes, block_num, is_effective) INNER JOIN hive_accounts ha_v ON ha_v.name = t.voter INNER JOIN hive_accounts ha_a ON ha_a.name = t.author INNER JOIN hive_permlink_data hpd_p ON hpd_p.permlink = t.permlink @@ -100,7 +104,7 @@ class Votes(DbAdapterHolder): rshares = CASE EXCLUDED.is_effective WHEN true THEN EXCLUDED.rshares ELSE hive_votes.rshares END, vote_percent = EXCLUDED.vote_percent, last_update = EXCLUDED.last_update, - num_changes = hive_votes.num_changes + 1 + num_changes = hive_votes.num_changes + EXCLUDED.num_changes + 1 WHERE hive_votes.voter_id = EXCLUDED.voter_id and hive_votes.author_id = EXCLUDED.author_id and hive_votes.permlink_id = EXCLUDED.permlink_id; """ # WHERE clause above seems superfluous (and works all the same without it, at least up to 5mln) @@ -109,10 +113,10 @@ class Votes(DbAdapterHolder): values_limit = 1000 for _, vd in cls._votes_data.items(): - values.append("({}, '{}', '{}', {}, {}, {}, {}, '{}'::timestamp, {}, {})".format( + values.append("({}, '{}', '{}', {}, {}, {}, {}, '{}'::timestamp, {}, {}, {})".format( len(values), # for ordering vd['voter'], vd['author'], vd['permlink'], vd['weight'], vd['rshares'], - vd['vote_percent'], vd['last_update'], vd['block_num'], vd['is_effective'])) + vd['vote_percent'], vd['last_update'], vd['num_changes'], vd['block_num'], vd['is_effective'])) if len(values) >= values_limit: values_str = ','.join(values) diff --git a/tests/tests_api b/tests/tests_api index 3cd0ed4857180f0fa29e9eab9337acdc129aac5f..aa45a4b82acf3004cc12a9016a5ad24454f2a92e 160000 --- a/tests/tests_api +++ b/tests/tests_api @@ -1 +1 @@ -Subproject commit 3cd0ed4857180f0fa29e9eab9337acdc129aac5f +Subproject commit aa45a4b82acf3004cc12a9016a5ad24454f2a92e