From 12171b9b19711dc826a75e990c497d1b36492f30 Mon Sep 17 00:00:00 2001 From: Peter Dudfield <34686298+peterdudfield@users.noreply.github.com> Date: Thu, 28 Nov 2024 14:10:10 +0000 Subject: [PATCH] Dev (#301) * Add URL Filtering Capabilities to API Request Retrieval Functions (#299) * feat: Implement URL filtering for API request retrieval * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * added tests for include_url and exclude_url * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * formant and fix test * lint * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Sahil Chhoker Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- nowcasting_datamodel/read/read_user.py | 42 ++++++++++++++++---------- tests/read/test_read_user.py | 16 ++++++++-- 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/nowcasting_datamodel/read/read_user.py b/nowcasting_datamodel/read/read_user.py index 743e6f17..97313463 100644 --- a/nowcasting_datamodel/read/read_user.py +++ b/nowcasting_datamodel/read/read_user.py @@ -18,53 +18,54 @@ def get_user(session: Session, email: str) -> UserSQL: :param session: database session :param email: email of user - return: One Metric SQl object - """ # start main query query = session.query(UserSQL) - # filter on name query = query.filter(UserSQL.email == email) - # get all results users = query.all() - if len(users) == 0: logger.debug(f"User for name {email} does not exist so going to add it") - user = UserSQL(email=email) - session.add(user) session.commit() - else: user = users[0] - return user -def get_all_last_api_request(session: Session) -> List[APIRequestSQL]: +def get_all_last_api_request( + session: Session, include_in_url: Optional[str] = None, exclude_in_url: Optional[str] = None +) -> List[APIRequestSQL]: """ Get all last api requests for all users :param session: database session - :return: + :param include_in_url: Optional filter to include only URLs containing this string + :param exclude_in_url: Optional filter to exclude URLs containing this string + :return: List of last API requests """ - last_requests_sql = ( + query = ( session.query(APIRequestSQL) .distinct(APIRequestSQL.user_uuid) .join(UserSQL) .options(contains_eager(APIRequestSQL.user)) .populate_existing() .order_by(APIRequestSQL.user_uuid, APIRequestSQL.created_utc.desc()) - .all() ) - return last_requests_sql + # Apply URL filtering if specified + if include_in_url is not None: + query = query.filter(APIRequestSQL.url.like(f"%{include_in_url}%")) + + if exclude_in_url is not None: + query = query.filter(~APIRequestSQL.url.like(f"%{exclude_in_url}%")) + + return query.all() def get_api_requests_for_one_user( @@ -72,6 +73,8 @@ def get_api_requests_for_one_user( email: str, start_datetime: Optional[datetime] = None, end_datetime: Optional[datetime] = None, + include_in_url: Optional[str] = None, + exclude_in_url: Optional[str] = None, ) -> List[APIRequestSQL]: """ Get all api requests for one user @@ -80,6 +83,8 @@ def get_api_requests_for_one_user( :param email: user email :param start_datetime: only get api requests after start datetime :param end_datetime: only get api requests before end datetime + :param include_in_url: Optional filter to include only URLs containing this string + :param exclude_in_url: Optional filter to exclude URLs containing this string """ query = session.query(APIRequestSQL).join(UserSQL).filter(UserSQL.email == email) @@ -90,6 +95,11 @@ def get_api_requests_for_one_user( if end_datetime is not None: query = query.filter(APIRequestSQL.created_utc <= end_datetime) - api_requests = query.order_by(APIRequestSQL.created_utc.desc()).all() + # Apply URL filtering if specified + if include_in_url is not None: + query = query.filter(APIRequestSQL.url.like(f"%{include_in_url}%")) + + if exclude_in_url is not None: + query = query.filter(~APIRequestSQL.url.like(f"%{exclude_in_url}%")) - return api_requests + return query.order_by(APIRequestSQL.created_utc.desc()).all() diff --git a/tests/read/test_read_user.py b/tests/read/test_read_user.py index d6da18ca..ebd56cd5 100644 --- a/tests/read/test_read_user.py +++ b/tests/read/test_read_user.py @@ -39,10 +39,22 @@ def test_get_all_last_api_request(db_session): def test_get_api_requests_for_one_user(db_session): user = get_user(session=db_session, email="test@test.com") db_session.add(APIRequestSQL(user_uuid=user.uuid, url="test")) + db_session.add(APIRequestSQL(user_uuid=user.uuid, url="url")) requests_sql = get_api_requests_for_one_user(session=db_session, email=user.email) - assert len(requests_sql) == 1 - assert requests_sql[0].url == "test" + assert len(requests_sql) == 2 + + include_filtered = get_api_requests_for_one_user( + session=db_session, email=user.email, include_in_url="test" + ) + assert len(include_filtered) == 1 + assert include_filtered[0].url == "test" + + exclude_filtered = get_api_requests_for_one_user( + session=db_session, email=user.email, exclude_in_url="test" + ) + assert len(exclude_filtered) == 1 + assert exclude_filtered[0].url == "url" def test_get_api_requests_for_one_user_start_datetime(db_session):