Skip to content

Commit

Permalink
Rewrite get_trending_topics method to sql
Browse files Browse the repository at this point in the history
  • Loading branch information
Michał Kudela committed Oct 29, 2024
1 parent 3600740 commit b551e7b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions hive/db/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ def setup_runtime_code(db):
"postgrest/utilities/list_comments.sql",
"postgrest/database_api/database_api_list_comments.sql",
"postgrest/bridge_api/bridge_api_list_subscribers.sql",
"postgrest/bridge_api/bridge_api_get_trending_topics.sql",
]

sql_scripts_dir_path = Path(__file__).parent / 'sql_scripts'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
DROP FUNCTION IF EXISTS hivemind_endpoints.bridge_api_get_trending_topics;
CREATE FUNCTION hivemind_endpoints.bridge_api_get_trending_topics(IN _json_is_object BOOLEAN, IN _params JSONB)
RETURNS JSONB
LANGUAGE 'plpgsql'
STABLE
AS
$$
DECLARE
_limit INTEGER := 10;
_observer TEXT;
_lowest_community_rank INTEGER := 2147483647; -- MAX INT
_fallback_tags_array TEXT[] := ARRAY['food', 'music', 'newsteem', 'crypto', 'gaming', 'travel', 'photography'];
result JSONB;
BEGIN
PERFORM hivemind_postgrest_utilities.validate_json_parameters(_json_is_object, _params, '{"limit", "observer"}', '{"number", "string"}');

_limit := hivemind_postgrest_utilities.parse_integer_argument_from_json(_params, _json_is_object, 'limit', 0, False);
_limit := hivemind_postgrest_utilities.valid_number(_limit, 10, 1, 25, 'limit');

_observer := hivemind_postgrest_utilities.parse_string_argument_from_json(_params, _json_is_object, 'observer', 1, False);
PERFORM hivemind_postgrest_utilities.valid_account(_observer, True);

WITH main_communities AS (
SELECT jsonb_build_array(
community_data->>0,
COALESCE(NULLIF(community_data->>1, ''), community_data->>0)
) AS entry,
rank
FROM (
SELECT community_data, rank
FROM hivemind_postgrest_utilities.list_top_communities(_limit)
) AS subquery
),
fallback_tags AS (
SELECT jsonb_build_array(tag, '#' || tag) AS entry,
_lowest_community_rank - ROW_NUMBER() OVER (ORDER BY array_position(_fallback_tags_array, tag)) AS rank
FROM unnest(_fallback_tags_array) AS tag
),
combined_results AS (
(
SELECT entry, rank FROM main_communities
UNION ALL
SELECT entry, rank FROM fallback_tags
) ORDER BY rank LIMIT _limit
)

SELECT jsonb_agg(entry ORDER BY rank) INTO result FROM combined_results;
RETURN result;
END
$$
;
2 changes: 2 additions & 0 deletions hive/db/sql_scripts/postgrest/utilities/get_api_method.sql
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ BEGIN
result := hivemind_endpoints.bridge_api_post_notifications(__json_with_params_is_object, __params);
WHEN __method_type = 'list_subscribers' THEN
result := hivemind_endpoints.bridge_api_list_subscribers(__json_with_params_is_object, __params);
WHEN __method_type = 'get_trending_topics' THEN
result := hivemind_endpoints.bridge_api_get_trending_topics(__json_with_params_is_object, __params);
ELSE
RAISE EXCEPTION '%', hivemind_postgrest_utilities.raise_method_not_found_exception(__method_type);
END CASE;
Expand Down

0 comments on commit b551e7b

Please sign in to comment.