Skip to content

Commit e746634

Browse files
committed
feat: add endpoint for downloading logs
Signed-off-by: Lukas Schaefer <lukas@lschaefer.xyz>
1 parent 3165b55 commit e746634

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

appinfo/info.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ Setup background job workers as described here: https://docs.nextcloud.com/serve
3737
<image>nextcloud/context_chat_backend</image>
3838
<image-tag>4.3.0</image-tag>
3939
</docker-install>
40+
<routes>
41+
<route>
42+
<url>download-logs</url>
43+
<verb>GET</verb>
44+
<access_level>ADMIN</access_level>
45+
<headers_to_exclude>[]</headers_to_exclude>
46+
</route>
47+
</routes>
4048
<environment-variables>
4149
<variable>
4250
<name>EXTERNAL_DB</name>

context_chat_backend/controller.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
33
# SPDX-License-Identifier: AGPL-3.0-or-later
44
#
5+
import zipfile
6+
7+
from starlette.responses import FileResponse
58

69
# isort: off
710
from .chain.types import ContextException, LLMOutput, ScopeType, SearchResult
@@ -12,6 +15,7 @@
1215
import logging
1316
import multiprocessing as mp
1417
import os
18+
import tempfile
1519
import threading
1620
from collections.abc import Callable
1721
from contextlib import asynccontextmanager
@@ -492,3 +496,16 @@ def _(query: Query) -> list[SearchResult]:
492496
query.scopeType,
493497
query.scopeList,
494498
))
499+
500+
501+
@app.get('/download-logs')
502+
@enabled_guard(app)
503+
def download_logs() -> FileResponse:
504+
with tempfile.NamedTemporaryFile('wb', delete=False) as tmp:
505+
with zipfile.ZipFile(tmp, mode='w', compression=zipfile.ZIP_DEFLATED) as zip_file:
506+
files = os.listdir(os.path.join(persistent_storage(), 'logs'))
507+
for file in files:
508+
file_path = os.path.join(persistent_storage(), 'logs', file)
509+
if os.path.isfile(file_path): # Might be a folder (just skip it then)
510+
zip_file.write(file_path)
511+
return FileResponse(tmp.name, media_type='application/zip', filename='docker_logs.zip')

0 commit comments

Comments
 (0)