Skip to content

Commit

Permalink
Add GenerateReportResponse class and update create_report function si…
Browse files Browse the repository at this point in the history
…gnature
  • Loading branch information
taylorwalton committed Mar 2, 2024
1 parent 0c19bf3 commit de58e23
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
8 changes: 4 additions & 4 deletions backend/app/reporting/routes/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from app.db.db_session import get_db
from sqlalchemy.ext.asyncio import AsyncSession
from app.reporting.testing import generate_report
from app.reporting.schema.reporting import GenerateReportRequest
from app.reporting.schema.reporting import GenerateReportRequest, GenerateReportResponse

report_generation_router = APIRouter()

Expand All @@ -24,8 +24,8 @@
async def create_report(
request: GenerateReportRequest,
session: AsyncSession = Depends(get_db)
):
) -> GenerateReportResponse:
logger.info("Generating report")
await generate_report(request, session)
return {"message": "Report generation started."}
return await generate_report(request, session)


10 changes: 10 additions & 0 deletions backend/app/reporting/schema/reporting.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pydantic import BaseModel, Field
from typing import List


class GenerateReportRequest(BaseModel):
Expand All @@ -11,3 +12,12 @@ class GenerateReportRequest(BaseModel):
],
description="List of URLs to generate screenshots for",
)

class Base64Image(BaseModel):
base64_image: str
url: str

class GenerateReportResponse(BaseModel):
base64_images: List[Base64Image]
message: str
success: bool
17 changes: 13 additions & 4 deletions backend/app/reporting/testing.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import asyncio
from playwright.async_api import async_playwright
from loguru import logger
import base64
from sqlalchemy.ext.asyncio import AsyncSession
from app.reporting.schema.reporting import GenerateReportRequest
from app.reporting.schema.reporting import GenerateReportRequest, GenerateReportResponse, Base64Image
from app.utils import get_connector_attribute


Expand All @@ -28,10 +29,14 @@ async def check_login_success(page):
return False

async def capture_screenshots(page, urls):
base64_images = []
for url in urls:
await page.goto(url)
await page.wait_for_load_state(state='networkidle')
await page.screenshot(path=f'example-chromium-{urls.index(url)}.png')
screenshot = await page.screenshot(type='png')
base64_image = base64.b64encode(screenshot).decode('utf-8')
base64_images.append({"url": url, "base64_image": base64_image})
return base64_images

async def generate_report(
request: GenerateReportRequest,
Expand All @@ -46,6 +51,10 @@ async def generate_report(
if not await check_login_success(page):
await browser.close()
return
await capture_screenshots(page, request.urls)
base64_images = await capture_screenshots(page, request.urls)
await browser.close()

return GenerateReportResponse(
base64_images=[Base64Image(url=img['url'], base64_image=img['base64_image']) for img in base64_images],
message="Report generated successfully",
success=True
)

0 comments on commit de58e23

Please sign in to comment.