Skip to content

Commit ee1f841

Browse files
committed
Shorten query mess on get logs
1 parent 104fc5a commit ee1f841

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

chronos/views.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from fastapi import APIRouter, Depends, HTTPException
55
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
6-
from sqlalchemy import func
76
from sqlalchemy.exc import NoResultFound
87
from sqlmodel import Session, select
98

@@ -178,24 +177,12 @@ async def get_logs(
178177
except NoResultFound as e:
179178
return {'message': f'WebhookEndpoint with TC ID: {tc_id} not found: {e}', 'logs': [], 'count': 0}
180179

181-
# Get the total count of logs for the relevant endpoint
182-
count_stmt = select(func.count(WebhookLog.id)).where(WebhookLog.webhook_endpoint_id == endpoint.id)
183-
count = db.exec(count_stmt).one()
184-
185180
offset = page * 50
186-
if count <= offset:
187-
return {'message': f'No logs found for page: {page}', 'logs': [], 'count': count}
188181

189182
# Get the Logs and related endpoint
190-
statement = (
191-
select(WebhookLog)
192-
.where(WebhookLog.webhook_endpoint_id == endpoint.id)
193-
.order_by(WebhookLog.timestamp.desc())
194-
.offset(offset)
195-
.limit(50)
196-
)
197-
results = db.exec(statement)
198-
logs = results.all()
183+
logs = db.exec(
184+
select(WebhookLog).where(WebhookLog.webhook_endpoint_id == endpoint.id).offset(offset).limit(100)
185+
).all()
199186
list_of_webhooks = [
200187
{
201188
'request_headers': json.loads(log.request_headers),
@@ -209,7 +196,14 @@ async def get_logs(
209196
}
210197
for log in logs
211198
]
212-
return {'logs': list_of_webhooks, 'count': count}
199+
200+
# If another page is available this will show in TC2 without extra slow queries
201+
# Its a false count that just indicates more pages available
202+
count = offset + len(list_of_webhooks)
203+
if count <= offset:
204+
return {'message': f'No logs found for page: {page}', 'logs': [], 'count': count}
205+
206+
return {'logs': list_of_webhooks[:50], 'count': count}
213207

214208

215209
@main_router.get('/', description='Index page for Chronos')

0 commit comments

Comments
 (0)