Skip to content

AIK-5109 Make sure API Spec only gets checked every first 20 times #380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions aikido_zen/api_discovery/update_route_info.py
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 13 in aikido_zen/api_discovery/update_route_info.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/api_discovery/update_route_info.py#L13

Added line #L13 was not covered by tests
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),
Expand Down
10 changes: 4 additions & 6 deletions aikido_zen/sources/functions/request_handler.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
43 changes: 33 additions & 10 deletions aikido_zen/sources/functions/request_handler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def mock_context():
"method": "GET",
"url": "http://localhost:8080/test/route",
}
context.body = {}
return context


Expand All @@ -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},
}


Expand Down