From 45ebf6245f94a170b3bcd61d9a1aea7b7832d8af Mon Sep 17 00:00:00 2001 From: BitterPanda63 Date: Thu, 15 May 2025 11:57:20 +0200 Subject: [PATCH 1/3] Create new function update_route_info_from_context --- aikido_zen/api_discovery/update_route_info.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/aikido_zen/api_discovery/update_route_info.py b/aikido_zen/api_discovery/update_route_info.py index 5ddcd97f1..8635f9133 100644 --- a/aikido_zen/api_discovery/update_route_info.py +++ b/aikido_zen/api_discovery/update_route_info.py @@ -1,12 +1,22 @@ """Exports update_route_info function""" from aikido_zen.helpers.logging import logger +from .get_api_info import get_api_info from .merge_data_schemas import merge_data_schemas from .merge_auth_types import merge_auth_types ANALYSIS_ON_FIRST_X_ROUTES = 20 +def update_route_info_from_context(context, route): + if context is None: + return + if route["hits"] > ANALYSIS_ON_FIRST_X_ROUTES: + return + new_apispec = get_api_info(context) + route["apispec"] = update_api_info(new_apispec, route["apispec"]) + + def update_route_info(new_apispec, route): """ Checks if a route still needs to be updated (only analyzes first x routes), From f469afe690a7fcd55074d0f9b61b1de12bd58588 Mon Sep 17 00:00:00 2001 From: BitterPanda63 Date: Thu, 15 May 2025 11:57:57 +0200 Subject: [PATCH 2/3] Run this new function that first checks hits --- aikido_zen/sources/functions/request_handler.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/aikido_zen/sources/functions/request_handler.py b/aikido_zen/sources/functions/request_handler.py index 16b58788b..cbec77647 100644 --- a/aikido_zen/sources/functions/request_handler.py +++ b/aikido_zen/sources/functions/request_handler.py @@ -1,8 +1,7 @@ """Exports request_handler function""" import aikido_zen.context as ctx -from aikido_zen.api_discovery.get_api_info import get_api_info -from aikido_zen.api_discovery.update_route_info import update_route_info +from aikido_zen.api_discovery.update_route_info import update_route_info_from_context from aikido_zen.helpers.is_useful_route import is_useful_route from aikido_zen.helpers.logging import logger from aikido_zen.thread.thread_cache import get_cache @@ -88,7 +87,6 @@ def post_response(status_code): if cache: cache.routes.increment_route(route_metadata) - # Run API Discovery : - update_route_info( - new_apispec=get_api_info(context), route=cache.routes.get(route_metadata) - ) + # api spec generation + route = cache.routes.get(route_metadata) + update_route_info_from_context(context, route) From 1dd41a7dba629150fd5375dd7cf51ebef583940a Mon Sep 17 00:00:00 2001 From: BitterPanda63 Date: Thu, 15 May 2025 12:08:21 +0200 Subject: [PATCH 3/3] Add a test to check it now only updates on first 20 hits --- .../sources/functions/request_handler_test.py | 43 ++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/aikido_zen/sources/functions/request_handler_test.py b/aikido_zen/sources/functions/request_handler_test.py index 44d73c90a..e13f6bb74 100644 --- a/aikido_zen/sources/functions/request_handler_test.py +++ b/aikido_zen/sources/functions/request_handler_test.py @@ -17,6 +17,7 @@ def mock_context(): "method": "GET", "url": "http://localhost:8080/test/route", } + context.body = {} return context @@ -34,18 +35,40 @@ def test_post_response_useful_route(mock_context): cache = get_cache() # Creates a new cache assert cache.routes.routes == {} - with patch("aikido_zen.context.get_current_context", return_value=mock_context): - request_handler("post_response", status_code=200) + for i in range(25): + with patch("aikido_zen.context.get_current_context", return_value=mock_context): + mock_context.body[str(i + 1)] = i + 1 + request_handler("post_response", status_code=200) # Check that the route was initialized and updated - assert cache.routes.routes == { - "GET:/test/route": { - "apispec": {}, - "hits": 1, - "method": "GET", - "path": "/test/route", - "hits_delta_since_sync": 1, - } + route = cache.routes.routes.get("GET:/test/route") + assert route["hits"] == route["hits_delta_since_sync"] == 25 + assert route["method"] == "GET" + assert route["path"] == "/test/route" + + # Test only updates on first 20 hits + body_props = route["apispec"]["body"]["schema"]["properties"] + assert body_props == { + "1": {"type": "number"}, + "2": {"type": "number", "optional": True}, + "3": {"type": "number", "optional": True}, + "4": {"type": "number", "optional": True}, + "5": {"type": "number", "optional": True}, + "6": {"type": "number", "optional": True}, + "7": {"type": "number", "optional": True}, + "8": {"type": "number", "optional": True}, + "9": {"type": "number", "optional": True}, + "10": {"type": "number", "optional": True}, + "11": {"type": "number", "optional": True}, + "12": {"type": "number", "optional": True}, + "13": {"type": "number", "optional": True}, + "14": {"type": "number", "optional": True}, + "15": {"type": "number", "optional": True}, + "16": {"type": "number", "optional": True}, + "17": {"type": "number", "optional": True}, + "18": {"type": "number", "optional": True}, + "19": {"type": "number", "optional": True}, + "20": {"type": "number", "optional": True}, }