Skip to content

Commit f9f8c3e

Browse files
authored
Merge pull request #338 from openclimatefix/api_user_fix
limit last api request by datetime
2 parents c42acf6 + 7d1ee45 commit f9f8c3e

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

nowcasting_datamodel/read/read_user.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,20 @@ def get_user(session: Session, email: str) -> UserSQL:
3838

3939

4040
def get_all_last_api_request(
41-
session: Session, include_in_url: Optional[str] = None, exclude_in_url: Optional[str] = None
41+
session: Session,
42+
include_in_url: Optional[str] = None,
43+
exclude_in_url: Optional[str] = None,
44+
start_datetime: Optional[datetime] = None,
45+
end_datetime: Optional[datetime] = None,
4246
) -> List[APIRequestSQL]:
4347
"""
4448
Get all last api requests for all users
4549
4650
:param session: database session
4751
:param include_in_url: Optional filter to include only URLs containing this string
4852
:param exclude_in_url: Optional filter to exclude URLs containing this string
53+
:param start_datetime: only get api requests after start datetime
54+
:param end_datetime: only get api requests before end datetime
4955
:return: List of last API requests
5056
"""
5157

@@ -57,14 +63,19 @@ def get_all_last_api_request(
5763
.populate_existing()
5864
.order_by(APIRequestSQL.user_uuid, APIRequestSQL.created_utc.desc())
5965
)
60-
6166
# Apply URL filtering if specified
6267
if include_in_url is not None:
6368
query = query.filter(APIRequestSQL.url.like(f"%{include_in_url}%"))
6469

6570
if exclude_in_url is not None:
6671
query = query.filter(~APIRequestSQL.url.like(f"%{exclude_in_url}%"))
6772

73+
if start_datetime is not None:
74+
query = query.filter(APIRequestSQL.created_utc >= start_datetime)
75+
76+
if end_datetime is not None:
77+
query = query.filter(APIRequestSQL.created_utc <= end_datetime)
78+
6879
return query.all()
6980

7081

0 commit comments

Comments
 (0)