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

Merge fixes in follow.py

- Conditional updates in python code was moved to SQL.
parent 989547f1
No related branches found
No related tags found
4 merge requests!456Release candidate v1 24,!230Setup monitoring with pghero,!135Enable postgres monitoring on CI server,!16Dk issue 3 concurrent block query rebase
......@@ -17,12 +17,42 @@ FOLLOWERS = 'followers'
FOLLOWING = 'following'
FOLLOW_ITEM_INSERT_QUERY = """
INSERT INTO hive_follows as hf (follower, following, created_at, state)
VALUES( :flr, :flg, :at, :state )
ON CONFLICT (follower, following) DO UPDATE SET state = (CASE hf.state
when 0 then 0 -- 0 blocks possibility to update state
ELSE 1
END)
INSERT INTO hive_follows as hf (follower, following, created_at, state, blacklisted, follow_blacklists)
VALUES
(
:flr,
:flg,
:at,
:state,
(CASE :state
WHEN 3 THEN TRUE
WHEN 4 THEN FALSE
ELSE FALSE
END
),
(CASE :state
WHEN 3 THEN FALSE
WHEN 4 THEN TRUE
ELSE TRUE
END
)
)
ON CONFLICT (follower, following) DO UPDATE
SET
state = (CASE EXCLUDED.state
WHEN 0 THEN 0 -- 0 blocks possibility to update state
ELSE EXCLUDED.state
END),
blacklisted = (CASE EXCLUDED.state
WHEN 3 THEN TRUE
WHEN 5 THEN FALSE
ELSE EXCLUDED.blacklisted
END),
follow_blacklists = (CASE EXCLUDED.state
WHEN 4 THEN TRUE
WHEN 6 THEN FALSE
ELSE EXCLUDED.follow_blacklists
END)
"""
def _flip_dict(dict_to_flip):
......@@ -61,47 +91,15 @@ class Follow:
cls.follow_items_to_flush[k] = old_value
else:
cls.follow_items_to_flush[k] = dict(
flr = op['flr'],
flg = op['flg'],
state = op['state'],
at = op['at'],
blk=False,
follow_blk=False)
flr=op['flr'],
flg=op['flg'],
state=op['state'],
at=op['at'])
else:
old_state = cls._get_follow_db_state(op['flr'], op['flg'])
if new_state == (old_state or 0):
return
sql = ''
# insert or update state
if old_state is None:
sql = """INSERT INTO hive_follows (follower, following,
created_at, state, blacklisted, follow_blacklists) VALUES (:flr, :flg, :at, :state, %s)"""
if new_state == 3:
sql = sql % """ true, false """
elif new_state == 4:
sql = sql % """ false, true """
else:
sql = sql % """false, false"""
else:
if new_state < 3:
sql = """UPDATE hive_follows SET state = :state
WHERE follower = :flr AND following = :flg"""
elif new_state == 3:
sql = """UPDATE hive_follows SET blacklisted = true
WHERE follower = :flr AND following = :flg"""
elif new_state == 4:
sql = """UPDATE hive_follows SET follow_blacklists = true
WHERE follower = :flr AND following = :flg"""
elif new_state == 5:
sql = """UPDATE hive_follows SET blacklisted = false
WHERE follower = :flr AND following = :flg"""
elif new_state == 6:
sql = """UPDATE hive_follows SET follow_blacklists = false
WHERE follower = :flr AND following = :flg"""
DB.query(sql, **op)
DB.query(FOLLOW_ITEM_INSERT_QUERY, **op)
if new_state == 1:
Follow.follow(op['flr'], op['flg'])
if old_state is None:
......@@ -177,32 +175,49 @@ class Follow:
VALUES """
sql_postfix = """
ON CONFLICT ON CONSTRAINT hive_follows_pk DO UPDATE SET
state = (CASE hf.state
WHEN 0 THEN 0 -- 0 blocks possibility to update state
ELSE EXCLUDED.state
END)
ON CONFLICT ON CONSTRAINT hive_follows_pk DO UPDATE
SET
state = (CASE EXCLUDED.state
WHEN 0 THEN 0 -- 0 blocks possibility to update state
ELSE EXCLUDED.state
END),
blacklisted = (CASE EXCLUDED.state
WHEN 3 THEN TRUE
WHEN 5 THEN FALSE
ELSE EXCLUDED.blacklisted
END),
follow_blacklists = (CASE EXCLUDED.state
WHEN 4 THEN TRUE
WHEN 6 THEN FALSE
ELSE EXCLUDED.follow_blacklists
END)
WHERE hf.following = EXCLUDED.following AND hf.follower = EXCLUDED.follower
"""
values = []
limit = 1000
count = 0
for (k, follow_item) in cls.follow_items_to_flush.items():
if count < limit:
values.append("({}, {}, '{}', {}, {}, {})".format(follow_item['flr'], follow_item['flg'], follow_item['at'], follow_item['state'], follow_item['blk'], follow_item['follow_blk']))
count = count + 1
else:
for _, follow_item in cls.follow_items_to_flush.items():
if count < limit:
values.append("({}, {}, '{}', {}, {}, {})".format(follow_item['flr'], follow_item['flg'],
follow_item['at'], follow_item['state'],
follow_item['state'] == 3,
follow_item['state'] == 4))
count = count + 1
else:
query = sql_prefix + ",".join(values)
query += sql_postfix
DB.query(query)
values.clear()
values.append("({}, {}, '{}', {}, {}, {})".format(follow_item['flr'], follow_item['flg'],
follow_item['at'], follow_item['state'],
follow_item['state'] == 3,
follow_item['state'] == 4))
count = 1
if len(values) > 0:
query = sql_prefix + ",".join(values)
query += sql_postfix
DB.query(query)
values.clear()
values.append("({}, {}, '{}', {}, {}, {})".format(follow_item['flr'], follow_item['flg'], follow_item['at'], follow_item['state'], follow_item['blk'], follow_item['follow_blk']))
count = 1
if len(values):
query = sql_prefix + ",".join(values)
query += sql_postfix
DB.query(query)
cls.follow_items_to_flush.clear()
......
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