From cdf49a0bbb9822cdb56b3912a1971c05bd1ce538 Mon Sep 17 00:00:00 2001 From: ABW <andrzejl@syncad.com> Date: Wed, 4 Nov 2020 15:44:01 +0100 Subject: [PATCH] [ABW]: get_follow_count reimplemented as SQL function --- hive/db/schema.py | 1 + hive/db/sql_scripts/condenser_follows.sql | 15 +++++++++++++++ hive/server/condenser_api/cursor.py | 9 --------- hive/server/condenser_api/methods.py | 11 ++++++----- tests/tests_api | 2 +- 5 files changed, 23 insertions(+), 15 deletions(-) create mode 100644 hive/db/sql_scripts/condenser_follows.sql diff --git a/hive/db/schema.py b/hive/db/schema.py index 4fffe4c35..8be645fe1 100644 --- a/hive/db/schema.py +++ b/hive/db/schema.py @@ -1606,6 +1606,7 @@ def setup(db): "condenser_get_content.sql", "condenser_get_discussions_by_blog.sql", "condenser_tags.sql", + "condenser_follows.sql", "hot_and_trends.sql", "update_hive_posts_children_count.sql" ] diff --git a/hive/db/sql_scripts/condenser_follows.sql b/hive/db/sql_scripts/condenser_follows.sql new file mode 100644 index 000000000..52237835e --- /dev/null +++ b/hive/db/sql_scripts/condenser_follows.sql @@ -0,0 +1,15 @@ +DROP FUNCTION IF EXISTS condenser_get_follow_count; +CREATE FUNCTION condenser_get_follow_count( in _account VARCHAR, + out following hive_accounts.following%TYPE, out followers hive_accounts.followers%TYPE ) +AS +$function$ +DECLARE + __account_id INT; +BEGIN + __account_id = find_account_id( _account, True ); + SELECT ha.following, ha.followers INTO following, followers FROM hive_accounts ha WHERE ha.id = __account_id; + -- following equals (SELECT COUNT(*) FROM hive_follows WHERE state = 1 AND following = __account_id) + -- followers equals (SELECT COUNT(*) FROM hive_follows WHERE state = 1 AND follower = __account_id) +END +$function$ +language plpgsql STABLE; diff --git a/hive/server/condenser_api/cursor.py b/hive/server/condenser_api/cursor.py index 66f8a279b..5a69fdb12 100644 --- a/hive/server/condenser_api/cursor.py +++ b/hive/server/condenser_api/cursor.py @@ -93,15 +93,6 @@ async def get_following(db, account: str, start: str, follow_type: str, limit: i state=state, limit=limit) -async def get_follow_counts(db, account: str): - """Return following/followers count for `account`.""" - account_id = await _get_account_id(db, account) - sql = """SELECT following, followers - FROM hive_accounts - WHERE id = :account_id""" - return dict(await db.query_row(sql, account_id=account_id)) - - async def get_reblogged_by(db, author: str, permlink: str): """Return all rebloggers of a post.""" post_id = await _get_post_id(db, author, permlink) diff --git a/hive/server/condenser_api/methods.py b/hive/server/condenser_api/methods.py index 466c23ccc..43659cf9f 100644 --- a/hive/server/condenser_api/methods.py +++ b/hive/server/condenser_api/methods.py @@ -115,12 +115,13 @@ async def get_following(context, account: str, start: str, follow_type: str = No @return_error_info async def get_follow_count(context, account: str): """Get follow count stats. (EOL)""" - count = await cursor.get_follow_counts( - context['db'], - valid_account(account)) + db = context['db'] + account = valid_account(account) + sql = "SELECT * FROM condenser_get_follow_count( (:account)::VARCHAR )" + counters = await db.query_row(sql, account=account) return dict(account=account, - following_count=count['following'], - follower_count=count['followers']) + following_count=counters[0], + follower_count=counters[1]) @return_error_info async def get_reblogged_by(context, author: str, permlink: str): diff --git a/tests/tests_api b/tests/tests_api index fbc0c9724..143134910 160000 --- a/tests/tests_api +++ b/tests/tests_api @@ -1 +1 @@ -Subproject commit fbc0c97245465eb46377a642b57ae1f9fce770ae +Subproject commit 143134910db644cb159e1f3e60f1d76e8eebc485 -- GitLab