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' ...@@ -17,12 +17,42 @@ FOLLOWERS = 'followers'
FOLLOWING = 'following' FOLLOWING = 'following'
FOLLOW_ITEM_INSERT_QUERY = """ FOLLOW_ITEM_INSERT_QUERY = """
INSERT INTO hive_follows as hf (follower, following, created_at, state) INSERT INTO hive_follows as hf (follower, following, created_at, state, blacklisted, follow_blacklists)
VALUES( :flr, :flg, :at, :state ) VALUES
ON CONFLICT (follower, following) DO UPDATE SET state = (CASE hf.state (
when 0 then 0 -- 0 blocks possibility to update state :flr,
ELSE 1 :flg,
END) :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): def _flip_dict(dict_to_flip):
...@@ -61,47 +91,15 @@ class Follow: ...@@ -61,47 +91,15 @@ class Follow:
cls.follow_items_to_flush[k] = old_value cls.follow_items_to_flush[k] = old_value
else: else:
cls.follow_items_to_flush[k] = dict( cls.follow_items_to_flush[k] = dict(
flr = op['flr'], flr=op['flr'],
flg = op['flg'], flg=op['flg'],
state = op['state'], state=op['state'],
at = op['at'], at=op['at'])
blk=False,
follow_blk=False)
else: else:
old_state = cls._get_follow_db_state(op['flr'], op['flg']) old_state = cls._get_follow_db_state(op['flr'], op['flg'])
if new_state == (old_state or 0):
return
sql = ''
# insert or update state # insert or update state
if old_state is None: DB.query(FOLLOW_ITEM_INSERT_QUERY, **op)
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)
if new_state == 1: if new_state == 1:
Follow.follow(op['flr'], op['flg']) Follow.follow(op['flr'], op['flg'])
if old_state is None: if old_state is None:
...@@ -177,32 +175,49 @@ class Follow: ...@@ -177,32 +175,49 @@ class Follow:
VALUES """ VALUES """
sql_postfix = """ sql_postfix = """
ON CONFLICT ON CONSTRAINT hive_follows_pk DO UPDATE SET ON CONFLICT ON CONSTRAINT hive_follows_pk DO UPDATE
state = (CASE hf.state SET
WHEN 0 THEN 0 -- 0 blocks possibility to update state state = (CASE EXCLUDED.state
ELSE EXCLUDED.state WHEN 0 THEN 0 -- 0 blocks possibility to update state
END) 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 WHERE hf.following = EXCLUDED.following AND hf.follower = EXCLUDED.follower
""" """
values = [] values = []
limit = 1000 limit = 1000
count = 0 count = 0
for (k, follow_item) in cls.follow_items_to_flush.items(): for _, follow_item in cls.follow_items_to_flush.items():
if count < limit: 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'])) values.append("({}, {}, '{}', {}, {}, {})".format(follow_item['flr'], follow_item['flg'],
count = count + 1 follow_item['at'], follow_item['state'],
else: 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_prefix + ",".join(values)
query += sql_postfix query += sql_postfix
DB.query(query) 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() 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