Skip to content

Commit

Permalink
Dev (#301)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 28, 2024
1 parent acbcfa9 commit 12171b9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
42 changes: 26 additions & 16 deletions nowcasting_datamodel/read/read_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,60 +18,63 @@ 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(
session: Session,
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
Expand All @@ -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)
Expand All @@ -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()
16 changes: 14 additions & 2 deletions tests/read/test_read_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]")
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):
Expand Down

0 comments on commit 12171b9

Please sign in to comment.