Skip to content
Snippets Groups Projects
Commit 2c0b485e authored by Dariusz Kędzierski's avatar Dariusz Kędzierski
Browse files

Fixes and cleanup

parent 23f4a461
No related branches found
No related tags found
5 merge requests!456Release candidate v1 24,!230Setup monitoring with pghero,!135Enable postgres monitoring on CI server,!16Dk issue 3 concurrent block query rebase,!15Dk issue 3 concurrent block query
......@@ -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(
......
......@@ -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)
......
......@@ -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 """
......
......@@ -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';
......
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