Skip to content
Snippets Groups Projects
Commit 0df52b19 authored by Bartek Wrona's avatar Bartek Wrona
Browse files

Simplified queries related to votes processing. Still is needed unification to...

Simplified queries related to votes processing. Still is needed unification to have one insertion path using ON CONFLICT extension
parent e208f9d1
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
...@@ -29,18 +29,11 @@ class Votes: ...@@ -29,18 +29,11 @@ class Votes:
def get_vote_count(cls, author, permlink): def get_vote_count(cls, author, permlink):
""" Get vote count for given post """ """ Get vote count for given post """
sql = """ sql = """
SELECT SELECT count(hv.id)
count(hv.id) FROM hive_votes hv
FROM INNER JOIN hive_accounts ha_a ON ha_a.id = hv.author_id
hive_votes hv INNER JOIN hive_permlink_data hpd_p ON hpd_p.id = hv.permlink_id
WHERE WHERE ha_a.name = :author AND hpd_p.permlink = :permlink
hv.id = (SELECT
hp.id
FROM
hive_posts hp
INNER JOIN hive_accounts ha_a ON ha_a.id = hp.author_id
INNER JOIN hive_permlink_data hpd_p ON hpd_p.id = hp.permlink_id
WHERE ha_a.name = :author AND hpd_p.permlink = :permlink)
""" """
ret = DB.query_row(sql, author=author, permlink=permlink) ret = DB.query_row(sql, author=author, permlink=permlink)
return 0 if ret is None else int(ret.count) return 0 if ret is None else int(ret.count)
...@@ -49,19 +42,12 @@ class Votes: ...@@ -49,19 +42,12 @@ class Votes:
def get_upvote_count(cls, author, permlink): def get_upvote_count(cls, author, permlink):
""" Get vote count for given post """ """ Get vote count for given post """
sql = """ sql = """
SELECT SELECT count(hv.id)
count(hv.id) FROM hive_votes hv
FROM INNER JOIN hive_accounts ha_a ON ha_a.id = hv.author_id
hive_votes hv INNER JOIN hive_permlink_data hpd_p ON hpd_p.id = hv.permlink_id
WHERE WHERE ha_a.name = :author AND hpd_p.permlink = :permlink
hv.id = (SELECT vote_percent > 0
hp.id
FROM
hive_posts hp
INNER JOIN hive_accounts ha_a ON ha_a.id = hp.author_id
INNER JOIN hive_permlink_data hpd_p ON hpd_p.id = hp.permlink_id
WHERE ha_a.name = :author AND hpd_p.permlink = :permlink)
AND vote_percent > 0
""" """
ret = DB.query_row(sql, author=author, permlink=permlink) ret = DB.query_row(sql, author=author, permlink=permlink)
return 0 if ret is None else int(ret.count) return 0 if ret is None else int(ret.count)
...@@ -70,19 +56,12 @@ class Votes: ...@@ -70,19 +56,12 @@ class Votes:
def get_downvote_count(cls, author, permlink): def get_downvote_count(cls, author, permlink):
""" Get vote count for given post """ """ Get vote count for given post """
sql = """ sql = """
SELECT SELECT count(hv.id)
count(hv.id) FROM hive_votes hv
FROM INNER JOIN hive_accounts ha_a ON ha_a.id = hv.author_id
hive_votes hv INNER JOIN hive_permlink_data hpd_p ON hpd_p.id = hv.permlink_id
WHERE WHERE ha_a.name = :author AND hpd_p.permlink = :permlink
hv.id = (SELECT vote_percent < 0
hp.id
FROM
hive_posts hp
INNER JOIN hive_accounts ha_a ON ha_a.id = hp.author_id
INNER JOIN hive_permlink_data hpd_p ON hpd_p.id = hp.permlink_id
WHERE ha_a.name = :author AND hpd_p.permlink = :permlink)
AND vote_percent < 0
""" """
ret = DB.query_row(sql, author=author, permlink=permlink) ret = DB.query_row(sql, author=author, permlink=permlink)
return 0 if ret is None else int(ret.count) return 0 if ret is None else int(ret.count)
...@@ -109,25 +88,15 @@ class Votes: ...@@ -109,25 +88,15 @@ class Votes:
permlink = vop['value']['permlink'] permlink = vop['value']['permlink']
vote_percent = vop['value']['vote_percent'] vote_percent = vop['value']['vote_percent']
sql = """ sql = """
INSERT INTO INSERT INTO hive_votes
hive_votes (post_id, voter_id, author_id, permlink_id, weight, rshares, vote_percent, last_update) (post_id, voter_id, author_id, permlink_id, weight, rshares, vote_percent, last_update)
VALUES ( SELECT hp.id, ha_v.id, ha_a.id, hpd_p.id, :weight, :rshares, :vote_percent, :last_update
(SELECT FROM hive_accounts ha_v,
hp.id hive_posts hp
FROM INNER JOIN hive_accounts ha_a ON ha_a.name = hp.author
hive_posts hp INNER JOIN hive_permlink_data hpd_p ON hpd_p.permlink = hp.permlink
INNER JOIN hive_accounts ha_a ON ha_a.id = hp.author_id WHERE ha_a.name = :author AND hpd_p.permlink = :permlink AND ha_v.name = :voter
INNER JOIN hive_permlink_data hpd_p ON hpd_p.id = hp.permlink_id """
WHERE ha_a.name = :author AND hpd_p.permlink = :permlink
),
(SELECT id FROM hive_accounts WHERE name = :voter),
(SELECT id FROM hive_accounts WHERE name = :author),
(SELECT id FROM hive_permlink_data WHERE permlink = :permlink),
:weight,
:rshares,
:vote_percent,
:last_update
)"""
weight = vop['value']['weight'] weight = vop['value']['weight']
rshares = vop['value']['rshares'] rshares = vop['value']['rshares']
DB.query(sql, voter=voter, author=author, permlink=permlink, weight=weight, rshares=rshares, DB.query(sql, voter=voter, author=author, permlink=permlink, weight=weight, rshares=rshares,
...@@ -138,15 +107,14 @@ class Votes: ...@@ -138,15 +107,14 @@ class Votes:
""" Update existing vote """ """ Update existing vote """
vote_percent = vop['value']['vote_percent'] vote_percent = vop['value']['vote_percent']
sql = """ sql = """
UPDATE UPDATE hive_votes as hv
hive_votes
SET SET
weight = :weight, weight = :weight,
rshares = :rshares, rshares = :rshares,
vote_percent = :vote_percent, vote_percent = :vote_percent,
last_update = :last_update, last_update = :last_update,
num_changes = (SELECT num_changes FROM hive_votes WHERE id = :id)::int + 1 num_changes = hv.num_changes + 1
WHERE id = :id WHERE hv.id = :id
""" """
weight = vop['value']['weight'] weight = vop['value']['weight']
rshares = vop['value']['rshares'] rshares = vop['value']['rshares']
......
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