Add download_dump endpoint

This commit is contained in:
2022-08-21 14:15:25 +03:00
parent eb9cf2fe29
commit 0d252fc532
8 changed files with 109 additions and 62 deletions

3
src/app/utils.py Normal file
View File

@@ -0,0 +1,3 @@
class DummyWriter:
def write(self, line):
return line

View File

@@ -1,5 +1,7 @@
import asyncio
import base64
import csv
from typing import AsyncIterator
from fastapi import APIRouter, Depends, HTTPException, status, Request
from fastapi.responses import StreamingResponse
@@ -17,6 +19,7 @@ from app.services.caption_getter import get_caption
from app.services.downloader import get_filename
from app.services.files_client import download_file as download_file_from_cache
from app.services.library_client import get_book
from app.utils import DummyWriter
router = APIRouter(
@@ -25,13 +28,15 @@ router = APIRouter(
@router.get("/{object_id}/{object_type}", response_model=CachedFile)
async def get_cached_file(object_id: int, object_type: str):
async def get_cached_file(request: Request, object_id: int, object_type: str):
cached_file = await CachedFileDB.objects.get_or_none(
object_id=object_id, object_type=object_type
)
if not cached_file:
cached_file = await cache_file_by_book_id({}, object_id, object_type)
cached_file = await cache_file_by_book_id(
{"redis": request.app.state.redis_client}, object_id, object_type
)
if not cached_file:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
@@ -40,13 +45,15 @@ async def get_cached_file(object_id: int, object_type: str):
@router.get("/download/{object_id}/{object_type}")
async def download_cached_file(object_id: int, object_type: str):
async def download_cached_file(request: Request, object_id: int, object_type: str):
cached_file = await CachedFileDB.objects.get_or_none(
object_id=object_id, object_type=object_type
)
if not cached_file:
cached_file = await cache_file_by_book_id({}, object_id, object_type)
cached_file = await cache_file_by_book_id(
{"redis": request.app.state.redis_client}, object_id, object_type
)
if not cached_file:
raise HTTPException(status_code=status.HTTP_204_NO_CONTENT)
@@ -117,6 +124,22 @@ async def update_cache(request: Request):
return "Ok!"
@router.get("/download_dump")
async def download_dump():
async def get_data() -> AsyncIterator[str]:
writer = csv.writer(DummyWriter())
async for c_file in CachedFileDB.objects.iterate():
yield writer.writerow([c_file.object_id, c_file.object_type, c_file.data])
return StreamingResponse(
get_data(),
headers={
"Content-Disposition": "attachment; filename=dump.csv",
},
)
healthcheck_router = APIRouter(
tags=["healthcheck"],
)

View File

@@ -6,6 +6,7 @@ from prometheus_fastapi_instrumentator import Instrumentator
from app.views import router, healthcheck_router
from core.arq_pool import get_arq_pool
from core.db import database
from core.redis_client import get_client
import core.sentry # noqa: F401
@@ -24,6 +25,7 @@ def start_app() -> FastAPI:
await database_.connect()
app.state.arq_pool = await get_arq_pool()
app.state.redis_client = get_client()
@app.on_event("shutdown")
async def shutdown() -> None:

9
src/core/redis_client.py Normal file
View File

@@ -0,0 +1,9 @@
import aioredis
from core.config import env_config
def get_client() -> aioredis.Redis:
return aioredis.Redis(
host=env_config.REDIS_HOST, port=env_config.REDIS_PORT, db=env_config.REDIS_DB
)

View File

@@ -1,13 +1,11 @@
import aioredis
from app.services.cache_updater import (
check_books,
cache_file_by_book_id,
check_books_page,
)
from core.arq_pool import get_redis_settings, get_arq_pool
from core.config import env_config
from core.db import database
from core.redis_client import get_client
import core.sentry # noqa: F401
@@ -16,9 +14,7 @@ async def startup(ctx):
await database.connect()
ctx["arc_pool"] = await get_arq_pool()
ctx["redis"] = aioredis.Redis(
host=env_config.REDIS_HOST, port=env_config.REDIS_PORT, db=env_config.REDIS_DB
)
ctx["redis"] = get_client()
async def shutdown(ctx):