Skip to content

Commit

Permalink
Check in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
NolanTrem committed Aug 30, 2024
1 parent 162eade commit 73dfae6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 27 deletions.
15 changes: 10 additions & 5 deletions py/cli/commands/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ def server_stats(client):


@cli.command()
@click.option("--run-type-filter", help="Filter for log types")
@click.option(
"--max-runs", default=None, help="Maximum number of runs to fetch"
"--offset", default=None, help="Pagination offset. Default is None."
)
@click.option(
"--limit", default=None, help="Pagination limit. Defaults to 100."
)
@click.option("--run-type-filter", help="Filter for log types")
@click.pass_obj
def logs(client, run_type_filter, max_runs):
def logs(client, run_type_filter, offset, limit):
"""Retrieve logs with optional type filter."""
with timer():
response = client.logs(run_type_filter, max_runs)
response = client.logs(
offset=offset, limit=limit, run_type_filter=run_type_filter
)

for log in response["results"]:
click.echo(f"Run ID: {log['run_id']}")
Expand All @@ -58,7 +63,7 @@ def logs(client, run_type_filter, max_runs):
click.echo(f" - {entry['key']}: {entry['value'][:100]}")
click.echo("---")

click.echo(f"Total runs: {len(response)}")
click.echo(f"Total runs: {len(response['results'])}")


@cli.command()
Expand Down
28 changes: 17 additions & 11 deletions py/core/base/logging/run_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ async def info_log(
@abstractmethod
async def get_info_logs(
self,
limit: int = 10,
offset: int = 0,
limit: int = 100,
run_type_filter: Optional[RunType] = None,
user_ids: Optional[list[UUID]] = None,
) -> list[RunInfoLog]:
Expand Down Expand Up @@ -177,7 +178,8 @@ async def info_log(

async def get_info_logs(
self,
limit: int = 10,
offset: int = 0,
limit: int = 100,
run_type_filter: Optional[RunType] = None,
user_ids: Optional[list[UUID]] = None,
) -> list[RunInfoLog]:
Expand All @@ -194,8 +196,8 @@ async def get_info_logs(
params.extend([str(user_id) for user_id in user_ids])
if conditions:
query += " WHERE " + " AND ".join(conditions)
query += " ORDER BY timestamp DESC LIMIT ?"
params.append(limit)
query += " ORDER BY timestamp DESC LIMIT ? OFFSET ?"
params.extend([limit, offset])
await cursor.execute(query, params)
rows = await cursor.fetchall()
return [
Expand Down Expand Up @@ -408,7 +410,8 @@ async def info_log(

async def get_info_logs(
self,
limit: int = 10,
offset: int = 0,
limit: int = 100,
run_type_filter: Optional[RunType] = None,
user_ids: Optional[list[UUID]] = None,
) -> list[RunInfoLog]:
Expand All @@ -430,8 +433,8 @@ async def get_info_logs(
if conditions:
query += " WHERE " + " AND ".join(conditions)

query += f" ORDER BY timestamp DESC LIMIT ${param_count}"
params.append(limit)
query += f" ORDER BY timestamp DESC LIMIT ${param_count} OFFSET ${param_count + 1}"
params.extend([limit, offset])

async with self.pool.acquire() as conn:
rows = await conn.fetch(query, *params)
Expand Down Expand Up @@ -599,12 +602,13 @@ async def info_log(

async def get_info_logs(
self,
limit: int = 10,
offset: int = 0,
limit: int = 100,
run_type_filter: Optional[RunType] = None,
user_ids: Optional[list[UUID]] = None,
) -> list[RunInfoLog]:
run_info_list = []
start = 0
start = offset
count_per_batch = 100 # Adjust batch size as needed

while len(run_info_list) < limit:
Expand Down Expand Up @@ -752,13 +756,15 @@ async def info_log(
@classmethod
async def get_info_logs(
cls,
limit: int = 10,
offset: int = 0,
limit: int = 100,
run_type_filter: Optional[RunType] = None,
user_ids: Optional[list[UUID]] = None,
) -> list[RunInfoLog]:
async with cls.get_instance() as provider:
return await provider.get_info_logs(
limit,
offset=offset,
limit=limit,
run_type_filter=run_type_filter,
user_ids=user_ids,
)
Expand Down
6 changes: 4 additions & 2 deletions py/core/main/api/routes/management/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ async def get_analytics_app(
@self.base_endpoint
async def logs_app(
run_type_filter: Optional[str] = Query(""),
max_runs: int = Query(100, ge=1, le=1000),
offset: int = Query(0, ge=0),
limit: int = Query(100, ge=1, le=1000),
auth_user=Depends(self.engine.providers.auth.auth_wrapper),
) -> WrappedLogResponse:
if not auth_user.is_superuser:
Expand All @@ -132,7 +133,8 @@ async def logs_app(

return await self.engine.alogs(
run_type_filter=run_type_filter,
max_runs=min(max(max_runs, 1), 1000),
offset=offset,
limit=limit,
)

@self.router.get("/app_settings")
Expand Down
15 changes: 13 additions & 2 deletions py/core/main/services/management_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,32 @@ def __init__(

@telemetry_event("Logs")
async def alogs(
self, run_type_filter: Optional[str] = None, max_runs: int = 100
self,
offset: int = 0,
limit: int = 100,
run_type_filter: Optional[str] = None,
):
logger.info(f"alogs called with offset={offset}, limit={limit}")
if self.logging_connection is None:
raise R2RException(
status_code=404, message="Logging provider not found."
)

run_info = await self.logging_connection.get_info_logs(
limit=max_runs,
offset=offset,
limit=limit,
run_type_filter=run_type_filter,
)
logger.info(
f"get_info_logs returned {len(run_info)} entries and they are: {run_info}"
)
run_ids = [run.run_id for run in run_info]
if not run_ids:
return []
logs = await self.logging_connection.get_logs(run_ids)
logger.info(
f"get_logs returned {len(logs)} entries and they are: {logs}"
)

aggregated_logs = []

Expand Down
20 changes: 13 additions & 7 deletions py/sdk/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,28 @@ async def server_stats(client) -> dict:
@staticmethod
async def logs(
client,
offset: int = None,
limit: int = None,
run_type_filter: Optional[str] = None,
max_runs: int = None,
) -> dict:
"""
Get logs from the server.
Args:
offset (Optional[int]): The offset to start from.
limit (Optional[int]): The maximum number of logs to return.
run_type_filter (Optional[str]): The run type to filter by.
max_runs (int): Specifies the maximum number of runs to return. Values outside the range of 1 to 1000 will be adjusted to the nearest valid value with a default of 100.
Returns:
dict: The logs from the server.
"""
params = {}
if run_type_filter is not None:
params["run_type_filter"] = run_type_filter
if max_runs is not None:
params["max_runs"] = max_runs
params = {
key: value
for key, value in {
"offset": offset,
"limit": limit,
"run_type_filter": run_type_filter,
}.items()
if value is not None
}
return await client._make_request("GET", "logs", params=params)

0 comments on commit 73dfae6

Please sign in to comment.