From 2d21aa98fce15bcb1c4ea2e7cb6bfe4f7f42aff1 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 22 Sep 2024 14:27:43 +0100 Subject: [PATCH] add more routes to backend --- backend/models/pipelines.py | 34 +++++++++++++++++++++++++++++----- backend/routes/route.py | 37 ++++++++++++++++++++++++++++++++----- backend/schema/schemas.py | 16 +++++++++++++++- 3 files changed, 76 insertions(+), 11 deletions(-) diff --git a/backend/models/pipelines.py b/backend/models/pipelines.py index d42fd78..c3efed9 100644 --- a/backend/models/pipelines.py +++ b/backend/models/pipelines.py @@ -1,5 +1,29 @@ -aggregate_mangrove_by_country = [ - { - "$match": {"year": "2020"}, - }, -] +def mangrove_by_country_latest(): + pipeline = [ + { + "$match": {"year": "2020"}, + }, + ] + return pipeline + + +def mangrove_by_country_agg(query): + pipeline = [ + {"$match": {"country_with_parent": query["country_with_parent"]}}, + { + "$group": { + "_id": {"country_with_parent": "$country_with_parent", "year": "$year"}, + "total_pixels": {"$sum": "$total_n_pixels"}, + } + }, + { + "$project": { + "_id": 0, + "country_with_parent": "$_id.country_with_parent", + "year": "$_id.year", + "total_pixels": 1, + } + }, + {"$sort": {"year": 1}}, + ] + return pipeline diff --git a/backend/routes/route.py b/backend/routes/route.py index 309dcbc..f3f024d 100644 --- a/backend/routes/route.py +++ b/backend/routes/route.py @@ -4,18 +4,45 @@ from models.mongodb_handler import MongoDBHandler import models.pipelines as pipelines import schema.schemas as schemas from schema.schemas import DataSerializer +import ast router = APIRouter() -@router.get("/mangrove_country_data") -async def mangrove_country_data(): +@router.get("/mangrove_by_country_latest") +async def mangrove_by_country_latest(): db = client.baseddata collection_name = db["final__protected_mangroves_summary_stats_by_country_agg"] - schema = schemas.mangrove_country_schema - query = pipelines.aggregate_mangrove_by_country + schema = schemas.mangrove_by_country_latest_schema + pipeline = pipelines.mangrove_by_country_latest() serializer = DataSerializer(schema) handler = MongoDBHandler(collection_name) - rawData = handler.aggregate(query) + rawData = handler.aggregate(pipeline) + serializedData = serializer.serialize_many(rawData) + return serializedData + +@router.get("/mangrove_by_country_agg") +async def mangrove_by_country_agg(query: str): + query = ast.literal_eval(query) + db = client.baseddata + collection_name = db["final__protected_mangroves_summary_stats_by_country_agg"] + schema = schemas.mangrove_by_country_agg_schema + pipeline = pipelines.mangrove_by_country_agg(query) + serializer = DataSerializer(schema) + handler = MongoDBHandler(collection_name) + rawData = handler.aggregate(pipeline) + serializedData = serializer.serialize_many(rawData) + return serializedData + +@router.get("/bitcoin_business_growth") +async def bitcoin_business_growth(query: str): + query = ast.literal_eval(query) + db = client.baseddata + collection_name = db["final__bitcoin_business_growth_by_country"] + schema = schemas.bitcoin_business_growth + pipeline = pipelines.bitcoin_business_growth(query) + serializer = DataSerializer(schema) + handler = MongoDBHandler(collection_name) + rawData = handler.aggregate(pipeline) serializedData = serializer.serialize_many(rawData) return serializedData diff --git a/backend/schema/schemas.py b/backend/schema/schemas.py index 04b9b2e..41aaa42 100644 --- a/backend/schema/schemas.py +++ b/backend/schema/schemas.py @@ -1,4 +1,4 @@ -def mangrove_country_schema(data): +def mangrove_by_country_latest_schema(data): return { "country_with_parent": str(data["country_with_parent"]), "original_pixels": int(data["original_pixels"]), @@ -7,6 +7,20 @@ def mangrove_country_schema(data): "cumulative_pct_diff": float(data["cumulative_pct_diff"]), } +def mangrove_by_country_agg_schema(data): + return { + "country_with_parent": str(data["country_with_parent"]), + "year": int(data["year"]), + "total_pixels": int(data["total_pixels"]) + } + +def bitcoin_business_growth_schema(data): + return { + "country_name": str(data["country_name"]), + "cumulative_current_value": str(data["cumulative_current_value"]), + "year": int(data["year"]), + "total_pixels": int(data["total_pixels"]) + } class DataSerializer: def __init__(self, schema_func):