-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #268 from hotosm/feature/metrics
Feature : Download Metrics for rawdataapi
- Loading branch information
Showing
5 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Standard library imports | ||
from datetime import datetime | ||
|
||
# Third party imports | ||
from fastapi import APIRouter, Depends, HTTPException, Query | ||
from fastapi_versioning import version | ||
|
||
# Reader imports | ||
from src.app import DownloadMetrics | ||
|
||
from .auth import admin_required | ||
|
||
router = APIRouter(prefix="/metrics", tags=["Metrics"]) | ||
|
||
|
||
@router.get("/summary") | ||
@version(1) | ||
def get_stats( | ||
start_date: str = Query( | ||
..., | ||
description="Start date (YYYY-MM-DD)", | ||
regex=r"^\d{4}-\d{2}-\d{2}$", | ||
example="2023-04-01", | ||
), | ||
end_date: str = Query( | ||
..., | ||
description="End date (YYYY-MM-DD)", | ||
regex=r"^\d{4}-\d{2}-\d{2}$", | ||
example="2023-04-30", | ||
), | ||
group_by: str = Query( | ||
"day", | ||
description="Group by: day, month, or quarter", | ||
regex=r"^(day|month|quarter|year)$", | ||
), | ||
_: bool = Depends(admin_required), | ||
): | ||
""" | ||
Retrieve download metrics summary statistics. | ||
- **start_date**: The start date for the metrics, in the format "YYYY-MM-DD". | ||
- **end_date**: The end date for the metrics, in the format "YYYY-MM-DD". | ||
- **group_by**: The time period to group the metrics by. Can be "day", "month", "quarter", or "year". | ||
The API requires admin authentication to access. | ||
""" | ||
if group_by not in ["day", "month", "quarter", "year"]: | ||
raise HTTPException( | ||
status_code=400, detail={"error": "Invalid group_by parameter"} | ||
) | ||
|
||
try: | ||
start_date_obj = datetime.strptime(start_date, "%Y-%m-%d") | ||
end_date_obj = datetime.strptime(end_date, "%Y-%m-%d") | ||
except ValueError: | ||
raise HTTPException( | ||
status_code=400, | ||
detail={"error": "Invalid date format, expected YYYY-MM-DD"}, | ||
) | ||
|
||
metrics = DownloadMetrics() | ||
return metrics.get_summary_stats(start_date, end_date, group_by) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters