Skip to content

Commit

Permalink
initial testing of reporting using playwright
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorwalton committed Mar 2, 2024
1 parent d788f70 commit 3975174
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 22 deletions.
27 changes: 27 additions & 0 deletions backend/app/reporting/routes/reporting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

from loguru import logger


import asyncio
from playwright.async_api import async_playwright

from fastapi import APIRouter
from fastapi import Depends
from concurrent.futures import ProcessPoolExecutor
import multiprocessing
from app.db.db_session import get_db
from sqlalchemy.ext.asyncio import AsyncSession
from app.reporting.testing import generate_report

report_generation_router = APIRouter()


@report_generation_router.post(
"/generate-report",
description="Create a new report.",
)
async def create_report(session: AsyncSession = Depends(get_db)):
logger.info("Generating report")
await generate_report()
return {"message": "Report generation started."}

55 changes: 35 additions & 20 deletions backend/app/reporting/testing.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
import asyncio
from playwright.async_api import async_playwright
from playwright.sync_api import sync_playwright
from fastapi import APIRouter
from fastapi import Depends
from fastapi import HTTPException
from app.middleware.license import verify_license_key
from app.db.db_session import get_db
from sqlalchemy.ext.asyncio import AsyncSession
from loguru import logger


reporting_router = APIRouter()

@reporting_router.post(
"/generate_report",
description="Generate a report",
)
async def main(session: AsyncSession = Depends(get_db)):
await verify_license_key(session)
async def generate_report():
logger.info("Generating report")
async with async_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = await browser_type.launch()
urls = [
'http://ashdevcopilot01.socfortress.local:3000/d-solo/ab9bab2c-5d86-43e7-bac2-c1d68fc91342/huntress-summary?orgId=1&from=1708725633941&to=1709330433941&panelId=5',
'http://ashdevcopilot01.socfortress.local:3000/d-solo/ab9bab2c-5d86-43e7-bac2-c1d68fc91342/huntress-summary?orgId=1&from=1708725654862&to=1709330454862&panelId=1',
'http://ashdevcopilot01.socfortress.local:3000/d-solo/a1891b09-fba9-498e-807e-1ad774c8557f/sap-users-auth?orgId=44&from=1709303384274&to=1709389784274&panelId=43',
'http://ashdevcopilot01.socfortress.local:3000/d-solo/ab9bab2c-5d86-43e7-bac2-c1d68fc91342/huntress-summary?orgId=1&from=1706799780600&to=1709391780600&panelId=10'
# Add more URLs here
]
for browser_type in [p.chromium]:
browser = await browser_type.launch(headless=False)
page = await browser.new_page()
await page.goto('http://playwright.dev')
await page.screenshot(path=f'example-{browser_type.name}.png')
# Navigate to the login page
await page.goto('http://ashdevcopilot01.socfortress.local:3000/login')
# Enter the username and password
await page.fill('input[name="user"]', 'admin')
await page.fill('input[name="password"]', 'socfortress')
# Click the login button
await page.click('button[data-testid="data-testid Login button"]')
# Wait for navigation to complete
await page.wait_for_load_state(state='networkidle')
# Check if login was successful by checking for an element that is only visible when logged in
body_class = await page.evaluate('document.body.className')
if 'app-grafana no-overlay-scrollbar page-dashboard' in body_class:
print("Login successful")
else:
print("Login failed")
await browser.close()
return
for url in urls:
await page.goto(url)
#await asyncio.sleep(15)
await page.wait_for_load_state(state='networkidle')
await page.screenshot(path=f'example-{browser_type.name}-{urls.index(url)}.png')
await browser.close()
return {"message": "Report generated"}

4 changes: 2 additions & 2 deletions backend/app/routers/reporting.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from fastapi import APIRouter

from app.reporting.testing import reporting_router
from app.reporting.routes.reporting import report_generation_router

# Instantiate the APIRouter
router = APIRouter()

# Include the reporting related routes
router.include_router(
reporting_router,
report_generation_router,
prefix="/reporting",
tags=["Reporting"],
)

0 comments on commit 3975174

Please sign in to comment.