Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions bases/ecoindex/backend/routers/ecoindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
from ecoindex.database.engine import get_session
from ecoindex.database.models import (
ApiEcoindex,
BestPracticesAnalysisResponse,
PageApiEcoindexes,
)
from ecoindex.database.repositories.ecoindex import (
get_best_practice_results_by_analysis_id_db,
get_count_analysis_db,
get_ecoindex_result_by_id_db,
get_ecoindex_result_list_db,
Expand Down Expand Up @@ -184,6 +186,48 @@ async def get_ecoindex_analysis_requests_by_id(
)


@router.get(
name="Get ecoindex analysis best practices by id",
path="/{id}/best-practices",
response_model=BestPracticesAnalysisResponse,
response_description="Best practices results of the ecoindex analysis",
responses={
status.HTTP_204_NO_CONTENT: {
"description": (
"Analysis exists but best practices were not collected"
)
},
status.HTTP_404_NOT_FOUND: example_ecoindex_not_found,
},
description=(
"This returns the RWEB best practices evaluation for the analysis. "
"Returns 204 when the analysis exists but best practices were not collected."
),
)
async def get_ecoindex_analysis_best_practices_by_id(
id: IdParameter,
version: VersionParameter = Version.v1,
session: AsyncSession = Depends(get_session),
) -> BestPracticesAnalysisResponse | Response:
ecoindex = await get_ecoindex_result_by_id_db(
session=session, id=id, version=version
)

if not ecoindex:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Analysis {id} not found for version {version.value}",
)

report = await get_best_practice_results_by_analysis_id_db(
session=session, analysis_id=id
)
if report is None:
return Response(status_code=status.HTTP_204_NO_CONTENT)

return report


@router.get(
name="Get screenshot",
path="/{id}/screenshot",
Expand Down
10 changes: 10 additions & 0 deletions bases/ecoindex/backend/routers/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ async def add_ecoindex_analysis_task(
example=False,
),
] = False,
include_best_practices: Annotated[
bool,
Body(
description=(
"If true, evaluate and store RWEB best practices for the page"
),
example=False,
),
] = False,
session: AsyncSession = Depends(get_session),
) -> str:
if Settings().DAILY_LIMIT_PER_HOST:
Expand Down Expand Up @@ -151,6 +160,7 @@ async def add_ecoindex_analysis_task(
height=web_page.height,
custom_headers=headers,
include_requests_detail=include_requests_detail,
include_best_practices=include_best_practices,
**_enqueue_settings(),
)

Expand Down
8 changes: 8 additions & 0 deletions bases/ecoindex/worker/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def ecoindex_task(
height: int,
custom_headers: dict[str, str],
include_requests_detail: bool = False,
include_best_practices: bool = False,
) -> str:
queue_task_result = run(
async_ecoindex_task(
Expand All @@ -53,6 +54,7 @@ def ecoindex_task(
height=height,
custom_headers=custom_headers,
include_requests_detail=include_requests_detail,
include_best_practices=include_best_practices,
)
)

Expand All @@ -66,6 +68,7 @@ async def async_ecoindex_task(
height: int,
custom_headers: dict[str, str],
include_requests_detail: bool = False,
include_best_practices: bool = False,
) -> QueueTaskResult:
try:
settings = Settings()
Expand All @@ -91,6 +94,7 @@ async def async_ecoindex_task(
screenshot_gid=settings.SCREENSHOTS_GID,
screenshot_uid=settings.SCREENSHOTS_UID,
custom_headers=custom_headers,
best_practices=include_best_practices,
)
ecoindex = await scraper.get_page_analysis()
request_details = (
Expand All @@ -101,6 +105,9 @@ async def async_ecoindex_task(
if include_requests_detail
else None
)
best_practices_report = (
await scraper.get_best_practices() if include_best_practices else None
)

if screenshot:
persist_screenshot(screenshot=screenshot, version=Version.v1.value)
Expand All @@ -110,6 +117,7 @@ async def async_ecoindex_task(
id=task_id,
ecoindex_result=ecoindex,
requests=request_details,
best_practices=best_practices_report,
)

return QueueTaskResult(status=TaskStatus.SUCCESS, detail=db_result)
Expand Down
7 changes: 6 additions & 1 deletion components/ecoindex/database/engine.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from typing import AsyncGenerator

from ecoindex.config import Settings
from ecoindex.database.models import ApiEcoindex, ApiEcoindexRequest # noqa: F401
from ecoindex.database.models import ( # noqa: F401
ApiEcoindex,
ApiEcoindexBestPractice,
ApiEcoindexBestPracticeResult,
ApiEcoindexRequest,
)
from ecoindex.models.api import * # noqa: F401, F403
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from sqlalchemy.pool import NullPool
Expand Down
Loading
Loading