From 2c0b485e17130b4ce97d0bc485d905d66523de28 Mon Sep 17 00:00:00 2001 From: Dariusz Kedzierski <dkedzierski@syncad.com> Date: Wed, 17 Jun 2020 14:28:30 +0200 Subject: [PATCH] Fixes and cleanup --- hive/db/schema.py | 4 +++- hive/indexer/posts.py | 30 +++++++++++++++------------- hive/indexer/votes.py | 36 +++++++++++++++++++++++++++++++--- scripts/update_hivemind_db.sql | 2 ++ 4 files changed, 54 insertions(+), 18 deletions(-) diff --git a/hive/db/schema.py b/hive/db/schema.py index 721a34d61..da9288752 100644 --- a/hive/db/schema.py +++ b/hive/db/schema.py @@ -205,7 +205,9 @@ def build_metadata(): sa.Index('hive_votes_voter_id_idx', 'voter_id'), sa.Index('hive_votes_author_id_idx', 'author_id'), - sa.Index('hive_votes_permlink_id_idx', 'permlink_id') + sa.Index('hive_votes_permlink_id_idx', 'permlink_id'), + sa.Index('hive_votes_upvote_idx', 'vote_percent', postgresql_where=sql_text("vote_percent > 0")), + sa.Index('hive_votes_downvote_idx', 'vote_percent', postgresql_where=sql_text("vote_percent < 0")) ) sa.Table( diff --git a/hive/indexer/posts.py b/hive/indexer/posts.py index 3b6946e78..aa4caa686 100644 --- a/hive/indexer/posts.py +++ b/hive/indexer/posts.py @@ -35,8 +35,6 @@ class Posts: @classmethod def find_root(cls, author, permlink): """ Find root for post """ - print("A: ", author, "P: ", permlink) - sql = """WITH RECURSIVE parent AS ( SELECT id, parent_id, 1 AS level from hive_posts WHERE id = (SELECT hp.id @@ -193,8 +191,6 @@ class Posts: @classmethod def insert(cls, op, date): """Inserts new post records.""" - print("New Post") - # inserting new post # * Check for permlink, parent_permlink, root_permlink # * Check for authro, parent_author, root_author @@ -290,7 +286,6 @@ class Posts: @classmethod def undelete(cls, op, date, pid): """Re-allocates an existing record flagged as deleted.""" - print("Undelete") # add category to category table sql = """ @@ -326,7 +321,6 @@ class Posts: @classmethod def delete(cls, op): """Marks a post record as being deleted.""" - print("Delete post") pid, depth = cls.get_id_and_depth(op['author'], op['permlink']) DB.query("UPDATE hive_posts SET is_deleted = '1' WHERE id = :id", id=pid) @@ -348,16 +342,24 @@ class Posts: Here we could also build content diffs, but for now just used a signal to update cache record. """ - print("Update post") # pylint: disable=unused-argument + post = cls._build_post(op, date) + + # add permlinks to permlink table + for permlink in ['permlink', 'parent_permlink', 'root_permlink']: + if permlink in op: + sql = """ + INSERT INTO hive_permlink_data (permlink) + VALUES (:permlink) + ON CONFLICT (permlink) DO NOTHING""" + DB.query(sql, permlink=op[permlink]) # add category to category table - if 'category' in op: - sql = """ - INSERT INTO hive_category_data (category) - VALUES (:category) - ON CONFLICT (category) DO NOTHING""" - DB.query(sql, category=op['category']) + sql = """ + INSERT INTO hive_category_data (category) + VALUES (:category) + ON CONFLICT (category) DO NOTHING""" + DB.query(sql, category=post['category']) sql = """ UPDATE hive_posts @@ -375,7 +377,7 @@ class Posts: parent_permlink_id = (SELECT id FROM hive_permlink_data WHERE permlink = :parent_permlink) WHERE id = :id """ - post = cls._build_post(op, date) + post['id'] = pid DB.query(sql, **post) diff --git a/hive/indexer/votes.py b/hive/indexer/votes.py index 27d8915c0..dc27058b2 100644 --- a/hive/indexer/votes.py +++ b/hive/indexer/votes.py @@ -30,16 +30,46 @@ class Votes: """ Get vote count for given post """ sql = """ SELECT - count(hv.id) + count(hv.id) FROM - hive_votes hv - INNER JOIN hive_accounts ha_a ON (ha_a.id = hv.author_id) + hive_votes hv + INNER JOIN hive_accounts ha_a ON (ha_a.id = hv.author_id) INNER JOIN hive_permlink_data hpd ON (hpd.id = hv.permlink_id) WHERE ha_a.name = :author AND hpd.permlink = :permlink """ ret = DB.query_row(sql, author=author, permlink=permlink) return 0 if ret is None else int(ret.count) + @classmethod + def get_upvote_count(cls, author, permlink): + """ Get vote count for given post """ + sql = """ + SELECT + count(hv.id) + FROM + hive_votes hv + INNER JOIN hive_accounts ha_a ON (ha_a.id = hv.author_id) + INNER JOIN hive_permlink_data hpd ON (hpd.id = hv.permlink_id) + WHERE ha_a.name = :author AND hpd.permlink = :permlink AND vote_percent > 0 + """ + ret = DB.query_row(sql, author=author, permlink=permlink) + return 0 if ret is None else int(ret.count) + + @classmethod + def get_downvote_count(cls, author, permlink): + """ Get vote count for given post """ + sql = """ + SELECT + count(hv.id) + FROM + hive_votes hv + INNER JOIN hive_accounts ha_a ON (ha_a.id = hv.author_id) + INNER JOIN hive_permlink_data hpd ON (hpd.id = hv.permlink_id) + WHERE ha_a.name = :author AND hpd.permlink = :permlink AND vote_percent < 0 + """ + ret = DB.query_row(sql, author=author, permlink=permlink) + return 0 if ret is None else int(ret.count) + @classmethod def vote_op(cls, vop, date): """ Process vote_operation """ diff --git a/scripts/update_hivemind_db.sql b/scripts/update_hivemind_db.sql index cb824f3d4..6533362e4 100644 --- a/scripts/update_hivemind_db.sql +++ b/scripts/update_hivemind_db.sql @@ -170,6 +170,8 @@ CREATE TABLE IF NOT EXISTS hive_votes ( CREATE INDEX IF NOT EXISTS hive_votes_voter_id_idx ON hive_votes (voter_id); CREATE INDEX IF NOT EXISTS hive_votes_author_id_idx ON hive_votes (author_id); CREATE INDEX IF NOT EXISTS hive_votes_permlink_id_idx ON hive_votes (permlink_id); +CREATE INDEX IF NOT EXISTS hive_votes_upvote_idx ON hive_votes (vote_percent) WHERE vote_percent > 0; +CREATE INDEX IF NOT EXISTS hive_votes_downvote_idx ON hive_votes (vote_percent) WHERE vote_percent < 0; -- Copy data from hive_posts table to new table -- RAISE NOTICE 'Copy data from hive_posts table to new table'; -- GitLab