Skip to content

Commit 9391bfe

Browse files
authored
feat: add endpoint for downloading logs (#192)
2 parents 3165b55 + e5c1c1e commit 9391bfe

2 files changed

Lines changed: 23 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>downloadLogs</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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import logging
1313
import multiprocessing as mp
1414
import os
15+
import tempfile
1516
import threading
17+
import zipfile
1618
from collections.abc import Callable
1719
from contextlib import asynccontextmanager
1820
from functools import wraps
@@ -25,6 +27,7 @@
2527
from nc_py_api import AsyncNextcloudApp, NextcloudApp
2628
from nc_py_api.ex_app import persistent_storage, set_handlers
2729
from pydantic import BaseModel, ValidationInfo, field_validator
30+
from starlette.responses import FileResponse
2831

2932
from .chain.context import do_doc_search
3033
from .chain.ingest.injest import embed_sources
@@ -492,3 +495,15 @@ def _(query: Query) -> list[SearchResult]:
492495
query.scopeType,
493496
query.scopeList,
494497
))
498+
499+
500+
@app.get('/downloadLogs')
501+
def download_logs() -> FileResponse:
502+
with tempfile.NamedTemporaryFile('wb', delete=False) as tmp:
503+
with zipfile.ZipFile(tmp, mode='w', compression=zipfile.ZIP_DEFLATED) as zip_file:
504+
files = os.listdir(os.path.join(persistent_storage(), 'logs'))
505+
for file in files:
506+
file_path = os.path.join(persistent_storage(), 'logs', file)
507+
if os.path.isfile(file_path): # Might be a folder (just skip it then)
508+
zip_file.write(file_path)
509+
return FileResponse(tmp.name, media_type='application/zip', filename='docker_logs.zip')

0 commit comments

Comments
 (0)