From 948daef310ebe7e0961680624153bb6f9779b807 Mon Sep 17 00:00:00 2001 From: Kraiem Taha Yassine Date: Fri, 26 Jan 2024 14:31:51 +0100 Subject: [PATCH] Dev (#1859) * fix(chalice): fixed NewRelic integration * fix(chalice): restrict EE usability-tests to admin level only * refactor(chalice): changed assist stats logs --- api/chalicelib/core/log_tool_newrelic.py | 7 +- api/schemas/schemas.py | 4 +- ee/api/.gitignore | 1 - ee/api/chalicelib/core/assist_stats.py | 10 +- ee/api/clean-dev.sh | 1 - ee/api/routers/subs/usability_tests.py | 126 +++++++++++++++++++++++ 6 files changed, 135 insertions(+), 14 deletions(-) create mode 100644 ee/api/routers/subs/usability_tests.py diff --git a/api/chalicelib/core/log_tool_newrelic.py b/api/chalicelib/core/log_tool_newrelic.py index 1eb23c6aae..c25e2d15dc 100644 --- a/api/chalicelib/core/log_tool_newrelic.py +++ b/api/chalicelib/core/log_tool_newrelic.py @@ -14,8 +14,8 @@ def get(project_id): def update(tenant_id, project_id, changes): options = {} - if "region" in changes and len(changes["region"]) == 0: - options["region"] = "US" + if "region" in changes: + options["region"] = changes["region"] if "applicationId" in changes: options["applicationId"] = changes["applicationId"] if "xQueryKey" in changes: @@ -25,8 +25,7 @@ def update(tenant_id, project_id, changes): def add(tenant_id, project_id, application_id, x_query_key, region): - if region is None or len(region) == 0: - region = "US" + # region=False => US; region=True => EU options = {"applicationId": application_id, "xQueryKey": x_query_key, "region": region} return log_tools.add(project_id=project_id, integration=IN_TY, options=options) diff --git a/api/schemas/schemas.py b/api/schemas/schemas.py index 00a693fb70..25d315c7b2 100644 --- a/api/schemas/schemas.py +++ b/api/schemas/schemas.py @@ -295,7 +295,7 @@ class IntegartionStackdriverSchema(IntegrationBase): class IntegrationNewrelicSchema(IntegrationBase): application_id: str = Field(...) x_query_key: str = Field(...) - region: str = Field(...) + region: bool = Field(default=False) class IntegrationRollbarSchema(IntegrationBase): @@ -1587,5 +1587,3 @@ class TagCreate(TagUpdate): selector: str = Field(..., min_length=1, max_length=255) ignoreClickRage: bool = Field(default=False) ignoreDeadClick: bool = Field(default=False) - - diff --git a/ee/api/.gitignore b/ee/api/.gitignore index f7341db458..a0743c6dbd 100644 --- a/ee/api/.gitignore +++ b/ee/api/.gitignore @@ -264,7 +264,6 @@ Pipfile.lock /routers/core.py /routers/subs/__init__.py /routers/subs/v1_api.py -/routers/subs/usability_tests.py /run-alerts-dev.sh /run-dev.sh /schemas/overrides.py diff --git a/ee/api/chalicelib/core/assist_stats.py b/ee/api/chalicelib/core/assist_stats.py index 1f837ac58f..9439a15713 100644 --- a/ee/api/chalicelib/core/assist_stats.py +++ b/ee/api/chalicelib/core/assist_stats.py @@ -16,12 +16,12 @@ def insert_aggregated_data(): try: - logging.info("Assist Stats: Inserting aggregated data") + logging.debug("Assist Stats: Inserting aggregated data") end_timestamp = int(datetime.timestamp(datetime.now())) * 1000 start_timestamp = __last_run_end_timestamp_from_aggregates() if start_timestamp is None: # first run - logging.info("Assist Stats: First run, inserting data for last 7 days") + logging.debug("Assist Stats: First run, inserting data for last 7 days") start_timestamp = end_timestamp - (7 * 24 * 60 * 60 * 1000) offset = 0 @@ -40,14 +40,14 @@ def insert_aggregated_data(): "step_size": f"{60} seconds", } - logging.info(f"Assist Stats: Fetching data from {start_timestamp} to {end_timestamp}") + logging.debug(f"Assist Stats: Fetching data from {start_timestamp} to {end_timestamp}") aggregated_data = __get_all_events_hourly_averages(constraints, params) if not aggregated_data: # No more data to insert - logging.info("Assist Stats: No more data to insert") + logging.debug("Assist Stats: No more data to insert") break - logging.info(f"Assist Stats: Inserting {len(aggregated_data)} rows") + logging.debug(f"Assist Stats: Inserting {len(aggregated_data)} rows") for data in aggregated_data: sql = """ diff --git a/ee/api/clean-dev.sh b/ee/api/clean-dev.sh index 36c2642e89..cdd2943865 100755 --- a/ee/api/clean-dev.sh +++ b/ee/api/clean-dev.sh @@ -87,7 +87,6 @@ rm -rf ./routers/base.py rm -rf ./routers/core.py rm -rf ./routers/subs/__init__.py rm -rf ./routers/subs/v1_api.py -rm -rf ./routers/subs/usability_tests.py rm -rf ./run-alerts-dev.sh rm -rf ./run-dev.sh rm -rf ./schemas/overrides.py diff --git a/ee/api/routers/subs/usability_tests.py b/ee/api/routers/subs/usability_tests.py new file mode 100644 index 0000000000..541a8fe45d --- /dev/null +++ b/ee/api/routers/subs/usability_tests.py @@ -0,0 +1,126 @@ +from fastapi import Body, Depends + +from chalicelib.core.usability_testing import service +from chalicelib.core.usability_testing.schema import UTTestCreate, UTTestUpdate, UTTestSearch +from or_dependencies import OR_context, OR_role +from routers.base import get_routers +from schemas import schemas + +public_app, app, app_apikey = get_routers(extra_dependencies=[OR_role("owner", "admin")]) +tags = ["usability-tests"] + + +@app.post('/{projectId}/usability-tests/search', tags=tags) +async def search_ui_tests( + projectId: int, + search: UTTestSearch = Body(..., + description="The search parameters including the query, page, limit, sort_by, " + "and sort_order.") +): + """ + Search for UT tests within a given project with pagination and optional sorting. + + - **projectId**: The unique identifier of the project to search within. + - **search**: The search parameters including the query, page, limit, sort_by, and sort_order. + """ + + return service.search_ui_tests(projectId, search) + + +@app.post('/{projectId}/usability-tests', tags=tags) +async def create_ut_test(projectId: int, test_data: UTTestCreate, + context: schemas.CurrentContext = Depends(OR_context)): + """ + Create a new UT test in the specified project. + + - **projectId**: The unique identifier of the project. + - **test_data**: The data for the new UT test. + """ + test_data.project_id = projectId + test_data.created_by = context.user_id + return service.create_ut_test(test_data) + + +@app.get('/{projectId}/usability-tests/{test_id}', tags=tags) +async def get_ut_test(projectId: int, test_id: int): + """ + Retrieve a specific UT test by its ID. + + - **projectId**: The unique identifier of the project. + - **test_id**: The unique identifier of the UT test. + """ + return service.get_ut_test(projectId, test_id) + + +@app.delete('/{projectId}/usability-tests/{test_id}', tags=tags) +async def delete_ut_test(projectId: int, test_id: int): + """ + Delete a specific UT test by its ID. + + - **projectId**: The unique identifier of the project. + - **test_id**: The unique identifier of the UT test to be deleted. + """ + return service.delete_ut_test(projectId, test_id) + + +@app.put('/{projectId}/usability-tests/{test_id}', tags=tags) +async def update_ut_test(projectId: int, test_id: int, test_update: UTTestUpdate): + """ + Update a specific UT test by its ID. + + - **project_id**: The unique identifier of the project. + - **test_id**: The unique identifier of the UT test to be updated. + - **test_update**: The updated data for the UT test. + """ + + return service.update_ut_test(projectId, test_id, test_update) + + +@app.get('/{projectId}/usability-tests/{test_id}/sessions', tags=tags) +async def get_sessions(projectId: int, test_id: int, page: int = 1, limit: int = 10, + live: bool = False, + user_id: str = None): + """ + Get sessions related to a specific UT test. + + - **projectId**: The unique identifier of the project. + - **test_id**: The unique identifier of the UT test. + """ + + if live: + return service.ut_tests_sessions_live(projectId, test_id, page, limit) + else: + return service.ut_tests_sessions(projectId, test_id, page, limit, user_id, live) + + +@app.get('/{projectId}/usability-tests/{test_id}/responses/{task_id}', tags=tags) +async def get_responses(projectId: int, test_id: int, task_id: int, page: int = 1, limit: int = 10, query: str = None): + """ + Get responses related to a specific UT test. + + - **project_id**: The unique identifier of the project. + - **test_id**: The unique identifier of the UT test. + """ + return service.get_responses(test_id, task_id, page, limit, query) + + +@app.get('/{projectId}/usability-tests/{test_id}/statistics', tags=tags) +async def get_statistics(projectId: int, test_id: int): + """ + Get statistics related to a specific UT test. + + :param test_id: + :return: + """ + return service.get_statistics(test_id=test_id) + + +@app.get('/{projectId}/usability-tests/{test_id}/task-statistics', tags=tags) +async def get_task_statistics(projectId: int, test_id: int): + """ + Get statistics related to a specific UT test. + + :param test_id: + :return: + """ + return service.get_task_statistics(test_id=test_id)